2 * Copyright 2001-2006 Internet2
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
20 * Thread and locking wrappers
23 #ifndef _xmltooling_threads_h
24 #define _xmltooling_threads_h
26 #include <xmltooling/base.h>
27 #include <xmltooling/exceptions.h>
31 DECL_XMLTOOLING_EXCEPTION(ThreadingException,XMLTOOL_EXCEPTIONAPI(XMLTOOL_API),xmltooling,XMLToolingException,Exceptions during threading/locking operations);
34 * A class for manual thread creation and synchronization.
36 class XMLTOOL_API Thread
38 MAKE_NONCOPYABLE(Thread);
44 * Disassociate from the thread.
46 * @return 0 for success, non-zero for failure
48 virtual int detach()=0;
51 * Join with the thread and wait for its completion.
53 * @param thread_return holds the return value of the thread routine
54 * @return 0 for success, non-zero for failure
56 virtual int join(void** thread_return)=0;
61 * @param signo the signal to send to the thread
62 * @return 0 for success, non-zero for failure
64 virtual int kill(int signo)=0;
67 * Creates a new thread object to run the supplied start routine.
69 * @param start_routine the function to execute on the thread
70 * @param arg a parameter for the start routine
71 * @return the created and running thread object
73 static Thread* create(void* (*start_routine)(void*), void* arg);
76 * Exits a thread gracefully.
78 * @param return_val the return value for the thread
80 static void exit(void* return_val);
83 * Sleeps the current thread for the specified amount of time.
85 * @param seconds time to sleep
87 static void sleep(int secounds);
90 * Masks all signals from a thread.
92 static void mask_all_signals(void);
95 * Masks specific signals from a thread.
98 * @param newmask the new signal mask
99 * @param oldmask the old signal mask
100 * @return 0 for success, non-zero for failure
102 static int mask_signals(int how, const sigset_t *newmask, sigset_t *oldmask);
107 * A class for managing Thread Local Storage values.
109 class XMLTOOL_API ThreadKey
111 MAKE_NONCOPYABLE(ThreadKey);
114 virtual ~ThreadKey() {}
117 * Sets the value for a TLS key.
119 * @param data the value to set
120 * @return 0 for success, non-zero for failure
122 virtual int setData(void* data)=0;
125 * Returns the value for a TLS key.
127 * @return the value or NULL
129 virtual void* getData() const=0;
132 * Creates a new TLS key.
134 * @param destroy_fn a functon to cleanup key values
135 * @return the new key
137 static ThreadKey* create(void (*destroy_fn)(void*));
141 * A class for managing exclusive access to resources.
143 class XMLTOOL_API Mutex
145 MAKE_NONCOPYABLE(Mutex);
151 * Locks the mutex for exclusive access.
153 * @return 0 for success, non-zero for failure
155 virtual int lock()=0;
158 * Unlocks the mutex for exclusive access.
160 * @return 0 for success, non-zero for failure
162 virtual int unlock()=0;
165 * Creates a new mutex object.
167 * @return the new mutex
169 static Mutex* create();
173 * A class for managing shared and exclusive access to resources.
175 class XMLTOOL_API RWLock
177 MAKE_NONCOPYABLE(RWLock);
183 * Obtains a shared lock.
185 * @return 0 for success, non-zero for failure
187 virtual int rdlock()=0;
190 * Obtains an exclusive lock.
192 * @return 0 for success, non-zero for failure
194 virtual int wrlock()=0;
199 * @return 0 for success, non-zero for failure
201 virtual int unlock()=0;
204 * Creates a new read/write lock.
206 * @return the new lock
208 static RWLock* create();
212 * A class for establishing queues on a mutex based on a periodic condition.
214 class XMLTOOL_API CondWait
216 MAKE_NONCOPYABLE(CondWait);
219 virtual ~CondWait() {}
222 * Waits for a condition variable using the supplied mutex as a queue.
224 * @param lock mutex to queue on
225 * @return 0 for success, non-zero for failure
227 virtual int wait(Mutex* lock)=0;
230 * Waits for a condition variable using the supplied mutex as a queue,
231 * but only for a certain time limit.
233 * @param lock mutex to queue on
234 * @param delay_seconds maximum time to wait before waking up
235 * @return 0 for success, non-zero for failure
237 virtual int timedwait(Mutex* lock, int delay_seconds)=0;
240 * Signal a single thread to wake up if a condition changes.
242 * @return 0 for success, non-zero for failure
244 virtual int signal()=0;
247 * Signal all threads to wake up if a condition changes.
249 * @return 0 for success, non-zero for failure
251 virtual int broadcast()=0;
254 * Creates a new condition variable.
256 * @return the new condition variable
258 static CondWait* create();
262 * RAII wrapper for a mutex lock.
264 class XMLTOOL_API Lock {
265 MAKE_NONCOPYABLE(Lock);
268 * Locks and wraps the designated mutex.
270 * @param mtx mutex to lock
272 Lock(Mutex* mtx) : mutex(mtx) {
277 * Unlocks the wrapped mutex.
288 * RAII wrapper for a shared lock.
290 class XMLTOOL_API SharedLock {
291 MAKE_NONCOPYABLE(SharedLock);
294 * Locks and wraps the designated shared lock.
296 * @param lock lock to acquire
297 * @param lockit true if the lock should be acquired here, false if already acquired
299 SharedLock(RWLock* lock, bool lockit=true) : rwlock(lock) {
305 * Unlocks the wrapped shared lock.
317 #endif /* _xmltooling_threads_h */