mirror of
https://github.com/warmcat/libwebsockets.git
synced 2025-03-09 00:00:04 +01:00
logs: lock log context refcount changes
Enabling the locking requires -DLWS_MAX_SMP=2 or more.
This commit is contained in:
parent
176b2ca5a1
commit
e5e1b34f84
2 changed files with 27 additions and 0 deletions
|
@ -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;
|
||||
|
||||
/**
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Add table
Reference in a new issue