/* Exported globals */
time_t watchdog_time; /* this has granularity of SLEEP_TIME */
-
-#define TIMEOUT_SIGNAL SIGUSR2
#define SLEEP_TIME 30 /* examine things every 30 seconds */
/* Forward referenced functions */
static btimer_id btimer_start_common(uint32_t wait);
/* Static globals */
-static pthread_mutex_t mutex;
-static pthread_cond_t timer;
+static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
+static pthread_cond_t timer = PTHREAD_COND_INITIALIZER;
static int quit;
static btimer_t *timer_chain = NULL;
/*
* Timeout signal comes here
*/
-static void timeout_handler(int sig)
+void timeout_handler(int sig)
{
return; /* thus interrupting the function */
}
sigfillset(&sigtimer.sa_mask);
sigaction(TIMEOUT_SIGNAL, &sigtimer, NULL);
watchdog_time = time(NULL);
- if ((stat = pthread_mutex_init(&mutex, NULL)) != 0) {
- return stat;
- }
- if ((stat = pthread_cond_init(&timer, NULL)) != 0) {
- pthread_mutex_destroy(&mutex);
- return stat;
- }
quit = FALSE;
if ((stat = pthread_create(&wdid, NULL, btimer_thread, (void *)NULL)) != 0) {
- pthread_mutex_destroy(&mutex);
- pthread_cond_destroy(&timer);
return stat;
}
return 0;
{
int stat;
- P(mutex);
quit = TRUE;
-
+ P(mutex);
if ((stat = pthread_cond_signal(&timer)) != 0) {
V(mutex);
return stat;
*/
static void *btimer_thread(void *arg)
{
- struct timespec timeout;
- int stat;
JCR *jcr;
BSOCK *fd;
btimer_t *wid;
Dmsg0(200, "Start watchdog thread\n");
pthread_detach(pthread_self());
- P(mutex);
for ( ;!quit; ) {
- struct timeval tv;
- struct timezone tz;
time_t timer_start, now;
- Dmsg0(200, "Top of for loop\n");
+ Dmsg0(200, "Top of watchdog loop\n");
watchdog_time = time(NULL); /* update timer */
if (fd) {
timer_start = fd->timer_start;
if (timer_start && (watchdog_time - timer_start) > fd->timeout) {
+ fd->timer_start = 0; /* turn off timer */
fd->timed_out = TRUE;
Jmsg(jcr, M_ERROR, 0, _(
"Watchdog sending kill after %d secs to thread stalled reading Storage daemon.\n"),
if (fd) {
timer_start = fd->timer_start;
if (timer_start && (watchdog_time - timer_start) > fd->timeout) {
+ fd->timer_start = 0; /* turn off timer */
fd->timed_out = TRUE;
Jmsg(jcr, M_ERROR, 0, _(
"Watchdog sending kill after %d secs to thread stalled reading File daemon.\n"),
if (fd) {
timer_start = fd->timer_start;
if (timer_start && (watchdog_time - timer_start) > fd->timeout) {
+ fd->timer_start = 0; /* turn off timer */
fd->timed_out = TRUE;
Jmsg(jcr, M_ERROR, 0, _(
"Watchdog sending kill after %d secs to thread stalled reading Director.\n"),
}
unlock_jcr_chain();
- gettimeofday(&tv, &tz);
- timeout.tv_nsec = 0;
- timeout.tv_sec = tv.tv_sec + SLEEP_TIME;
-
- Dmsg1(200, "pthread_cond_timedwait sec=%d\n", timeout.tv_sec);
- /* Note, this unlocks mutex during the sleep */
- stat = pthread_cond_timedwait(&timer, &mutex, &timeout);
- Dmsg1(200, "pthread_cond_timedwait stat=%d\n", stat);
-
+ Dmsg0(200, "Watchdog sleep.\n");
+ bmicrosleep(SLEEP_TIME, 0);
now = time(NULL);
+ /*
+ * Now handle child and thread timers set by the code.
+ */
/* Walk child chain killing off any process overdue */
+ P(mutex);
for (wid = timer_chain; wid; wid=wid->next) {
int killed = FALSE;
/* First ask him politely to go away */
/* If we asked a child to die, wait 3 seconds and slam him */
if (killed) {
btimer_t *wid1;
- sleep(3);
+ bmicrosleep(3, 0);
for (wid1 = timer_chain; wid1; wid1=wid1->next) {
if (wid->type == TYPE_CHILD &&
!wid1->killed && now > (wid1->start_time + wid1->wait)) {
}
}
}
-
+ V(mutex);
} /* end of big for loop */
- V(mutex);
Dmsg0(200, "End watchdog\n");
return NULL;
}