|
Lines 15-181
Link Here
|
| 15 |
|
15 |
|
| 16 |
|
16 |
|
| 17 |
#include "my_global.h" |
17 |
#include "my_global.h" |
| 18 |
#include "my_thread.h" /* my_thread_init, my_thread_end */ |
|
|
| 19 |
#include "my_sys.h" /* my_message_local */ |
| 20 |
#include "my_timer.h" /* my_timer_t */ |
18 |
#include "my_timer.h" /* my_timer_t */ |
| 21 |
|
19 |
|
| 22 |
#include <string.h> /* memset */ |
|
|
| 23 |
#include <signal.h> |
20 |
#include <signal.h> |
|
|
21 |
#include <string.h> /* memset */ |
| 24 |
|
22 |
|
| 25 |
#if defined(HAVE_SIGEV_THREAD_ID) |
23 |
typedef union mysigval { |
| 26 |
#include <sys/syscall.h> /* SYS_gettid */ |
24 |
int sival_int; |
| 27 |
|
25 |
void *sival_ptr; |
| 28 |
#ifndef sigev_notify_thread_id |
26 |
} mysigval; |
| 29 |
#define sigev_notify_thread_id _sigev_un._tid |
|
|
| 30 |
#endif |
| 31 |
|
| 32 |
#define MY_TIMER_EVENT_SIGNO (SIGRTMIN) |
| 33 |
#define MY_TIMER_KILL_SIGNO (SIGRTMIN+1) |
| 34 |
|
| 35 |
/* Timer thread ID (TID). */ |
| 36 |
static pid_t timer_notify_thread_id; |
| 37 |
|
| 38 |
#elif defined(HAVE_SIGEV_PORT) |
| 39 |
#include <port.h> |
| 40 |
|
| 41 |
int port_id= -1; |
| 42 |
|
| 43 |
#endif |
| 44 |
|
| 45 |
/* Timer thread object. */ |
| 46 |
static my_thread_handle timer_notify_thread; |
| 47 |
|
27 |
|
| 48 |
#if defined(HAVE_SIGEV_THREAD_ID) |
|
|
| 49 |
/** |
28 |
/** |
| 50 |
Timer expiration notification thread. |
29 |
Timer expiration notification thread. |
| 51 |
|
30 |
|
| 52 |
@param arg Barrier object. |
31 |
@param arg Event info. |
| 53 |
*/ |
32 |
*/ |
| 54 |
|
33 |
|
| 55 |
static void * |
34 |
static void |
| 56 |
timer_notify_thread_func(void *arg) |
35 |
timer_notify_thread_func(mysigval arg) { |
| 57 |
{ |
36 |
my_timer_t *timer = (my_timer_t *)(arg.sival_ptr); |
| 58 |
sigset_t set; |
37 |
timer->notify_function(timer); |
| 59 |
siginfo_t info; |
|
|
| 60 |
my_timer_t *timer; |
| 61 |
pthread_barrier_t *barrier= arg; |
| 62 |
|
| 63 |
my_thread_init(); |
| 64 |
|
| 65 |
sigemptyset(&set); |
| 66 |
sigaddset(&set, MY_TIMER_EVENT_SIGNO); |
| 67 |
sigaddset(&set, MY_TIMER_KILL_SIGNO); |
| 68 |
|
| 69 |
/* Get the thread ID of the current thread. */ |
| 70 |
timer_notify_thread_id= (pid_t) syscall(SYS_gettid); |
| 71 |
|
| 72 |
/* Wake up parent thread, timer_notify_thread_id is available. */ |
| 73 |
pthread_barrier_wait(barrier); |
| 74 |
|
| 75 |
while (1) |
| 76 |
{ |
| 77 |
if (sigwaitinfo(&set, &info) < 0) |
| 78 |
continue; |
| 79 |
|
| 80 |
if (info.si_signo == MY_TIMER_EVENT_SIGNO) |
| 81 |
{ |
| 82 |
timer= (my_timer_t*)info.si_value.sival_ptr; |
| 83 |
timer->notify_function(timer); |
| 84 |
} |
| 85 |
else if (info.si_signo == MY_TIMER_KILL_SIGNO) |
| 86 |
break; |
| 87 |
} |
| 88 |
|
| 89 |
my_thread_end(); |
| 90 |
|
| 91 |
return NULL; |
| 92 |
} |
38 |
} |
| 93 |
|
39 |
|
| 94 |
|
|
|
| 95 |
/** |
| 96 |
Create a helper thread to dispatch timer expiration notifications. |
| 97 |
|
| 98 |
@return On success, 0. On error, -1 is returned. |
| 99 |
*/ |
| 100 |
|
| 101 |
static int |
| 102 |
start_helper_thread(void) |
| 103 |
{ |
| 104 |
pthread_barrier_t barrier; |
| 105 |
|
| 106 |
if (pthread_barrier_init(&barrier, NULL, 2)) |
| 107 |
{ |
| 108 |
my_message_local(ERROR_LEVEL, |
| 109 |
"Failed to initialize pthread barrier. errno=%d", errno); |
| 110 |
return -1; |
| 111 |
} |
| 112 |
|
| 113 |
if (mysql_thread_create(key_thread_timer_notifier, &timer_notify_thread, |
| 114 |
NULL, timer_notify_thread_func, &barrier)) |
| 115 |
{ |
| 116 |
my_message_local(ERROR_LEVEL, |
| 117 |
"Failed to create timer notify thread (errno= %d).", |
| 118 |
errno); |
| 119 |
pthread_barrier_destroy(&barrier); |
| 120 |
return -1; |
| 121 |
} |
| 122 |
|
| 123 |
pthread_barrier_wait(&barrier); |
| 124 |
pthread_barrier_destroy(&barrier); |
| 125 |
|
| 126 |
return 0; |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
/** |
| 131 |
Initialize internal components. |
| 132 |
|
| 133 |
@return On success, 0. |
| 134 |
On error, -1 is returned, and errno is set to indicate the error. |
| 135 |
*/ |
| 136 |
|
| 137 |
int |
40 |
int |
| 138 |
my_timer_initialize(void) |
41 |
my_timer_initialize() |
| 139 |
{ |
42 |
{ |
| 140 |
int rc; |
43 |
return 0; |
| 141 |
sigset_t set, old_set; |
|
|
| 142 |
|
| 143 |
if (sigfillset(&set)) |
| 144 |
{ |
| 145 |
my_message_local(ERROR_LEVEL, |
| 146 |
"Failed to intialize signal set (errno=%d).", errno); |
| 147 |
return -1; |
| 148 |
} |
| 149 |
|
| 150 |
/* |
| 151 |
Temporarily block all signals. New thread will inherit signal |
| 152 |
mask of the current thread. |
| 153 |
*/ |
| 154 |
if (pthread_sigmask(SIG_BLOCK, &set, &old_set)) |
| 155 |
return -1; |
| 156 |
|
| 157 |
/* Create a helper thread. */ |
| 158 |
rc= start_helper_thread(); |
| 159 |
|
| 160 |
/* Restore the signal mask. */ |
| 161 |
pthread_sigmask(SIG_SETMASK, &old_set, NULL); |
| 162 |
|
| 163 |
return rc; |
| 164 |
} |
44 |
} |
| 165 |
|
45 |
|
| 166 |
|
46 |
|
| 167 |
/** |
|
|
| 168 |
Release any resources that were allocated as part of initialization. |
| 169 |
*/ |
| 170 |
|
| 171 |
void |
47 |
void |
| 172 |
my_timer_deinitialize(void) |
48 |
my_timer_deinitialize(void) |
| 173 |
{ |
49 |
{ |
| 174 |
/* Kill helper thread. */ |
|
|
| 175 |
pthread_kill(timer_notify_thread.thread, MY_TIMER_KILL_SIGNO); |
| 176 |
|
| 177 |
/* Wait for helper thread termination. */ |
| 178 |
my_thread_join(&timer_notify_thread, NULL); |
| 179 |
} |
50 |
} |
| 180 |
|
51 |
|
| 181 |
|
52 |
|
|
Lines 195-330
my_timer_create(my_timer_t *timer)
Link Here
|
| 195 |
|
66 |
|
| 196 |
memset(&sigev, 0, sizeof(sigev)); |
67 |
memset(&sigev, 0, sizeof(sigev)); |
| 197 |
|
68 |
|
|
|
69 |
sigev.sigev_notify= SIGEV_THREAD; |
| 198 |
sigev.sigev_value.sival_ptr= timer; |
70 |
sigev.sigev_value.sival_ptr= timer; |
| 199 |
sigev.sigev_signo= MY_TIMER_EVENT_SIGNO; |
71 |
sigev.sigev_notify_function= &timer_notify_thread_func; |
| 200 |
sigev.sigev_notify= SIGEV_SIGNAL | SIGEV_THREAD_ID; |
|
|
| 201 |
sigev.sigev_notify_thread_id= timer_notify_thread_id; |
| 202 |
|
| 203 |
return timer_create(CLOCK_MONOTONIC, &sigev, &timer->id); |
| 204 |
} |
| 205 |
#elif defined(HAVE_SIGEV_PORT) |
| 206 |
/** |
| 207 |
Timer expiration notification thread. |
| 208 |
|
| 209 |
@param arg Barrier object. |
| 210 |
*/ |
| 211 |
|
| 212 |
static void * |
| 213 |
timer_notify_thread_func(void *arg MY_ATTRIBUTE((unused))) |
| 214 |
{ |
| 215 |
port_event_t port_event; |
| 216 |
my_timer_t *timer; |
| 217 |
|
| 218 |
my_thread_init(); |
| 219 |
|
| 220 |
while (1) |
| 221 |
{ |
| 222 |
if (port_get(port_id, &port_event, NULL)) |
| 223 |
break; |
| 224 |
|
| 225 |
if (port_event.portev_source != PORT_SOURCE_TIMER) |
| 226 |
continue; |
| 227 |
|
| 228 |
timer= (my_timer_t*)port_event.portev_user; |
| 229 |
timer->notify_function(timer); |
| 230 |
} |
| 231 |
|
| 232 |
my_thread_end(); |
| 233 |
|
| 234 |
return NULL; |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
/** |
| 239 |
Create a helper thread to dispatch timer expiration notifications. |
| 240 |
|
| 241 |
@return On success, 0. On error, -1 is returned. |
| 242 |
*/ |
| 243 |
|
| 244 |
static int |
| 245 |
start_helper_thread(void) |
| 246 |
{ |
| 247 |
if (mysql_thread_create(key_thread_timer_notifier, &timer_notify_thread, |
| 248 |
NULL, timer_notify_thread_func, NULL)) |
| 249 |
{ |
| 250 |
my_message_local(ERROR_LEVEL, |
| 251 |
"Failed to create timer notify thread (errno= %d).", |
| 252 |
errno); |
| 253 |
return -1; |
| 254 |
} |
| 255 |
|
| 256 |
return 0; |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
/** |
| 261 |
Initialize internal components. |
| 262 |
|
| 263 |
@return On success, 0. |
| 264 |
On error, -1 is returned, and errno is set to indicate the error. |
| 265 |
*/ |
| 266 |
|
| 267 |
int |
| 268 |
my_timer_initialize(void) |
| 269 |
{ |
| 270 |
int rc; |
| 271 |
|
| 272 |
if ((port_id= port_create()) < 0) |
| 273 |
{ |
| 274 |
my_message_local(ERROR_LEVEL, "Failed to create port (errno= %d).", errno); |
| 275 |
return -1; |
| 276 |
} |
| 277 |
|
| 278 |
/* Create a helper thread. */ |
| 279 |
rc= start_helper_thread(); |
| 280 |
|
| 281 |
return rc; |
| 282 |
} |
| 283 |
|
| 284 |
|
| 285 |
/** |
| 286 |
Release any resources that were allocated as part of initialization. |
| 287 |
*/ |
| 288 |
|
| 289 |
void |
| 290 |
my_timer_deinitialize(void) |
| 291 |
{ |
| 292 |
DBUG_ASSERT(port_id >= 0); |
| 293 |
|
| 294 |
// close port |
| 295 |
close(port_id); |
| 296 |
|
| 297 |
/* Wait for helper thread termination. */ |
| 298 |
my_thread_join(&timer_notify_thread, NULL); |
| 299 |
} |
| 300 |
|
| 301 |
|
| 302 |
/** |
| 303 |
Create a timer object. |
| 304 |
|
| 305 |
@param timer Location where the timer ID is returned. |
| 306 |
|
| 307 |
@return On success, 0. |
| 308 |
On error, -1 is returned, and errno is set to indicate the error. |
| 309 |
*/ |
| 310 |
|
| 311 |
int |
| 312 |
my_timer_create(my_timer_t *timer) |
| 313 |
{ |
| 314 |
struct sigevent sigev; |
| 315 |
port_notify_t port_notify; |
| 316 |
|
| 317 |
port_notify.portnfy_port= port_id; |
| 318 |
port_notify.portnfy_user= timer; |
| 319 |
|
| 320 |
memset(&sigev, 0, sizeof(sigev)); |
| 321 |
sigev.sigev_value.sival_ptr= &port_notify; |
| 322 |
sigev.sigev_notify= SIGEV_PORT; |
| 323 |
|
72 |
|
|
|
73 |
#ifdef __sun // CLOCK_MONOTONIC not supported on Solaris even if it compiles. |
| 324 |
return timer_create(CLOCK_REALTIME, &sigev, &timer->id); |
74 |
return timer_create(CLOCK_REALTIME, &sigev, &timer->id); |
| 325 |
} |
75 |
#else |
|
|
76 |
return timer_create(CLOCK_MONOTONIC, &sigev, &timer->id); |
| 326 |
#endif |
77 |
#endif |
| 327 |
|
78 |
} |
| 328 |
|
79 |
|
| 329 |
/** |
80 |
/** |
| 330 |
Set the time until the next expiration of the timer. |
81 |
Set the time until the next expiration of the timer. |