diff --git a/vm/builtin/thread.cpp b/vm/builtin/thread.cpp index abfec4923b..c038ad9c98 100644 --- a/vm/builtin/thread.cpp +++ b/vm/builtin/thread.cpp @@ -17,13 +17,15 @@ #include "vm/object_utils.hpp" #include "vm.hpp" +#include "windows_compat.h" + #include /* HACK: returns a value that should identify a native thread * for debugging threading issues. The winpthreads library * defines pthread_t to be a structure not a pointer. */ -intptr_t thread_debug_id() { +intptr_t thread_debug_self() { #ifdef RBX_WINDOWS return (intptr_t)(pthread_self()).p; #else @@ -31,6 +33,15 @@ intptr_t thread_debug_id() { #endif } +static intptr_t thread_debug_id(pthread_t thr) { +#ifdef RBX_WINDOWS + return (intptr_t)thr.p; +#else + return (intptr_t)thr; +#endif +} + + namespace rubinius { @@ -90,7 +101,7 @@ namespace rubinius { vm->shared.gc_dependent(vm); if(cDebugThreading) { - std::cerr << "[THREAD " << thread_debug_id() << " started thread]\n"; + std::cerr << "[THREAD " << thread_debug_self() << " started thread]\n"; } vm->set_stack_bounds(reinterpret_cast(&calculate_stack), 4194304); @@ -130,7 +141,7 @@ namespace rubinius { VM::discard(vm, vm); if(cDebugThreading) { - std::cerr << "[LOCK thread " << thread_debug_id() << " exitted]\n"; + std::cerr << "[LOCK thread " << thread_debug_self() << " exitted]\n"; } return 0; @@ -158,16 +169,12 @@ namespace rubinius { Object* Thread::priority(STATE) { pthread_t id = vm_->os_thread(); - if(id) { - int _policy; - struct sched_param params; + int _policy; + struct sched_param params; - pthread_check(pthread_getschedparam(id, &_policy, ¶ms)); + pthread_check(pthread_getschedparam(id, &_policy, ¶ms)); - return Fixnum::from(params.sched_priority); - } - - return Qnil; + return Fixnum::from(params.sched_priority); } Object* Thread::raise(STATE, Exception* exc) { @@ -236,7 +243,7 @@ namespace rubinius { pthread_t id = vm->os_thread(); if(cDebugThreading) { - std::cerr << "[THREAD joining " << id << "]\n"; + std::cerr << "[THREAD joining " << thread_debug_id(id) << "]\n"; } init_lock_.unlock(); @@ -250,10 +257,10 @@ namespace rubinius { case 0: break; case EDEADLK: - std::cerr << "Join deadlock: " << id << "/" << thread_debug_id() << "\n"; + std::cerr << "Join deadlock: " << thread_debug_id(id) << "/" << thread_debug_self() << "\n"; break; case EINVAL: - std::cerr << "Invalid thread id: " << id << "\n"; + std::cerr << "Invalid thread id: " << thread_debug_id(id) << "\n"; break; case ESRCH: // This means that the thread finished execution and detached diff --git a/vm/missing/windows.c b/vm/missing/windows.c index 8c1b512bd9..0937c1d43d 100644 --- a/vm/missing/windows.c +++ b/vm/missing/windows.c @@ -3,6 +3,7 @@ #include "windows_compat.h" #include #include +#include #include int uname(struct utsname *name) { @@ -29,4 +30,8 @@ int setenv(const char *name, const char *value, int overwrite) { return putenv(str); } +int nanosleep(const struct timespec *rqtp, struct timespec *rmtp) { + return 0; +} + #endif diff --git a/vm/util/thread.hpp b/vm/util/thread.hpp index 5eaf550981..c9622031ea 100644 --- a/vm/util/thread.hpp +++ b/vm/util/thread.hpp @@ -16,7 +16,7 @@ #include "util/atomic.hpp" -intptr_t thread_debug_id(); +intptr_t thread_debug_self(); #define pthread_check(expr) if((expr) != 0) { assert(0 && "failed: " #expr); } @@ -220,19 +220,19 @@ namespace thread { void lock() { if(locked_) return; if(cDebugLockGuard) { - std::cout << "[[ " << thread_debug_id() << " Locking " << lock_.describe() << " ]]\n"; + std::cout << "[[ " << thread_debug_self() << " Locking " << lock_.describe() << " ]]\n"; } lock_.lock(); locked_ = true; if(cDebugLockGuard) { - std::cout << "[[ " << thread_debug_id() << " Locked " << lock_.describe() << " ]]\n"; + std::cout << "[[ " << thread_debug_self() << " Locked " << lock_.describe() << " ]]\n"; } } void unlock() { if(!locked_) return; if(cDebugLockGuard) { - std::cout << "[[ " << thread_debug_id() << " Unlocking " << lock_.describe() << " ]]\n"; + std::cout << "[[ " << thread_debug_self() << " Unlocking " << lock_.describe() << " ]]\n"; } lock_.unlock(); locked_ = false; @@ -312,7 +312,7 @@ namespace thread { void lock() { if(cDebugLockGuard) { - std::cout << "[[ " << thread_debug_id() << " MLocking " << describe() << " ]]\n"; + std::cout << "[[ " << thread_debug_self() << " MLocking " << describe() << " ]]\n"; } int err = pthread_mutex_lock(&native_); @@ -331,7 +331,7 @@ namespace thread { owner_ = pthread_self(); if(cDebugLockGuard) { - std::cout << "[[ " << thread_debug_id() << " MLocked " << describe() << " ]]\n"; + std::cout << "[[ " << thread_debug_self() << " MLocked " << describe() << " ]]\n"; } } @@ -349,7 +349,7 @@ namespace thread { Code unlock() { if(cDebugLockGuard) { - std::cout << "[[ " << thread_debug_id() << " MUnlocking " << describe() << " ]]\n"; + std::cout << "[[ " << thread_debug_self() << " MUnlocking " << describe() << " ]]\n"; } int err = pthread_mutex_unlock(&native_); @@ -395,24 +395,24 @@ namespace thread { void wait(Mutex& mutex) { if(cDebugLockGuard) { - std::cout << "[[ " << thread_debug_id() << " CUnlocking " << mutex.describe() << " ]]\n"; + std::cout << "[[ " << thread_debug_self() << " CUnlocking " << mutex.describe() << " ]]\n"; } pthread_check(pthread_cond_wait(&native_, mutex.native())); if(cDebugLockGuard) { - std::cout << "[[ " << thread_debug_id() << " CLocked " << mutex.describe() << " ]]\n"; + std::cout << "[[ " << thread_debug_self() << " CLocked " << mutex.describe() << " ]]\n"; } } Code wait_until(Mutex& mutex, const struct timespec* ts) { if(cDebugLockGuard) { - std::cout << "[[ " << thread_debug_id() << " CUnlocking " << mutex.describe() << " ]]\n"; + std::cout << "[[ " << thread_debug_self() << " CUnlocking " << mutex.describe() << " ]]\n"; } int err = pthread_cond_timedwait(&native_, mutex.native(), ts); if(cDebugLockGuard) { - std::cout << "[[ " << thread_debug_id() << " CLocked " << mutex.describe() << " ]]\n"; + std::cout << "[[ " << thread_debug_self() << " CLocked " << mutex.describe() << " ]]\n"; } if(err != 0) { diff --git a/vm/windows_compat.h b/vm/windows_compat.h index 41d92f8eb3..5b924642eb 100644 --- a/vm/windows_compat.h +++ b/vm/windows_compat.h @@ -32,6 +32,8 @@ char* realpath(const char* file_name, char* resolved_name); // time #define timezone _timezone +int nanosleep(const struct timespec *rqtp, struct timespec *rmtp); + #endif // RBX_WINDOWS // Keep this ifdef short so it's clear