forked from emscripten-core/emscripten
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspecific.c
93 lines (75 loc) · 2.52 KB
/
specific.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
* Copyright 2013 The Emscripten Authors. All rights reserved.
* Emscripten is available under two separate licenses, the MIT license and the
* University of Illinois/NCSA Open Source License. Both these licenses can be
* found in the LICENSE file.
*/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <errno.h>
#include <pthread.h>
int main(void)
{
pthread_key_t key = 0;
int rv;
int data = 123;
int *data2;
assert(pthread_key_delete(key) != 0);
assert(pthread_getspecific(key) == NULL);
rv = pthread_key_create(&key, NULL);
printf("pthread_key_create = %d\n", rv);
assert(rv == 0);
assert(pthread_getspecific(key) == NULL);
rv = pthread_setspecific(key, (void*) &data);
printf("pthread_setspecific = %d\n", rv);
assert(rv == 0);
data2 = pthread_getspecific(key);
assert(data2 != NULL);
printf("pthread_getspecific = %d\n", *data2);
assert(*data2 == 123);
rv = pthread_setspecific(key, NULL);
printf("valid pthread_setspecific for value NULL = %d\n", rv);
assert(rv == 0);
data2 = pthread_getspecific(key);
assert(data2 == NULL);
printf("pthread_getspecific = %p\n", data2);
rv = pthread_key_create(&key, NULL);
data2 = pthread_getspecific(key);
printf("pthread_getspecific after key recreate = %p\n", data2);
assert(data2 == NULL);
rv = pthread_key_delete(key);
printf("pthread_key_delete = %d\n", rv);
assert(rv == 0);
rv = pthread_key_delete(key);
printf("pthread_key_delete repeated = %d\n", rv);
assert(rv == EINVAL);
rv = pthread_setspecific(key, NULL);
printf("pthread_setspecific for value NULL = %d\n", rv);
assert(rv == EINVAL);
rv = pthread_key_create(&key, NULL);
assert(rv == 0);
rv = pthread_key_delete(key);
printf("pthread_key_delete just after created = %d\n", rv);
assert(rv == 0);
{
/* Test creating multiple keys and overflowing the tls_entries array*/
const int n = 5;
int i;
pthread_key_t *keys = malloc(sizeof (pthread_key_t) * n);
for (i = 0; i < n; ++i) {
rv = pthread_key_create(&keys[i], NULL);
assert(rv == 0);
rv = pthread_setspecific(keys[i], (void*)(intptr_t)(i + 1));
assert(rv == 0);
}
for (i = 0; i < n; ++i) {
void *d = pthread_getspecific(keys[i]);
assert (i+1 == (intptr_t)d);
rv = pthread_key_delete(keys[i]);
assert(rv == 0);
}
free (keys);
}
return 0;
}