Sfoglia il codice sorgente

add mutex and TLS externs

Simon Krajewski 6 anni fa
parent
commit
fa74d666ab
2 ha cambiato i file con 78 aggiunte e 0 eliminazioni
  1. 18 0
      libs/uv/uv.ml
  2. 60 0
      libs/uv/uv_stubs.c

+ 18 - 0
libs/uv/uv.ml

@@ -247,6 +247,24 @@ external pipe_accept_pending : t_loop -> t_pipe -> pipe_accepted uv_result = "w_
 external pipe_getsockname : t_pipe -> string uv_result = "w_pipe_getsockname"
 external pipe_getpeername : t_pipe -> string uv_result = "w_pipe_getpeername"
 
+(* ------------- MUTEXES ---------------------------------------------- *)
+
+type t_mutex
+
+external mutex_init : unit -> t_mutex uv_result = "w_mutex_init"
+external mutex_lock : t_mutex -> unit = "w_mutex_lock"
+external mutex_trylock : t_mutex -> unit uv_result = "w_mutex_trylock"
+external mutex_unlock : t_mutex -> unit = "w_mutex_unlock"
+
+(* ------------- TLS ---------------------------------------------- *)
+
+type t_key
+
+external key_create : unit -> t_key uv_result = "w_key_create"
+external key_delete : t_key -> unit = "w_key_delete"
+external key_get : t_key -> 'a = "w_key_get"
+external key_set : t_key -> 'a -> unit = "w_key_set"
+
 (* ------------- HAXE ---------------------------------------------- *)
 
 external get_file_open_flags : unit -> (string * int) array = "hx_get_file_open_flags"

+ 60 - 0
libs/uv/uv_stubs.c

@@ -58,6 +58,8 @@
 #define Handle_val(v) UV_UNWRAP(v, uv_handle_t)
 #define Loop_val(v) UV_UNWRAP(v, uv_loop_t)
 #define Pipe_val(v) UV_UNWRAP(v, uv_pipe_t)
+#define Mutex_val(v) UV_UNWRAP(v, uv_mutex_t)
+#define Key_val(v) UV_UNWRAP(v, uv_key_t)
 #define Process_val(v) UV_UNWRAP(v, uv_process_t)
 #define Shutdown_val(v) UV_UNWRAP(v, uv_shutdown_t)
 #define Stream_val(v) UV_UNWRAP(v, uv_stream_t)
@@ -1230,6 +1232,64 @@ CAMLprim value w_pipe_write_handle(value handle, value data, value send_handle,
 	UV_SUCCESS_UNIT;
 }
 
+// ------------- MUTEXES -----------------------------------------------
+
+CAMLprim value w_mutex_init(value unit) {
+	CAMLparam1(unit);
+	UV_ALLOC_CHECK(handle, uv_mutex_t);
+	UV_ERROR_CHECK_C(uv_mutex_init(Mutex_val(handle)), free(Mutex_val(handle)));
+	if ((UV_HANDLE_DATA(Mutex_val(handle)) = alloc_data()) == NULL)
+		UV_ERROR(0);
+	UV_SUCCESS(handle);
+}
+
+CAMLprim value w_mutex_lock(value handle) {
+	CAMLparam1(handle);
+	uv_mutex_lock(Mutex_val(handle));
+	UV_SUCCESS_UNIT;
+}
+
+CAMLprim value w_mutex_trylock(value handle) {
+	CAMLparam1(handle);
+	UV_ERROR_CHECK(uv_mutex_trylock(Mutex_val(handle)));
+	UV_SUCCESS_UNIT;
+}
+
+CAMLprim value w_mutex_unlock(value handle) {
+	CAMLparam1(handle);
+	uv_mutex_unlock(Mutex_val(handle));
+	UV_SUCCESS_UNIT;
+}
+
+// ------------- TLS -----------------------------------------------
+
+CAMLprim value w_key_create(value unit) {
+	CAMLparam1(unit);
+	UV_ALLOC_CHECK(handle, uv_key_t);
+	UV_ERROR_CHECK_C(uv_key_create(Key_val(handle)), free(Key_val(handle)));
+	if ((UV_HANDLE_DATA(Key_val(handle)) = alloc_data()) == NULL)
+		UV_ERROR(0);
+	UV_SUCCESS(handle);
+}
+
+CAMLprim value w_key_delete(value handle) {
+	CAMLparam1(handle);
+	uv_key_delete(Key_val(handle));
+	UV_SUCCESS_UNIT;
+}
+
+CAMLprim value w_key_get(value handle) {
+	CAMLparam1(handle);
+	void* r = uv_key_get(Key_val(handle));
+	UV_SUCCESS(r);
+}
+
+CAMLprim value w_key_set(value handle, value v) {
+	CAMLparam1(handle);
+	uv_key_set(Key_val(handle), &v);
+	UV_SUCCESS_UNIT;
+}
+
 // ------------- GLUE -----------------------------------------------
 
 static value build_fields(int num_fields, const char* names[], int values[]) {