1
/******************************************************
2
The thread local storage
6
Created 10/5/1995 Heikki Tuuri
7
*******************************************************/
14
#include "sync0sync.h"
15
#include "hash0hash.h"
20
IMPLEMENTATION OF THREAD LOCAL STORAGE
21
======================================
23
The threads sometimes need private data which depends on the thread id.
24
This is implemented as a hash table, where the hash value is calculated
25
from the thread id, to prepare for a large number of threads. The hash table
26
is protected by a mutex. If you need modify the program and put new data to
27
the thread local storage, just add it to struct thr_local_struct in the
30
/* Mutex protecting the local storage hash table */
31
mutex_t thr_local_mutex;
33
/* The hash table. The module is not yet initialized when it is NULL. */
34
hash_table_t* thr_local_hash = NULL;
36
/* The private data for each thread should be put to
37
the structure below and the accessor functions written
39
typedef struct thr_local_struct thr_local_t;
41
struct thr_local_struct{
42
os_thread_id_t id; /* id of the thread which owns this struct */
43
os_thread_t handle; /* operating system handle to the thread */
44
ulint slot_no;/* the index of the slot in the thread table
46
ibool in_ibuf;/* TRUE if the the thread is doing an ibuf
48
hash_node_t hash; /* hash chain node */
52
#define THR_LOCAL_MAGIC_N 1231234
54
/***********************************************************************
55
Returns the local storage struct for a thread. */
60
/* out: local storage */
61
os_thread_id_t id) /* in: thread id of the thread */
66
ut_ad(thr_local_hash);
67
ut_ad(mutex_own(&thr_local_mutex));
69
/* Look for the local struct in the hash table */
73
HASH_SEARCH(hash, thr_local_hash, os_thread_pf(id),
74
local, os_thread_eq(local->id, id));
76
mutex_exit(&thr_local_mutex);
80
mutex_enter(&thr_local_mutex);
85
ut_ad(local->magic_n == THR_LOCAL_MAGIC_N);
90
/***********************************************************************
91
Gets the slot number in the thread table of a thread. */
94
thr_local_get_slot_no(
95
/*==================*/
96
/* out: slot number */
97
os_thread_id_t id) /* in: thread id of the thread */
102
mutex_enter(&thr_local_mutex);
104
local = thr_local_get(id);
106
slot_no = local->slot_no;
108
mutex_exit(&thr_local_mutex);
113
/***********************************************************************
114
Sets the slot number in the thread table of a thread. */
117
thr_local_set_slot_no(
118
/*==================*/
119
os_thread_id_t id, /* in: thread id of the thread */
120
ulint slot_no)/* in: slot number */
124
mutex_enter(&thr_local_mutex);
126
local = thr_local_get(id);
128
local->slot_no = slot_no;
130
mutex_exit(&thr_local_mutex);
133
/***********************************************************************
134
Returns pointer to the 'in_ibuf' field within the current thread local
138
thr_local_get_in_ibuf_field(void)
139
/*=============================*/
140
/* out: pointer to the in_ibuf field */
144
mutex_enter(&thr_local_mutex);
146
local = thr_local_get(os_thread_get_curr_id());
148
mutex_exit(&thr_local_mutex);
150
return(&(local->in_ibuf));
153
/***********************************************************************
154
Creates a local storage struct for the calling new thread. */
157
thr_local_create(void)
158
/*==================*/
162
if (thr_local_hash == NULL) {
166
local = mem_alloc(sizeof(thr_local_t));
168
local->id = os_thread_get_curr_id();
169
local->handle = os_thread_get_curr();
170
local->magic_n = THR_LOCAL_MAGIC_N;
172
local->in_ibuf = FALSE;
174
mutex_enter(&thr_local_mutex);
176
HASH_INSERT(thr_local_t, hash, thr_local_hash,
177
os_thread_pf(os_thread_get_curr_id()),
180
mutex_exit(&thr_local_mutex);
183
/***********************************************************************
184
Frees the local storage struct for the specified thread. */
189
os_thread_id_t id) /* in: thread id */
193
mutex_enter(&thr_local_mutex);
195
/* Look for the local struct in the hash table */
197
HASH_SEARCH(hash, thr_local_hash, os_thread_pf(id),
198
local, os_thread_eq(local->id, id));
200
mutex_exit(&thr_local_mutex);
205
HASH_DELETE(thr_local_t, hash, thr_local_hash,
206
os_thread_pf(id), local);
208
mutex_exit(&thr_local_mutex);
210
ut_a(local->magic_n == THR_LOCAL_MAGIC_N);
215
/********************************************************************
216
Initializes the thread local storage module. */
223
ut_a(thr_local_hash == NULL);
225
thr_local_hash = hash_create(OS_THREAD_MAX_N + 100);
227
mutex_create(&thr_local_mutex, SYNC_THR_LOCAL);