Browse Source

Use reentrant mutex for core thread

pull/1/head
Xadhoom 5 years ago committed by staphen
parent
commit
a57a8a2130
  1. 12
      ports/unix/port/sys_arch.c
  2. 6
      ports/win32/sys_arch.c

12
ports/unix/port/sys_arch.c

@ -204,15 +204,19 @@ sys_thread_new(const char *name, lwip_thread_fn function, void *arg, int stacksi
#if LWIP_TCPIP_CORE_LOCKING
static pthread_t lwip_core_lock_holder_thread_id;
static int lwip_core_lock_holder_reclevel = 0;
void sys_lock_tcpip_core(void)
{
sys_mutex_lock(&lock_tcpip_core);
lwip_core_lock_holder_thread_id = pthread_self();
lwip_core_lock_holder_reclevel += 1;
}
void sys_unlock_tcpip_core(void)
{
lwip_core_lock_holder_thread_id = 0;
lwip_core_lock_holder_reclevel -= 1;
if(!lwip_core_lock_holder_reclevel)
lwip_core_lock_holder_thread_id = 0;
sys_mutex_unlock(&lock_tcpip_core);
}
#endif /* LWIP_TCPIP_CORE_LOCKING */
@ -591,10 +595,14 @@ err_t
sys_mutex_new(struct sys_mutex **mutex)
{
struct sys_mutex *mtx;
pthread_mutexattr_t attr;
mtx = (struct sys_mutex *)malloc(sizeof(struct sys_mutex));
if (mtx != NULL) {
pthread_mutex_init(&(mtx->mutex), NULL);
pthread_mutexattr_init(&attr);
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
pthread_mutex_init(&(mtx->mutex), &attr);
*mutex = mtx;
return ERR_OK;
}

6
ports/win32/sys_arch.c

@ -470,18 +470,22 @@ sys_thread_new(const char *name, lwip_thread_fn function, void *arg, int stacksi
#if LWIP_TCPIP_CORE_LOCKING
static DWORD lwip_core_lock_holder_thread_id;
static int lwip_core_lock_holder_reclevel = 0;
void
sys_lock_tcpip_core(void)
{
sys_mutex_lock(&lock_tcpip_core);
lwip_core_lock_holder_thread_id = GetCurrentThreadId();
lwip_core_lock_holder_reclevel += 1;
}
void
sys_unlock_tcpip_core(void)
{
lwip_core_lock_holder_thread_id = 0;
lwip_core_lock_holder_reclevel -= 1;
if(!lwip_core_lock_holder_reclevel)
lwip_core_lock_holder_thread_id = 0;
sys_mutex_unlock(&lock_tcpip_core);
}
#endif /* LWIP_TCPIP_CORE_LOCKING */

Loading…
Cancel
Save