Skip to content

Commit

Permalink
chore(cthreads): improve usage
Browse files Browse the repository at this point in the history
This commit improves CThreads usage by destroying properly the mutexes, and expanding its usage to examples.
  • Loading branch information
ThePedroo committed Feb 3, 2024
1 parent 7717c3d commit cafb3ff
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 22 deletions.
4 changes: 2 additions & 2 deletions core/cthreads.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions core/cthreads.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion core/threadpool.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
4 changes: 4 additions & 0 deletions core/websockets.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 6 additions & 5 deletions examples/slash-commands2.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <pthread.h>
#include <assert.h>
#include <errno.h>
#include <inttypes.h> /* SCNu64 */

#include "discord.h"
#include "cthreads.h"
#include "log.h"

u64snowflake g_app_id;
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -254,7 +254,7 @@ read_input(void *p_client)
print_help();
}

pthread_exit(NULL);
cthreads_thread_exit(NULL);
}

int
Expand Down Expand Up @@ -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);

Expand Down
4 changes: 4 additions & 0 deletions include/discord-internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
19 changes: 12 additions & 7 deletions src/concord-once.c
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
#include <string.h>
#include <signal.h>
#include <curl/curl.h>
#include <pthread.h>
#include <unistd.h>
#include <poll.h>
#include <sys/ioctl.h>

#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,
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -83,7 +85,7 @@ ccord_global_init()
}
}
}
pthread_mutex_unlock(&lock);
cthreads_mutex_unlock(&lock);
return CCORD_OK;

fail_pipe_init:
Expand All @@ -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();
Expand All @@ -115,7 +117,7 @@ ccord_global_cleanup()
shutdown_fds[i] = -1;
}
}
pthread_mutex_unlock(&lock);
cthreads_mutex_unlock(&lock);
}

int
Expand All @@ -130,5 +132,8 @@ discord_dup_shutdown_fd(void)
fd = -1;
}
}

cthreads_mutex_destroy(&lock);

return fd;
}
1 change: 1 addition & 0 deletions src/discord-voice.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
8 changes: 4 additions & 4 deletions test/cthreads.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit cafb3ff

Please sign in to comment.