Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00034 #include "config.h"
00035 #include "shared/locks.h"
00036 #include "shared/log.h"
00037
00038 #include <errno.h>
00039 #include <signal.h>
00040 #include <string.h>
00041 #ifdef HAVE_SYS_TIME_H
00042 #include <sys/time.h>
00043 #endif
00044 #ifdef HAVE_TIME_H
00045 #include <time.h>
00046 #endif
00047
00048 static const char* lock_str = "lock";
00049
00050 #if !defined(HAVE_PTHREAD)
00051 #include <sys/wait.h>
00052 #include <sys/types.h>
00053 #include <unistd.h>
00054
00055
00064 void
00065 ods_thr_fork_create(ods_thread_type* thr, void* (*func)(void*), void* arg)
00066 {
00067 pid_t pid = fork();
00068
00069 switch (pid) {
00070 case 0:
00071 *thr = (ods_thread_type)getpid();
00072 (void)(*func)(arg);
00073 exit(0);
00074 case -1:
00075 ods_fatal_exit("[%s] unable to fork thread: %s", lock_str,
00076 strerror(errno));
00077 default:
00078 *thr = (ods_thread_type)pid;
00079 return;
00080 }
00081 return;
00082 }
00083
00084
00090 void ods_thr_fork_wait(ods_thread_type thread)
00091 {
00092 int status = 0;
00093
00094 if (waitpid((pid_t)thread, &status, 0) == -1) {
00095 ods_log_error("[%s] waitpid(%d): %s", lock_str, (int)thread,
00096 strerror(errno));
00097 }
00098 if (status != 0) {
00099 ods_log_warning("[%s] process %d abnormal exit with status %d",
00100 lock_str, (int)thread, status);
00101 }
00102 return;
00103 }
00104 #else
00105
00106
00107 int
00108 ods_thread_wait(cond_basic_type* cond, lock_basic_type* lock, time_t wait)
00109 {
00110 struct timespec ts;
00111 int ret = 0;
00112
00113
00114
00115
00116 #ifndef HAVE_CLOCK_GETTIME
00117 struct timeval tv;
00118 if (gettimeofday(&tv, NULL) != 0) {
00119 ods_log_error("[%s] clock_gettime() error: %s", lock_str,
00120 strerror(errno));
00121 return 1;
00122 }
00123 ts.tv_sec = tv.tv_sec;
00124 ts.tv_nsec = (tv.tv_usec/1000);
00125 #else
00126 if (clock_gettime(CLOCK_REALTIME, &ts) < 0) {
00127 ods_log_error("[%s] clock_gettime() error: %s", lock_str,
00128 strerror(errno));
00129 return 1;
00130 }
00131 #endif
00132
00133 if (wait > 0) {
00134 ts.tv_sec = ts.tv_sec + wait;
00135 ret = pthread_cond_timedwait(cond, lock, &ts);
00136 } else {
00137 ret = pthread_cond_wait(cond, lock);
00138 }
00139
00140 if (ret == ETIMEDOUT) {
00141 return 0;
00142 }
00143 return ret;
00144 }
00145
00146 #endif
00147
00148
00149 void
00150 ods_thread_blocksigs(void)
00151 {
00152 int err = 0;
00153 sigset_t sigset;
00154 sigfillset(&sigset);
00155
00156 #ifndef HAVE_PTHREAD
00157 if((err=pthread_sigmask(SIG_SETMASK, &sigset, NULL)))
00158 ods_fatal_exit("[%s] pthread_sigmask: %s", lock_str, strerror(err));
00159 #else
00160
00161 if((err=sigprocmask(SIG_SETMASK, &sigset, NULL)))
00162 ods_fatal_exit("[%s] sigprocmask: %s", lock_str, strerror(errno));
00163 #endif
00164 }