Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[wip] Allow callbacks to be registered for GVL related events #119

Draft
wants to merge 11 commits into
base: master
Choose a base branch
from
Prev Previous commit
Next Next commit
Make gvl->waiting atomic
  • Loading branch information
byroot committed Jan 26, 2022
commit 720262b1b97a642a61a5a7ca02d41f5f93c65501
3 changes: 2 additions & 1 deletion include/ruby/thread_native.h
Original file line number Diff line number Diff line change
@@ -216,9 +216,10 @@ typedef struct gvl_hook {
} gvl_hook_t;

#include "ruby/internal/memory.h"
#include "ruby/atomic.h"

gvl_hook_t * rb_gvl_event_new(void *callback, rb_event_flag_t event);
bool rb_gvl_event_delete(gvl_hook_t * hook);
void rb_gvl_execute_hooks(rb_event_flag_t event, unsigned long waiting);
void rb_gvl_execute_hooks(rb_event_flag_t event, rb_atomic_t waiting);
RBIMPL_SYMBOL_EXPORT_END()
#endif
6 changes: 3 additions & 3 deletions thread_pthread.c
Original file line number Diff line number Diff line change
@@ -155,7 +155,7 @@ rb_gvl_event_delete(gvl_hook_t * hook) {
}

void
rb_gvl_execute_hooks(rb_event_flag_t event, unsigned long waiting) {
rb_gvl_execute_hooks(rb_event_flag_t event, rb_atomic_t waiting) {
if (pthread_rwlock_rdlock(&rb_gvl_hooks_rw_lock)) {
// TODO: better way to deal with error?
return;
@@ -362,7 +362,7 @@ gvl_acquire_common(rb_global_vm_lock_t *gvl, rb_thread_t *th)
"we must not be in ubf_list and GVL waitq at the same time");

list_add_tail(&gvl->waitq, &nd->node.gvl);
gvl->waiting++;
ATOMIC_INC(gvl->waiting);
if (rb_gvl_hooks) {
rb_gvl_execute_hooks(RUBY_INTERNAL_EVENT_GVL_ACQUIRE_ENTER, gvl->waiting);
}
@@ -377,7 +377,7 @@ gvl_acquire_common(rb_global_vm_lock_t *gvl, rb_thread_t *th)
} while (gvl->owner);

list_del_init(&nd->node.gvl);
gvl->waiting--;
ATOMIC_DEC(gvl->waiting);

if (gvl->need_yield) {
gvl->need_yield = 0;
2 changes: 1 addition & 1 deletion thread_pthread.h
Original file line number Diff line number Diff line change
@@ -59,7 +59,7 @@ typedef struct rb_global_vm_lock_struct {
* timer.
*/
struct list_head waitq; /* <=> native_thread_data_t.node.ubf */
volatile unsigned long waiting;
rb_atomic_t waiting;
const struct rb_thread_struct *timer;
int timer_err;