source: tests/concurrency/pthread/pthread_key_test.cfa@ d03a386

Last change on this file since d03a386 was d923fca, checked in by Andrew Beach <ajbeach@…>, 7 months ago

Clean-up the warnings of the concurrency tests. A lot of little test level fixes, the most interesting repeated one is some formally redundent fallthough statements. pthread_attr_test had to be rewritten because of library restrictions. Changed some types so they would always be pointer sized. There was a library change, there was a function that could not be implemented; I trust that it is included for a reason so I just put it in a comment. There is a change to the compiler, wait-until now uses goto. The labelled breaks were code generated as unlabelled breaks and although it worked out slipped through some checks. Finally, there is one warning that I cannot solve at this time so tests that produce it have been put in their own lax group.

  • Property mode set to 100644
File size: 3.7 KB
Line 
1
2/* test pthread_key_create/set_specific/get_specific
3 get specific == set specific
4 dtor is invoked (no mem leak)
5*/
6
7
8extern "C"{
9
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <errno.h>
13 #include <pthread.h>
14
15 #define THREADS 5
16 #define BUFFSZ 48
17 pthread_key_t key;
18 volatile size_t total_value;
19 volatile size_t total_value_getspec;
20 pthread_mutex_t value_mutex;
21
22 void *threadfunc(void *parm)
23 {
24 int status;
25 void *value;
26 int threadnum;
27 int *tnum;
28 void *getvalue;
29
30 tnum = (int*)parm;
31 threadnum = *tnum;
32
33 //printf("Thread %d executing\n", threadnum);
34 value = (void *)(size_t)(rand()%100);
35 status = pthread_setspecific(key, (void *) value);
36 if ( status != 0) {
37 printf("pthread_setspecific failed, thread %d, errno %d",
38 threadnum, errno);
39 return (void*)12;
40 }
41 pthread_mutex_lock(&value_mutex);
42 total_value_getspec += (size_t)value;
43 total_value += (size_t)pthread_getspecific(key);
44 pthread_mutex_unlock(&value_mutex);
45
46
47 if (!(value = malloc(BUFFSZ)))
48 printf("Thread %d could not allocate storage, errno = %d\n",
49 threadnum, errno);
50 status = pthread_setspecific(key, (void *) value);
51 if ( status != 0) {
52 printf("pthread_setspecific failed, thread %d, errno %d",
53 threadnum, errno);
54 return (void*)12;
55 }
56 //printf("Thread %d setspecific value: %d\n", threadnum, value);
57
58 getvalue = 0;
59 getvalue = pthread_getspecific(key);
60
61 if (getvalue != value) {
62 printf("getvalue not valid, getvalue=%p", getvalue);
63 return (void*)68;
64 }
65
66 pthread_exit((void *)0);
67 }
68
69 void destr_fn(void *parm)
70 {
71
72 printf("Destructor function invoked\n");
73 free(parm);
74 }
75
76
77 int main() {
78 int status;
79 int i;
80 int threadparm[THREADS];
81 pthread_t threadid[THREADS];
82 void* thread_stat[THREADS];
83
84 // rand seed for testing
85 srand(1003);
86 pthread_mutex_init(&value_mutex, NULL);
87
88 // testing getspec and setspec
89 total_value = 0;
90 total_value_getspec = 0;
91
92 if ((status = pthread_key_create(&key, destr_fn )) < 0) {
93 printf("pthread_key_create failed, errno=%d", errno);
94 exit(1);
95 }
96
97 // create 3 THREADS, pass each its number
98 for (i=0; i<THREADS; i++) {
99 threadparm[i] = i+1;
100 status = pthread_create( &threadid[i],
101 NULL,
102 threadfunc,
103 (void *)&threadparm[i]);
104 if ( status < 0) {
105 printf("pthread_create failed, errno=%d", errno);
106 exit(2);
107 }
108 }
109
110 for ( i=0; i<THREADS; i++) {
111 status = pthread_join( threadid[i], (void **)&thread_stat[i]);
112 if ( status < 0) {
113 printf("pthread_join failed, thread %d, errno=%d\n", i+1, errno);
114 }
115
116 if (thread_stat[i] != 0) {
117 printf("bad thread status, thread %d, status=%zd\n", i+1,
118 (size_t)thread_stat[i]);
119 }
120 }
121 printf("total value is %zd, total value by pthread_getspecific is %zd\n", total_value, total_value_getspec);
122 exit(0);
123 } // main
124}
125
Note: See TracBrowser for help on using the repository browser.