]> git.saurik.com Git - apple/libpthread.git/blame - tests/tsd.c
libpthread-454.40.3.tar.gz
[apple/libpthread.git] / tests / tsd.c
CommitLineData
964d3577
A
1#include <pthread.h>
2#include <stdio.h>
c1f56ec9 3#include <sys/sysctl.h>
964d3577 4
a0619f9c 5#include "darwintest_defaults.h"
964d3577 6
2546420a
A
7static void *ptr = NULL;
8
9static void destructor(void *value)
964d3577
A
10{
11 ptr = value;
12}
13
2546420a 14static void *thread(void *param)
964d3577 15{
964d3577 16 pthread_key_t key = *(pthread_key_t *)param;
2546420a 17 T_ASSERT_POSIX_ZERO(pthread_setspecific(key, (void *)0x12345678), NULL);
964d3577
A
18 void *value = pthread_getspecific(key);
19
2546420a
A
20 T_ASSERT_POSIX_ZERO(pthread_key_create(&key, NULL), NULL);
21 T_ASSERT_POSIX_ZERO(pthread_setspecific(key, (void *)0x55555555), NULL);
964d3577
A
22
23 return value;
24}
25
2546420a 26T_DECL(tsd, "tsd",
a0619f9c 27 T_META_ALL_VALID_ARCHS(YES))
964d3577 28{
964d3577
A
29 pthread_key_t key;
30
2546420a
A
31 T_ASSERT_POSIX_ZERO(pthread_key_create(&key, destructor), NULL);
32 T_LOG("key = %ld", key);
964d3577
A
33
34 pthread_t p = NULL;
2546420a 35 T_ASSERT_POSIX_ZERO(pthread_create(&p, NULL, thread, &key), NULL);
964d3577
A
36
37 void *value = NULL;
2546420a
A
38 T_ASSERT_POSIX_ZERO(pthread_join(p, &value), NULL);
39 T_LOG("value = %p; ptr = %p\n", value, ptr);
964d3577 40
2546420a 41 T_EXPECT_EQ(ptr, value, NULL);
964d3577 42
2546420a 43 T_ASSERT_POSIX_ZERO(pthread_key_delete(key), NULL);
964d3577 44}
c1f56ec9
A
45
46static uint32_t
47get_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
59T_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}