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);
84 * Masks all signals from a thread.
86 static void mask_all_signals(void);
89 * Masks specific signals from a thread.
92 * @param newmask the new signal mask
93 * @param oldmask the old signal mask
94 * @return 0 for success, non-zero for failure
96 static int mask_signals(int how, const sigset_t *newmask, sigset_t *oldmask);
101 * A class for managing Thread Local Storage values.
103 class XMLTOOL_API ThreadKey
105 MAKE_NONCOPYABLE(ThreadKey);
108 virtual ~ThreadKey() {}
111 * Sets the value for a TLS key.
113 * @param data the value to set
114 * @return 0 for success, non-zero for failure
116 virtual int setData(void* data)=0;
119 * Returns the value for a TLS key.
121 * @return the value or NULL
123 virtual void* getData() const=0;
126 * Creates a new TLS key.
128 * @param destroy_fn a functon to cleanup key values
129 * @return the new key
131 static ThreadKey* create(void (*destroy_fn)(void*));
135 * A class for managing exclusive access to resources.
137 class XMLTOOL_API Mutex
139 MAKE_NONCOPYABLE(Mutex);
145 * Locks the mutex for exclusive access.
147 * @return 0 for success, non-zero for failure
149 virtual int lock()=0;
152 * Unlocks the mutex for exclusive access.
154 * @return 0 for success, non-zero for failure
156 virtual int unlock()=0;
159 * Creates a new mutex object.
161 * @return the new mutex
163 static Mutex* create();
167 * A class for managing shared and exclusive access to resources.
169 class XMLTOOL_API RWLock
171 MAKE_NONCOPYABLE(RWLock);
177 * Obtains a shared lock.
179 * @return 0 for success, non-zero for failure
181 virtual int rdlock()=0;
184 * Obtains an exclusive lock.
186 * @return 0 for success, non-zero for failure
188 virtual int wrlock()=0;
193 * @return 0 for success, non-zero for failure
195 virtual int unlock()=0;
198 * Creates a new read/write lock.
200 * @return the new lock
202 static RWLock* create();
206 * A class for establishing queues on a mutex based on a periodic condition.
208 class XMLTOOL_API CondWait
210 MAKE_NONCOPYABLE(CondWait);
213 virtual ~CondWait() {}
216 * Waits for a condition variable using the supplied mutex as a queue.
218 * @param lock mutex to queue on
219 * @return 0 for success, non-zero for failure
221 virtual int wait(Mutex* lock)=0;
224 * Waits for a condition variable using the supplied mutex as a queue,
225 * but only for a certain time limit.
227 * @param lock mutex to queue on
228 * @param delay_seconds maximum time to wait before waking up
229 * @return 0 for success, non-zero for failure
231 virtual int timedwait(Mutex* lock, int delay_seconds)=0;
234 * Signal a single thread to wake up if a condition changes.
236 * @return 0 for success, non-zero for failure
238 virtual int signal()=0;
241 * Signal all threads to wake up if a condition changes.
243 * @return 0 for success, non-zero for failure
245 virtual int broadcast()=0;
248 * Creates a new condition variable.
250 * @return the new condition variable
252 static CondWait* create();
256 * RAII wrapper for a mutex lock.
258 class XMLTOOL_API Lock {
259 MAKE_NONCOPYABLE(Lock);
262 * Locks and wraps the designated mutex.
264 * @param mtx mutex to lock
266 Lock(Mutex* mtx) : mutex(mtx) {
271 * Unlocks the wrapped mutex.
282 * RAII wrapper for a shared lock.
284 class XMLTOOL_API ReadLock {
285 MAKE_NONCOPYABLE(ReadLock);
288 * Locks and wraps the designated shared lock.
290 * @param lock lock to acquire
292 ReadLock(RWLock* lock) : rwlock(lock) {
297 * Unlocks the wrapped shared lock.
309 #endif /* _xmltooling_threads_h */