Skip to content

Instantly share code, notes, and snippets.

@oss92
Created May 18, 2017 13:24
Show Gist options
  • Save oss92/13d2a610a61981dc076aa82aaca9a98b to your computer and use it in GitHub Desktop.
Save oss92/13d2a610a61981dc076aa82aaca9a98b to your computer and use it in GitHub Desktop.
static void
rb_check_deadlock(rb_vm_t *vm)
{
int found = 0;
rb_thread_t *th = 0;
if (vm_living_thread_num(vm) > vm->sleeper) return;
if (vm_living_thread_num(vm) < vm->sleeper) rb_bug("sleeper must not be more than vm_living_thread_num(vm)");
if (patrol_thread && patrol_thread != GET_THREAD()) return;
list_for_each(&vm->living_threads, th, vmlt_node) {
if (th->status != THREAD_STOPPED_FOREVER || RUBY_VM_INTERRUPTED(th)) {
found = 1;
}
else if (th->locking_mutex) {
rb_mutex_t *mutex;
GetMutexPtr(th->locking_mutex, mutex);
if (mutex->th == th || (!mutex->th && !list_empty(&mutex->waitq))) {
found = 1;
}
}
if (found)
break;
}
if (!found) {
VALUE argv[2];
argv[0] = rb_eFatal;
argv[1] = rb_str_new2("No live threads left. Deadlock?");
debug_deadlock_check(vm, argv[1]);
vm->sleeper--;
rb_threadptr_raise(vm->main_thread, 2, argv);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment