-
Notifications
You must be signed in to change notification settings - Fork 0
/
threadname.c
47 lines (32 loc) · 926 Bytes
/
threadname.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#define _GNU_SOURCE
#include <pthread.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include "macro.h"
#include "threadname.h"
int threadname_get (char *buf, int size) {
return pthread_getname_np(pthread_self(), buf, size);
}
int threadname_set (const char *name) {
return pthread_setname_np(pthread_self(), name);
}
int threadname_format (const char *format, ...) {
char name[THREADNAME_SIZE];
va_list ap;
va_start(ap, format);
vsnprintf(name, sizeof(name), format, ap);
va_end(ap);
return threadname_set(name);
}
int threadname_append (const char *format, ...) {
pthread_t thread = pthread_self();
char name[THREADNAME_SIZE];
return_nonzero (pthread_getname_np(thread, name, sizeof(name)));
int len = strlen(name);
va_list ap;
va_start(ap, format);
vsnprintf(name + len, sizeof(name) - len, format, ap);
va_end(ap);
return pthread_setname_np(thread, name);
}