Skip to content

Commit

Permalink
contrib/ports: Add semaphore per thread option for unix
Browse files Browse the repository at this point in the history
  • Loading branch information
david-cermak authored and goldsimon committed May 10, 2023
1 parent 3a9c1e4 commit a6874a4
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
9 changes: 9 additions & 0 deletions contrib/ports/unix/port/include/arch/sys_arch.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@ typedef struct sys_mbox * sys_mbox_t;
struct sys_thread;
typedef struct sys_thread * sys_thread_t;

#if LWIP_NETCONN_SEM_PER_THREAD
sys_sem_t* sys_arch_netconn_sem_get(void);
void sys_arch_netconn_sem_alloc(void);
void sys_arch_netconn_sem_free(void);
#define LWIP_NETCONN_THREAD_SEM_GET() sys_arch_netconn_sem_get()
#define LWIP_NETCONN_THREAD_SEM_ALLOC() sys_arch_netconn_sem_alloc()
#define LWIP_NETCONN_THREAD_SEM_FREE() sys_arch_netconn_sem_free()
#endif /* #if LWIP_NETCONN_SEM_PER_THREAD */

#define LWIP_EXAMPLE_APP_ABORT() lwip_unix_keypressed()
int lwip_unix_keypressed(void);

Expand Down
69 changes: 69 additions & 0 deletions contrib/ports/unix/port/sys_arch.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@
#include "lwip/stats.h"
#include "lwip/tcpip.h"

#if LWIP_NETCONN_SEM_PER_THREAD
/* pthread key to *our* thread local storage entry */
static pthread_key_t sys_thread_sem_key;
#endif

/* Return code for an interrupted timed wait */
#define SYS_ARCH_INTR 0xfffffffeUL

Expand Down Expand Up @@ -664,6 +669,67 @@ sys_mutex_free(struct sys_mutex **mutex)

#endif /* !NO_SYS */

#if LWIP_NETCONN_SEM_PER_THREAD
/*-----------------------------------------------------------------------------------*/
/* Semaphore per thread located TLS */

static void
sys_thread_sem_free(void* data)
{
sys_sem_t *sem = (sys_sem_t*)(data);

if (sem) {
sys_sem_free(sem);
free(sem);
}
}

static sys_sem_t*
sys_thread_sem_alloc(void)
{
sys_sem_t *sem;
err_t err;
int ret;

sem = (sys_sem_t*)malloc(sizeof(sys_sem_t*));
LWIP_ASSERT("failed to allocate memory for TLS semaphore", sem != NULL);
err = sys_sem_new(sem, 0);
LWIP_ASSERT("failed to initialise TLS semaphore", err == ERR_OK);
ret = pthread_setspecific(sys_thread_sem_key, sem);
LWIP_ASSERT("failed to initialise TLS semaphore storage", ret == 0);
return sem;
}

sys_sem_t*
sys_arch_netconn_sem_get(void)
{
sys_sem_t* sem = (sys_sem_t*)pthread_getspecific(sys_thread_sem_key);
if (!sem) {
sem = sys_thread_sem_alloc();
}
LWIP_DEBUGF(SYS_DEBUG, ("sys_thread_sem_get s=%p\n", (void*)sem));
return sem;
}

void
sys_arch_netconn_sem_alloc(void)
{
sys_sem_t* sem = sys_thread_sem_alloc();
LWIP_DEBUGF(SYS_DEBUG, ("sys_thread_sem created s=%p\n", (void*)sem));
}

void
sys_arch_netconn_sem_free(void)
{
int ret;

sys_sem_t *sem = (sys_sem_t *)pthread_getspecific(sys_thread_sem_key);
sys_thread_sem_free(sem);
ret = pthread_setspecific(sys_thread_sem_key, NULL);
LWIP_ASSERT("failed to de-init TLS semaphore storage", ret == 0);
}
#endif /* LWIP_NETCONN_SEM_PER_THREAD */

/*-----------------------------------------------------------------------------------*/
/* Time */
u32_t
Expand Down Expand Up @@ -695,6 +761,9 @@ sys_jiffies(void)
void
sys_init(void)
{
#if LWIP_NETCONN_SEM_PER_THREAD
pthread_key_create(&sys_thread_sem_key, sys_thread_sem_free);
#endif
}

/*-----------------------------------------------------------------------------------*/
Expand Down

0 comments on commit a6874a4

Please sign in to comment.