Skip to content

Commit

Permalink
Merge pull request #500 from yschaeff/lock_clean
Browse files Browse the repository at this point in the history
Don't pretent we can work without pthread
  • Loading branch information
halderen committed Aug 16, 2016
2 parents d0b4ec8 + 05de01b commit 9c7b373
Show file tree
Hide file tree
Showing 36 changed files with 312 additions and 467 deletions.
99 changes: 6 additions & 93 deletions common/locks.c
Expand Up @@ -36,68 +36,10 @@
#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)
{
Expand Down Expand Up @@ -125,59 +67,30 @@ ods_thread_create(pthread_t *thr, void *(*func)(void *), void *arg)
}

int
ods_thread_wait(cond_basic_type* cond, lock_basic_type* lock, time_t wait)
ods_thread_wait(pthread_cond_t* cond, pthread_mutex_t* 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 (wait <= 0)
return pthread_cond_wait(cond, lock);

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;
ts.tv_sec += wait;
return pthread_cond_timedwait(cond, lock, &ts);
}

#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 */
}
68 changes: 5 additions & 63 deletions common/locks.h
Expand Up @@ -38,76 +38,18 @@
#include <errno.h>
#include <stdlib.h>

#define LOCKRET(func) do { \
int err; \
if ( (err=(func)) != 0) \
ods_log_error("%s at %d could not " #func ": %s", \
__FILE__, __LINE__, strerror(err)); \
} while(0)

#if defined(HAVE_PTHREAD)

#include <pthread.h>

/** ods-signerd will crash if the thread stacksize is too small */
#define ODS_MINIMUM_STACKSIZE 524288

/** use pthread mutex for basic lock */
typedef pthread_mutex_t lock_basic_type;
/** use pthread cond for basic condition */
typedef pthread_cond_t cond_basic_type;

/** small front for pthread init func, NULL is default attrs. */
#define lock_basic_init(lock) LOCKRET(pthread_mutex_init(lock, NULL))
#define lock_basic_destroy(lock) LOCKRET(pthread_mutex_destroy(lock))
#define lock_basic_lock(lock) LOCKRET(pthread_mutex_lock(lock))
#define lock_basic_unlock(lock) LOCKRET(pthread_mutex_unlock(lock))

/** our own alarm clock */
#define lock_basic_set(cond) LOCKRET(pthread_cond_init(cond, NULL))
#define lock_basic_sleep(cond, lock, sleep) LOCKRET(ods_thread_wait(cond, lock, sleep))
#define lock_basic_alarm(cond) LOCKRET(pthread_cond_signal(cond))
#define lock_basic_broadcast(cond) LOCKRET(pthread_cond_broadcast(cond))
#define lock_basic_off(cond) LOCKRET(pthread_cond_destroy(cond))

/** thread creation */
typedef pthread_t ods_thread_type;
/** Pass where to store tread_t in thr. */
#define ods_thread_detach(thr) LOCKRET(pthread_detach(thr))
#define ods_thread_self() pthread_self()
#define ods_thread_join(thr) LOCKRET(pthread_join(thr, NULL))
#define ods_thread_kill(thr, sig) LOCKRET(pthread_kill(thr, sig))
int ods_thread_create(pthread_t *thr, void *(*func)(void *), void *arg);
int ods_thread_wait(cond_basic_type* cond, lock_basic_type* lock, time_t wait);

#else /* !HAVE_PTHREAD */

/* we do not have PTHREADS */
#define PTHREADS_DISABLED 1

typedef int lock_basic_type;
#define lock_basic_init(lock) /* nop */
#define lock_basic_destroy(lock) /* nop */
#define lock_basic_lock(lock) /* nop */
#define lock_basic_unlock(lock) /* nop */

#define lock_basic_set(cond) /* nop */
#define lock_basic_sleep(cond, lock, sleep) /* nop */
#define lock_basic_alarm(cond) /* nop */
#define lock_basic_broadcast(cond) /* nop */
#define lock_basic_off(cond) /* nop */

typedef pid_t ods_thread_type;
#define ods_thread_create(thr, func, arg) ods_thr_fork_create(thr, func, arg)
#define ods_thread_detach(thr) /* nop */
#define ods_thread_self() getpid()
#define ods_thread_join(thr) ods_thr_fork_wait(thr)

void ods_thr_fork_create(ods_thread_type* thr, void* (*func)(void*), void* arg);
void ods_thr_fork_wait(ods_thread_type thread);

#endif /* HAVE_PTHREAD */
int ods_thread_wait(pthread_cond_t* cond, pthread_mutex_t* lock, time_t wait);

/**
* Explicitly block all signals for calling thread so we are sure any
* signal coming from our OS will end up at the main thread.
*/
void ods_thread_blocksigs(void);

#endif /* SHARED_LOCKS_H */
18 changes: 4 additions & 14 deletions enforcer/src/daemon/engine.c
Expand Up @@ -51,6 +51,7 @@
#include "db/database_version.h"
#include "hsmkey/hsm_key_factory.h"
#include "libhsm.h"
#include "locks.h"

