1*10465441SEvalZero /* Making a library function that uses static variables thread-safe.
2*10465441SEvalZero Illustrates: thread-specific data, pthread_once(). */
3*10465441SEvalZero
4*10465441SEvalZero #include <stddef.h>
5*10465441SEvalZero #include <stdio.h>
6*10465441SEvalZero #include <stdlib.h>
7*10465441SEvalZero #include <string.h>
8*10465441SEvalZero #include <pthread.h>
9*10465441SEvalZero
10*10465441SEvalZero /* This is a typical example of a library function that uses
11*10465441SEvalZero static variables to accumulate results between calls.
12*10465441SEvalZero Here, it just returns the concatenation of all string arguments
13*10465441SEvalZero that were given to it. */
14*10465441SEvalZero
15*10465441SEvalZero #if 0
16*10465441SEvalZero
17*10465441SEvalZero char * str_accumulate(char * s)
18*10465441SEvalZero {
19*10465441SEvalZero static char accu[1024] = { 0 };
20*10465441SEvalZero strcat(accu, s);
21*10465441SEvalZero return accu;
22*10465441SEvalZero }
23*10465441SEvalZero
24*10465441SEvalZero #endif
25*10465441SEvalZero
26*10465441SEvalZero /* Of course, this cannot be used in a multi-threaded program
27*10465441SEvalZero because all threads store "accu" at the same location.
28*10465441SEvalZero So, we'll use thread-specific data to have a different "accu"
29*10465441SEvalZero for each thread. */
30*10465441SEvalZero
31*10465441SEvalZero /* Key identifying the thread-specific data */
32*10465441SEvalZero static pthread_key_t str_key;
33*10465441SEvalZero /* "Once" variable ensuring that the key for str_alloc will be allocated
34*10465441SEvalZero exactly once. */
35*10465441SEvalZero static pthread_once_t str_alloc_key_once = PTHREAD_ONCE_INIT;
36*10465441SEvalZero
37*10465441SEvalZero /* Forward functions */
38*10465441SEvalZero static void str_alloc_key(void);
39*10465441SEvalZero static void str_alloc_destroy_accu(void * accu);
40*10465441SEvalZero
41*10465441SEvalZero /* Thread-safe version of str_accumulate */
42*10465441SEvalZero
str_accumulate(const char * s)43*10465441SEvalZero char * str_accumulate(const char * s)
44*10465441SEvalZero {
45*10465441SEvalZero char * accu;
46*10465441SEvalZero
47*10465441SEvalZero /* Make sure the key is allocated */
48*10465441SEvalZero pthread_once(&str_alloc_key_once, str_alloc_key);
49*10465441SEvalZero /* Get the thread-specific data associated with the key */
50*10465441SEvalZero accu = (char *) pthread_getspecific(str_key);
51*10465441SEvalZero /* It's initially NULL, meaning that we must allocate the buffer first. */
52*10465441SEvalZero if (accu == NULL) {
53*10465441SEvalZero accu = malloc(1024);
54*10465441SEvalZero if (accu == NULL) return NULL;
55*10465441SEvalZero accu[0] = 0;
56*10465441SEvalZero /* Store the buffer pointer in the thread-specific data. */
57*10465441SEvalZero pthread_setspecific(str_key, (void *) accu);
58*10465441SEvalZero printf("Thread %lx: allocating buffer at %p\n", pthread_self(), accu);
59*10465441SEvalZero }
60*10465441SEvalZero /* Now we can use accu just as in the non thread-safe code. */
61*10465441SEvalZero strcat(accu, s);
62*10465441SEvalZero return accu;
63*10465441SEvalZero }
64*10465441SEvalZero
65*10465441SEvalZero /* Function to allocate the key for str_alloc thread-specific data. */
66*10465441SEvalZero
str_alloc_key(void)67*10465441SEvalZero static void str_alloc_key(void)
68*10465441SEvalZero {
69*10465441SEvalZero pthread_key_create(&str_key, str_alloc_destroy_accu);
70*10465441SEvalZero printf("Thread %lx: allocated key %d\n", pthread_self(), str_key);
71*10465441SEvalZero }
72*10465441SEvalZero
73*10465441SEvalZero /* Function to free the buffer when the thread exits. */
74*10465441SEvalZero /* Called only when the thread-specific data is not NULL. */
75*10465441SEvalZero
str_alloc_destroy_accu(void * accu)76*10465441SEvalZero static void str_alloc_destroy_accu(void * accu)
77*10465441SEvalZero {
78*10465441SEvalZero printf("Thread %lx: freeing buffer at %p\n", pthread_self(), accu);
79*10465441SEvalZero free(accu);
80*10465441SEvalZero }
81*10465441SEvalZero
82*10465441SEvalZero /* Test program */
83*10465441SEvalZero
process(void * arg)84*10465441SEvalZero static void *process(void * arg)
85*10465441SEvalZero {
86*10465441SEvalZero char *res;
87*10465441SEvalZero res = str_accumulate("Result of ");
88*10465441SEvalZero res = str_accumulate((char *) arg);
89*10465441SEvalZero res = str_accumulate(" thread");
90*10465441SEvalZero printf("Thread %lx: \"%s\"\n", pthread_self(), res);
91*10465441SEvalZero return NULL;
92*10465441SEvalZero }
93*10465441SEvalZero
libc_ex4()94*10465441SEvalZero int libc_ex4()
95*10465441SEvalZero {
96*10465441SEvalZero char * res;
97*10465441SEvalZero pthread_t th1, th2;
98*10465441SEvalZero
99*10465441SEvalZero // res = str_accumulate("Result of ");
100*10465441SEvalZero pthread_create(&th1, NULL, process, (void *) "first");
101*10465441SEvalZero pthread_create(&th2, NULL, process, (void *) "second");
102*10465441SEvalZero // res = str_accumulate("initial thread");
103*10465441SEvalZero printf("Thread %lx: \"%s\"\n", pthread_self(), res);
104*10465441SEvalZero pthread_join(th1, NULL);
105*10465441SEvalZero pthread_join(th2, NULL);
106*10465441SEvalZero }
107*10465441SEvalZero #include <finsh.h>
108*10465441SEvalZero FINSH_FUNCTION_EXPORT(libc_ex4, example 4 for libc);
109