1
by brian
clean slate |
1 |
/* Copyright (C) 2000 MySQL AB
|
2 |
||
3 |
This program is free software; you can redistribute it and/or modify
|
|
4 |
it under the terms of the GNU General Public License as published by
|
|
5 |
the Free Software Foundation; version 2 of the License.
|
|
6 |
||
7 |
This program is distributed in the hope that it will be useful,
|
|
8 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
9 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
10 |
GNU General Public License for more details.
|
|
11 |
||
12 |
You should have received a copy of the GNU General Public License
|
|
13 |
along with this program; if not, write to the Free Software
|
|
14 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
|
|
15 |
||
16 |
/*
|
|
17 |
Functions to handle initializating and allocationg of all mysys & debug
|
|
18 |
thread variables.
|
|
19 |
*/
|
|
20 |
||
1280.1.10
by Monty Taylor
Put everything in drizzled into drizzled namespace. |
21 |
#include "config.h" |
22 |
||
23 |
#include "drizzled/internal/my_sys.h" |
|
1241.9.64
by Monty Taylor
Moved remaining non-public portions of mysys and mystrings to drizzled/internal. |
24 |
#include "drizzled/internal/my_pthread.h" |
1689.2.10
by Brian Aker
Move thread_var out to its own include file. |
25 |
#include "drizzled/internal/thread_var.h" |
1241.9.64
by Monty Taylor
Moved remaining non-public portions of mysys and mystrings to drizzled/internal. |
26 |
#include "drizzled/internal/m_string.h" |
612.2.6
by Monty Taylor
OpenSolaris builds. |
27 |
|
1280.1.10
by Monty Taylor
Put everything in drizzled into drizzled namespace. |
28 |
#include <cstdio> |
1
by brian
clean slate |
29 |
#include <signal.h> |
30 |
||
481.1.15
by Monty Taylor
Removed time.h and sys/time.h from global.h. |
31 |
#if TIME_WITH_SYS_TIME
|
32 |
# include <sys/time.h>
|
|
33 |
# include <time.h>
|
|
34 |
#else
|
|
35 |
# if HAVE_SYS_TIME_H
|
|
36 |
# include <sys/time.h>
|
|
37 |
# else
|
|
38 |
# include <time.h>
|
|
39 |
# endif
|
|
660.1.3
by Eric Herman
removed trailing whitespace with simple script: |
40 |
#endif
|
481.1.15
by Monty Taylor
Removed time.h and sys/time.h from global.h. |
41 |
|
1280.1.10
by Monty Taylor
Put everything in drizzled into drizzled namespace. |
42 |
namespace drizzled |
43 |
{
|
|
44 |
namespace internal |
|
45 |
{
|
|
46 |
||
860
by Brian Aker
Remove USE_TLS (dead define) |
47 |
pthread_key_t THR_KEY_mysys; |
596
by Brian Aker
Refactor out dead mutexes |
48 |
pthread_mutex_t THR_LOCK_lock; |
660.1.3
by Eric Herman
removed trailing whitespace with simple script: |
49 |
pthread_mutex_t THR_LOCK_threads; |
1
by brian
clean slate |
50 |
pthread_cond_t THR_COND_threads; |
482
by Brian Aker
Remove uint. |
51 |
uint32_t THR_thread_count= 0; |
1165.1.95
by Stewart Smith
make my_thread_end_wait_time just local to mysys/my_thr_init.cc |
52 |
static uint32_t my_thread_end_wait_time= 5; |
1
by brian
clean slate |
53 |
#ifdef PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
|
54 |
pthread_mutexattr_t my_fast_mutexattr; |
|
55 |
#endif
|
|
56 |
||
57 |
/*
|
|
58 |
initialize thread environment
|
|
59 |
||
60 |
SYNOPSIS
|
|
61 |
my_thread_global_init()
|
|
62 |
||
63 |
RETURN
|
|
64 |
0 ok
|
|
65 |
1 error (Couldn't create THR_KEY_mysys)
|
|
66 |
*/
|
|
67 |
||
146
by Brian Aker
my_bool cleanup. |
68 |
bool my_thread_global_init(void) |
1
by brian
clean slate |
69 |
{
|
70 |
int pth_ret; |
|
71 |
||
72 |
if ((pth_ret= pthread_key_create(&THR_KEY_mysys, NULL)) != 0) |
|
73 |
{
|
|
74 |
fprintf(stderr,"Can't initialize threads: error %d\n", pth_ret); |
|
75 |
return 1; |
|
76 |
}
|
|
77 |
||
78 |
#ifdef PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
|
|
79 |
/*
|
|
80 |
Set mutex type to "fast" a.k.a "adaptive"
|
|
81 |
||
82 |
In this case the thread may steal the mutex from some other thread
|
|
83 |
that is waiting for the same mutex. This will save us some
|
|
84 |
context switches but may cause a thread to 'starve forever' while
|
|
85 |
waiting for the mutex (not likely if the code within the mutex is
|
|
86 |
short).
|
|
87 |
*/
|
|
88 |
pthread_mutexattr_init(&my_fast_mutexattr); |
|
89 |
pthread_mutexattr_settype(&my_fast_mutexattr, |
|
90 |
PTHREAD_MUTEX_ADAPTIVE_NP); |
|
91 |
#endif
|
|
92 |
||
93 |
pthread_mutex_init(&THR_LOCK_lock,MY_MUTEX_INIT_FAST); |
|
94 |
pthread_mutex_init(&THR_LOCK_threads,MY_MUTEX_INIT_FAST); |
|
95 |
pthread_cond_init(&THR_COND_threads, NULL); |
|
96 |
if (my_thread_init()) |
|
97 |
{
|
|
98 |
my_thread_global_end(); /* Clean up */ |
|
99 |
return 1; |
|
100 |
}
|
|
101 |
return 0; |
|
102 |
}
|
|
103 |
||
104 |
||
105 |
void my_thread_global_end(void) |
|
106 |
{
|
|
107 |
struct timespec abstime; |
|
146
by Brian Aker
my_bool cleanup. |
108 |
bool all_threads_killed= 1; |
1
by brian
clean slate |
109 |
|
110 |
set_timespec(abstime, my_thread_end_wait_time); |
|
111 |
pthread_mutex_lock(&THR_LOCK_threads); |
|
112 |
while (THR_thread_count > 0) |
|
113 |
{
|
|
114 |
int error= pthread_cond_timedwait(&THR_COND_threads, &THR_LOCK_threads, |
|
115 |
&abstime); |
|
116 |
if (error == ETIMEDOUT || error == ETIME) |
|
117 |
{
|
|
118 |
/*
|
|
119 |
We shouldn't give an error here, because if we don't have
|
|
120 |
pthread_kill(), programs like mysqld can't ensure that all threads
|
|
121 |
are killed when we enter here.
|
|
122 |
*/
|
|
123 |
if (THR_thread_count) |
|
124 |
fprintf(stderr, |
|
125 |
"Error in my_thread_global_end(): %d threads didn't exit\n", |
|
126 |
THR_thread_count); |
|
127 |
all_threads_killed= 0; |
|
128 |
break; |
|
129 |
}
|
|
130 |
}
|
|
131 |
pthread_mutex_unlock(&THR_LOCK_threads); |
|
132 |
||
133 |
pthread_key_delete(THR_KEY_mysys); |
|
134 |
#ifdef PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
|
|
135 |
pthread_mutexattr_destroy(&my_fast_mutexattr); |
|
136 |
#endif
|
|
137 |
pthread_mutex_destroy(&THR_LOCK_lock); |
|
138 |
if (all_threads_killed) |
|
139 |
{
|
|
140 |
pthread_mutex_destroy(&THR_LOCK_threads); |
|
141 |
pthread_cond_destroy(&THR_COND_threads); |
|
142 |
}
|
|
143 |
}
|
|
144 |
||
1241.9.57
by Monty Taylor
Oy. Bigger change than I normally like - but this stuff is all intertwined. |
145 |
static uint64_t thread_id= 0; |
1
by brian
clean slate |
146 |
|
147 |
/*
|
|
51.3.28
by Jay Pipes
DBUG entirely removed from server and client |
148 |
Allocate thread specific memory for the thread, used by mysys
|
1
by brian
clean slate |
149 |
|
150 |
SYNOPSIS
|
|
151 |
my_thread_init()
|
|
152 |
||
153 |
RETURN
|
|
154 |
0 ok
|
|
155 |
1 Fatal error; mysys/dbug functions can't be used
|
|
156 |
*/
|
|
157 |
||
146
by Brian Aker
my_bool cleanup. |
158 |
bool my_thread_init(void) |
1
by brian
clean slate |
159 |
{
|
146
by Brian Aker
my_bool cleanup. |
160 |
bool error=0; |
1101.1.27
by Monty Taylor
Fixed the plugin string valgrind leak. |
161 |
st_my_thread_var *tmp= NULL; |
1
by brian
clean slate |
162 |
|
163 |
#ifdef EXTRA_DEBUG_THREADS
|
|
164 |
fprintf(stderr,"my_thread_init(): thread_id: 0x%lx\n", |
|
298
by Brian Aker
ulong conversion. |
165 |
(uint32_t) pthread_self()); |
660.1.3
by Eric Herman
removed trailing whitespace with simple script: |
166 |
#endif
|
1
by brian
clean slate |
167 |
|
5
by Brian Aker
Removed my_pthread_setprio() |
168 |
if (pthread_getspecific(THR_KEY_mysys)) |
1
by brian
clean slate |
169 |
{
|
170 |
#ifdef EXTRA_DEBUG_THREADS
|
|
171 |
fprintf(stderr,"my_thread_init() called more than once in thread 0x%lx\n", |
|
172 |
(long) pthread_self()); |
|
660.1.3
by Eric Herman
removed trailing whitespace with simple script: |
173 |
#endif
|
1
by brian
clean slate |
174 |
goto end; |
175 |
}
|
|
1101.1.27
by Monty Taylor
Fixed the plugin string valgrind leak. |
176 |
tmp= static_cast<st_my_thread_var *>(calloc(1, sizeof(*tmp))); |
177 |
if (tmp == NULL) |
|
1
by brian
clean slate |
178 |
{
|
179 |
error= 1; |
|
180 |
goto end; |
|
181 |
}
|
|
182 |
pthread_setspecific(THR_KEY_mysys,tmp); |
|
183 |
tmp->pthread_self= pthread_self(); |
|
184 |
pthread_mutex_init(&tmp->mutex,MY_MUTEX_INIT_FAST); |
|
185 |
pthread_cond_init(&tmp->suspend, NULL); |
|
186 |
tmp->init= 1; |
|
187 |
||
188 |
pthread_mutex_lock(&THR_LOCK_threads); |
|
189 |
tmp->id= ++thread_id; |
|
190 |
++THR_thread_count; |
|
191 |
pthread_mutex_unlock(&THR_LOCK_threads); |
|
192 |
||
193 |
end: |
|
194 |
return error; |
|
195 |
}
|
|
196 |
||
197 |
||
198 |
/*
|
|
199 |
Deallocate memory used by the thread for book-keeping
|
|
200 |
||
201 |
SYNOPSIS
|
|
202 |
my_thread_end()
|
|
203 |
||
204 |
NOTE
|
|
205 |
This may be called multiple times for a thread.
|
|
206 |
This happens for example when one calls 'mysql_server_init()'
|
|
207 |
mysql_server_end() and then ends with a mysql_end().
|
|
208 |
*/
|
|
209 |
||
210 |
void my_thread_end(void) |
|
211 |
{
|
|
1101.1.28
by Monty Taylor
Merged up with Brian. |
212 |
st_my_thread_var *tmp= |
213 |
static_cast<st_my_thread_var *>(pthread_getspecific(THR_KEY_mysys)); |
|
1
by brian
clean slate |
214 |
|
215 |
#ifdef EXTRA_DEBUG_THREADS
|
|
216 |
fprintf(stderr,"my_thread_end(): tmp: 0x%lx pthread_self: 0x%lx thread_id: %ld\n", |
|
217 |
(long) tmp, (long) pthread_self(), tmp ? (long) tmp->id : 0L); |
|
660.1.3
by Eric Herman
removed trailing whitespace with simple script: |
218 |
#endif
|
1
by brian
clean slate |
219 |
if (tmp && tmp->init) |
220 |
{
|
|
221 |
#if !defined(__bsdi__) && !defined(__OpenBSD__)
|
|
222 |
/* bsdi and openbsd 3.5 dumps core here */
|
|
223 |
pthread_cond_destroy(&tmp->suspend); |
|
224 |
#endif
|
|
225 |
pthread_mutex_destroy(&tmp->mutex); |
|
1106.1.3
by Brian Aker
Fix cleanup of thread specific stuff (which... should all go away...). |
226 |
free(tmp); |
1
by brian
clean slate |
227 |
|
228 |
/*
|
|
229 |
Decrement counter for number of running threads. We are using this
|
|
230 |
in my_thread_global_end() to wait until all threads have called
|
|
231 |
my_thread_end and thus freed all memory they have allocated in
|
|
660.1.3
by Eric Herman
removed trailing whitespace with simple script: |
232 |
my_thread_init()
|
1
by brian
clean slate |
233 |
*/
|
234 |
pthread_mutex_lock(&THR_LOCK_threads); |
|
51.3.15
by Jay Pipes
Phase 3 removal of DBUG in mysys |
235 |
assert(THR_thread_count != 0); |
1
by brian
clean slate |
236 |
if (--THR_thread_count == 0) |
237 |
pthread_cond_signal(&THR_COND_threads); |
|
1101.1.27
by Monty Taylor
Fixed the plugin string valgrind leak. |
238 |
pthread_mutex_unlock(&THR_LOCK_threads); |
1
by brian
clean slate |
239 |
}
|
240 |
}
|
|
241 |
||
242 |
struct st_my_thread_var *_my_thread_var(void) |
|
243 |
{
|
|
5
by Brian Aker
Removed my_pthread_setprio() |
244 |
struct st_my_thread_var *tmp= (struct st_my_thread_var*)pthread_getspecific(THR_KEY_mysys); |
1
by brian
clean slate |
245 |
return tmp; |
246 |
}
|
|
247 |
||
1280.1.10
by Monty Taylor
Put everything in drizzled into drizzled namespace. |
248 |
} /* namespace internal */ |
249 |
} /* namespace drizzled */ |