]> git.saurik.com Git - apple/libpthread.git/blob - tests/tsd.c
d20dcf156db087695ffd019c9d157897d52732dc
[apple/libpthread.git] / tests / tsd.c
1 #include <pthread.h>
2 #include <stdio.h>
3 #include <sys/sysctl.h>
4
5 #include "darwintest_defaults.h"
6
7 static void *ptr = NULL;
8
9 static void destructor(void *value)
10 {
11 ptr = value;
12 }
13
14 static void *thread(void *param)
15 {
16 pthread_key_t key = *(pthread_key_t *)param;
17 T_ASSERT_POSIX_ZERO(pthread_setspecific(key, (void *)0x12345678), NULL);
18 void *value = pthread_getspecific(key);
19
20 T_ASSERT_POSIX_ZERO(pthread_key_create(&key, NULL), NULL);
21 T_ASSERT_POSIX_ZERO(pthread_setspecific(key, (void *)0x55555555), NULL);
22
23 return value;
24 }
25
26 T_DECL(tsd, "tsd",
27 T_META_ALL_VALID_ARCHS(YES))
28 {
29 pthread_key_t key;
30
31 T_ASSERT_POSIX_ZERO(pthread_key_create(&key, destructor), NULL);
32 T_LOG("key = %ld", key);
33
34 pthread_t p = NULL;
35 T_ASSERT_POSIX_ZERO(pthread_create(&p, NULL, thread, &key), NULL);
36
37 void *value = NULL;
38 T_ASSERT_POSIX_ZERO(pthread_join(p, &value), NULL);
39 T_LOG("value = %p; ptr = %p\n", value, ptr);
40
41 T_EXPECT_EQ(ptr, value, NULL);
42
43 T_ASSERT_POSIX_ZERO(pthread_key_delete(key), NULL);
44 }
45
46 static uint32_t
47 get_ncpu(void)
48 {
49 static uint32_t activecpu;
50 if (!activecpu) {
51 uint32_t n;
52 size_t s = sizeof(activecpu);
53 sysctlbyname("hw.activecpu", &n, &s, NULL, 0);
54 activecpu = n;
55 }
56 return activecpu;
57 }
58
59 T_DECL(cpuid, "cpu id", T_META_ALL_VALID_ARCHS(YES))
60 {
61 pthread_t child;
62
63 size_t cpu_id;
64 if (pthread_cpu_number_np(&cpu_id)) {
65 T_FAIL("Should not fail to get CPU id");
66 }
67
68 T_ASSERT_LE(cpu_id, get_ncpu(), "Got a valid CPU id");
69 }