[20be782] | 1 |
|
---|
[428adbc] | 2 | /* test pthread_key_create/set_specific/get_specific
|
---|
[20be782] | 3 | get specific == set specific
|
---|
| 4 | dtor is invoked (no mem leak)
|
---|
| 5 | */
|
---|
| 6 |
|
---|
| 7 |
|
---|
| 8 | extern "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;
|
---|
[d923fca] | 18 | volatile size_t total_value;
|
---|
| 19 | volatile size_t total_value_getspec;
|
---|
[20be782] | 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);
|
---|
[d923fca] | 34 | value = (void *)(size_t)(rand()%100);
|
---|
[20be782] | 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);
|
---|
[d923fca] | 42 | total_value_getspec += (size_t)value;
|
---|
| 43 | total_value += (size_t)pthread_getspecific(key);
|
---|
[20be782] | 44 | pthread_mutex_unlock(&value_mutex);
|
---|
| 45 |
|
---|
| 46 |
|
---|
[d923fca] | 47 | if (!(value = malloc(BUFFSZ)))
|
---|
[20be782] | 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) {
|
---|
[d923fca] | 62 | printf("getvalue not valid, getvalue=%p", getvalue);
|
---|
[20be782] | 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) {
|
---|
[d923fca] | 117 | printf("bad thread status, thread %d, status=%zd\n", i+1,
|
---|
| 118 | (size_t)thread_stat[i]);
|
---|
[20be782] | 119 | }
|
---|
| 120 | }
|
---|
[d923fca] | 121 | printf("total value is %zd, total value by pthread_getspecific is %zd\n", total_value, total_value_getspec);
|
---|
[20be782] | 122 | exit(0);
|
---|
| 123 | } // main
|
---|
| 124 | }
|
---|
| 125 |
|
---|