Class: AtomicRuby::ThreadPoolMonitor

Inherits:
Object
  • Object
show all
Defined in:
ext/atomic_ruby/atomic_ruby.c

Instance Method Summary collapse

Instance Method Details

#register_workerObject



709
710
711
712
713
714
715
716
717
718
719
720
721
# File 'ext/atomic_ruby/atomic_ruby.c', line 709

static VALUE rb_cThreadPoolMonitor_register_worker(VALUE self) {
  atomic_ruby_thread_pool_monitor_t *monitor;
  TypedData_Get_Struct(self, atomic_ruby_thread_pool_monitor_t, &atomic_ruby_thread_pool_monitor_type, monitor);

  VALUE thread = rb_thread_current();
  atomic_ruby_thread_pool_worker_state_t *state = ALLOC(atomic_ruby_thread_pool_worker_state_t);
  state->monitor = monitor;
  state->phase = ATOMIC_RUBY_THREAD_POOL_WORKER_INACTIVE;
  state->phase_started_at = 0;
  state->running_cpu_started_at = 0;
  rb_internal_thread_specific_set(thread, atomic_ruby_thread_pool_worker_key, state);
  return Qnil;
}

#snapshotObject



747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
# File 'ext/atomic_ruby/atomic_ruby.c', line 747

static VALUE rb_cThreadPoolMonitor_snapshot(VALUE self) {
  atomic_ruby_thread_pool_monitor_t *monitor;
  TypedData_Get_Struct(self, atomic_ruby_thread_pool_monitor_t, &atomic_ruby_thread_pool_monitor_type, monitor);

  return rb_ary_new_from_args(
    7,
    UINT2NUM(atomic_load_explicit(&monitor->running_count, memory_order_relaxed)),
    UINT2NUM(atomic_load_explicit(&monitor->waiting_count, memory_order_relaxed)),
    UINT2NUM(atomic_load_explicit(&monitor->blocked_count, memory_order_relaxed)),
    ULL2NUM(atomic_load_explicit(&monitor->running_time, memory_order_relaxed)),
    ULL2NUM(atomic_load_explicit(&monitor->waiting_time, memory_order_relaxed)),
    ULL2NUM(atomic_load_explicit(&monitor->blocked_time, memory_order_relaxed)),
    ULL2NUM(atomic_load_explicit(&monitor->running_cpu_time, memory_order_relaxed))
  );
}

#startObject



682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
# File 'ext/atomic_ruby/atomic_ruby.c', line 682

static VALUE rb_cThreadPoolMonitor_start(VALUE self) {
  atomic_ruby_thread_pool_monitor_t *monitor;
  TypedData_Get_Struct(self, atomic_ruby_thread_pool_monitor_t, &atomic_ruby_thread_pool_monitor_type, monitor);
  rb_nativethread_lock_lock(&atomic_ruby_thread_pool_monitor_lock);
  if (!monitor->active) {
    monitor->active = true;
    if (atomic_ruby_thread_pool_monitor_count++ == 0) {
      atomic_ruby_thread_pool_event_hook = rb_internal_thread_add_event_hook(
        atomic_ruby_thread_pool_event_callback,
        RUBY_INTERNAL_THREAD_EVENT_READY |
          RUBY_INTERNAL_THREAD_EVENT_RESUMED |
          RUBY_INTERNAL_THREAD_EVENT_SUSPENDED,
        NULL
      );
    }
  }
  rb_nativethread_lock_unlock(&atomic_ruby_thread_pool_monitor_lock);
  return Qnil;
}

#start_workObject



734
735
736
737
738
# File 'ext/atomic_ruby/atomic_ruby.c', line 734

static VALUE rb_cThreadPoolMonitor_start_work(VALUE self) {
  atomic_ruby_thread_pool_worker_state_t *state = rb_internal_thread_specific_get(rb_thread_current(), atomic_ruby_thread_pool_worker_key);
  atomic_ruby_thread_pool_worker_enter_phase(state, ATOMIC_RUBY_THREAD_POOL_WORKER_RUNNING, atomic_ruby_monotonic_time());
  return Qnil;
}

#stopObject



702
703
704
705
706
707
# File 'ext/atomic_ruby/atomic_ruby.c', line 702

static VALUE rb_cThreadPoolMonitor_stop(VALUE self) {
  atomic_ruby_thread_pool_monitor_t *monitor;
  TypedData_Get_Struct(self, atomic_ruby_thread_pool_monitor_t, &atomic_ruby_thread_pool_monitor_type, monitor);
  atomic_ruby_thread_pool_monitor_stop(monitor);
  return Qnil;
}

#stop_workObject



740
741
742
743
744
745
# File 'ext/atomic_ruby/atomic_ruby.c', line 740

static VALUE rb_cThreadPoolMonitor_stop_work(VALUE self) {
  atomic_ruby_thread_pool_worker_state_t *state = rb_internal_thread_specific_get(rb_thread_current(), atomic_ruby_thread_pool_worker_key);
  atomic_ruby_thread_pool_worker_leave_phase(state, atomic_ruby_monotonic_time());
  state->phase = ATOMIC_RUBY_THREAD_POOL_WORKER_INACTIVE;
  return Qnil;
}

#unregister_workerObject



723
724
725
726
727
728
729
730
731
732
# File 'ext/atomic_ruby/atomic_ruby.c', line 723

static VALUE rb_cThreadPoolMonitor_unregister_worker(VALUE self) {
  VALUE thread = rb_thread_current();
  atomic_ruby_thread_pool_worker_state_t *state = rb_internal_thread_specific_get(thread, atomic_ruby_thread_pool_worker_key);
  if (state == NULL) return Qnil;

  atomic_ruby_thread_pool_worker_leave_phase(state, atomic_ruby_monotonic_time());
  rb_internal_thread_specific_set(thread, atomic_ruby_thread_pool_worker_key, NULL);
  xfree(state);
  return Qnil;
}