#include <errno.h>
#include <libxml/parser.h>
Expand Down Expand Up @@ -111,15 +112,8 @@ engine_dealloc(engine_type* engine)
static void*
cmdhandler_thread_start(void* arg)
{
int err;
sigset_t sigset;
cmdhandler_type* cmd = (cmdhandler_type*) arg;

sigfillset(&sigset);
if((err=pthread_sigmask(SIG_SETMASK, &sigset, NULL)))
ods_fatal_exit("[%s] pthread_sigmask: %s", engine_str, strerror(err));

cmdhandler_start(cmd);
ods_thread_blocksigs();
cmdhandler_start((cmdhandler_type*) arg);
return NULL;
}

Expand Down Expand Up @@ -190,13 +184,9 @@ engine_create_workers(engine_type* engine)
static void*
worker_thread_start(void* arg)
{
int err;
sigset_t sigset;
worker_type* worker = (worker_type*) arg;

sigfillset(&sigset);
if((err=pthread_sigmask(SIG_SETMASK, &sigset, NULL)))
ods_fatal_exit("[%s] pthread_sigmask: %s", engine_str, strerror(err));
ods_thread_blocksigs();

worker->dbconn = get_database_connection(worker->engine->dbcfg_list);
if (!worker->dbconn) {
Expand Down
3 changes: 3 additions & 0 deletions enforcer/src/scheduler/schedule.c
Expand Up @@ -62,9 +62,12 @@ static task_t* get_first_task(schedule_type *schedule);
static void*
alarm_handler(sig_atomic_t sig)
{
pthread_t tid = pthread_self();
switch (sig) {
case SIGALRM:

ods_log_debug("[%s] SIGALRM received", schedule_str);
printf("[%s] SIGALRM received: %d\n", schedule_str, tid);
pthread_mutex_lock(schedule_lock);
pthread_cond_signal(schedule_cond);
pthread_mutex_unlock(schedule_lock);
Expand Down
20 changes: 10 additions & 10 deletions signer/src/adapter/adapi.c
Expand Up @@ -114,23 +114,23 @@ adapi_trans_full(zone_type* zone, unsigned more_coming)
namedb_diff(zone->db, 0, more_coming);

if (zone->stats) {
lock_basic_lock(&zone->stats->stats_lock);
pthread_mutex_lock(&zone->stats->stats_lock);
zone->stats->nsec_time = 0;
zone->stats->nsec_count = 0;
lock_basic_unlock(&zone->stats->stats_lock);
pthread_mutex_unlock(&zone->stats->stats_lock);
}
start = time(NULL);
/* nsecify(3) */
namedb_nsecify(zone->db, &num_added);
end = time(NULL);
if (zone->stats) {
lock_basic_lock(&zone->stats->stats_lock);
pthread_mutex_lock(&zone->stats->stats_lock);
if (!zone->stats->start_time) {
zone->stats->start_time = start;
}
zone->stats->nsec_time = (end-start);
zone->stats->nsec_count = num_added;
lock_basic_unlock(&zone->stats->stats_lock);
pthread_mutex_unlock(&zone->stats->stats_lock);
}
}

Expand All @@ -151,23 +151,23 @@ adapi_trans_diff(zone_type* zone, unsigned more_coming)
namedb_diff(zone->db, 1, more_coming);

if (zone->stats) {
lock_basic_lock(&zone->stats->stats_lock);
pthread_mutex_lock(&zone->stats->stats_lock);
zone->stats->nsec_time = 0;
zone->stats->nsec_count = 0;
lock_basic_unlock(&zone->stats->stats_lock);
pthread_mutex_unlock(&zone->stats->stats_lock);
}
start = time(NULL);
/* nsecify(3) */
namedb_nsecify(zone->db, &num_added);
end = time(NULL);
if (zone->stats) {
lock_basic_lock(&zone->stats->stats_lock);
pthread_mutex_lock(&zone->stats->stats_lock);
if (!zone->stats->start_time) {
zone->stats->start_time = start;
}
zone->stats->nsec_time = (end-start);
zone->stats->nsec_count = num_added;
lock_basic_unlock(&zone->stats->stats_lock);
pthread_mutex_unlock(&zone->stats->stats_lock);
}
}

Expand Down Expand Up @@ -440,11 +440,11 @@ adapi_printixfr(FILE* fd, zone_type* zone)
if (status != ODS_STATUS_OK) {
return status;
}
lock_basic_lock(&zone->ixfr->ixfr_lock);
pthread_mutex_lock(&zone->ixfr->ixfr_lock);
if (ixfr_print(fd, zone->ixfr)) {
zone->adoutbound->error = 1;
}
lock_basic_unlock(&zone->ixfr->ixfr_lock);
pthread_mutex_unlock(&zone->ixfr->ixfr_lock);
rrset_print(fd, rrset, 1, &status);
return status;
}

0 comments on commit 9c7b373

Please sign in to comment.