Skip to content

Commit

Permalink
Added nanosleep stub and more working around pthread_t definition in …
Browse files Browse the repository at this point in the history
…winpthreads.
  • Loading branch information
Brian Ford committed Jan 18, 2011
1 parent 8a9e9cf commit 36f0d93
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 25 deletions.
35 changes: 21 additions & 14 deletions vm/builtin/thread.cpp
Expand Up @@ -17,20 +17,31 @@
#include "vm/object_utils.hpp"
#include "vm.hpp"

#include "windows_compat.h"

#include <sys/time.h>

/* 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
return (intptr_t)pthread_self();
#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 {


Expand Down Expand Up @@ -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<uintptr_t>(&calculate_stack), 4194304);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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, &params));
pthread_check(pthread_getschedparam(id, &_policy, &params));

return Fixnum::from(params.sched_priority);
}

return Qnil;
return Fixnum::from(params.sched_priority);
}

Object* Thread::raise(STATE, Exception* exc) {
Expand Down Expand Up @@ -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();
Expand All @@ -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
Expand Down
5 changes: 5 additions & 0 deletions vm/missing/windows.c
Expand Up @@ -3,6 +3,7 @@
#include "windows_compat.h"
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <io.h>

int uname(struct utsname *name) {
Expand All @@ -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
22 changes: 11 additions & 11 deletions vm/util/thread.hpp
Expand Up @@ -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); }

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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_);
Expand All @@ -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";
}
}

Expand All @@ -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_);
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 2 additions & 0 deletions vm/windows_compat.h
Expand Up @@ -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
Expand Down

0 comments on commit 36f0d93

Please sign in to comment.