Skip to content

Commit

Permalink
Implement pthread_detach, improve pthread_join, close thread handle o…
Browse files Browse the repository at this point in the history
…n thread exit
  • Loading branch information
dmitryvk committed Jun 5, 2010
1 parent 82a6712 commit 66936fc
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 12 deletions.
68 changes: 56 additions & 12 deletions src/runtime/pthreads_win32.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include <time.h>
#include <sys/time.h>

void odprintf(const char *fmt, ...);

int pthread_attr_init(pthread_attr_t *attr)
{
attr->stack_size = 0;
Expand Down Expand Up @@ -68,16 +70,40 @@ pthread_t pthread_self()
return (pthread_t)TlsGetValue(thread_self_tls_index);
}

const char * state_to_str(pthread_thread_state state)
{
switch (state) {
case pthread_state_running: return "running";
case pthread_state_finished: return "finished";
case pthread_state_joined: return "joined";
}
}

DWORD WINAPI Thread_Function(LPVOID param)
{
pthread_t self = (pthread_t)param;
void* arg = self->arg;
void* retval = NULL;
pthread_fn fn = self->start_routine;
TlsSetValue(thread_self_tls_index, self);
retval = fn(arg);
self->retval = fn(arg);
pthread_mutex_lock(&self->lock);
self->state = pthread_state_finished;
odprintf("thread function returned, state is %s", state_to_str(self->state));
pthread_cond_broadcast(&self->cond);
while (!self->detached && self->state != pthread_state_joined) {
pthread_cond_wait(&self->cond, &self->lock);
odprintf("detached = %d, state = %s", self->detached, state_to_str(self->state));
}
pthread_mutex_unlock(&self->lock);

odprintf("destroying the thread");

pthread_mutex_destroy(&self->lock);
pthread_cond_destroy(&self->cond);
CloseHandle(self->handle);
free(self);
return (DWORD)retval;
return 0;
}

int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg)
Expand All @@ -101,6 +127,10 @@ int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_
}
for (i = 1; i < NSIG; ++i)
pth->signal_is_pending[i] = 0;
pth->state = pthread_state_running;
pthread_mutex_init(&pth->lock, NULL);
pthread_cond_init(&pth->cond, NULL);
pth->detached = 0;
ResumeThread(createdThread);
if (thread)
*thread = createdThread;
Expand All @@ -114,19 +144,35 @@ int pthread_equal(pthread_t thread1, pthread_t thread2)

int pthread_detach(pthread_t thread)
{
// FIXME: What to do??
return 0;
int retval = 0;
pthread_mutex_lock(&thread->lock);
if (thread->detached)
odprintf("thread_detach on 0x%p, already detached", thread);
thread->detached = 1;
pthread_cond_broadcast(&thread->cond);
pthread_mutex_unlock(&thread->lock);
return retval;
}

int pthread_join(pthread_t thread, void **retval)
{
if (WaitForSingleObject(thread->handle, INFINITE) == WAIT_OBJECT_0) {
CloseHandle(thread->handle);
return 0;
} else {
CloseHandle(thread->handle);
return 1;
odprintf("pthread_join on 0x%p started", thread);
pthread_mutex_lock(&thread->lock);
if (thread->detached)
odprintf("pthread_join on 0x%p, thread is detached", thread);
odprintf("joining 0x%p, state is %s", thread, state_to_str(thread->state));
while (thread->state != pthread_state_finished) {
pthread_cond_wait(&thread->cond, &thread->lock);
odprintf("woke up joining 0x%p, state is %s", thread, state_to_str(thread->state));
}
thread->state = pthread_state_joined;
pthread_cond_broadcast(&thread->cond);
if (retval)
*retval = thread->retval;
odprintf("done joining 0x%p, state is %s", thread, state_to_str(thread->state));
pthread_mutex_unlock(&thread->lock);
odprintf("pthread_join on 0x%p returned", thread);
return 0;
}

int pthread_key_create(pthread_key_t *key, void (*destructor)(void*))
Expand All @@ -153,8 +199,6 @@ int pthread_setspecific(pthread_key_t key, const void *value)
return TlsSetValue(key, (LPVOID)value) != FALSE;
}

void odprintf(const char *fmt, ...);

int pthread_sigmask(int how, const sigset_t *set, sigset_t *oldset)
{
pthread_t self = pthread_self();
Expand Down
12 changes: 12 additions & 0 deletions src/runtime/pthreads_win32.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,12 @@ void pthread_unlock_structures();

typedef void *(*pthread_fn)(void*);

typedef enum {
pthread_state_running,
pthread_state_finished,
pthread_state_joined
} pthread_thread_state;

typedef struct pthread_thread {
pthread_fn start_routine;
void* arg;
Expand All @@ -154,6 +160,12 @@ typedef struct pthread_thread {
unsigned int in_safepoint;
sigset_t blocked_signal_set;
unsigned int signal_is_pending[NSIG];
void * retval;

pthread_mutex_t lock;
pthread_cond_t cond;
int detached;
pthread_thread_state state;
} pthread_thread;

void pthread_np_pending_signal_handler(int signum);
Expand Down

0 comments on commit 66936fc

Please sign in to comment.