Skip to content

Instantly share code, notes, and snippets.

@t-mat
Last active December 30, 2023 03:03
Show Gist options
  • Save t-mat/f07d0450157ea8abffad51821b0d951b to your computer and use it in GitHub Desktop.
Save t-mat/f07d0450157ea8abffad51821b0d951b to your computer and use it in GitHub Desktop.
[lz4] support <threads.h>
+ #if 0
# include <pthread.h> /* pthread_* */
+ #else
+ # include <threads.h> /* thrd_*, mtx_*, cnd_* */
+ # define pthread_t thrd_t
+ # define pthread_create thrd_create
+ # define pthread_join thrd_join
+ # define pthread_mutex_t mtx_t
+ # define pthread_mutex_init mtx_init
+ # define pthread_mutex_destroy mtx_destroy
+ # define pthread_mutex_lock mtx_lock
+ # define pthread_mutex_unlock mtx_unlock
+ # define pthread_cond_t cnd_t
+ # define pthread_cond_init cnd_init
+ # define pthread_cond_destroy cnd_destroy
+ # define pthread_cond_wait cnd_wait
+ # define pthread_cond_signal cnd_signal
+ # define pthread_cond_broadcast cnd_broadcast
+ #endif
- static void* TPOOL_thread(void* opaque);
+ static int TPOOL_thread(void* opaque);
TPOOL_ctx* TPOOL_create(int nbThreads, int queueSize)
{
...
{ int error = 0;
- error |= pthread_mutex_init(&ctx->queueMutex, NULL);
- error |= pthread_cond_init(&ctx->queuePushCond, NULL);
- error |= pthread_cond_init(&ctx->queuePopCond, NULL);
+ error |= pthread_mutex_init(&ctx->queueMutex, mtx_plain);
+ error |= pthread_cond_init(&ctx->queuePushCond);
+ error |= pthread_cond_init(&ctx->queuePopCond);
for (i = 0; i < nbThreads; ++i) {
- if (pthread_create(&ctx->threads[i], NULL, &TPOOL_thread, ctx)) {
+ if (pthread_create(&ctx->threads[i], &TPOOL_thread, ctx)) {
- static void* TPOOL_thread(void* opaque) {
+ static int TPOOL_thread(void* opaque) {
- if (!ctx) { return NULL; }
+ if (!ctx) { return 0; }
- return opaque;
+ return 1;
@t-mat
Copy link
Author

t-mat commented Dec 30, 2023

Another patch

        /* pthread only */
        #include <stdlib.h>  /* malloc, free*/
-       #include <pthread.h> /* pthread_* */
+       #if defined(USE_C11_THREADS) // || defined(_MSC_VER)
+       #  if defined(_MSC_VER) /* TODO : determine new _MSC_VER, Use HAS_*() functionality/header query, fallback */
+       #    include <B577b3a1006464e90b10197d5d419526c638381100848159134_threads.h> /* https://developercommunity.visualstudio.com/t/Missing-threadsh-in-MSVC-178/10514752#T-N10540847 */
+       #  else
+       #    include <threads.h> /* thrd_*, mtx_*, cnd_* */
+       #  endif
+       #  define pthread_t                     thrd_t
+       #  define pthread_create(a,b,c,d)       thrd_create(a,c,d)    /* ignore 2nd argument */
+       #  define pthread_join                  thrd_join
+       #  define pthread_mutex_t               mtx_t
+       #  define pthread_mutex_init(a,b)       mtx_init(a,mtx_plain) /* ignore and replace 2nd argument */
+       #  define pthread_mutex_destroy         mtx_destroy
+       #  define pthread_mutex_lock            mtx_lock
+       #  define pthread_mutex_unlock          mtx_unlock
+       #  define pthread_cond_t                cnd_t
+       #  define pthread_cond_init(a,b)        cnd_init(a)           /* ignore 2nd argument */
+       #  define pthread_cond_destroy          cnd_destroy
+       #  define pthread_cond_wait             cnd_wait
+       #  define pthread_cond_signal           cnd_signal
+       #  define pthread_cond_broadcast        cnd_broadcast
+       #  define TPOOL_thread_ReturnType       int
+       #  define TPOOL_thread_ReturnFailure    0
+       #  define TPOOL_thread_ReturnSuccess    (!0)
+       #else
+       #  include <pthread.h> /* pthread_* */
+       #  define TPOOL_thread_ReturnType       void*
+       #  define TPOOL_thread_ReturnFailure    NULL
+       #  define TPOOL_thread_ReturnSuccess    (!NULL)
+       #endif

-       static void* TPOOL_thread(void* opaque);
+       static TPOOL_thread_ReturnType TPOOL_thread(void* opaque);

-       static void* TPOOL_thread(void* opaque) {
+       static TPOOL_thread_ReturnType TPOOL_thread(void* opaque) {

-           if (!ctx) { return NULL; }
+           if (!ctx) { return TPOOL_thread_ReturnFailure; }

-                       return opaque;
+                       return TPOOL_thread_ReturnSuccess;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment