Skip to content

Commit

Permalink
Cello should now work on environments without threads.
Browse files Browse the repository at this point in the history
  • Loading branch information
orangeduck committed Sep 10, 2015
1 parent f390717 commit 3ab2d49
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
36 changes: 28 additions & 8 deletions src/Thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ static int64_t Thread_C_Int(var self) {
return (int64_t)t->thread;
#elif defined(CELLO_WINDOWS)
return (int64_t)t->id;
#else
return 0;
#endif

}
Expand Down Expand Up @@ -283,6 +285,12 @@ static var Thread_Call(var self, var args) {

struct Thread* t = self;

t->args = assign(alloc_raw(type_of(args)), args);

/* Call Init Thread & Run */

#if defined(CELLO_UNIX)

/* Setup Thread Local Storage */

if (not Thread_TLS_Key_Created) {
Expand All @@ -291,12 +299,6 @@ static var Thread_Call(var self, var args) {
atexit(Thread_TLS_Key_Delete);
}

t->args = assign(alloc_raw(type_of(args)), args);

/* Call Init Thread & Run */

#if defined(CELLO_UNIX)

int err = pthread_create(&t->thread, NULL, Thread_Init_Run, t);

if (err is EINVAL) {
Expand All @@ -313,13 +315,25 @@ static var Thread_Call(var self, var args) {

#elif defined(CELLO_WINDOWS)

/* Setup Thread Local Storage */

if (not Thread_TLS_Key_Created) {
Thread_TLS_Key_Create();
Thread_TLS_Key_Created = true;
atexit(Thread_TLS_Key_Delete);
}

t->thread = CreateThread(NULL, 0,
(LPTHREAD_START_ROUTINE)Thread_Init_Run, t, 0, &t->id);

if (t->thread is NULL) {
throw(ValueError, "Unable to Create WinThread");
}

#else

throw(ResourceError, "Unsupported Threading Environment");

#endif

return self;
Expand All @@ -340,6 +354,8 @@ static var Thread_Current(void) {
var wrapper = pthread_getspecific(Thread_Key_Wrapper);
#elif defined(CELLO_WINDOWS)
var wrapper = TlsGetValue(Thread_Key_Wrapper);
#else
var wrapper = NULL;
#endif

/*
Expand Down Expand Up @@ -386,27 +402,29 @@ static void Thread_Start(var self) {

static void Thread_Stop(var self) {
struct Thread* t = self;
if (not t->thread) { return; }

#if defined(CELLO_UNIX)
if (not t->thread) { return; }
int err = pthread_kill(t->thread, SIGINT);
if (err is EINVAL) { throw(ValueError, "Invalid Argument to Thread Stop"); }
if (err is ESRCH) { throw(ValueError, "Invalid Thread"); }
#elif defined(CELLO_WINDOWS)
if (not t->thread) { return; }
TerminateThread(t->thread, FALSE);
#endif

}

static void Thread_Join(var self) {
struct Thread* t = self;
if (not t->thread) { return; }

#if defined(CELLO_UNIX)
if (not t->thread) { return; }
int err = pthread_join(t->thread, NULL);
if (err is EINVAL) { throw(ValueError, "Invalid Argument to Thread Join"); }
if (err is ESRCH) { throw(ValueError, "Invalid Thread"); }
#elif defined(CELLO_WINDOWS)
if (not t->thread) { return; }
WaitForSingleObject(t->thread, INFINITE);
#endif

Expand Down Expand Up @@ -631,6 +649,8 @@ static bool Mutex_Lock_Try(var self) {
return true;
#elif defined(CELLO_WINDOWS)
return not (WaitForSingleObject(m->mutex, 0) is WAIT_TIMEOUT);
#else
return true;
#endif

}
Expand Down
4 changes: 4 additions & 0 deletions tests/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -2890,13 +2890,17 @@ int main(int argc, char** argv) {
pt_add_suite(suite_int);
pt_add_suite(suite_list);
pt_add_suite(suite_map);
#if defined(CELLO_WINDOWS) || defined(CELLO_UNIX)
pt_add_suite(suite_mutex);
#endif
pt_add_suite(suite_range);
pt_add_suite(suite_ref);
pt_add_suite(suite_slice);
pt_add_suite(suite_string);
pt_add_suite(suite_table);
#if defined(CELLO_WINDOWS) || defined(CELLO_UNIX)
pt_add_suite(suite_thread);
#endif
pt_add_suite(suite_tree);
pt_add_suite(suite_tuple);
pt_add_suite(suite_type);
Expand Down

0 comments on commit 3ab2d49

Please sign in to comment.