]>
git.saurik.com Git - apple/libpthread.git/blob - tests/pthread_threadid_np.c
2 #include <pthread/private.h>
3 #include <dispatch/dispatch.h>
7 #include "darwintest_defaults.h"
9 extern __uint64_t
__thread_selfid( void );
11 static void *do_test(void * arg
)
13 uint64_t threadid
= __thread_selfid();
14 T_ASSERT_NE(threadid
, (uint64_t)0, "__thread_selfid()");
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()");
21 pth_threadid
= _pthread_threadid_self_np_direct();
22 T_EXPECT_EQ(threadid
, pth_threadid
, "pthread_threadid_np_direct()");
25 *(uint64_t *)arg
= pth_threadid
;
30 T_DECL(pthread_threadid_np
, "pthread_threadid_np",
31 T_META_ALL_VALID_ARCHS(YES
))
37 for (int i
= 0; i
< 100; i
++) {
38 uint64_t tid1
= 0, tid2
= 0;
40 T_ASSERT_POSIX_ZERO(pthread_create(&pth
, NULL
, do_test
, &tid1
), NULL
);
41 T_EXPECT_POSIX_ZERO(pthread_threadid_np(pth
, &tid2
), NULL
);
42 T_ASSERT_POSIX_ZERO(pthread_join(pth
, NULL
), NULL
);
43 T_EXPECT_EQ(tid1
, tid2
, "parent and child agree");
46 T_LOG("Workqueue Thread");
47 dispatch_queue_t dq
= dispatch_queue_create("myqueue", NULL
);
48 dispatch_async(dq
, ^{ do_test(NULL
); });
49 dispatch_sync(dq
, ^{});
51 T_LOG("Workqueue Thread Reuse");
52 dispatch_async(dq
, ^{ do_test(NULL
); });
53 dispatch_sync(dq
, ^{});
56 T_DECL(pthread_threadid_fork
, "pthread_threadid_np post-fork test")
58 uint64_t tid
= __thread_selfid();
59 T_ASSERT_NE(tid
, (uint64_t)0, "__thread_selfid()");
62 T_ASSERT_POSIX_ZERO(pthread_threadid_np(NULL
, &ptid
), NULL
);
63 T_ASSERT_EQ(ptid
, tid
, NULL
);
68 uint64_t ntid
= __thread_selfid();
70 T_LOG("FAIL: forked child tid is equal to parent tid");
75 if (pthread_threadid_np(NULL
, &nptid
) != 0) {
76 T_LOG("FAIL: pthread_threadid_np: %d", errno
);
81 T_LOG("FAIL: pthread_threadid_np == tid (expected: %lld == %lld)",
88 T_ASSERT_EQ(waitpid(pid
, &status
, 0), pid
, NULL
);
89 int exitstatus
= WEXITSTATUS(status
);
90 T_ASSERT_EQ(exitstatus
, 0, NULL
);