From c5ed14a6bacb253b3cfe0c6387ec4a0f5b95e4fb Mon Sep 17 00:00:00 2001 From: Anton Bachin Date: Thu, 8 Feb 2024 09:10:20 +0300 Subject: [PATCH] Exp. uv_thread_setpriority, uv_thread_getpriority --- src/c/luv_c_function_descriptions.ml | 8 ++++++ src/c/luv_c_type_descriptions.ml | 9 +++++++ src/c/shims.h | 22 ++++++++++++++++ src/feature/detect_features.ml | 4 ++- src/thread.ml | 28 ++++++++++++++++++++ src/thread.mli | 38 ++++++++++++++++++++++++++++ 6 files changed, 108 insertions(+), 1 deletion(-) diff --git a/src/c/luv_c_function_descriptions.ml b/src/c/luv_c_function_descriptions.ml index c152e641..7b3a4ed5 100644 --- a/src/c/luv_c_function_descriptions.ml +++ b/src/c/luv_c_function_descriptions.ml @@ -1483,6 +1483,14 @@ struct foreign "uv_thread_equal" (ptr t @-> ptr t @-> returning bool) + let setpriority = + foreign "uv_thread_setpriority" + (t @-> int @-> returning error_code) + + let getpriority = + foreign "uv_thread_getpriority" + (t @-> ptr int @-> returning error_code) + let cpumask_size = foreign "uv_cpumask_size" (void @-> returning int) diff --git a/src/c/luv_c_type_descriptions.ml b/src/c/luv_c_type_descriptions.ml index 541f9c6d..4ea7af07 100644 --- a/src/c/luv_c_type_descriptions.ml +++ b/src/c/luv_c_type_descriptions.ml @@ -813,6 +813,15 @@ struct let no_flags = constant "UV_THREAD_NO_FLAGS" int let has_stack_size = constant "UV_THREAD_HAS_STACK_SIZE" int end + + module Priority = + struct + let highest = constant "UV_THREAD_PRIORITY_HIGHEST" int + let above_normal = constant "UV_THREAD_PRIORITY_ABOVE_NORMAL" int + let normal = constant "UV_THREAD_PRIORITY_NORMAL" int + let below_normal = constant "UV_THREAD_PRIORITY_BELOW_NORMAL" int + let lowest = constant "UV_THREAD_PRIORITY_LOWEST" int + end end module TLS = diff --git a/src/c/shims.h b/src/c/shims.h index 08824010..92633d72 100644 --- a/src/c/shims.h +++ b/src/c/shims.h @@ -758,6 +758,8 @@ #endif #if UV_VERSION_MAJOR == 1 && UV_VERSION_MINOR < 47 + + static size_t uv_utf16_length_as_wtf8( const uint16_t *utf16, ssize_t utf16_len) { @@ -782,3 +784,23 @@ abort(); } #endif + +#if UV_VERSION_MAJOR == 1 && UV_VERSION_MINOR < 48 + enum { + UV_THREAD_PRIORITY_HIGHEST = 2, + UV_THREAD_PRIORITY_ABOVE_NORMAL = 1, + UV_THREAD_PRIORITY_NORMAL = 0, + UV_THREAD_PRIORITY_BELOW_NORMAL = -1, + UV_THREAD_PRIORITY_LOWEST = -2, + }; + + static int uv_thread_setpriority(uv_thread_t id, int priority) + { + return ENOSYS; + } + + static int uv_thread_getpriority(uv_thread_t id, int *priority) + { + return ENOSYS; + } +#endif diff --git a/src/feature/detect_features.ml b/src/feature/detect_features.ml index 13abaf0c..e3c8c278 100644 --- a/src/feature/detect_features.ml +++ b/src/feature/detect_features.ml @@ -254,6 +254,7 @@ let () = needs 12 "get_osfhandle" "See {!Luv.File.get_osfhandle}."; needs 45 "getaffinity" "See {!Luv.Thread.getaffinity}"; needs 45 "getcpu" "See {!Luv.Thread.getcpu}."; + needs 48 "getpriority" "See {!Luv.Thread.getpriority}."; needs 28 "gettimeofday" "See {!Luv.Time.gettimeofday}."; needs 16 "if_indextoiid" "See {!Luv.Network.if_indextoiid}."; needs 16 "if_indextoname" "See {!Luv.Network.if_indextoname}."; @@ -286,7 +287,8 @@ let () = needs 24 "process_windows_hide_gui" "See {!Luv.Process.spawn}."; needs 33 "random" "See {!Luv.Random.random}."; needs 28 "readdir" "See {!Luv.File.readdir}."; - needs 45 "setaffinity" "See {!Luv.Thread.setaffinity}"; + needs 45 "setaffinity" "See {!Luv.Thread.setaffinity}."; + needs 48 "setpriority" "See {!Luv.Thread.setpriority}."; needs 12 "signal_start_oneshot" "See {!Luv.Signal.start_oneshot}."; needs 34 "sleep" "See {!Luv.Time.sleep}."; needs 41 "socketpair" "See {!Luv.TCP.socketpair}."; diff --git a/src/thread.ml b/src/thread.ml index e251f362..625e905b 100644 --- a/src/thread.ml +++ b/src/thread.ml @@ -53,6 +53,34 @@ let join thread = C.Blocking.Thread.join thread |> Error.to_result () +module Priority = +struct + type t = [ + | `HIGHEST + | `ABOVE_NORMAL + | `NORMAL + | `BELOW_NORMAL + | `LOWEST + ] +end + +let setpriority thread priority = + let priority = + match priority with + | `HIGHEST -> C.Types.Thread.Priority.highest + | `ABOVE_NORMAL -> C.Types.Thread.Priority.above_normal + | `NORMAL -> C.Types.Thread.Priority.normal + | `BELOW_NORMAL -> C.Types.Thread.Priority.below_normal + | `LOWEST -> C.Types.Thread.Priority.lowest + in + C.Functions.Thread.setpriority (Ctypes.(!@) thread) priority + |> Error.to_result () + +let getpriority thread = + let priority = Ctypes.(allocate int) 0 in + C.Functions.Thread.getpriority (Ctypes.(!@) thread) priority + |> Error.to_result_f (fun () -> Ctypes.(!@) priority) + let c mask = mask |> Bytes.unsafe_to_string diff --git a/src/thread.mli b/src/thread.mli index 395ea724..00cb6184 100644 --- a/src/thread.mli +++ b/src/thread.mli @@ -63,6 +63,44 @@ val join : t -> (unit, Error.t) result {{:http://man7.org/linux/man-pages/man3/pthread_join.3p.html} [pthread_join(3p)]}. *) +(** Constants for use with {!Luv.Thread.setpriority}. *) +module Priority : +sig + type t = [ + | `HIGHEST + | `ABOVE_NORMAL + | `NORMAL + | `BELOW_NORMAL + | `LOWEST + ] +end + +val setpriority : t -> Priority.t -> (unit, Error.t) result +(** Sets the given thread's priority. + + Binds + {{:https://docs.libuv.org/en/v1.x/threading.html#c.uv_thread_setpriority} + [uv_thread_setpriority]}. See + {{:https://www.man7.org/linux/man-pages/man3/setpriority.3p.html} + [setpriority(3p)]}. + + Requires Luv 0.5.13 and libuv 1.48.0. + + {{!Luv.Require} Feature check}: [Luv.Require.(has setpriority)] *) + +val getpriority : t -> (int, Error.t) result +(** Gets the given thread's priority. + + Binds + {{:https://docs.libuv.org/en/v1.x/threading.html#c.uv_thread_getpriority} + [uv_thread_getpriority]}. See + {{:https://www.man7.org/linux/man-pages/man3/getpriority.3p.html} + [getpriority(3p)]}. + + Requires Luv 0.5.13 and libuv 1.48.0. + + {{!Luv.Require} Feature check}: [Luv.Require.(has getpriority)] *) + val setaffinity : t -> bytes -> (bytes, Error.t) result (** Sets the thread's processor affinity mask.