Skip to content

Commit

Permalink
Exp. uv_thread_setpriority, uv_thread_getpriority
Browse files Browse the repository at this point in the history
  • Loading branch information
aantron committed Feb 8, 2024
1 parent 21d8084 commit c5ed14a
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/c/luv_c_function_descriptions.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions src/c/luv_c_type_descriptions.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
22 changes: 22 additions & 0 deletions src/c/shims.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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
4 changes: 3 additions & 1 deletion src/feature/detect_features.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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}.";
Expand Down Expand Up @@ -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}.";
Expand Down
28 changes: 28 additions & 0 deletions src/thread.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
38 changes: 38 additions & 0 deletions src/thread.mli
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit c5ed14a

Please sign in to comment.