1
/* Copyright (C) 2000 MySQL AB
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.
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.
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 */
16
#include "mysys_priv.h"
18
#if !defined(DONT_USE_THR_ALARM)
20
#include <my_pthread.h>
23
#include <mystrings/m_string.h>
25
#include "thr_alarm.h"
27
#ifdef HAVE_SYS_SELECT_H
28
#include <sys/select.h> /* AIX needs this for fd_set */
31
#if TIME_WITH_SYS_TIME
32
# include <sys/time.h>
36
# include <sys/time.h>
44
#define ETIME ETIMEDOUT
47
uint32_t thr_client_alarm;
48
static int alarm_aborted=1; /* No alarm thread */
49
bool thr_alarm_inited= 0;
50
volatile bool alarm_thread_running= 0;
51
time_t next_alarm_expire_time= ~ (time_t) 0;
52
static RETSIGTYPE process_alarm_part2(int sig);
54
static pthread_mutex_t LOCK_alarm;
55
static pthread_cond_t COND_alarm;
56
static sigset_t full_signal_set;
57
static QUEUE alarm_queue;
58
static uint32_t max_used_alarms=0;
59
pthread_t alarm_thread;
61
#ifdef USE_ALARM_THREAD
62
static void *alarm_handler(void *arg);
63
#define reschedule_alarms() pthread_cond_signal(&COND_alarm)
65
#define reschedule_alarms() pthread_kill(alarm_thread,THR_SERVER_ALARM)
68
static RETSIGTYPE thread_alarm(int sig __attribute__((unused)));
70
static int compare_uint32_t(void *not_used __attribute__((unused)),
71
unsigned char *a_ptr,unsigned char* b_ptr)
73
uint32_t a=*((uint32_t*) a_ptr),b= *((uint32_t*) b_ptr);
74
return (a < b) ? -1 : (a == b) ? 0 : 1;
77
void init_thr_alarm(uint32_t max_alarms)
81
next_alarm_expire_time= ~ (time_t) 0;
82
init_queue(&alarm_queue,max_alarms+1,offsetof(ALARM,expire_time),0,
83
compare_uint32_t,NULL);
84
sigfillset(&full_signal_set); /* Neaded to block signals */
85
pthread_mutex_init(&LOCK_alarm,MY_MUTEX_INIT_FAST);
86
pthread_cond_init(&COND_alarm,NULL);
87
if (thd_lib_detected == THD_LIB_LT)
88
thr_client_alarm= SIGALRM;
90
thr_client_alarm= SIGUSR1;
91
#ifndef USE_ALARM_THREAD
92
if (thd_lib_detected != THD_LIB_LT)
95
my_sigset(thr_client_alarm, thread_alarm);
98
sigaddset(&s, THR_SERVER_ALARM);
99
alarm_thread=pthread_self();
100
#if defined(USE_ALARM_THREAD)
102
pthread_attr_t thr_attr;
103
pthread_attr_init(&thr_attr);
104
pthread_attr_setscope(&thr_attr,PTHREAD_SCOPE_PROCESS);
105
pthread_attr_setdetachstate(&thr_attr,PTHREAD_CREATE_DETACHED);
106
pthread_attr_setstacksize(&thr_attr,8196);
108
my_pthread_attr_setprio(&thr_attr,100); /* Very high priority */
109
pthread_create(&alarm_thread,&thr_attr,alarm_handler,NULL);
110
pthread_attr_destroy(&thr_attr);
112
#elif defined(USE_ONE_SIGNAL_HAND)
113
pthread_sigmask(SIG_BLOCK, &s, NULL); /* used with sigwait() */
114
if (thd_lib_detected == THD_LIB_LT)
116
my_sigset(thr_client_alarm, process_alarm); /* Linuxthreads */
117
pthread_sigmask(SIG_UNBLOCK, &s, NULL);
120
my_sigset(THR_SERVER_ALARM, process_alarm);
121
pthread_sigmask(SIG_UNBLOCK, &s, NULL);
122
#endif /* USE_ALARM_THREAD */
127
void resize_thr_alarm(uint32_t max_alarms)
129
pthread_mutex_lock(&LOCK_alarm);
131
It's ok not to shrink the queue as there may be more pending alarms than
134
if (alarm_queue.elements < max_alarms)
135
resize_queue(&alarm_queue,max_alarms+1);
136
pthread_mutex_unlock(&LOCK_alarm);
141
Request alarm after sec seconds.
145
alrm Pointer to alarm detection
146
alarm_data Structure to store in alarm queue
149
This function can't be called from the alarm-handling thread.
153
1 If no more alarms are allowed (aborted by process)
155
Stores in first argument a pointer to a non-zero int which is set to 0
156
when the alarm has been given
159
bool thr_alarm(thr_alarm_t *alrm, uint32_t sec, ALARM *alarm_data)
162
#ifndef USE_ONE_SIGNAL_HAND
166
struct st_my_thread_var *current_my_thread_var= my_thread_var;
169
#ifndef USE_ONE_SIGNAL_HAND
170
pthread_sigmask(SIG_BLOCK,&full_signal_set,&old_mask);
172
pthread_mutex_lock(&LOCK_alarm); /* Lock from threads & alarms */
173
if (alarm_aborted > 0)
174
{ /* No signal thread */
175
*alrm= 0; /* No alarm */
176
pthread_mutex_unlock(&LOCK_alarm);
177
#ifndef USE_ONE_SIGNAL_HAND
178
pthread_sigmask(SIG_SETMASK,&old_mask,NULL);
182
if (alarm_aborted < 0)
183
sec= 1; /* Abort mode */
185
if (alarm_queue.elements >= max_used_alarms)
187
if (alarm_queue.elements == alarm_queue.max_elements)
189
fprintf(stderr,"Warning: thr_alarm queue is full\n");
190
*alrm= 0; /* No alarm */
191
pthread_mutex_unlock(&LOCK_alarm);
192
#ifndef USE_ONE_SIGNAL_HAND
193
pthread_sigmask(SIG_SETMASK,&old_mask,NULL);
197
max_used_alarms=alarm_queue.elements+1;
199
reschedule= (uint32_t) next_alarm_expire_time > (uint32_t) now + sec;
202
if (!(alarm_data=(ALARM*) my_malloc(sizeof(ALARM),MYF(MY_WME))))
204
*alrm= 0; /* No alarm */
205
pthread_mutex_unlock(&LOCK_alarm);
206
#ifndef USE_ONE_SIGNAL_HAND
207
pthread_sigmask(SIG_SETMASK,&old_mask,NULL);
211
alarm_data->malloced=1;
214
alarm_data->malloced=0;
215
alarm_data->expire_time=now+sec;
216
alarm_data->alarmed=0;
217
alarm_data->thread= current_my_thread_var->pthread_self;
218
alarm_data->thread_id= current_my_thread_var->id;
219
queue_insert(&alarm_queue,(unsigned char*) alarm_data);
221
/* Reschedule alarm if the current one has more than sec left */
224
if (pthread_equal(pthread_self(),alarm_thread))
226
alarm(sec); /* purecov: inspected */
227
next_alarm_expire_time= now + sec;
230
reschedule_alarms(); /* Reschedule alarms */
232
pthread_mutex_unlock(&LOCK_alarm);
233
#ifndef USE_ONE_SIGNAL_HAND
234
pthread_sigmask(SIG_SETMASK,&old_mask,NULL);
236
(*alrm)= &alarm_data->alarmed;
242
Remove alarm from list of alarms
245
void thr_end_alarm(thr_alarm_t *alarmed)
248
#ifndef USE_ONE_SIGNAL_HAND
253
#ifndef USE_ONE_SIGNAL_HAND
254
pthread_sigmask(SIG_BLOCK,&full_signal_set,&old_mask);
256
pthread_mutex_lock(&LOCK_alarm);
258
alarm_data= (ALARM*) ((unsigned char*) *alarmed - offsetof(ALARM,alarmed));
259
for (i=0 ; i < alarm_queue.elements ; i++)
261
if ((ALARM*) queue_element(&alarm_queue,i) == alarm_data)
263
queue_remove(&alarm_queue,i),MYF(0);
264
if (alarm_data->malloced)
265
free((unsigned char*) alarm_data);
270
assert(!*alarmed || found == 1);
274
fprintf(stderr,"Warning: Didn't find alarm 0x%lx in queue of %d alarms\n",
275
(long) *alarmed, alarm_queue.elements);
277
pthread_mutex_unlock(&LOCK_alarm);
278
#ifndef USE_ONE_SIGNAL_HAND
279
pthread_sigmask(SIG_SETMASK,&old_mask,NULL);
285
Come here when some alarm in queue is due.
286
Mark all alarms with are finnished in list.
287
Shedule alarms to be sent again after 1-10 sec (many alarms at once)
288
If alarm_aborted is set then all alarms are given and resent
292
RETSIGTYPE process_alarm(int sig __attribute__((unused)))
296
if (thd_lib_detected == THD_LIB_LT &&
297
!pthread_equal(pthread_self(),alarm_thread))
299
#if defined(MAIN) && !defined(__bsdi__)
300
printf("thread_alarm in process_alarm\n"); fflush(stdout);
302
#ifndef HAVE_BSD_SIGNALS
303
my_sigset(thr_client_alarm, process_alarm); /* int. thread system calls */
308
#ifndef USE_ALARM_THREAD
309
pthread_sigmask(SIG_SETMASK,&full_signal_set,&old_mask);
310
pthread_mutex_lock(&LOCK_alarm);
312
process_alarm_part2(sig);
313
#ifndef USE_ALARM_THREAD
314
#if !defined(HAVE_BSD_SIGNALS) && !defined(USE_ONE_SIGNAL_HAND)
315
my_sigset(THR_SERVER_ALARM,process_alarm);
317
pthread_mutex_unlock(&LOCK_alarm);
318
pthread_sigmask(SIG_SETMASK,&old_mask,NULL);
324
static RETSIGTYPE process_alarm_part2(int sig __attribute__((unused)))
329
printf("process_alarm\n"); fflush(stdout);
331
if (alarm_queue.elements)
336
for (i=0 ; i < alarm_queue.elements ;)
338
alarm_data=(ALARM*) queue_element(&alarm_queue,i);
339
alarm_data->alarmed=1; /* Info to thread */
340
if (pthread_equal(alarm_data->thread,alarm_thread) ||
341
pthread_kill(alarm_data->thread, thr_client_alarm))
344
printf("Warning: pthread_kill couldn't find thread!!!\n");
346
queue_remove(&alarm_queue,i); /* No thread. Remove alarm */
349
i++; /* Signal next thread */
351
#ifndef USE_ALARM_THREAD
352
if (alarm_queue.elements)
353
alarm(1); /* Signal soon again */
358
uint32_t now=(uint32_t) my_time(0);
359
uint32_t next=now+10-(now%10);
360
while ((alarm_data=(ALARM*) queue_top(&alarm_queue))->expire_time <= now)
362
alarm_data->alarmed=1; /* Info to thread */
363
if (pthread_equal(alarm_data->thread,alarm_thread) ||
364
pthread_kill(alarm_data->thread, thr_client_alarm))
367
printf("Warning: pthread_kill couldn't find thread!!!\n");
369
queue_remove(&alarm_queue,0); /* No thread. Remove alarm */
370
if (!alarm_queue.elements)
375
alarm_data->expire_time=next;
376
queue_replaced(&alarm_queue);
379
#ifndef USE_ALARM_THREAD
380
if (alarm_queue.elements)
382
alarm((uint) (alarm_data->expire_time-now));
383
next_alarm_expire_time= alarm_data->expire_time;
391
Ensure that next time we call thr_alarm(), we will schedule a new alarm
393
next_alarm_expire_time= ~(time_t) 0;
400
Schedule all alarms now and optionally free all structures
404
free_structures Set to 1 if we should free memory used for
406
When we call this we should KNOW that there
409
Set alarm_abort to -1 which will change the behavior of alarms as follows:
410
- All old alarms will be rescheduled at once
411
- All new alarms will be rescheduled to one second
414
void end_thr_alarm(bool free_structures)
416
if (alarm_aborted != 1) /* If memory not freed */
418
pthread_mutex_lock(&LOCK_alarm);
419
alarm_aborted= -1; /* mark aborted */
420
if (alarm_queue.elements || (alarm_thread_running && free_structures))
422
if (pthread_equal(pthread_self(),alarm_thread))
423
alarm(1); /* Shut down everything soon */
429
struct timespec abstime;
431
assert(!alarm_queue.elements);
433
/* Wait until alarm thread dies */
434
set_timespec(abstime, 10); /* Wait up to 10 seconds */
435
while (alarm_thread_running)
437
int error= pthread_cond_timedwait(&COND_alarm, &LOCK_alarm, &abstime);
438
if (error == ETIME || error == ETIMEDOUT)
439
break; /* Don't wait forever */
441
delete_queue(&alarm_queue);
443
pthread_mutex_unlock(&LOCK_alarm);
444
if (!alarm_thread_running) /* Safety */
446
pthread_mutex_destroy(&LOCK_alarm);
447
pthread_cond_destroy(&COND_alarm);
451
pthread_mutex_unlock(&LOCK_alarm);
458
Remove another thread from the alarm
461
void thr_alarm_kill(my_thread_id thread_id)
466
pthread_mutex_lock(&LOCK_alarm);
467
for (i=0 ; i < alarm_queue.elements ; i++)
469
if (((ALARM*) queue_element(&alarm_queue,i))->thread_id == thread_id)
471
ALARM *tmp=(ALARM*) queue_remove(&alarm_queue,i);
473
queue_insert(&alarm_queue,(unsigned char*) tmp);
478
pthread_mutex_unlock(&LOCK_alarm);
482
void thr_alarm_info(ALARM_INFO *info)
484
pthread_mutex_lock(&LOCK_alarm);
485
info->next_alarm_time= 0;
486
info->max_used_alarms= max_used_alarms;
487
if ((info->active_alarms= alarm_queue.elements))
489
uint32_t now=(uint32_t) my_time(0);
491
ALARM *alarm_data= (ALARM*) queue_top(&alarm_queue);
492
time_diff= (long) (alarm_data->expire_time - now);
493
info->next_alarm_time= (uint32_t) (time_diff < 0 ? 0 : time_diff);
495
pthread_mutex_unlock(&LOCK_alarm);
499
This is here for thread to get interruptet from read/write/fcntl
504
static RETSIGTYPE thread_alarm(int sig)
507
printf("thread_alarm\n"); fflush(stdout);
509
#ifndef HAVE_BSD_SIGNALS
510
my_sigset(sig,thread_alarm); /* int. thread system calls */
515
#ifdef HAVE_TIMESPEC_TS_SEC
516
#define tv_sec ts_sec
517
#define tv_nsec ts_nsec
520
/* set up a alarm thread with uses 'sleep' to sleep between alarms */
522
#ifdef USE_ALARM_THREAD
523
static void *alarm_handler(void *arg __attribute__((unused)))
526
struct timespec abstime;
528
puts("Starting alarm thread");
531
alarm_thread_running= 1;
532
pthread_mutex_lock(&LOCK_alarm);
535
if (alarm_queue.elements)
537
uint32_t sleep_time,now= my_time(0);
541
sleep_time= ((ALARM*) queue_top(&alarm_queue))->expire_time;
542
if (sleep_time > now)
544
abstime.tv_sec=sleep_time;
546
next_alarm_expire_time= sleep_time;
547
if ((error=pthread_cond_timedwait(&COND_alarm,&LOCK_alarm,&abstime)) &&
548
error != ETIME && error != ETIMEDOUT)
551
printf("Got error: %d from ptread_cond_timedwait (errno: %d)\n",
557
else if (alarm_aborted == -1)
561
next_alarm_expire_time= ~ (time_t) 0;
562
if ((error=pthread_cond_wait(&COND_alarm,&LOCK_alarm)))
565
printf("Got error: %d from ptread_cond_wait (errno: %d)\n",
572
memset(&alarm_thread, 0, sizeof(alarm_thread)); /* For easy debugging */
573
alarm_thread_running= 0;
574
pthread_cond_signal(&COND_alarm);
575
pthread_mutex_unlock(&LOCK_alarm);
577
return 0; /* Impossible */
579
#endif /* USE_ALARM_THREAD */
584
/****************************************************************************
585
Handling of test case (when compiled with -DMAIN)
586
***************************************************************************/
589
#if !defined(DONT_USE_THR_ALARM)
591
static pthread_cond_t COND_thread_count;
592
static pthread_mutex_t LOCK_thread_count;
593
static uint32_t thread_count;
596
typedef int * fd_set_ptr;
598
typedef fd_set * fd_set_ptr;
601
static void *test_thread(void *arg)
603
int i,param=*((int*) arg),wait_time,retry;
605
thr_alarm_t got_alarm;
609
printf("Thread %d (%s) started\n",param,my_thread_name()); fflush(stdout);
610
for (i=1 ; i <= 10 ; i++)
612
wait_time=param ? 11-i : i;
613
start_time= my_time(0);
614
if (thr_alarm(&got_alarm,wait_time,0))
616
printf("Thread: %s Alarms aborted\n",my_thread_name());
621
printf("Thread: %s Simulation of no alarm needed\n",my_thread_name());
626
for (retry=0 ; !thr_got_alarm(&got_alarm) && retry < 10 ; retry++)
628
printf("Thread: %s Waiting %d sec\n",my_thread_name(),wait_time);
629
select(0,(fd_set_ptr) &fd,0,0,0);
631
if (!thr_got_alarm(&got_alarm))
633
printf("Thread: %s didn't get an alarm. Aborting!\n",
638
{ /* Simulate alarm-miss */
640
uint32_t max_connection=fileno(stdin);
642
FD_SET(max_connection,&readFDs);
646
printf("Thread: %s Simulating alarm miss\n",my_thread_name());
648
if (select(max_connection+1, (fd_set_ptr) &readFDs,0,0,0) < 0)
651
break; /* Got new interrupt */
652
printf("Got errno: %d from select. Retrying..\n",errno);
655
printf("Warning: Interrupt of select() doesn't set errno!\n");
659
else /* This shouldn't happen */
661
if (!FD_ISSET(max_connection,&readFDs))
663
printf("Select interrupted, but errno not set\n");
669
getchar(); /* Somebody was playing */
674
printf("Thread: %s Slept for %d (%d) sec\n",my_thread_name(),
675
(int) (my_time(0)-start_time), wait_time); fflush(stdout);
676
thr_end_alarm(&got_alarm);
679
pthread_mutex_lock(&LOCK_thread_count);
681
pthread_cond_signal(&COND_thread_count); /* Tell main we are ready */
682
pthread_mutex_unlock(&LOCK_thread_count);
683
free((unsigned char*) arg);
687
#ifdef USE_ONE_SIGNAL_HAND
688
static RETSIGTYPE print_signal_warning(int sig)
690
printf("Warning: Got signal %d from thread %s\n",sig,my_thread_name());
692
#ifndef HAVE_BSD_SIGNALS
693
my_sigset(sig,print_signal_warning); /* int. thread system calls */
696
alarm(2); /* reschedule alarm */
698
#endif /* USE_ONE_SIGNAL_HAND */
701
static void *signal_hand(void *arg __attribute__((unused)))
704
int sig,error,err_count=0;;
707
pthread_detach_this_thread();
708
init_thr_alarm(10); /* Setup alarm handler */
709
pthread_mutex_lock(&LOCK_thread_count); /* Required by bsdi */
710
pthread_cond_signal(&COND_thread_count); /* Tell main we are ready */
711
pthread_mutex_unlock(&LOCK_thread_count);
713
sigemptyset(&set); /* Catch all signals */
714
sigaddset(&set,SIGINT);
715
sigaddset(&set,SIGQUIT);
716
sigaddset(&set,SIGTERM);
717
sigaddset(&set,SIGHUP);
719
sigaddset(&set,SIGTSTP);
721
#ifdef USE_ONE_SIGNAL_HAND
722
sigaddset(&set,THR_SERVER_ALARM); /* For alarms */
723
puts("Starting signal and alarm handling thread");
725
puts("Starting signal handling thread");
727
printf("server alarm: %d thread alarm: %d\n",
728
THR_SERVER_ALARM, thr_client_alarm);
731
while ((error=my_sigwait(&set,&sig)) == EINTR)
732
printf("sigwait restarted\n");
735
fprintf(stderr,"Got error %d from sigwait\n",error);
737
exit(1); /* Too many errors in test */
740
#ifdef USE_ONE_SIGNAL_HAND
741
if (sig != THR_SERVER_ALARM)
743
printf("Main thread: Got signal %d\n",sig);
749
printf("Aborting nicely\n");
754
printf("Aborting\n");
756
return 0; /* Keep some compilers happy */
758
#ifdef USE_ONE_SIGNAL_HAND
759
case THR_SERVER_ALARM:
768
int main(int argc __attribute__((unused)),char **argv __attribute__((unused)))
771
pthread_attr_t thr_attr;
774
ALARM_INFO alarm_info;
777
pthread_mutex_init(&LOCK_thread_count,MY_MUTEX_INIT_FAST);
778
pthread_cond_init(&COND_thread_count,NULL);
780
/* Start a alarm handling thread */
782
sigaddset(&set,SIGINT);
783
sigaddset(&set,SIGQUIT);
784
sigaddset(&set,SIGTERM);
785
sigaddset(&set,SIGHUP);
786
signal(SIGTERM,SIG_DFL); /* If it's blocked by parent */
788
sigaddset(&set,SIGTSTP);
790
sigaddset(&set,THR_SERVER_ALARM);
791
sigdelset(&set, thr_client_alarm);
792
(void) pthread_sigmask(SIG_SETMASK,&set,NULL);
795
sigaddset(&set, thr_client_alarm);
796
pthread_sigmask(SIG_UNBLOCK, &set, (sigset_t*) 0);
799
pthread_attr_init(&thr_attr);
800
pthread_attr_setscope(&thr_attr,PTHREAD_SCOPE_PROCESS);
801
pthread_attr_setdetachstate(&thr_attr,PTHREAD_CREATE_DETACHED);
802
pthread_attr_setstacksize(&thr_attr,65536L);
804
/* Start signal thread and wait for it to start */
805
pthread_mutex_lock(&LOCK_thread_count);
806
pthread_create(&tid,&thr_attr,signal_hand,NULL);
807
pthread_cond_wait(&COND_thread_count,&LOCK_thread_count);
808
pthread_mutex_unlock(&LOCK_thread_count);
810
thr_setconcurrency(3);
811
pthread_attr_setscope(&thr_attr,PTHREAD_SCOPE_PROCESS);
812
printf("Main thread: %s\n",my_thread_name());
813
for (i=0 ; i < 2 ; i++)
815
param=(int*) malloc(sizeof(int));
817
pthread_mutex_lock(&LOCK_thread_count);
818
if ((error=pthread_create(&tid,&thr_attr,test_thread,(void*) param)))
820
printf("Can't create thread %d, error: %d\n",i,error);
824
pthread_mutex_unlock(&LOCK_thread_count);
827
pthread_attr_destroy(&thr_attr);
828
pthread_mutex_lock(&LOCK_thread_count);
829
thr_alarm_info(&alarm_info);
830
printf("Main_thread: Alarms: %u max_alarms: %u next_alarm_time: %lu\n",
831
alarm_info.active_alarms, alarm_info.max_used_alarms,
832
alarm_info.next_alarm_time);
835
pthread_cond_wait(&COND_thread_count,&LOCK_thread_count);
836
if (thread_count == 1)
838
printf("Calling end_thr_alarm. This should cancel the last thread\n");
842
pthread_mutex_unlock(&LOCK_thread_count);
843
thr_alarm_info(&alarm_info);
845
printf("Main_thread: Alarms: %u max_alarms: %u next_alarm_time: %lu\n",
846
alarm_info.active_alarms, alarm_info.max_used_alarms,
847
alarm_info.next_alarm_time);
848
printf("Test succeeded\n");
852
#else /* DONT_USE_THR_ALARM */
854
int main(int argc __attribute__((unused)),char **argv __attribute__((unused)))
856
printf("thr_alarm disabled with DONT_USE_THR_ALARM\n");
860
#endif /* DONT_USE_THR_ALARM */