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