diff --git a/READMEs/README.coding.md b/READMEs/README.coding.md index 56d9645a7..e844a8f85 100644 --- a/READMEs/README.coding.md +++ b/READMEs/README.coding.md @@ -899,18 +899,43 @@ You can set fd_limit_per_thread to a nonzero number to control this manually, eg the overall supported fd limit is less than the process allowance. You can control the context basic data allocation for multithreading from Cmake -using -DLWS_MAX_SMP=, if not given it's set to 32. The serv_buf allocation +using -DLWS_MAX_SMP=, if not given it's set to 1. The serv_buf allocation for the threads (currently 4096) is made at runtime only for active threads. Because lws will limit the requested number of actual threads supported according to LWS_MAX_SMP, there is an api lws_get_count_threads(context) to discover how many threads were actually allowed when the context was created. -It's required to implement locking in the user code in the same way that -libwebsockets-test-server-pthread does it, for the FD locking callbacks. +See the test-server-pthreads.c sample for how to use. -There is no knowledge or dependency in lws itself about pthreads. How the -locking is implemented is entirely up to the user code. +@section smplocking SMP Locking Helpers + +Lws provide a set of pthread mutex helpers that reduce to no code or +variable footprint in the case that LWS_MAX_SMP == 1. + +Define your user mutex like this + +``` + lws_pthread_mutex(name); +``` + +If LWS_MAX_SMP > 1, this produces `pthread_mutex_t name;`. In the case +LWS_MAX_SMP == 1, it produces nothing. + +Likewise these helpers for init, destroy, lock and unlock + + +``` + void lws_pthread_mutex_init(pthread_mutex_t *lock) + void lws_pthread_mutex_destroy(pthread_mutex_t *lock) + void lws_pthread_mutex_lock(pthread_mutex_t *lock) + void lws_pthread_mutex_unlock(pthread_mutex_t *lock) +``` + +resolve to nothing if LWS_MAX_SMP == 1, otherwise produce the equivalent +pthread api. + +pthreads is required in lws only if LWS_MAX_SMP > 1. @section libevuv libev / libuv / libevent support diff --git a/lib/libwebsockets.h b/lib/libwebsockets.h index 7bd858f06..20a5fd8d5 100644 --- a/lib/libwebsockets.h +++ b/lib/libwebsockets.h @@ -215,6 +215,49 @@ typedef unsigned long long lws_intptr_t; #endif /* not USE_WOLFSSL */ #endif +/* + * Helpers for pthread mutex in user code... if lws is built for + * multiple service threads, these resolve to pthread mutex + * operations. In the case LWS_MAX_SMP is 1 (the default), they + * are all NOPs and no pthread type or api is referenced. + */ + +#if LWS_MAX_SMP > 1 + +#define lws_pthread_mutex(name) pthread_mutex_t name + +static LWS_INLINE void +lws_pthread_mutex_init(pthread_mutex_t *lock) +{ + pthread_mutex_init(lock, NULL); +} + +static LWS_INLINE void +lws_pthread_mutex_destroy(pthread_mutex_t *lock) +{ + pthread_mutex_destroy(lock); +} + +static LWS_INLINE void +lws_pthread_mutex_lock(pthread_mutex_t *lock) +{ + pthread_mutex_lock(lock); +} + +static LWS_INLINE void +lws_pthread_mutex_unlock(pthread_mutex_t *lock) +{ + pthread_mutex_unlock(lock); +} + +#else +#define lws_pthread_mutex(name) +#define lws_pthread_mutex_init(_a) +#define lws_pthread_mutex_destroy(_a) +#define lws_pthread_mutex_lock(_a) +#define lws_pthread_mutex_unlock(_a) +#endif + #define CONTEXT_PORT_NO_LISTEN -1 #define CONTEXT_PORT_NO_LISTEN_SERVER -2