Skip to content

Instantly share code, notes, and snippets.

@valpackett
Created November 9, 2020 21:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save valpackett/38db360a5e50babd443f95160efb1c44 to your computer and use it in GitHub Desktop.
Save valpackett/38db360a5e50babd443f95160efb1c44 to your computer and use it in GitHub Desktop.
Thread ID testing on FreeBSD
% cc -pthread -o /tmp/tid tids.c && truss /tmp/tid
[…]
__sysctl("vm.overcommit",2,0x7fffffffa314,0x7fffffffa308,0x0,0) = 0 (0x0)
mmap(0x0,2097152,PROT_READ|PROT_WRITE,MAP_PRIVATE|MAP_ANON|MAP_ALIGNED(21),-1,0x0) = 34374418432 (0x800e00000)
mmap(0x0,2097152,PROT_READ|PROT_WRITE,MAP_PRIVATE|MAP_ANON|MAP_ALIGNED(12),-1,0x0) = 34410536960 (0x803072000)
mmap(0x0,4194304,PROT_READ|PROT_WRITE,MAP_PRIVATE|MAP_ANON|MAP_ALIGNED(21),-1,0x0) = 34422652928 (0x803c00000)
__sysctl("kern.usrstack",2,0x800ac8108,0x7fffffffc228,0x0,0) = 0 (0x0)
getrlimit(RLIMIT_STACK,{ cur=536870912,max=536870912 }) = 0 (0x0)
thr_self(0x803084000) = 0 (0x0)
mmap(0x7fffdfffd000,4096,PROT_NONE,MAP_ANON,-1,0x0) = 140736951472128 (0x7fffdfffd000)
rtprio_thread(RTP_LOOKUP,103046,0x7fffffffc1e8) = 0 (0x0)
sigaction(SIGTHR,{ 0x800ac0130 SA_SIGINFO ss_t },0x0) = 0 (0x0)
sigprocmask(SIG_UNBLOCK,{ },0x0) = 0 (0x0)
_umtx_op(0x7fffffffc1f0,UMTX_OP_WAKE,0x1,0x0,0x0) = 0 (0x0)
mprotect(0x0,0,PROT_NONE) = 0 (0x0)
getpid() = 80933 (0x13c25)
getpid() = 80933 (0x13c25)
sigprocmask(SIG_BLOCK,{ SIGHUP|SIGINT|SIGQUIT|SIGILL|SIGTRAP|SIGABRT|SIGEMT|SIGFPE|SIGKILL|SIGBUS|SIGSEGV|SIGSYS|SIGPIPE|SIGALRM|SIGTERM|SIGURG|SIGSTOP|SIGTSTP|SIGCONT|SIGCHLD|SIGTTIN|SIGTTOU|SIGIO|SIGXCPU|SIGXFSZ|SIGVTALRM|SIGPROF|SIGWINCH|SIGINFO|SIGUSR1|SIGUSR2 },{ }) = 0 (0x0)
sigfastblock(0x3,0x0) = 0 (0x0)
sigprocmask(SIG_SETMASK,{ },0x0) = 0 (0x0)
sigfastblock(0x1,0x803084038) = 0 (0x0)
getcontext(0x7fffffffbcf0) = 0 (0x0)
sysarch(AMD64_GET_XFPUSTATE,0x7fffffffbcb8) = 0 (0x0)
mprotect(0x202000,4096,PROT_READ) = 0 (0x0)
fstat(1,{ mode=crw--w---- ,inode=493,size=0,blksize=4096 }) = 0 (0x0)
ioctl(1,TIOCGETA,0x7fffffffcb10) = 0 (0x0)
pthread_getthreadid_np 103046
write(1,"pthread_getthreadid_np 103046\n",30) = 30 (0x1e)
thr_self(0x7fffffffd1c0) = 0 (0x0)
tid 103046
write(1,"tid 103046\n",11) = 11 (0xb)
rtprio_thread(RTP_SET,103046,0x7fffffffd1b8) = 0 (0x0)
exit(0x0)
#include <assert.h>
#include <stdio.h>
#include <sys/thr.h>
#include <sys/types.h>
#include <sys/rtprio.h>
#include <pthread.h>
#include <pthread_np.h>
// Don't forget -lthr or -pthread
int main() {
printf("pthread_getthreadid_np %d\n", pthread_getthreadid_np());
long tid;
assert(thr_self(&tid) == 0);
printf("tid %ld\n", tid);
struct rtprio prio = { .type = RTP_PRIO_NORMAL, .prio = 6 };
assert(rtprio_thread(RTP_SET, tid, &prio) == 0);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment