Conversation
|
This is the preprocessor flow I started following:
However, it is not appropriate, since the Currently, in commit 61a3332, if the user compiles the library and program with Below is the indented preprocessor code of 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. |

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