thread_pthread.c
Go to the documentation of this file.
1 /* Copyright (c) 2015, Google Inc.
2  *
3  * Permission to use, copy, modify, and/or distribute this software for any
4  * purpose with or without fee is hereby granted, provided that the above
5  * copyright notice and this permission notice appear in all copies.
6  *
7  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10  * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14 
15 #include "internal.h"
16 
17 #if defined(OPENSSL_PTHREADS)
18 
19 #include <pthread.h>
20 #include <stdlib.h>
21 #include <string.h>
22 
23 #include <openssl/mem.h>
24 #include <openssl/type_check.h>
25 
26 
27 OPENSSL_STATIC_ASSERT(sizeof(CRYPTO_MUTEX) >= sizeof(pthread_rwlock_t),
28  "CRYPTO_MUTEX is too small");
29 #if defined(__GNUC__) || defined(__clang__)
30 OPENSSL_STATIC_ASSERT(alignof(CRYPTO_MUTEX) >= alignof(pthread_rwlock_t),
31  "CRYPTO_MUTEX has insufficient alignment");
32 #endif
33 
34 void CRYPTO_MUTEX_init(CRYPTO_MUTEX *lock) {
35  if (pthread_rwlock_init((pthread_rwlock_t *) lock, NULL) != 0) {
36  abort();
37  }
38 }
39 
41  if (pthread_rwlock_rdlock((pthread_rwlock_t *) lock) != 0) {
42  abort();
43  }
44 }
45 
47  if (pthread_rwlock_wrlock((pthread_rwlock_t *) lock) != 0) {
48  abort();
49  }
50 }
51 
53  if (pthread_rwlock_unlock((pthread_rwlock_t *) lock) != 0) {
54  abort();
55  }
56 }
57 
59  if (pthread_rwlock_unlock((pthread_rwlock_t *) lock) != 0) {
60  abort();
61  }
62 }
63 
65  pthread_rwlock_destroy((pthread_rwlock_t *) lock);
66 }
67 
69  if (pthread_rwlock_rdlock(&lock->lock) != 0) {
70  abort();
71  }
72 }
73 
75  if (pthread_rwlock_wrlock(&lock->lock) != 0) {
76  abort();
77  }
78 }
79 
81  if (pthread_rwlock_unlock(&lock->lock) != 0) {
82  abort();
83  }
84 }
85 
87  if (pthread_rwlock_unlock(&lock->lock) != 0) {
88  abort();
89  }
90 }
91 
92 void CRYPTO_once(CRYPTO_once_t *once, void (*init)(void)) {
93  if (pthread_once(once, init) != 0) {
94  abort();
95  }
96 }
97 
98 static pthread_mutex_t g_destructors_lock = PTHREAD_MUTEX_INITIALIZER;
100 
101 // thread_local_destructor is called when a thread exits. It releases thread
102 // local data for that thread only.
103 static void thread_local_destructor(void *arg) {
104  if (arg == NULL) {
105  return;
106  }
107 
109  if (pthread_mutex_lock(&g_destructors_lock) != 0) {
110  return;
111  }
112  OPENSSL_memcpy(destructors, g_destructors, sizeof(destructors));
113  pthread_mutex_unlock(&g_destructors_lock);
114 
115  unsigned i;
116  void **pointers = arg;
117  for (i = 0; i < NUM_OPENSSL_THREAD_LOCALS; i++) {
118  if (destructors[i] != NULL) {
119  destructors[i](pointers[i]);
120  }
121  }
122 
123  OPENSSL_free(pointers);
124 }
125 
126 static pthread_once_t g_thread_local_init_once = PTHREAD_ONCE_INIT;
127 static pthread_key_t g_thread_local_key;
128 static int g_thread_local_key_created = 0;
129 
130 static void thread_local_init(void) {
131  g_thread_local_key_created =
132  pthread_key_create(&g_thread_local_key, thread_local_destructor) == 0;
133 }
134 
136  CRYPTO_once(&g_thread_local_init_once, thread_local_init);
137  if (!g_thread_local_key_created) {
138  return NULL;
139  }
140 
141  void **pointers = pthread_getspecific(g_thread_local_key);
142  if (pointers == NULL) {
143  return NULL;
144  }
145  return pointers[index];
146 }
147 
149  thread_local_destructor_t destructor) {
150  CRYPTO_once(&g_thread_local_init_once, thread_local_init);
151  if (!g_thread_local_key_created) {
152  destructor(value);
153  return 0;
154  }
155 
156  void **pointers = pthread_getspecific(g_thread_local_key);
157  if (pointers == NULL) {
158  pointers = OPENSSL_malloc(sizeof(void *) * NUM_OPENSSL_THREAD_LOCALS);
159  if (pointers == NULL) {
160  destructor(value);
161  return 0;
162  }
163  OPENSSL_memset(pointers, 0, sizeof(void *) * NUM_OPENSSL_THREAD_LOCALS);
164  if (pthread_setspecific(g_thread_local_key, pointers) != 0) {
165  OPENSSL_free(pointers);
166  destructor(value);
167  return 0;
168  }
169  }
170 
171  if (pthread_mutex_lock(&g_destructors_lock) != 0) {
172  destructor(value);
173  return 0;
174  }
175  g_destructors[index] = destructor;
176  pthread_mutex_unlock(&g_destructors_lock);
177 
178  pointers[index] = value;
179  return 1;
180 }
181 
182 #endif // OPENSSL_PTHREADS
init
const char * init
Definition: upb/upb/bindings/lua/main.c:49
CRYPTO_STATIC_MUTEX_unlock_write
#define CRYPTO_STATIC_MUTEX_unlock_write
Definition: boringssl_prefix_symbols.h:1135
internal.h
string.h
CRYPTO_MUTEX_unlock_read
#define CRYPTO_MUTEX_unlock_read
Definition: boringssl_prefix_symbols.h:1127
CRYPTO_once
#define CRYPTO_once
Definition: boringssl_prefix_symbols.h:1182
CRYPTO_MUTEX_init
#define CRYPTO_MUTEX_init
Definition: boringssl_prefix_symbols.h:1124
CRYPTO_get_thread_local
#define CRYPTO_get_thread_local
Definition: boringssl_prefix_symbols.h:1169
OPENSSL_memset
static void * OPENSSL_memset(void *dst, int c, size_t n)
Definition: third_party/boringssl-with-bazel/src/crypto/internal.h:835
OPENSSL_malloc
#define OPENSSL_malloc
Definition: boringssl_prefix_symbols.h:1885
CRYPTO_STATIC_MUTEX
Definition: third_party/boringssl-with-bazel/src/crypto/internal.h:533
CRYPTO_set_thread_local
#define CRYPTO_set_thread_local
Definition: boringssl_prefix_symbols.h:1199
CRYPTO_STATIC_MUTEX_unlock_read
#define CRYPTO_STATIC_MUTEX_unlock_read
Definition: boringssl_prefix_symbols.h:1134
CRYPTO_STATIC_MUTEX_lock_write
#define CRYPTO_STATIC_MUTEX_lock_write
Definition: boringssl_prefix_symbols.h:1133
OPENSSL_memcpy
static void * OPENSSL_memcpy(void *dst, const void *src, size_t n)
Definition: third_party/boringssl-with-bazel/src/crypto/internal.h:819
arg
Definition: cmdline.cc:40
value
const char * value
Definition: hpack_parser_table.cc:165
CRYPTO_MUTEX_lock_read
#define CRYPTO_MUTEX_lock_read
Definition: boringssl_prefix_symbols.h:1125
CRYPTO_MUTEX_cleanup
#define CRYPTO_MUTEX_cleanup
Definition: boringssl_prefix_symbols.h:1123
once
absl::once_flag once
Definition: bm_opencensus_plugin.cc:38
thread_local_data_t
thread_local_data_t
Definition: third_party/boringssl-with-bazel/src/crypto/internal.h:635
index
int index
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/protobuf.h:1184
CRYPTO_MUTEX_lock_write
#define CRYPTO_MUTEX_lock_write
Definition: boringssl_prefix_symbols.h:1126
arg
struct arg arg
thread_local_destructor_t
void(* thread_local_destructor_t)(void *)
Definition: third_party/boringssl-with-bazel/src/crypto/internal.h:645
CRYPTO_once_t
uint32_t CRYPTO_once_t
Definition: third_party/boringssl-with-bazel/src/crypto/internal.h:470
CRYPTO_STATIC_MUTEX_lock_read
#define CRYPTO_STATIC_MUTEX_lock_read
Definition: boringssl_prefix_symbols.h:1132
mem.h
type_check.h
OPENSSL_free
#define OPENSSL_free
Definition: boringssl_prefix_symbols.h:1869
CRYPTO_MUTEX_unlock_write
#define CRYPTO_MUTEX_unlock_write
Definition: boringssl_prefix_symbols.h:1128
crypto_mutex_st
Definition: thread.h:70
OPENSSL_STATIC_ASSERT
#define OPENSSL_STATIC_ASSERT(cond, msg)
Definition: type_check.h:75
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
NUM_OPENSSL_THREAD_LOCALS
@ NUM_OPENSSL_THREAD_LOCALS
Definition: third_party/boringssl-with-bazel/src/crypto/internal.h:640


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:01:37