]> git.saurik.com Git - apple/libpthread.git/blame - tests/pthread_threadid_np.c
libpthread-330.230.1.tar.gz
[apple/libpthread.git] / tests / pthread_threadid_np.c
CommitLineData
2546420a
A
1#include <pthread.h>
2#include <pthread/private.h>
3#include <dispatch/dispatch.h>
c28b7a9d
A
4#include <sys/wait.h>
5#include <stdlib.h>
2546420a 6
a0619f9c 7#include "darwintest_defaults.h"
2546420a
A
8
9extern __uint64_t __thread_selfid( void );
10
11static void *do_test(void * __unused arg)
12{
13 uint64_t threadid = __thread_selfid();
214d78a2 14 T_ASSERT_NE(threadid, (uint64_t)0, "__thread_selfid()");
2546420a
A
15
16 uint64_t pth_threadid = 0;
17 T_ASSERT_POSIX_ZERO(pthread_threadid_np(NULL, &pth_threadid), NULL);
18 T_ASSERT_POSIX_ZERO(pthread_threadid_np(pthread_self(), &pth_threadid), NULL);
19 T_EXPECT_EQ(threadid, pth_threadid, "pthread_threadid_np()");
20
21 pth_threadid = _pthread_threadid_self_np_direct();
22 T_EXPECT_EQ(threadid, pth_threadid, "pthread_threadid_np_direct()");
23
24 return NULL;
25}
26
27T_DECL(pthread_threadid_np, "pthread_threadid_np",
28 T_META_ALL_VALID_ARCHS(YES))
29{
30 T_LOG("Main Thread");
31 do_test(NULL);
32
33 T_LOG("Pthread");
34 pthread_t pth;
35 T_ASSERT_POSIX_ZERO(pthread_create(&pth, NULL, do_test, NULL), NULL);
36 T_ASSERT_POSIX_ZERO(pthread_join(pth, NULL), NULL);
37
38 T_LOG("Workqueue Thread");
39 dispatch_queue_t dq = dispatch_queue_create("myqueue", NULL);
40 dispatch_async(dq, ^{ do_test(NULL); });
41 dispatch_sync(dq, ^{});
42
43 T_LOG("Workqueue Thread Reuse");
44 dispatch_async(dq, ^{ do_test(NULL); });
45 dispatch_sync(dq, ^{});
46}
c28b7a9d
A
47
48T_DECL(pthread_threadid_fork, "pthread_threadid_np post-fork test")
49{
50 uint64_t tid = __thread_selfid();
51 T_ASSERT_NE(tid, (uint64_t)0, "__thread_selfid()");
52
53 uint64_t ptid = 0;
54 T_ASSERT_POSIX_ZERO(pthread_threadid_np(NULL, &ptid), NULL);
55 T_ASSERT_EQ(ptid, tid, NULL);
56
57 pid_t pid = fork();
58 if (pid == 0) {
59 // child
60 uint64_t ntid = __thread_selfid();
61 if (ntid == tid) {
62 T_LOG("FAIL: forked child tid is equal to parent tid");
63 exit(1);
64 }
65
66 uint64_t nptid = 0;
67 if (pthread_threadid_np(NULL, &nptid) != 0) {
68 T_LOG("FAIL: pthread_threadid_np: %d", errno);
69 exit(1);
70 }
71
72 if (nptid != ntid) {
73 T_LOG("FAIL: pthread_threadid_np == tid (expected: %lld == %lld)",
74 nptid, ntid);
75 exit(1);
76 }
77 exit(0);
78 } else {
79 int status;
80 T_ASSERT_EQ(waitpid(pid, &status, 0), pid, NULL);
81 int exitstatus = WEXITSTATUS(status);
82 T_ASSERT_EQ(exitstatus, 0, NULL);
83 }
84}