OpenDNSSEC-signer  1.4.9
locks.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2009 NLNet Labs. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
17  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
19  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
21  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
22  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
23  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  *
25  */
26 
32 #ifndef SCHEDULER_LOCKS_H
33 #define SCHEDULER_LOCKS_H
34 
35 #include "config.h"
36 #include "shared/log.h"
37 
38 #include <errno.h>
39 #include <stdlib.h>
40 
41 #define LOCKRET(func) do { \
42  int err; \
43  if ( (err=(func)) != 0) \
44  ods_log_error("%s at %d could not " #func ": %s", \
45  __FILE__, __LINE__, strerror(err)); \
46  } while(0)
47 
48 #if defined(HAVE_PTHREAD)
49 
50 #include <pthread.h>
51 
53 #define ODS_MINIMUM_STACKSIZE 524288
54 
56 typedef pthread_mutex_t lock_basic_type;
58 typedef pthread_cond_t cond_basic_type;
59 
61 #define lock_basic_init(lock) LOCKRET(pthread_mutex_init(lock, NULL))
62 #define lock_basic_destroy(lock) LOCKRET(pthread_mutex_destroy(lock))
63 #define lock_basic_lock(lock) LOCKRET(pthread_mutex_lock(lock))
64 #define lock_basic_unlock(lock) LOCKRET(pthread_mutex_unlock(lock))
65 
67 #define lock_basic_set(cond) LOCKRET(pthread_cond_init(cond, NULL))
68 #define lock_basic_sleep(cond, lock, sleep) LOCKRET(ods_thread_wait(cond, lock, sleep))
69 #define lock_basic_alarm(cond) LOCKRET(pthread_cond_signal(cond))
70 #define lock_basic_broadcast(cond) LOCKRET(pthread_cond_broadcast(cond))
71 #define lock_basic_off(cond) LOCKRET(pthread_cond_destroy(cond))
72 
73 int ods_thread_wait(cond_basic_type* cond, lock_basic_type* lock, time_t wait);
74 
76 typedef pthread_t ods_thread_type;
78 #define ods_thread_detach(thr) LOCKRET(pthread_detach(thr))
79 #define ods_thread_self() pthread_self()
80 #define ods_thread_join(thr) LOCKRET(pthread_join(thr, NULL))
81 #define ods_thread_kill(thr, sig) LOCKRET(pthread_kill(thr, sig))
82 int ods_thread_create(pthread_t *thr, void *(*func)(void *), void *arg);
83 int ods_thread_wait(cond_basic_type* cond, lock_basic_type* lock, time_t wait);
84 void ods_thread_blocksigs(void);
85 
86 #else /* !HAVE_PTHREAD */
87 
88 /* we do not have PTHREADS */
89 #define PTHREADS_DISABLED 1
90 
91 typedef int lock_basic_type;
92 #define lock_basic_init(lock) /* nop */
93 #define lock_basic_destroy(lock) /* nop */
94 #define lock_basic_lock(lock) /* nop */
95 #define lock_basic_unlock(lock) /* nop */
96 
97 #define lock_basic_set(cond) /* nop */
98 #define lock_basic_sleep(cond, lock, sleep) /* nop */
99 #define lock_basic_alarm(cond) /* nop */
100 #define lock_basic_broadcast(cond) /* nop */
101 #define lock_basic_off(cond) /* nop */
102 
103 typedef pid_t ods_thread_type;
104 #define ods_thread_create(thr, func, arg) ods_thr_fork_create(thr, func, arg)
105 #define ods_thread_detach(thr) /* nop */
106 #define ods_thread_self() getpid()
107 #define ods_thread_join(thr) ods_thr_fork_wait(thr)
108 
109 void ods_thr_fork_create(ods_thread_type* thr, void* (*func)(void*), void* arg);
110 void ods_thr_fork_wait(ods_thread_type thread);
111 
112 #endif /* HAVE_PTHREAD */
113 
114 void ods_thread_blocksigs(void);
115 
116 #endif /* SHARED_LOCKS_H */
void ods_thread_blocksigs(void)
Definition: locks.c:173
void ods_thr_fork_create(ods_thread_type *thr, void *(*func)(void *), void *arg)
Definition: locks.c:63
pid_t ods_thread_type
Definition: locks.h:103
int lock_basic_type
Definition: locks.h:91
void ods_thr_fork_wait(ods_thread_type thread)
Definition: locks.c:88
#define ods_thread_create(thr, func, arg)
Definition: locks.h:104