Skip to content

Commit

Permalink
PR#7158: Event.sync, Mutex.create, Condition.create cause too many GCs
Browse files Browse the repository at this point in the history
Before, mutexes and condition variables were allocated with caml_alloc_custom and cost factor 1/N with N of a few hundreds or thousands.  Hence GCs were triggered every N allocations approximately, which is bad.  The motivation for this allocation cost was to cover the possibility that mutexes and condvars consume rare kernel resources.  This appears not to be the case in Linux nor in Windows, and is unlikely to be the case in any robust implementation of POSIX threads.  Hence this fix sets the cost factor to 0 in allocations of mutexes and condvars.
  • Loading branch information
xavierleroy committed Feb 16, 2017
1 parent eef390a commit 84be1bc
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
5 changes: 5 additions & 0 deletions Changes
Expand Up @@ -150,6 +150,11 @@ Next version (4.05.0):
`O_KEEPEXEC` flag for `openfile` by symmetry with `O_CLOEXEC`.
(Xavier Leroy)

- PR#7158: Event.sync, Mutex.create, Condition.create cause too many GCs.
The fix is to no longer consider mutexes and condition variables
as rare kernel resources.
(Xavier Leroy)

- PR#7264: document the different behaviors of Unix.lockf under POSIX
and under Win32.
(Xavier Leroy, report by David Allsopp)
Expand Down
9 changes: 3 additions & 6 deletions otherlibs/systhreads/st_stubs.c
Expand Up @@ -749,7 +749,6 @@ CAMLprim value caml_thread_join(value th) /* ML */
/* Mutex operations */

#define Mutex_val(v) (* ((st_mutex *) Data_custom_val(v)))
#define Max_mutex_number 5000

static void caml_mutex_finalize(value wrapper)
{
Expand Down Expand Up @@ -783,7 +782,7 @@ CAMLprim value caml_mutex_new(value unit) /* ML */
value wrapper;
st_check_error(st_mutex_create(&mut), "Mutex.create");
wrapper = caml_alloc_custom(&caml_mutex_ops, sizeof(st_mutex *),
1, Max_mutex_number);
0, 1);
Mutex_val(wrapper) = mut;
return wrapper;
}
Expand Down Expand Up @@ -828,7 +827,6 @@ CAMLprim value caml_mutex_try_lock(value wrapper) /* ML */
/* Conditions operations */

#define Condition_val(v) (* (st_condvar *) Data_custom_val(v))
#define Max_condition_number 5000

static void caml_condition_finalize(value wrapper)
{
Expand Down Expand Up @@ -863,7 +861,7 @@ CAMLprim value caml_condition_new(value unit) /* ML */
value wrapper;
st_check_error(st_condvar_create(&cond), "Condition.create");
wrapper = caml_alloc_custom(&caml_condition_ops, sizeof(st_condvar *),
1, Max_condition_number);
0, 1);
Condition_val(wrapper) = cond;
return wrapper;
}
Expand Down Expand Up @@ -900,7 +898,6 @@ CAMLprim value caml_condition_broadcast(value wrapper) /* ML */
/* Thread status blocks */

#define Threadstatus_val(v) (* ((st_event *) Data_custom_val(v)))
#define Max_threadstatus_number 500

static void caml_threadstatus_finalize(value wrapper)
{
Expand Down Expand Up @@ -930,7 +927,7 @@ static value caml_threadstatus_new (void)
value wrapper;
st_check_error(st_event_create(&ts), "Thread.create");
wrapper = caml_alloc_custom(&caml_threadstatus_ops, sizeof(st_event *),
1, Max_threadstatus_number);
0, 1);
Threadstatus_val(wrapper) = ts;
return wrapper;
}
Expand Down

0 comments on commit 84be1bc

Please sign in to comment.