forked from xapi-project/xen-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6c132b1
commit 4f0d1cf
Showing
1 changed file
with
49 additions
and
0 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
ocaml/libs/xapi-stdext/lib/xapi-stdext-threads/runtime_lock_waiters.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#define CAML_INTERNALS | ||
#include <caml/signals.h> | ||
#include <caml/threads.h> | ||
#include <stdatomic.h> | ||
|
||
static void noop(void) {} | ||
|
||
static void (*old_enter_blocking_section_hook)(void) = noop; | ||
static void (*old_leave_blocking_section_hook)(void) = noop; | ||
|
||
static atomic_int waiting; | ||
|
||
static void my_enter_blocking_section(void) | ||
{ | ||
old_enter_blocking_section_hook(); | ||
} | ||
|
||
static void my_leave_blocking_section(void) | ||
{ | ||
atomic_fetch_add(&waiting, 1); | ||
old_leave_blocking_section_hook(); | ||
atomic_fetch_add(&waiting, -1); | ||
} | ||
|
||
CAMLprim value caml_runtime_lock_waiters_get(value) | ||
{ | ||
return Int_val(atomic_load(&waiting)); | ||
} | ||
|
||
CAMLprim value caml_runtime_lock_waiters_init(value) | ||
{ | ||
void (*hook)(void); | ||
|
||
hook = caml_enter_blocking_section_hook; | ||
if (!hook) | ||
return Val_false; | ||
old_enter_blocking_section_hook = hook; | ||
|
||
hook = caml_leave_blocking_section_hook; | ||
if (!hook) | ||
return Val_false; | ||
old_leave_blocking_section_hook = hook; | ||
|
||
atomic_init(&waiting, 0); | ||
|
||
caml_enter_blocking_section_hook = my_enter_blocking_section; | ||
caml_leave_blocking_section_hook = my_leave_blocking_section; | ||
return Val_true; | ||
} |