- Notifications
You must be signed in to change notification settings - Fork 255
/
Copy pathmy_pthread.c
531 lines (452 loc) · 14.8 KB
/
my_pthread.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
/*
* Copyright (c) 2000, 2024, Oracle and/or its affiliates.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License, version 2.0, as
* published by the Free Software Foundation.
*
* This program is designed to work with certain software (including
* but not limited to OpenSSL) that is licensed under separate terms, as
* designated in a particular file or component or in included license
* documentation. The authors of MySQL hereby grant you an additional
* permission to link the program and your derivative works with the
* separately licensed software that they have either included with
* the program or referenced in the documentation.
*
* Without limiting anything contained in the foregoing, this file,
* which is part of Connector/C++, is also subject to the
* Universal FOSS Exception, version 1.0, a copy of which can be found at
* https://oss.oracle.com/licenses/universal-foss-exception.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License, version 2.0, for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/* Functions to get threads more portable */
#defineDONT_REMAP_PTHREAD_FUNCTIONS
#ifdefTHREAD
#include"my_pthread.h"
externpthread_mutex_tTHR_LOCK_malloc, THR_LOCK_open, THR_LOCK_keycache;
externpthread_mutex_tTHR_LOCK_lock, THR_LOCK_isam, THR_LOCK_net;
externpthread_mutex_tTHR_LOCK_charset, THR_LOCK_time;
#else
#include"my_no_pthread.h"
#endif
#ifdefTHREAD
#include<signal.h>
//#include "m_string.h"
#include"thr_alarm.h"
#if (defined(__BSD__) || defined(_BSDI_VERSION))
#defineSCHED_POLICY SCHED_RR
#else
#defineSCHED_POLICY SCHED_OTHER
#endif
uintthd_lib_detected=0;
/* To allow use of pthread_getspecific with two arguments */
#ifdefHAVE_NONPOSIX_PTHREAD_GETSPECIFIC
#undef pthread_getspecific
void*my_pthread_getspecific_imp(pthread_key_tkey)
{
void*value;
if (pthread_getspecific(key,(void*) &value))
return0;
returnvalue;
}
#endif
#ifdef__NETWARE__
/*
Don't kill the LibC Reaper thread or the main thread
*/
#include<nks/thread.h>
#undef pthread_exit
voidmy_pthread_exit(void*status)
{
NXThreadId_ttid;
NXContext_tctx;
charname[NX_MAX_OBJECT_NAME_LEN+1] ="";
tid=NXThreadGetId();
if (tid==NX_INVALID_THREAD_ID|| !tid)
return;
if (NXThreadGetContext(tid, &ctx) ||
NXContextGetName(ctx, name, sizeof(name)-1))
return;
/*
"MYSQLD.NLM's LibC Reaper" or "MYSQLD.NLM's main thread"
with a debug build of LibC the reaper can have different names
*/
if (!strindex(name, "\'s"))
pthread_exit(status);
}
#endif
/*
Some functions for RTS threads, AIX, Siemens Unix and UnixWare 7
(and DEC OSF/1 3.2 too)
*/
intmy_pthread_create_detached=1;
#if defined(HAVE_NONPOSIX_SIGWAIT) || defined(HAVE_DEC_3_2_THREADS)
intmy_sigwait(constsigset_t*set,int*sig)
{
intsignal=sigwait((sigset_t*) set);
if (signal<0)
returnerrno;
*sig=signal;
return0;
}
#endif
/* localtime_r for SCO 3.2V4.2 */
#if !defined(HAVE_LOCALTIME_R) || !defined(HAVE_GMTIME_R)
externpthread_mutex_tLOCK_localtime_r;
#endif
#if !defined(HAVE_LOCALTIME_R)
structtm*localtime_r(consttime_t*clock, structtm*res)
{
structtm*tmp;
pthread_mutex_lock(&LOCK_localtime_r);
tmp=localtime(clock);
*res=*tmp;
pthread_mutex_unlock(&LOCK_localtime_r);
returnres;
}
#endif
#if !defined(HAVE_GMTIME_R)
/*
Reentrant version of standard gmtime() function.
Needed on some systems which don't implement it.
*/
structtm*gmtime_r(consttime_t*clock, structtm*res)
{
structtm*tmp;
pthread_mutex_lock(&LOCK_localtime_r);
tmp=gmtime(clock);
*res=*tmp;
pthread_mutex_unlock(&LOCK_localtime_r);
returnres;
}
#endif
/****************************************************************************
** Replacement of sigwait if the system doesn't have one (like BSDI 3.0)
**
** Note:
** This version of sigwait() is assumed to called in a loop so the signalmask
** is permanently modified to reflect the signal set. This is done to get
** a much faster implementation.
**
** This implementation isn't thread safe: It assumes that only one
** thread is using sigwait.
**
** If one later supplies a different signal mask, all old signals that
** was used before are unblocked and set to SIGDFL.
**
** Author: Gary Wisniewski <garyw@spidereye.com.au>, much modified by Monty
****************************************************************************/
#if !defined(HAVE_SIGWAIT) && !defined(sigwait) && !defined(__WIN__) && !defined(HAVE_rts_threads) && !defined(HAVE_NONPOSIX_SIGWAIT) && !defined(HAVE_DEC_3_2_THREADS)
#if !defined(DONT_USE_SIGSUSPEND)
staticsigset_tsigwait_set,rev_sigwait_set,px_recd;
voidpx_handle_sig(intsig)
{
sigaddset(&px_recd, sig);
}
voidsigwait_setup(sigset_t*set)
{
inti;
structsigactionsact,sact1;
sigset_tunblock_mask;
sact.sa_flags=0;
sact.sa_handler=px_handle_sig;
memcpy_fixed(&sact.sa_mask,set,sizeof(*set)); /* handler isn't thread_safe */
sigemptyset(&unblock_mask);
pthread_sigmask(SIG_UNBLOCK,(sigset_t*) 0,&rev_sigwait_set);
for (i=1; i <= sizeof(sigwait_set)*8; i++)
{
if (sigismember(set,i))
{
sigdelset(&rev_sigwait_set,i);
if (!sigismember(&sigwait_set,i))
sigaction(i, &sact, (structsigaction*) 0);
}
else
{
sigdelset(&px_recd,i); /* Don't handle this */
if (sigismember(&sigwait_set,i))
{ /* Remove the old handler */
sigaddset(&unblock_mask,i);
sigdelset(&rev_sigwait_set,i);
sact1.sa_flags=0;
sact1.sa_handler=SIG_DFL;
sigemptyset(&sact1.sa_mask);
sigaction(i, &sact1, 0);
}
}
}
memcpy_fixed(&sigwait_set,set,sizeof(*set));
pthread_sigmask(SIG_BLOCK,(sigset_t*) set,(sigset_t*) 0);
pthread_sigmask(SIG_UNBLOCK,&unblock_mask,(sigset_t*) 0);
}
intsigwait(sigset_t*setp, int*sigp)
{
if (memcmp(setp,&sigwait_set,sizeof(sigwait_set)))
sigwait_setup(setp); /* Init or change of set */
for (;;)
{
/*
This is a fast, not 100% portable implementation to find the signal.
Because the handler is blocked there should be at most 1 bit set, but
the specification on this is somewhat shady so we use a set instead a
single variable.
*/
ulong*ptr= (ulong*) &px_recd;
ulong*end=ptr+sizeof(px_recd)/sizeof(ulong);
for ( ; ptr!=end ; ptr++)
{
if (*ptr)
{
ulongset=*ptr;
intfound= (int) ((char*) ptr- (char*) &px_recd)*8+1;
while (!(set&1))
{
found++;
set>>=1;
}
*sigp=found;
sigdelset(&px_recd,found);
return0;
}
}
sigsuspend(&rev_sigwait_set);
}
return0;
}
#else/* !DONT_USE_SIGSUSPEND */
/****************************************************************************
** Replacement of sigwait if the system doesn't have one (like BSDI 3.0)
**
** Note:
** This version of sigwait() is assumed to called in a loop so the signalmask
** is permanently modified to reflect the signal set. This is done to get
** a much faster implementation.
**
** This implementation uses a extra thread to handle the signals and one
** must always call sigwait() with the same signal mask!
**
** BSDI 3.0 NOTE:
**
** pthread_kill() doesn't work on a thread in a select() or sleep() loop?
** After adding the sleep to sigwait_thread, all signals are checked and
** delivered every second. This isn't that terrible performance vice, but
** someone should report this to BSDI and ask for a fix!
** Another problem is that when the sleep() ends, every select() in other
** threads are interrupted!
****************************************************************************/
staticsigset_tpending_set;
staticboolinited=0;
staticpthread_cond_tCOND_sigwait;
staticpthread_mutex_tLOCK_sigwait;
voidsigwait_handle_sig(intsig)
{
pthread_mutex_lock(&LOCK_sigwait);
sigaddset(&pending_set, sig);
pthread_cond_signal(&COND_sigwait); /* inform sigwait() about signal */
pthread_mutex_unlock(&LOCK_sigwait);
}
void*sigwait_thread(void*set_arg)
{
sigset_t*set=(sigset_t*) set_arg;
inti;
structsigactionsact;
sact.sa_flags=0;
sact.sa_handler=sigwait_handle_sig;
memcpy_fixed(&sact.sa_mask,set,sizeof(*set)); /* handler isn't thread_safe */
sigemptyset(&pending_set);
for (i=1; i <= sizeof(pending_set)*8; i++)
{
if (sigismember(set,i))
{
sigaction(i, &sact, (structsigaction*) 0);
}
}
/* Ensure that init_thr_alarm() is called */
DBUG_ASSERT(thr_client_alarm);
sigaddset(set, thr_client_alarm);
pthread_sigmask(SIG_UNBLOCK,(sigset_t*) set,(sigset_t*) 0);
alarm_thread=pthread_self(); /* For thr_alarm */
for (;;)
{ /* Wait for signals */
#ifdefHAVE_NOT_BROKEN_SELECT
fd_setfd;
FD_ZERO(&fd);
select(0,&fd,0,0,0);
#else
sleep(1); /* Because of broken BSDI */
#endif
}
}
intsigwait(sigset_t*setp, int*sigp)
{
if (!inited)
{
pthread_attr_tthr_attr;
pthread_tsigwait_thread_id;
inited=1;
sigemptyset(&pending_set);
pthread_mutex_init(&LOCK_sigwait,MY_MUTEX_INIT_FAST);
pthread_cond_init(&COND_sigwait,NULL);
pthread_attr_init(&thr_attr);
pthread_attr_setscope(&thr_attr,PTHREAD_SCOPE_PROCESS);
pthread_attr_setdetachstate(&thr_attr,PTHREAD_CREATE_DETACHED);
pthread_attr_setstacksize(&thr_attr,8196);
pthread_create(&sigwait_thread_id,&thr_attr,sigwait_thread,setp);
pthread_attr_destroy(&thr_attr);
}
pthread_mutex_lock(&LOCK_sigwait);
for (;;)
{
ulong*ptr= (ulong*) &pending_set;
ulong*end=ptr+sizeof(pending_set)/sizeof(ulong);
for ( ; ptr!=end ; ptr++)
{
if (*ptr)
{
ulongset=*ptr;
intfound= (int) ((char*) ptr- (char*) &pending_set)*8+1;
while (!(set&1))
{
found++;
set>>=1;
}
*sigp=found;
sigdelset(&pending_set,found);
pthread_mutex_unlock(&LOCK_sigwait);
return0;
}
}
pthread_cond_wait(&COND_sigwait,&LOCK_sigwait);
}
return0;
}
#endif/* DONT_USE_SIGSUSPEND */
#endif/* HAVE_SIGWAIT */
/****************************************************************************
The following functions fixes that all pthread functions should work
according to latest posix standard
****************************************************************************/
/* Undefined wrappers set my_pthread.h so that we call os functions */
#undef pthread_mutex_init
#undef pthread_mutex_lock
#undef pthread_mutex_unlock
#undef pthread_mutex_destroy
#undef pthread_mutex_wait
#undef pthread_mutex_timedwait
#undef pthread_mutex_trylock
#undef pthread_mutex_t
#undef pthread_cond_init
#undef pthread_cond_wait
#undef pthread_cond_timedwait
#undef pthread_cond_t
#undef pthread_attr_getstacksize
/*****************************************************************************
** Patches for AIX and DEC OSF/1 3.2
*****************************************************************************/
#if defined(HAVE_NONPOSIX_PTHREAD_MUTEX_INIT)
#include<netdb.h>
intmy_pthread_mutex_noposix_init(pthread_mutex_t*mp,
constpthread_mutexattr_t*attr)
{
interror;
if (!attr)
error=pthread_mutex_init(mp,pthread_mutexattr_default);
else
error=pthread_mutex_init(mp,*attr);
returnerror;
}
intmy_pthread_cond_noposix_init(pthread_cond_t*mp,
constpthread_condattr_t*attr)
{
interror;
if (!attr)
error=pthread_cond_init(mp,pthread_condattr_default);
else
error=pthread_cond_init(mp,*attr);
returnerror;
}
#endif
/*****************************************************************************
Patches for HPUX
We need these because the pthread_mutex.. code returns -1 on error,
instead of the error code.
Note that currently we only remap pthread_ functions used by MySQL.
If we are depending on the value for some other pthread_xxx functions,
this has to be added here.
****************************************************************************/
#if defined(HPUX10) || defined(HAVE_BROKEN_PTHREAD_COND_TIMEDWAIT)
intmy_pthread_cond_timedwait(pthread_cond_t*cond, pthread_mutex_t*mutex,
structtimespec*abstime)
{
interror=pthread_cond_timedwait(cond, mutex, abstime);
if (error==-1) /* Safety if the lib is fixed */
{
if (!(error=errno))
error=ETIMEDOUT; /* Can happen on HPUX */
}
if (error==EAGAIN) /* Correct errno to Posix */
error=ETIMEDOUT;
returnerror;
}
#endif
#if defined(HPUX10)
voidmy_pthread_attr_getstacksize(pthread_attr_t*connection_attrib,
size_t*stack_size)
{
*stack_size=pthread_attr_getstacksize(*connection_attrib);
}
#endif
#ifdefHAVE_POSIX1003_4a_MUTEX
/*
In HP-UX-10.20 and other old Posix 1003.4a Draft 4 implementations
pthread_mutex_trylock returns 1 on success, not 0 like
pthread_mutex_lock
From the HP-UX-10.20 man page:
RETURN VALUES
If the function fails, errno may be set to one of the following
values:
Return | Error | Description
_______|__________|_________________________________________
1 | | Successful completion.
0 | | The mutex is locked; therefore, it was
| | not acquired.
-1 | [EINVAL] | The value specified by mutex is invalid.
*/
/*
Convert pthread_mutex_trylock to return values according to latest POSIX
RETURN VALUES
0 If we are able successfully lock the mutex.
EBUSY Mutex was locked by another thread
# Other error number returned by pthread_mutex_trylock()
(Not likely)
*/
intmy_pthread_mutex_trylock(pthread_mutex_t*mutex)
{
interror=pthread_mutex_trylock(mutex);
if (error==1)
return0; /* Got lock on mutex */
if (error==0) /* Someon else is locking mutex */
returnEBUSY;
if (error==-1) /* Safety if the lib is fixed */
error=errno; /* Probably invalid parameter */
returnerror;
}
#endif/* HAVE_POSIX1003_4a_MUTEX */
/* Some help functions */
intpthread_no_free(void*not_used __attribute__((unused)))
{
return0;
}
intpthread_dummy(intret)
{
returnret;
}
#endif/* THREAD */