Skip to content

Reimplemented http_client using the ocurl library #127

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions config/Makefile.config.in
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ OCAMLDOC=@OCAMLDOC@
OCAMLMKTOP=@OCAMLMKTOP@
OCAMLVERSION_MAJOR=@OCAMLVERSION_MAJOR@
NUMS_INCLUDE=@NUMS_INCLUDE@
CURL_INCLUDE=@CURL_INCLUDE@
CURL_LIB=@CURL_LIB@
CURL_CLIB=@CURL_CLIB@
THREADS_INCLUDE=@THREADS_INCLUDE@

LABLGL_CMA=@LABLGL_CMA@
LABLGL_CMXA=@LABLGL_CMXA@
Expand Down
8 changes: 6 additions & 2 deletions config/Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ SRC_FILETP=src/networks/fileTP
SUBDIRS=$(EXTLIB) $(CDK) $(BITSTRING) $(LIB) $(RSS) $(XML) $(NET) tools \
$(COMMON) $(DRIVER) $(MP3) src/config/$(OS_FILES)

INCLUDES += $(foreach file, $(SUBDIRS), -I $(file)) -I +camlp4 $(NUMS_INCLUDE)
INCLUDES += $(foreach file, $(SUBDIRS), -I $(file)) -I +camlp4 $(NUMS_INCLUDE) $(THREADS_INCLUDE) $(CURL_INCLUDE)

CFLAGS:=$(CFLAGS) $(CONFIG_INCLUDES) $(GTKCFLAGS) $(GD_CFLAGS)

Expand Down Expand Up @@ -197,7 +197,8 @@ LIB_SRCS= \
$(LIB)/gettext.ml4 $(LIB)/md5_c.c $(LIB)/sha1_c.c \
$(LIB)/tiger.c \
$(LIB)/stubs_c.c $(LIB)/set2.ml $(LIB)/queues.ml \
$(LIB)/verificationBitmap.ml
$(LIB)/verificationBitmap.ml \
$(LIB)/threadPool.ml

ifeq ("$(MAGIC)", "yes")
MAGIC_LIBS_flags += -cclib -lmagic
Expand Down Expand Up @@ -580,6 +581,9 @@ DRIVER_SRCS+= \

ICONS_CMXA=icons.cmxa

LIBS_opt += $(OCAMLLIB)/threads/threads.cmxa
LIBS_opt += $(CURL_LIB) $(CURL_CLIB)

CDK_CMXA=cdk.cmxa
BITSTRING_CMXA=
BITSTRING_CMA=
Expand Down
47 changes: 47 additions & 0 deletions config/configure
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,10 @@ ac_header_c_list=
ac_subst_vars='LTLIBOBJS
LIBOBJS
NUMS_INCLUDE
CURL_INCLUDE
THREADS_INCLUDE
CURL_LIB
CURL_CLIB
OCAMLVERSION_MAJOR
DEVFLAGS
CONFIGURE_RUN
Expand Down Expand Up @@ -6100,6 +6104,49 @@ else
printf "%s\n" "found $NUMS_INCLUDE" >&6; }
fi

CURL=yes
{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking ocaml curl library" >&5
printf %s "checking ocaml curl library... " >&6; }
CURL_INCLUDE="`ocamlfind query -i-format curl 2> /dev/null`"
if test "$CURL_INCLUDE" == ""; then
CURL=no
fi
CURL_LIB="`ocamlfind query -a-format -predicates native curl 2> /dev/null`"
if test "$CURL_LIB" == ""; then
CURL=no
fi
CURL_CLIB="`ocamlfind query -l-format -predicates native curl 2> /dev/null`"
if test "$CURL_CLIB" == ""; then
CURL=no
fi

if test "$CURL" = "no"; then
{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: no" >&5
printf "%s\n" "no" >&6; }
echo "ERROR: ocurl is missing"
exit 1
else
{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: found $CURL_INCLUDE" >&5
printf "%s\n" "found $CURL_INCLUDE" >&6; }
fi

THREADS=yes
{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking ocaml posix thread module" >&5
printf %s "checking ocaml posix thread module... " >&6; }
THREADS_INCLUDE="`ocamlfind query -i-format threads.posix 2> /dev/null`"
if test "$THREADS_INCLUDE" == ""; then
THREADS=no
fi
if test "$THREADS" = "no"; then
{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: no" >&5
printf "%s\n" "no" >&6; }
echo "ERROR: thread module is missing"
exit 1
else
{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: found $THREADS_INCLUDE" >&5
printf "%s\n" "found $THREADS_INCLUDE" >&6; }
fi

