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