2 * Copyright 2001-2007 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.
18 * @file xmltooling/util/Threads.h
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>
33 DECL_XMLTOOLING_EXCEPTION(ThreadingException,XMLTOOL_EXCEPTIONAPI(XMLTOOL_API),xmltooling,XMLToolingException,Exceptions during threading/locking operations);
36 * A class for manual thread creation and synchronization.
38 class XMLTOOL_API Thread
40 MAKE_NONCOPYABLE(Thread);
46 * Disassociate from the thread.
48 * @return 0 for success, non-zero for failure
50 virtual int detach()=0;
53 * Join with the thread and wait for its completion.
55 * @param thread_return holds the return value of the thread routine
56 * @return 0 for success, non-zero for failure
58 virtual int join(void** thread_return)=0;
63 * @param signo the signal to send to the thread
64 * @return 0 for success, non-zero for failure
66 virtual int kill(int signo)=0;
69 * Creates a new thread object to run the supplied start routine.
71 * @param start_routine the function to execute on the thread
72 * @param arg a parameter for the start routine
73 * @return the created and running thread object
75 static Thread* create(void* (*start_routine)(void*), void* arg);
78 * Exits a thread gracefully.
80 * @param return_val the return value for the thread
82 static void exit(void* return_val);
85 * Sleeps the current thread for the specified amount of time.
87 * @param seconds time to sleep
89 static void sleep(int seconds);
92 * Masks all signals from a thread.
94 static void mask_all_signals(void);
97 * Masks specific signals from a thread.
100 * @param newmask the new signal mask
101 * @param oldmask the old signal mask
102 * @return 0 for success, non-zero for failure
104 static int mask_signals(int how, const sigset_t *newmask, sigset_t *oldmask);
109 * A class for managing Thread Local Storage values.
111 class XMLTOOL_API ThreadKey
113 MAKE_NONCOPYABLE(ThreadKey);
116 virtual ~ThreadKey() {}
119 * Sets the value for a TLS key.
121 * @param data the value to set
122 * @return 0 for success, non-zero for failure
124 virtual int setData(void* data)=0;
127 * Returns the value for a TLS key.
129 * @return the value or NULL
131 virtual void* getData() const=0;
134 * Creates a new TLS key.
136 * @param destroy_fn a functon to cleanup key values
137 * @return the new key
139 static ThreadKey* create(void (*destroy_fn)(void*));
143 * A class for managing exclusive access to resources.
145 class XMLTOOL_API Mutex
147 MAKE_NONCOPYABLE(Mutex);
153 * Locks the mutex for exclusive access.
155 * @return 0 for success, non-zero for failure
157 virtual int lock()=0;
160 * Unlocks the mutex for exclusive access.
162 * @return 0 for success, non-zero for failure
164 virtual int unlock()=0;
167 * Creates a new mutex object.
169 * @return the new mutex
171 static Mutex* create();
175 * A class for managing shared and exclusive access to resources.
177 class XMLTOOL_API RWLock
179 MAKE_NONCOPYABLE(RWLock);
185 * Obtains a shared lock.
187 * @return 0 for success, non-zero for failure
189 virtual int rdlock()=0;
192 * Obtains an exclusive lock.
194 * @return 0 for success, non-zero for failure
196 virtual int wrlock()=0;
201 * @return 0 for success, non-zero for failure
203 virtual int unlock()=0;
206 * Creates a new read/write lock.
208 * @return the new lock
210 static RWLock* create();
214 * A class for establishing queues on a mutex based on a periodic condition.
216 class XMLTOOL_API CondWait
218 MAKE_NONCOPYABLE(CondWait);
221 virtual ~CondWait() {}
224 * Waits for a condition variable using the supplied mutex as a queue.
226 * @param lock mutex to queue on
227 * @return 0 for success, non-zero for failure
229 virtual int wait(Mutex* lock)=0;
232 * Waits for a condition variable using the supplied mutex as a queue,
233 * but only for a certain time limit.
235 * @param lock mutex to queue on
236 * @param delay_seconds maximum time to wait before waking up
237 * @return 0 for success, non-zero for failure
239 virtual int timedwait(Mutex* lock, int delay_seconds)=0;
242 * Signal a single thread to wake up if a condition changes.
244 * @return 0 for success, non-zero for failure
246 virtual int signal()=0;
249 * Signal all threads to wake up if a condition changes.
251 * @return 0 for success, non-zero for failure
253 virtual int broadcast()=0;
256 * Creates a new condition variable.
258 * @return the new condition variable
260 static CondWait* create();
264 * RAII wrapper for a mutex lock.
266 class XMLTOOL_API Lock {
267 MAKE_NONCOPYABLE(Lock);
270 * Locks and wraps the designated mutex.
272 * @param mtx mutex to lock
274 Lock(Mutex* mtx) : mutex(mtx) {
279 * Unlocks the wrapped mutex.
290 * RAII wrapper for a shared lock.
292 class XMLTOOL_API SharedLock {
293 MAKE_NONCOPYABLE(SharedLock);
296 * Locks and wraps the designated shared lock.
298 * @param lock lock to acquire
299 * @param lockit true if the lock should be acquired here, false if already acquired
301 SharedLock(RWLock* lock, bool lockit=true) : rwlock(lock) {
307 * Unlocks the wrapped shared lock.
319 #endif /* _xmltooling_threads_h */