Skip to content

Conversation

@8dcc
Copy link
Owner

@8dcc 8dcc commented Feb 1, 2026

This PR adds optional support for multithreading, making the library thread-safe.

Fixes #3.

@8dcc
Copy link
Owner Author

8dcc commented Feb 1, 2026

This is the preprocessor flow I started following:

Diagram

However, it is not appropriate, since the pool_ext_mutex_* functions need to be declared (not defined) in the header, and the POOL_EXT_MUTEX_TYPE macro should also be defined there for the arguments.

Currently, in commit 61a3332, if the user compiles the library and program with LIBPOOL_THREAD_SAFE and LIBPOOL_NO_STDLIB defined, it must also define POOL_EXT_MUTEX_TYPE to a valid mutex type like pthread_mutex_t. This must be done both when compiling the library itself (since the mutex type is used from within libpool.c) and when compiling any source which includes libpool.h (since the mutex type is used when declaring the multithreading function pointers.

Below is the indented preprocessor code of libpool.h and libpool.c, for readability.

Open me
/*
 * External functions for thread-safe operations, only used if
 * 'LIBPOOL_THEAD_SAFE' is defined.
 *
 * If `LIBPOOL_NO_STDLIB' is defined, they are set to NULL, so the user must
 * initialize them. Otherwise, their default value are POSIX Thread (pthread)
 * functions.
 */
#if defined(LIBPOOL_THREAD_SAFE)
    #if defined(LIBPOOL_NO_STDLIB)
        #if !defined(POOL_EXT_MUTEX_TYPE)
            #error "POOL_EXT_MUTEX_TYPE must be defined if LIBPOOL_THREAD_SAFE and LIBPOOL_NO_STDLIB are defined."
        #endif /* !defined(POOL_EXT_MUTEX_TYPE) */
        typedef POOL_EXT_MUTEX_TYPE pool_ext_mutex_t;
    #else /* !defined(LIBPOOL_NO_STDLIB) */
        #include <pthread.h>
        typedef pthread_mutex_t pool_ext_mutex_t;
    #endif /* !defined(LIBPOOL_NO_STDLIB) */

    typedef bool (*PoolMutexInitFuncPtr)(pool_ext_mutex_t*);
    typedef bool (*PoolMutexLockFuncPtr)(pool_ext_mutex_t*);
    typedef bool (*PoolMutexUnlockFuncPtr)(pool_ext_mutex_t*);
    typedef bool (*PoolMutexDestroyFuncPtr)(pool_ext_mutex_t*);
    extern PoolMutexInitFuncPtr pool_ext_mutex_init;
    extern PoolMutexLockFuncPtr pool_ext_mutex_lock;
    extern PoolMutexUnlockFuncPtr pool_ext_mutex_unlock;
    extern PoolMutexDestroyFuncPtr pool_ext_mutex_destroy;
#endif /* defined(LIBPOOL_THREAD_SAFE) */
/*
 * External multithreading functions.
 */
#if defined(LIBPOOL_THREAD_SAFE)
    #if defined(LIBPOOL_NO_STDLIB)
        PoolMutexInitFuncPtr pool_ext_mutex_init       = NULL;
        PoolMutexLockFuncPtr pool_ext_mutex_lock       = NULL;
        PoolMutexUnlockFuncPtr pool_ext_mutex_unlock   = NULL;
        PoolMutexDestroyFuncPtr pool_ext_mutex_destroy = NULL;
    #else /* !defined(LIBPOOL_NO_STDLIB) */
        #include <pthread.h>
        static bool pool_ext_mutex_init_impl(pool_ext_mutex_t* mutex) {
            return pthread_mutex_init(mutex, NULL) == 0;
        }
        static bool pool_ext_mutex_lock_impl(pool_ext_mutex_t* mutex) {
            return pthread_mutex_lock(mutex) == 0;
        }
        static bool pool_ext_mutex_unlock_impl(pool_ext_mutex_t* mutex) {
            return pthread_mutex_unlock(mutex) == 0;
        }
        static bool pool_ext_mutex_destroy_impl(pool_ext_mutex_t* mutex) {
            return pthread_mutex_destroy(mutex) == 0;
        }
        PoolMutexInitFuncPtr pool_ext_mutex_init       = pool_ext_mutex_init_impl;
        PoolMutexLockFuncPtr pool_ext_mutex_lock       = pool_ext_mutex_lock_impl;
        PoolMutexUnlockFuncPtr pool_ext_mutex_unlock   = pool_ext_mutex_unlock_impl;
        PoolMutexDestroyFuncPtr pool_ext_mutex_destroy = pool_ext_mutex_destroy_impl;
    #endif /* !defined(LIBPOOL_NO_STDLIB) */
#endif /* defined(LIBPOOL_THEAD_SAFE) */

I personally don't like the current approach, and I plan on simplifying the usage and the internal code. The current code should work, although no multithreading-specific tests have been done.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Thread safety

2 participants