echo ""
echo "----------------------------------"
echo " Checking system headers."
Expand Down
34 changes: 34 additions & 0 deletions config/configure.in
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,40 @@ else
AC_MSG_RESULT([found $NUMS_INCLUDE])
fi

CURL=yes
{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking ocaml curl library" >&5
printf %s "checking ocaml curl library... " >&6; }
CURL_INCLUDE=$(ocamlfind query -i-format curl 2> /dev/null)
if [ $? -ne 0 ]; then
CURL=no
fi
CURL_LIB=$(ocamlfind query -a-format -predicates native curl 2> /dev/null)
if [ $? -ne 0 ]; then
CURL=no
fi
CURL_CLIB=$(ocamlfind query -l-format -predicates native curl 2> /dev/null)
if [ $? -ne 0 ]; then
CURL=no
fi
if test "$CURL" = "no"; then
AC_MSG_RESULT([no])
else
AC_MSG_RESULT([found $CURL_INCLUDE])
fi

THREADS=yes
{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking ocaml posix thread module" >&5
printf %s "checking ocaml posix thread module... " >&6; }
THREADS_INCLUDE=$(ocamlfind query -i-format threads.posix 2> /dev/null)
if [ $? -ne 0 ]; then
THREADS=no
fi
if test "$THREADS" = "no"; then
AC_MSG_RESULT([no])
else
AC_MSG_RESULT([found $THREADS_INCLUDE])
fi

echo ""
echo "----------------------------------"
echo " Checking system headers."
Expand Down
4 changes: 4 additions & 0 deletions src/daemon/common/commonOptions.ml
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ let min_connections = 50

let () =
lprintf_nl "Starting MLDonkey %s ... " Autoconf.current_version;

Curl.global_init Curl.CURLINIT_GLOBALALL;
lprintf_nl "Curl initialized. Version: %s" (Curl.version ());

let ulof_old = Unix2.c_getdtablesize () in
lprintf_nl "Language %s, locale %s, ulimit for open files %d"
Charset.Locale.default_language Charset.Locale.locale_string ulof_old;
Expand Down
83 changes: 83 additions & 0 deletions src/utils/lib/threadPool.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
(* Copyright 2025 Luca Carlon *)
(*
This file is part of mldonkey.

mldonkey is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

mldonkey is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with mldonkey; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*)

module TaskQueue = struct
type 'a t = {
queue : 'a Queue.t;
mutex : Mutex.t;
cond : Condition.t;
}

(* Create a new task queue *)
let create () = {
queue = Queue.create ();
mutex = Mutex.create ();
cond = Condition.create ();
}

(* Add a task to the queue *)
let add t task =
Mutex.lock t.mutex;
Queue.add task t.queue;
Condition.signal t.cond;
Mutex.unlock t.mutex

(* Retrieve a task from the queue (blocking if empty) *)
let take t =
Mutex.lock t.mutex;
while Queue.is_empty t.queue do
Condition.wait t.cond t.mutex
done;
let task = Queue.pop t.queue in
Mutex.unlock t.mutex;
task
end

(* Thread pool *)
type t = {
threads : Thread.t list;
tasks : (unit -> unit) TaskQueue.t;
stop_flag : bool ref;
}

(* Worker thread function *)
let rec worker_loop tasks stop_flag =
if !stop_flag then ()
else
let task = TaskQueue.take tasks in
(try task () with _ -> ());
worker_loop tasks stop_flag

(* Create a thread pool with a fixed number of threads *)
let create num_threads =
let tasks = TaskQueue.create () in
let stop_flag = ref false in
let threads = List.init num_threads (fun _ ->
Thread.create (fun () -> worker_loop tasks stop_flag) ()
) in
{ threads; tasks; stop_flag }

(* Add a task to the thread pool *)
let add_task pool task =
TaskQueue.add pool.tasks task

(* Stop the thread pool and wait for all threads to finish *)
let stop pool =
pool.stop_flag := true;
List.iter Thread.join pool.threads
31 changes: 31 additions & 0 deletions src/utils/lib/threadPool.mli
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
(* Copyright 2025 Luca Carlon *)
(*
This file is part of mldonkey.

mldonkey is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

mldonkey is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with mldonkey; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*)

(** The type of a thread pool. *)
type t

(** [create num_threads] creates a thread pool with [num_threads] worker threads. *)
val create : int -> t

(** [add_task pool task] adds [task] to the task queue of the thread pool [pool].
The task is a function that takes no arguments and returns [unit]. *)
val add_task : t -> (unit -> unit) -> unit

(** [stop pool] stops all worker threads in the thread pool [pool] and waits for them to finish. *)
val stop : t -> unit
Loading
Loading