mirror of
https://github.com/warmcat/libwebsockets.git
synced 2025-03-30 00:00:16 +01:00
logs: lock log context refcount changes
Enabling the locking requires -DLWS_MAX_SMP=2 or more.
This commit is contained in:
parent
f99bf7748c
commit
310c8baa81
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_t emit; /* legacy emit function */
|
||||||
lws_log_emit_cx_t emit_cx; /* LLLF_LOG_CONTEXT_AWARE */
|
lws_log_emit_cx_t emit_cx; /* LLLF_LOG_CONTEXT_AWARE */
|
||||||
} u;
|
} u;
|
||||||
|
|
||||||
|
#if LWS_MAX_SMP > 1
|
||||||
|
pthread_mutex_t refcount_lock;
|
||||||
|
#endif
|
||||||
|
|
||||||
lws_log_use_cx_t refcount_cb;
|
lws_log_use_cx_t refcount_cb;
|
||||||
/**< NULL, or a function called after each change to .refcount below,
|
/**< NULL, or a function called after each change to .refcount below,
|
||||||
* this enables implementing side-effects like opening and closing
|
* 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 */
|
/**< mask of log levels we want to emit in this context */
|
||||||
int32_t refcount;
|
int32_t refcount;
|
||||||
/**< refcount of objects bound to this log context */
|
/**< refcount of objects bound to this log context */
|
||||||
|
#if LWS_MAX_SMP > 1
|
||||||
|
char inited;
|
||||||
|
#endif
|
||||||
} lws_log_cx_t;
|
} lws_log_cx_t;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -506,9 +506,24 @@ int lwsl_visible_cx(lws_log_cx_t *cx, int level)
|
||||||
void
|
void
|
||||||
lwsl_refcount_cx(lws_log_cx_t *cx, int _new)
|
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)
|
if (!cx)
|
||||||
return;
|
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)
|
if (_new > 0)
|
||||||
cx->refcount++;
|
cx->refcount++;
|
||||||
else {
|
else {
|
||||||
|
@ -518,6 +533,10 @@ lwsl_refcount_cx(lws_log_cx_t *cx, int _new)
|
||||||
|
|
||||||
if (cx->refcount_cb)
|
if (cx->refcount_cb)
|
||||||
cx->refcount_cb(cx, _new);
|
cx->refcount_cb(cx, _new);
|
||||||
|
|
||||||
|
#if LWS_MAX_SMP > 1
|
||||||
|
lws_pthread_mutex_unlock(&cx->refcount_lock);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
|
|
Loading…
Add table
Reference in a new issue