diff --git a/include/libwebsockets/lws-logs.h b/include/libwebsockets/lws-logs.h index 512d6fa2c..9097caf3b 100644 --- a/include/libwebsockets/lws-logs.h +++ b/include/libwebsockets/lws-logs.h @@ -82,6 +82,11 @@ typedef struct lws_log_cx { lws_log_emit_t emit; /* legacy emit function */ lws_log_emit_cx_t emit_cx; /* LLLF_LOG_CONTEXT_AWARE */ } u; + +#if LWS_MAX_SMP > 1 + pthread_mutex_t refcount_lock; +#endif + lws_log_use_cx_t refcount_cb; /**< NULL, or a function called after each change to .refcount below, * this enables implementing side-effects like opening and closing @@ -99,6 +104,9 @@ typedef struct lws_log_cx { /**< mask of log levels we want to emit in this context */ int32_t refcount; /**< refcount of objects bound to this log context */ +#if LWS_MAX_SMP > 1 + char inited; +#endif } lws_log_cx_t; /** diff --git a/lib/core/logs.c b/lib/core/logs.c index 20b3c772f..14d6d617b 100644 --- a/lib/core/logs.c +++ b/lib/core/logs.c @@ -506,9 +506,24 @@ int lwsl_visible_cx(lws_log_cx_t *cx, int level) void lwsl_refcount_cx(lws_log_cx_t *cx, int _new) { +#if LWS_MAX_SMP > 1 + volatile lws_log_cx_t *vcx = (volatile lws_log_cx_t *)cx; +#endif + if (!cx) return; +#if LWS_MAX_SMP > 1 + if (!vcx->inited) { + vcx->inited = 1; + lws_pthread_mutex_init(&cx->refcount_lock); + vcx->inited = 2; + } + while (vcx->inited != 2) + ; + lws_pthread_mutex_lock(&cx->refcount_lock); +#endif + if (_new > 0) cx->refcount++; else { @@ -518,6 +533,10 @@ lwsl_refcount_cx(lws_log_cx_t *cx, int _new) if (cx->refcount_cb) cx->refcount_cb(cx, _new); + +#if LWS_MAX_SMP > 1 + lws_pthread_mutex_unlock(&cx->refcount_lock); +#endif } void