Skip to content

Commit

Permalink
thread: add thread name handling
Browse files Browse the repository at this point in the history
  • Loading branch information
sreimers committed Feb 26, 2023
1 parent 196df49 commit 04ef913
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 4 deletions.
5 changes: 5 additions & 0 deletions cmake/re-config.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ if(UNIX)
endif()
endif()

check_include_file(sys/prctl.h HAVE_PRCTL)
if(HAVE_PRCTL)
list(APPEND RE_DEFINITIONS -DHAVE_PRCTL)
endif()


list(APPEND RE_DEFINITIONS
-DHAVE_ATOMIC
Expand Down
5 changes: 4 additions & 1 deletion src/sdp/media.c
Original file line number Diff line number Diff line change
Expand Up @@ -273,9 +273,12 @@ void sdp_media_align_formats(struct sdp_media *m, bool offer)
lfmt->sup = false;
}

for (rle=m->rfmtl.head; rle; rle=rle->next) {
rle = m->rfmtl.head;
while (rle)
{

rfmt = rle->data;
rle = rle->next;

for (lle=m->lfmtl.head; lle; lle=lle->next) {

Expand Down
59 changes: 56 additions & 3 deletions src/thread/thread.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,27 @@
#include <re_types.h>
#include <re_fmt.h>
#include <re_mem.h>
#include <re_thread.h>
#ifdef HAVE_PRCTL
#include <sys/prctl.h>
#endif
#ifdef WIN32
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <windows.h>
#include <processthreadsapi.h>
#endif
#ifdef HAVE_PTHREAD
#include <pthread.h>
#endif


struct thread {
const char *name;
thrd_start_t func;
void *arg;
};


static void mutex_destructor(void *data)
Expand Down Expand Up @@ -41,19 +62,51 @@ int mutex_alloc(mtx_t **mtx)
}


static int handler(void *p)
{
struct thread th = *(struct thread *)p;

mem_deref(p);

#ifdef HAVE_PRCTL
(void)prctl(PR_SET_NAME, th.name);
#elif defined(WIN32)
wchar_t *name = str_wchar(th.name);
if (name) {
(void)SetThreadDescription(GetCurrentThread(), name);
mem_deref(name);
}
#elif defined(HAVE_PTHREAD)
(void)pthread_setname_np(th.name);
#endif

return th.func(th.arg);
}


int thread_create_name(thrd_t *thr, const char *name, thrd_start_t func,
void *arg)
void *arg)
{
struct thread *th;
int ret;
(void)name;

if (!thr || !func)
return EINVAL;

ret = thrd_create(thr, func, arg);
th = mem_alloc(sizeof(struct thread), NULL);
if (!th)
return ENOMEM;

th->name = name;
th->func = func;
th->arg = arg;

ret = thrd_create(thr, handler, th);
if (ret == thrd_success)
return 0;

mem_deref(th);

if (ret == thrd_nomem)
return ENOMEM;

Expand Down

0 comments on commit 04ef913

Please sign in to comment.