diff --git a/core/cthreads.c b/core/cthreads.c index 843d2b98..36361a2a 100644 --- a/core/cthreads.c +++ b/core/cthreads.c @@ -103,9 +103,9 @@ unsigned long cthreads_thread_id(struct cthreads_thread thread) { #endif } -void cthreads_thread_close(void *code) { +void cthreads_thread_exit(void *code) { #ifdef _WIN32 - #ifdef __WATCOMC__ + #if defined __WATCOMC__ || _MSC_VER || __DMC__ ExitThread((DWORD)code); #else ExitThread((DWORD)(uintptr_t)code); diff --git a/core/cthreads.h b/core/cthreads.h index 8696292b..c63df962 100644 --- a/core/cthreads.h +++ b/core/cthreads.h @@ -224,14 +224,14 @@ struct cthreads_thread cthreads_thread_self(void); unsigned long cthreads_thread_id(struct cthreads_thread thread); /** - * Closes a thread. + * Exits a thread. * * - pthread: pthread_exit * - windows threads: ExitThread * * @param code Pointer to the thread exit code. */ -void cthreads_thread_close(void *code); +void cthreads_thread_exit(void *code); /** * Initializes a mutex. diff --git a/core/threadpool.c b/core/threadpool.c index 7710ed09..39f0a959 100644 --- a/core/threadpool.c +++ b/core/threadpool.c @@ -300,6 +300,6 @@ static void *threadpool_thread(void *threadpool) pool->started--; cthreads_mutex_unlock(&(pool->lock)); - cthreads_thread_close(NULL); + cthreads_thread_exit(NULL); return(NULL); } \ No newline at end of file diff --git a/core/websockets.c b/core/websockets.c index e6fbf310..3574e015 100644 --- a/core/websockets.c +++ b/core/websockets.c @@ -50,7 +50,11 @@ struct websockets { /** lock for functions that may be called in other threads */ struct cthreads_mutex lock; /** lock for reading/writing the event-loop timestamp */ + #ifdef CTHREADS_RWLOCK struct cthreads_rwlock rwlock; + #else + #error "pthread_rwlock functions are not available on this system." + #endif /** * user-triggered actions diff --git a/examples/slash-commands2.c b/examples/slash-commands2.c index 74e9556d..0539b93e 100644 --- a/examples/slash-commands2.c +++ b/examples/slash-commands2.c @@ -2,12 +2,12 @@ #include #include #include -#include #include #include #include /* SCNu64 */ #include "discord.h" +#include "cthreads.h" #include "log.h" u64snowflake g_app_id; @@ -80,7 +80,7 @@ read_input(void *p_client) char cmd_action[9 + 1]; CCORDcode code; - pthread_detach(pthread_self()); + cthreads_thread_detach(cthreads_thread_self()); while (1) { memset(buf, 0, sizeof(buf)); @@ -254,7 +254,7 @@ read_input(void *p_client) print_help(); } - pthread_exit(NULL); + cthreads_thread_exit(NULL); } int @@ -284,8 +284,9 @@ main(int argc, char *argv[]) fscanf(stdin, "%" SCNu64, &g_app_id); } while (!g_app_id || errno == ERANGE); - pthread_t tid; - pthread_create(&tid, NULL, &read_input, client); + struct cthreads_thread tid; + struct cthreads_args args; + cthreads_thread_create(&tid, NULL, read_input, client, &args); discord_run(client); diff --git a/include/discord-internal.h b/include/discord-internal.h index e9eb083f..701538ee 100644 --- a/include/discord-internal.h +++ b/include/discord-internal.h @@ -780,7 +780,11 @@ struct discord_gateway { */ int ping_ms; /** ping rwlock */ + #ifdef CTHREADS_RWLOCK struct cthreads_rwlock rwlock; + #else + #error "pthread_rwlock functions are not available on this system." + #endif } * timer; /** the identify structure for client authentication */ diff --git a/src/concord-once.c b/src/concord-once.c index f9621c11..d50e1bf0 100644 --- a/src/concord-once.c +++ b/src/concord-once.c @@ -1,15 +1,15 @@ #include #include #include -#include #include #include #include #include "error.h" #include "discord-worker.h" +#include "cthreads.h" -static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; +static struct cthreads_mutex lock; static int shutdown_fds[2] = { -1, @@ -53,7 +53,9 @@ _ccord_sigint_handler(int signum) CCORDcode ccord_global_init() { - pthread_mutex_lock(&lock); + cthreads_mutex_init(&lock, NULL); + + cthreads_mutex_lock(&lock); if (0 == init_counter++) { #ifdef CCORD_SIGINTCATCH signal(SIGINT, &_ccord_sigint_handler); @@ -83,7 +85,7 @@ ccord_global_init() } } } - pthread_mutex_unlock(&lock); + cthreads_mutex_unlock(&lock); return CCORD_OK; fail_pipe_init: @@ -99,14 +101,14 @@ ccord_global_init() curl_global_cleanup(); init_counter = 0; - pthread_mutex_unlock(&lock); + cthreads_mutex_unlock(&lock); return CCORD_GLOBAL_INIT; } void ccord_global_cleanup() { - pthread_mutex_lock(&lock); + cthreads_mutex_lock(&lock); if (init_counter && 0 == --init_counter) { curl_global_cleanup(); discord_worker_global_cleanup(); @@ -115,7 +117,7 @@ ccord_global_cleanup() shutdown_fds[i] = -1; } } - pthread_mutex_unlock(&lock); + cthreads_mutex_unlock(&lock); } int @@ -130,5 +132,8 @@ discord_dup_shutdown_fd(void) fd = -1; } } + + cthreads_mutex_destroy(&lock); + return fd; } diff --git a/src/discord-voice.c b/src/discord-voice.c index d3b62db0..4a6c8d78 100644 --- a/src/discord-voice.c +++ b/src/discord-voice.c @@ -881,6 +881,7 @@ _discord_voice_cleanup(struct discord_voice *vc) if (vc->ws) ws_cleanup(vc->ws); if (vc->parse.pairs) free(vc->parse.pairs); if (vc->parse.tokens) free(vc->parse.tokens); + cthreads_mutex_destroy(&client_lock); } void diff --git a/test/Makefile b/test/Makefile index 3a594386..899922d4 100644 --- a/test/Makefile +++ b/test/Makefile @@ -6,7 +6,7 @@ INCLUDE_DIR = $(TOP)/include GENCODECS_DIR = $(TOP)/gencodecs TEST_DISCORD = racecond rest timeout -TEST_CORE = user-agent websockets queriec +TEST_CORE = user-agent websockets queriec cthreads TESTS = $(TEST_DISCORD) $(TEST_GITHUB) $(TEST_CORE) diff --git a/test/cthreads.c b/test/cthreads.c index de9a553e..4d0d77e0 100644 --- a/test/cthreads.c +++ b/test/cthreads.c @@ -18,7 +18,7 @@ TEST create_thread() { ret = cthreads_thread_create(&thread, NULL, thread_func, NULL, &args); ASSERT_EQ(0, ret); - ret = cthreads_join(&thread, 0); + ret = cthreads_thread_join(thread, 0); ASSERT_EQ(0, ret); PASS(); @@ -70,10 +70,10 @@ TEST equal() { struct cthreads_thread thread2; int ret; - thread1 = cthreads_self(); - thread2 = cthreads_self(); + thread1 = cthreads_thread_self(); + thread2 = cthreads_thread_self(); - ret = cthreads_equal(thread1, thread2); + ret = cthreads_thread_equal(thread1, thread2); ASSERT_EQ(1, ret); PASS();