Skip to content

Commit 9cdb2ab

Browse files
authored
Merge pull request #167 from linkmauve/thread-names
Add a helper function to set thread name in rthread
2 parents b560397 + daa6ad1 commit 9cdb2ab

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

include/rthreads/rthreads.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,6 @@ int sthread_detach(sthread_t *thread);
9090
* @thread to terminate. If that thread has already terminated, then
9191
* it will return immediately. The thread specified by @thread must
9292
* be joinable.
93-
*
94-
* Returns: 0 on success, otherwise it returns a non-zero error number.
9593
*/
9694
void sthread_join(sthread_t *thread);
9795

@@ -103,6 +101,16 @@ void sthread_join(sthread_t *thread);
103101
*/
104102
bool sthread_isself(sthread_t *thread);
105103

104+
/**
105+
* sthread_set_name:
106+
* @thread : pointer to thread object
107+
* @name : name to define for the thread (at most
108+
* 15 bytes)
109+
*
110+
* Set the thread name, useful for debugging.
111+
*/
112+
void sthread_setname(sthread_t *thread, const char *name);
113+
106114
/**
107115
* slock_new:
108116
*

rthreads/rthreads.c

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,8 +285,6 @@ int sthread_detach(sthread_t *thread)
285285
* @thread to terminate. If that thread has already terminated, then
286286
* it will return immediately. The thread specified by @thread must
287287
* be joinable.
288-
*
289-
* Returns: 0 on success, otherwise it returns a non-zero error number.
290288
*/
291289
void sthread_join(sthread_t *thread)
292290
{
@@ -320,6 +318,24 @@ bool sthread_isself(sthread_t *thread)
320318
#endif
321319
}
322320

321+
/**
322+
* sthread_set_name:
323+
* @thread : pointer to thread object
324+
* @name : name to define for the thread (at most
325+
* 15 bytes)
326+
*
327+
* Set the thread name, useful for debugging.
328+
*/
329+
void sthread_setname(sthread_t *thread, const char *name)
330+
{
331+
if (!thread)
332+
return;
333+
// TODO: implement that for Windows too.
334+
#ifndef USE_WIN32_THREADS
335+
pthread_setname_np(thread->id, name);
336+
#endif
337+
}
338+
323339
/**
324340
* slock_new:
325341
*

0 commit comments

Comments
 (0)