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
|
/*
* Copyright (c) 2009 NLNet Labs. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
/**
* Threading and locking.
*
*/
#include "config.h"
#include "locks.h"
#include "log.h"
#include <errno.h>
#include <signal.h> /* sigfillset(), sigprocmask() */
#include <string.h> /* strerror() */
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h> /* gettimeofday() */
#endif
#ifdef HAVE_TIME_H
#include <time.h> /* gettimeofday() */
#endif
static const char* lock_str = "lock";
#if !defined(HAVE_PTHREAD)
#include <sys/wait.h> /* waitpid() */
#include <sys/types.h> /* getpid(), waitpid() */
#include <unistd.h> /* fork(), getpid() */
/**
* No threading available: fork a new process.
* This means no shared data structure, and no locking.
* Only the main thread ever returns. Exits on errors.
* @param thr: the location where to store the thread-id.
* @param func: function body of the thread. Return value of func is lost.
* @param arg: user argument to func.
*/
void
ods_thr_fork_create(ods_thread_type* thr, void* (*func)(void*), void* arg)
{
pid_t pid = fork();
switch (pid) {
case 0: /* child */
*thr = (ods_thread_type)getpid();
(void)(*func)(arg);
exit(0);
case -1: /* error */
ods_fatal_exit("[%s] unable to fork thread: %s", lock_str,
strerror(errno));
default: /* main */
*thr = (ods_thread_type)pid;
}
}
/**
* There is no threading. Wait for a process to terminate.
* Note that ub_thread_t is defined as pid_t.
* @param thread: the process id to wait for.
*/
void ods_thr_fork_wait(ods_thread_type thread)
{
int status = 0;
if (waitpid((pid_t)thread, &status, 0) == -1) {
ods_log_error("[%s] waitpid(%d): %s", lock_str, (int)thread,
strerror(errno));
}
if (status != 0) {
ods_log_warning("[%s] process %d abnormal exit with status %d",
lock_str, (int)thread, status);
}
}
#else /* defined(HAVE_PTHREAD) */
int
ods_thread_create(pthread_t *thr, void *(*func)(void *), void *arg)
{
int ret, attr_set;
pthread_attr_t attr;
size_t stacksize;
attr_set = (
!pthread_attr_init(&attr)
&& !pthread_attr_getstacksize(&attr, &stacksize)
&& stacksize < ODS_MINIMUM_STACKSIZE
&& !pthread_attr_setstacksize(&attr, ODS_MINIMUM_STACKSIZE)
);
ret = pthread_create(thr, attr_set?&attr:NULL, func, arg);
if (attr_set)
(void) pthread_attr_destroy(&attr);
if ( ret != 0) {
ods_log_error("%s at %d could not pthread_create(thr, &attr, func, arg): %s",
__FILE__, __LINE__, strerror(ret));
}
return ret;
}
int
ods_thread_wait(cond_basic_type* cond, lock_basic_type* lock, time_t wait)
{
struct timespec ts;
int ret = 0;
#ifndef HAVE_CLOCK_GETTIME
struct timeval tv;
if (gettimeofday(&tv, NULL) != 0) {
ods_log_error("[%s] clock_gettime() error: %s", lock_str,
strerror(errno));
return 1;
}
ts.tv_sec = tv.tv_sec;
ts.tv_nsec = (tv.tv_usec/1000);
#else /* HAVE_CLOCK_GETTIME */
if (clock_gettime(CLOCK_REALTIME, &ts) < 0) {
ods_log_error("[%s] clock_gettime() error: %s", lock_str,
strerror(errno));
return 1;
}
#endif /* !HAVE_CLOCK_GETTIME */
if (wait > 0) {
ts.tv_sec = ts.tv_sec + wait;
ret = pthread_cond_timedwait(cond, lock, &ts);
} else {
ret = pthread_cond_wait(cond, lock);
}
if (ret == ETIMEDOUT) {
return 0;
}
return ret;
}
#endif /* defined(HAVE_PTHREAD) */
void
ods_thread_blocksigs(void)
{
#ifndef HAVE_PTHREAD
int err = 0;
#endif
sigset_t sigset;
sigfillset(&sigset);
#ifndef HAVE_PTHREAD
if((err=pthread_sigmask(SIG_SETMASK, &sigset, NULL)))
ods_fatal_exit("[%s] pthread_sigmask: %s", lock_str, strerror(err));
#else /* !HAVE_PTHREAD */
/* have nothing, do single process signal mask */
if(sigprocmask(SIG_SETMASK, &sigset, NULL) != 0)
ods_fatal_exit("[%s] sigprocmask: %s", lock_str, strerror(errno));
#endif /* HAVE_PTHREAD */
}
|