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 <my_global.h>
18
#if !defined(DONT_USE_THR_ALARM)
20
#include <my_pthread.h>
25
#include "thr_alarm.h"
27
#ifdef HAVE_SYS_SELECT_H
28
#include <sys/select.h> /* AIX needs this for fd_set */
32
#define ETIME ETIMEDOUT
35
uint thr_client_alarm;
36
static int alarm_aborted=1; /* No alarm thread */
37
bool thr_alarm_inited= 0;
38
volatile bool alarm_thread_running= 0;
39
time_t next_alarm_expire_time= ~ (time_t) 0;
40
static sig_handler process_alarm_part2(int sig);
42
static pthread_mutex_t LOCK_alarm;
43
static pthread_cond_t COND_alarm;
44
static sigset_t full_signal_set;
45
static QUEUE alarm_queue;
46
static uint max_used_alarms=0;
47
pthread_t alarm_thread;
49
#ifdef USE_ALARM_THREAD
50
static void *alarm_handler(void *arg);
51
#define reschedule_alarms() pthread_cond_signal(&COND_alarm)
53
#define reschedule_alarms() pthread_kill(alarm_thread,THR_SERVER_ALARM)
56
static sig_handler thread_alarm(int sig __attribute__((unused)));
58
static int compare_ulong(void *not_used __attribute__((unused)),
59
uchar *a_ptr,uchar* b_ptr)
61
ulong a=*((ulong*) a_ptr),b= *((ulong*) b_ptr);
62
return (a < b) ? -1 : (a == b) ? 0 : 1;
65
void init_thr_alarm(uint max_alarms)
69
next_alarm_expire_time= ~ (time_t) 0;
70
init_queue(&alarm_queue,max_alarms+1,offsetof(ALARM,expire_time),0,
72
sigfillset(&full_signal_set); /* Neaded to block signals */
73
pthread_mutex_init(&LOCK_alarm,MY_MUTEX_INIT_FAST);
74
pthread_cond_init(&COND_alarm,NULL);
75
if (thd_lib_detected == THD_LIB_LT)
76
thr_client_alarm= SIGALRM;
78
thr_client_alarm= SIGUSR1;
79
#ifndef USE_ALARM_THREAD
80
if (thd_lib_detected != THD_LIB_LT)
83
my_sigset(thr_client_alarm, thread_alarm);
86
sigaddset(&s, THR_SERVER_ALARM);
87
alarm_thread=pthread_self();
88
#if defined(USE_ALARM_THREAD)
90
pthread_attr_t thr_attr;
91
pthread_attr_init(&thr_attr);
92
pthread_attr_setscope(&thr_attr,PTHREAD_SCOPE_PROCESS);
93
pthread_attr_setdetachstate(&thr_attr,PTHREAD_CREATE_DETACHED);
94
pthread_attr_setstacksize(&thr_attr,8196);
96
my_pthread_attr_setprio(&thr_attr,100); /* Very high priority */
97
VOID(pthread_create(&alarm_thread,&thr_attr,alarm_handler,NULL));
98
VOID(pthread_attr_destroy(&thr_attr));
100
#elif defined(USE_ONE_SIGNAL_HAND)
101
pthread_sigmask(SIG_BLOCK, &s, NULL); /* used with sigwait() */
102
if (thd_lib_detected == THD_LIB_LT)
104
my_sigset(thr_client_alarm, process_alarm); /* Linuxthreads */
105
pthread_sigmask(SIG_UNBLOCK, &s, NULL);
108
my_sigset(THR_SERVER_ALARM, process_alarm);
109
pthread_sigmask(SIG_UNBLOCK, &s, NULL);
110
#endif /* USE_ALARM_THREAD */
115
void resize_thr_alarm(uint max_alarms)
117
pthread_mutex_lock(&LOCK_alarm);
119
It's ok not to shrink the queue as there may be more pending alarms than
122
if (alarm_queue.elements < max_alarms)
123
resize_queue(&alarm_queue,max_alarms+1);
124
pthread_mutex_unlock(&LOCK_alarm);
129
Request alarm after sec seconds.
133
alrm Pointer to alarm detection
134
alarm_data Structure to store in alarm queue
137
This function can't be called from the alarm-handling thread.
141
1 If no more alarms are allowed (aborted by process)
143
Stores in first argument a pointer to a non-zero int which is set to 0
144
when the alarm has been given
147
bool thr_alarm(thr_alarm_t *alrm, uint sec, ALARM *alarm_data)
150
#ifndef USE_ONE_SIGNAL_HAND
154
struct st_my_thread_var *current_my_thread_var= my_thread_var;
157
#ifndef USE_ONE_SIGNAL_HAND
158
pthread_sigmask(SIG_BLOCK,&full_signal_set,&old_mask);
160
pthread_mutex_lock(&LOCK_alarm); /* Lock from threads & alarms */
161
if (alarm_aborted > 0)
162
{ /* No signal thread */
163
*alrm= 0; /* No alarm */
164
pthread_mutex_unlock(&LOCK_alarm);
165
#ifndef USE_ONE_SIGNAL_HAND
166
pthread_sigmask(SIG_SETMASK,&old_mask,NULL);
170
if (alarm_aborted < 0)
171
sec= 1; /* Abort mode */
173
if (alarm_queue.elements >= max_used_alarms)
175
if (alarm_queue.elements == alarm_queue.max_elements)
177
fprintf(stderr,"Warning: thr_alarm queue is full\n");
178
*alrm= 0; /* No alarm */
179
pthread_mutex_unlock(&LOCK_alarm);
180
#ifndef USE_ONE_SIGNAL_HAND
181
pthread_sigmask(SIG_SETMASK,&old_mask,NULL);
185
max_used_alarms=alarm_queue.elements+1;
187
reschedule= (ulong) next_alarm_expire_time > (ulong) now + sec;
190
if (!(alarm_data=(ALARM*) my_malloc(sizeof(ALARM),MYF(MY_WME))))
192
*alrm= 0; /* No alarm */
193
pthread_mutex_unlock(&LOCK_alarm);
194
#ifndef USE_ONE_SIGNAL_HAND
195
pthread_sigmask(SIG_SETMASK,&old_mask,NULL);
199
alarm_data->malloced=1;
202
alarm_data->malloced=0;
203
alarm_data->expire_time=now+sec;
204
alarm_data->alarmed=0;
205
alarm_data->thread= current_my_thread_var->pthread_self;
206
alarm_data->thread_id= current_my_thread_var->id;
207
queue_insert(&alarm_queue,(uchar*) alarm_data);
209
/* Reschedule alarm if the current one has more than sec left */
212
if (pthread_equal(pthread_self(),alarm_thread))
214
alarm(sec); /* purecov: inspected */
215
next_alarm_expire_time= now + sec;
218
reschedule_alarms(); /* Reschedule alarms */
220
pthread_mutex_unlock(&LOCK_alarm);
221
#ifndef USE_ONE_SIGNAL_HAND
222
pthread_sigmask(SIG_SETMASK,&old_mask,NULL);
224
(*alrm)= &alarm_data->alarmed;
230
Remove alarm from list of alarms
233
void thr_end_alarm(thr_alarm_t *alarmed)
236
#ifndef USE_ONE_SIGNAL_HAND
241
#ifndef USE_ONE_SIGNAL_HAND
242
pthread_sigmask(SIG_BLOCK,&full_signal_set,&old_mask);
244
pthread_mutex_lock(&LOCK_alarm);
246
alarm_data= (ALARM*) ((uchar*) *alarmed - offsetof(ALARM,alarmed));
247
for (i=0 ; i < alarm_queue.elements ; i++)
249
if ((ALARM*) queue_element(&alarm_queue,i) == alarm_data)
251
queue_remove(&alarm_queue,i),MYF(0);
252
if (alarm_data->malloced)
253
my_free((uchar*) alarm_data,MYF(0));
258
assert(!*alarmed || found == 1);
262
fprintf(stderr,"Warning: Didn't find alarm 0x%lx in queue of %d alarms\n",
263
(long) *alarmed, alarm_queue.elements);
265
pthread_mutex_unlock(&LOCK_alarm);
266
#ifndef USE_ONE_SIGNAL_HAND
267
pthread_sigmask(SIG_SETMASK,&old_mask,NULL);
273
Come here when some alarm in queue is due.
274
Mark all alarms with are finnished in list.
275
Shedule alarms to be sent again after 1-10 sec (many alarms at once)
276
If alarm_aborted is set then all alarms are given and resent
280
sig_handler process_alarm(int sig __attribute__((unused)))
284
if (thd_lib_detected == THD_LIB_LT &&
285
!pthread_equal(pthread_self(),alarm_thread))
287
#if defined(MAIN) && !defined(__bsdi__)
288
printf("thread_alarm in process_alarm\n"); fflush(stdout);
290
#ifdef DONT_REMEMBER_SIGNAL
291
my_sigset(thr_client_alarm, process_alarm); /* int. thread system calls */
296
#ifndef USE_ALARM_THREAD
297
pthread_sigmask(SIG_SETMASK,&full_signal_set,&old_mask);
298
pthread_mutex_lock(&LOCK_alarm);
300
process_alarm_part2(sig);
301
#ifndef USE_ALARM_THREAD
302
#if defined(DONT_REMEMBER_SIGNAL) && !defined(USE_ONE_SIGNAL_HAND)
303
my_sigset(THR_SERVER_ALARM,process_alarm);
305
pthread_mutex_unlock(&LOCK_alarm);
306
pthread_sigmask(SIG_SETMASK,&old_mask,NULL);
312
static sig_handler process_alarm_part2(int sig __attribute__((unused)))
317
printf("process_alarm\n"); fflush(stdout);
319
if (alarm_queue.elements)
324
for (i=0 ; i < alarm_queue.elements ;)
326
alarm_data=(ALARM*) queue_element(&alarm_queue,i);
327
alarm_data->alarmed=1; /* Info to thread */
328
if (pthread_equal(alarm_data->thread,alarm_thread) ||
329
pthread_kill(alarm_data->thread, thr_client_alarm))
332
printf("Warning: pthread_kill couldn't find thread!!!\n");
334
queue_remove(&alarm_queue,i); /* No thread. Remove alarm */
337
i++; /* Signal next thread */
339
#ifndef USE_ALARM_THREAD
340
if (alarm_queue.elements)
341
alarm(1); /* Signal soon again */
346
ulong now=(ulong) my_time(0);
347
ulong next=now+10-(now%10);
348
while ((alarm_data=(ALARM*) queue_top(&alarm_queue))->expire_time <= now)
350
alarm_data->alarmed=1; /* Info to thread */
351
if (pthread_equal(alarm_data->thread,alarm_thread) ||
352
pthread_kill(alarm_data->thread, thr_client_alarm))
355
printf("Warning: pthread_kill couldn't find thread!!!\n");
357
queue_remove(&alarm_queue,0); /* No thread. Remove alarm */
358
if (!alarm_queue.elements)
363
alarm_data->expire_time=next;
364
queue_replaced(&alarm_queue);
367
#ifndef USE_ALARM_THREAD
368
if (alarm_queue.elements)
370
alarm((uint) (alarm_data->expire_time-now));
371
next_alarm_expire_time= alarm_data->expire_time;
379
Ensure that next time we call thr_alarm(), we will schedule a new alarm
381
next_alarm_expire_time= ~(time_t) 0;
388
Schedule all alarms now and optionally free all structures
392
free_structures Set to 1 if we should free memory used for
394
When we call this we should KNOW that there
397
Set alarm_abort to -1 which will change the behavior of alarms as follows:
398
- All old alarms will be rescheduled at once
399
- All new alarms will be rescheduled to one second
402
void end_thr_alarm(bool free_structures)
404
if (alarm_aborted != 1) /* If memory not freed */
406
pthread_mutex_lock(&LOCK_alarm);
407
alarm_aborted= -1; /* mark aborted */
408
if (alarm_queue.elements || (alarm_thread_running && free_structures))
410
if (pthread_equal(pthread_self(),alarm_thread))
411
alarm(1); /* Shut down everything soon */
417
struct timespec abstime;
419
assert(!alarm_queue.elements);
421
/* Wait until alarm thread dies */
422
set_timespec(abstime, 10); /* Wait up to 10 seconds */
423
while (alarm_thread_running)
425
int error= pthread_cond_timedwait(&COND_alarm, &LOCK_alarm, &abstime);
426
if (error == ETIME || error == ETIMEDOUT)
427
break; /* Don't wait forever */
429
delete_queue(&alarm_queue);
431
pthread_mutex_unlock(&LOCK_alarm);
432
if (!alarm_thread_running) /* Safety */
434
pthread_mutex_destroy(&LOCK_alarm);
435
pthread_cond_destroy(&COND_alarm);
439
pthread_mutex_unlock(&LOCK_alarm);
446
Remove another thread from the alarm
449
void thr_alarm_kill(my_thread_id thread_id)
454
pthread_mutex_lock(&LOCK_alarm);
455
for (i=0 ; i < alarm_queue.elements ; i++)
457
if (((ALARM*) queue_element(&alarm_queue,i))->thread_id == thread_id)
459
ALARM *tmp=(ALARM*) queue_remove(&alarm_queue,i);
461
queue_insert(&alarm_queue,(uchar*) tmp);
466
pthread_mutex_unlock(&LOCK_alarm);
470
void thr_alarm_info(ALARM_INFO *info)
472
pthread_mutex_lock(&LOCK_alarm);
473
info->next_alarm_time= 0;
474
info->max_used_alarms= max_used_alarms;
475
if ((info->active_alarms= alarm_queue.elements))
477
ulong now=(ulong) my_time(0);
479
ALARM *alarm_data= (ALARM*) queue_top(&alarm_queue);
480
time_diff= (long) (alarm_data->expire_time - now);
481
info->next_alarm_time= (ulong) (time_diff < 0 ? 0 : time_diff);
483
pthread_mutex_unlock(&LOCK_alarm);
487
This is here for thread to get interruptet from read/write/fcntl
492
static sig_handler thread_alarm(int sig)
495
printf("thread_alarm\n"); fflush(stdout);
497
#ifdef DONT_REMEMBER_SIGNAL
498
my_sigset(sig,thread_alarm); /* int. thread system calls */
503
#ifdef HAVE_TIMESPEC_TS_SEC
504
#define tv_sec ts_sec
505
#define tv_nsec ts_nsec
508
/* set up a alarm thread with uses 'sleep' to sleep between alarms */
510
#ifdef USE_ALARM_THREAD
511
static void *alarm_handler(void *arg __attribute__((unused)))
514
struct timespec abstime;
516
puts("Starting alarm thread");
519
alarm_thread_running= 1;
520
pthread_mutex_lock(&LOCK_alarm);
523
if (alarm_queue.elements)
525
ulong sleep_time,now= my_time(0);
529
sleep_time= ((ALARM*) queue_top(&alarm_queue))->expire_time;
530
if (sleep_time > now)
532
abstime.tv_sec=sleep_time;
534
next_alarm_expire_time= sleep_time;
535
if ((error=pthread_cond_timedwait(&COND_alarm,&LOCK_alarm,&abstime)) &&
536
error != ETIME && error != ETIMEDOUT)
539
printf("Got error: %d from ptread_cond_timedwait (errno: %d)\n",
545
else if (alarm_aborted == -1)
549
next_alarm_expire_time= ~ (time_t) 0;
550
if ((error=pthread_cond_wait(&COND_alarm,&LOCK_alarm)))
553
printf("Got error: %d from ptread_cond_wait (errno: %d)\n",
560
bzero((char*) &alarm_thread,sizeof(alarm_thread)); /* For easy debugging */
561
alarm_thread_running= 0;
562
pthread_cond_signal(&COND_alarm);
563
pthread_mutex_unlock(&LOCK_alarm);
565
return 0; /* Impossible */
567
#endif /* USE_ALARM_THREAD */
572
/****************************************************************************
573
Handling of test case (when compiled with -DMAIN)
574
***************************************************************************/
577
#if !defined(DONT_USE_THR_ALARM)
579
static pthread_cond_t COND_thread_count;
580
static pthread_mutex_t LOCK_thread_count;
581
static uint thread_count;
584
typedef int * fd_set_ptr;
586
typedef fd_set * fd_set_ptr;
589
static void *test_thread(void *arg)
591
int i,param=*((int*) arg),wait_time,retry;
593
thr_alarm_t got_alarm;
597
printf("Thread %d (%s) started\n",param,my_thread_name()); fflush(stdout);
598
for (i=1 ; i <= 10 ; i++)
600
wait_time=param ? 11-i : i;
601
start_time= my_time(0);
602
if (thr_alarm(&got_alarm,wait_time,0))
604
printf("Thread: %s Alarms aborted\n",my_thread_name());
609
printf("Thread: %s Simulation of no alarm needed\n",my_thread_name());
614
for (retry=0 ; !thr_got_alarm(&got_alarm) && retry < 10 ; retry++)
616
printf("Thread: %s Waiting %d sec\n",my_thread_name(),wait_time);
617
select(0,(fd_set_ptr) &fd,0,0,0);
619
if (!thr_got_alarm(&got_alarm))
621
printf("Thread: %s didn't get an alarm. Aborting!\n",
626
{ /* Simulate alarm-miss */
628
uint max_connection=fileno(stdin);
630
FD_SET(max_connection,&readFDs);
634
printf("Thread: %s Simulating alarm miss\n",my_thread_name());
636
if (select(max_connection+1, (fd_set_ptr) &readFDs,0,0,0) < 0)
639
break; /* Got new interrupt */
640
printf("Got errno: %d from select. Retrying..\n",errno);
643
printf("Warning: Interrupt of select() doesn't set errno!\n");
647
else /* This shouldn't happen */
649
if (!FD_ISSET(max_connection,&readFDs))
651
printf("Select interrupted, but errno not set\n");
657
VOID(getchar()); /* Somebody was playing */
662
printf("Thread: %s Slept for %d (%d) sec\n",my_thread_name(),
663
(int) (my_time(0)-start_time), wait_time); fflush(stdout);
664
thr_end_alarm(&got_alarm);
667
pthread_mutex_lock(&LOCK_thread_count);
669
VOID(pthread_cond_signal(&COND_thread_count)); /* Tell main we are ready */
670
pthread_mutex_unlock(&LOCK_thread_count);
675
#ifdef USE_ONE_SIGNAL_HAND
676
static sig_handler print_signal_warning(int sig)
678
printf("Warning: Got signal %d from thread %s\n",sig,my_thread_name());
680
#ifdef DONT_REMEMBER_SIGNAL
681
my_sigset(sig,print_signal_warning); /* int. thread system calls */
684
alarm(2); /* reschedule alarm */
686
#endif /* USE_ONE_SIGNAL_HAND */
689
static void *signal_hand(void *arg __attribute__((unused)))
692
int sig,error,err_count=0;;
695
pthread_detach_this_thread();
696
init_thr_alarm(10); /* Setup alarm handler */
697
pthread_mutex_lock(&LOCK_thread_count); /* Required by bsdi */
698
VOID(pthread_cond_signal(&COND_thread_count)); /* Tell main we are ready */
699
pthread_mutex_unlock(&LOCK_thread_count);
701
sigemptyset(&set); /* Catch all signals */
702
sigaddset(&set,SIGINT);
703
sigaddset(&set,SIGQUIT);
704
sigaddset(&set,SIGTERM);
705
sigaddset(&set,SIGHUP);
707
sigaddset(&set,SIGTSTP);
709
#ifdef USE_ONE_SIGNAL_HAND
710
sigaddset(&set,THR_SERVER_ALARM); /* For alarms */
711
puts("Starting signal and alarm handling thread");
713
puts("Starting signal handling thread");
715
printf("server alarm: %d thread alarm: %d\n",
716
THR_SERVER_ALARM, thr_client_alarm);
719
while ((error=my_sigwait(&set,&sig)) == EINTR)
720
printf("sigwait restarted\n");
723
fprintf(stderr,"Got error %d from sigwait\n",error);
725
exit(1); /* Too many errors in test */
728
#ifdef USE_ONE_SIGNAL_HAND
729
if (sig != THR_SERVER_ALARM)
731
printf("Main thread: Got signal %d\n",sig);
737
printf("Aborting nicely\n");
742
printf("Aborting\n");
744
return 0; /* Keep some compilers happy */
746
#ifdef USE_ONE_SIGNAL_HAND
747
case THR_SERVER_ALARM:
756
int main(int argc __attribute__((unused)),char **argv __attribute__((unused)))
759
pthread_attr_t thr_attr;
762
ALARM_INFO alarm_info;
765
pthread_mutex_init(&LOCK_thread_count,MY_MUTEX_INIT_FAST);
766
pthread_cond_init(&COND_thread_count,NULL);
768
/* Start a alarm handling thread */
770
sigaddset(&set,SIGINT);
771
sigaddset(&set,SIGQUIT);
772
sigaddset(&set,SIGTERM);
773
sigaddset(&set,SIGHUP);
774
signal(SIGTERM,SIG_DFL); /* If it's blocked by parent */
776
sigaddset(&set,SIGTSTP);
778
sigaddset(&set,THR_SERVER_ALARM);
779
sigdelset(&set, thr_client_alarm);
780
(void) pthread_sigmask(SIG_SETMASK,&set,NULL);
783
sigaddset(&set, thr_client_alarm);
784
VOID(pthread_sigmask(SIG_UNBLOCK, &set, (sigset_t*) 0));
787
pthread_attr_init(&thr_attr);
788
pthread_attr_setscope(&thr_attr,PTHREAD_SCOPE_PROCESS);
789
pthread_attr_setdetachstate(&thr_attr,PTHREAD_CREATE_DETACHED);
790
pthread_attr_setstacksize(&thr_attr,65536L);
792
/* Start signal thread and wait for it to start */
793
VOID(pthread_mutex_lock(&LOCK_thread_count));
794
pthread_create(&tid,&thr_attr,signal_hand,NULL);
795
VOID(pthread_cond_wait(&COND_thread_count,&LOCK_thread_count));
796
VOID(pthread_mutex_unlock(&LOCK_thread_count));
798
thr_setconcurrency(3);
799
pthread_attr_setscope(&thr_attr,PTHREAD_SCOPE_PROCESS);
800
printf("Main thread: %s\n",my_thread_name());
801
for (i=0 ; i < 2 ; i++)
803
param=(int*) malloc(sizeof(int));
805
pthread_mutex_lock(&LOCK_thread_count);
806
if ((error=pthread_create(&tid,&thr_attr,test_thread,(void*) param)))
808
printf("Can't create thread %d, error: %d\n",i,error);
812
pthread_mutex_unlock(&LOCK_thread_count);
815
pthread_attr_destroy(&thr_attr);
816
pthread_mutex_lock(&LOCK_thread_count);
817
thr_alarm_info(&alarm_info);
818
printf("Main_thread: Alarms: %u max_alarms: %u next_alarm_time: %lu\n",
819
alarm_info.active_alarms, alarm_info.max_used_alarms,
820
alarm_info.next_alarm_time);
823
VOID(pthread_cond_wait(&COND_thread_count,&LOCK_thread_count));
824
if (thread_count == 1)
826
printf("Calling end_thr_alarm. This should cancel the last thread\n");
830
pthread_mutex_unlock(&LOCK_thread_count);
831
thr_alarm_info(&alarm_info);
833
printf("Main_thread: Alarms: %u max_alarms: %u next_alarm_time: %lu\n",
834
alarm_info.active_alarms, alarm_info.max_used_alarms,
835
alarm_info.next_alarm_time);
836
printf("Test succeeded\n");
840
#else /* DONT_USE_THR_ALARM */
842
int main(int argc __attribute__((unused)),char **argv __attribute__((unused)))
844
printf("thr_alarm disabled with DONT_USE_THR_ALARM\n");
848
#endif /* DONT_USE_THR_ALARM */