From 78d01c6f48cc90194789f24f9a43d2236cf50fe8 Mon Sep 17 00:00:00 2001 From: Wouter Wijngaards Date: Tue, 27 Feb 2007 09:41:34 +0000 Subject: [PATCH] Fork if no threading is available. git-svn-id: file:///svn/unbound/trunk@151 be551aaa-1e26-0410-a405-d3ace91eadb9 --- doc/Changelog | 2 ++ util/locks.c | 6 +++--- util/locks.h | 7 ++++--- util/netevent.c | 2 ++ 4 files changed, 11 insertions(+), 6 deletions(-) diff --git a/doc/Changelog b/doc/Changelog index e4728f3a4..36f7c0fdc 100644 --- a/doc/Changelog +++ b/doc/Changelog @@ -1,6 +1,8 @@ 27 February 2007: Wouter - ub_thread_join portable definition. - forking is used if no threading is available. + Tested, it works, since pipes work across processes as well. + Thread_join is replaced with waitpid. 26 February 2007: Wouter - ub_random code used to select ID and port. diff --git a/util/locks.c b/util/locks.c index b6b821ce0..6df14ead8 100644 --- a/util/locks.c +++ b/util/locks.c @@ -104,10 +104,10 @@ ub_thr_fork_create(ub_thread_t* thr, void* (*func)(void*), void* arg) pid_t pid = fork(); switch(pid) { default: /* main */ - *thr = pid; + *thr = (ub_thread_t)pid; return; case 0: /* child */ - *thr = getpid(); + *thr = (ub_thread_t)getpid(); (void)(*func)(arg); exit(0); case -1: /* error */ @@ -123,7 +123,7 @@ ub_thr_fork_create(ub_thread_t* thr, void* (*func)(void*), void* arg) void ub_thr_fork_wait(ub_thread_t thread) { int status = 0; - if(waitpid(thread, &status, 0) == -1) + if(waitpid((pid_t)thread, &status, 0) == -1) log_err("waitpid(%d): %s", (int)thread, strerror(errno)); if(status != 0) log_warn("process %d abnormal exit with status %d", diff --git a/util/locks.h b/util/locks.h index 240a08982..fe04aae3b 100644 --- a/util/locks.h +++ b/util/locks.h @@ -184,13 +184,14 @@ typedef int lock_quick_t; /** Thread creation, threads do not exist */ typedef pid_t ub_thread_t; -/** ub_thread_create gives an error, it should not be called. */ +/** ub_thread_create is simulated with fork (extremely heavy threads, + * with no shared memory). */ #define ub_thread_create(thr, func, arg) \ ub_thr_fork_create(thr, func, arg) - fatal_exit("%s %d called thread create, but no thread support " \ - "has been compiled in.", __FILE__, __LINE__) #define ub_thread_self() getpid() #define ub_thread_join(thread) ub_thr_fork_wait(thread) +void ub_thr_fork_wait(ub_thread_t thread); +void ub_thr_fork_create(ub_thread_t* thr, void* (*func)(void*), void* arg); #endif /* HAVE_SOLARIS_THREADS */ #endif /* HAVE_PTHREAD */ diff --git a/util/netevent.c b/util/netevent.c index f183be6a7..a51d59217 100644 --- a/util/netevent.c +++ b/util/netevent.c @@ -173,6 +173,8 @@ comm_base_create() void comm_base_delete(struct comm_base* b) { + if(!b) + return; #ifdef HAVE_EVENT_BASE_FREE /* only libevent 1.2+ has it */ event_base_free(b->eb->base); -- 2.47.2