2 * Copyright 2001-2006 Internet2
\r
4 * Licensed under the Apache License, Version 2.0 (the "License");
\r
5 * you may not use this file except in compliance with the License.
\r
6 * You may obtain a copy of the License at
\r
8 * http://www.apache.org/licenses/LICENSE-2.0
\r
10 * Unless required by applicable law or agreed to in writing, software
\r
11 * distributed under the License is distributed on an "AS IS" BASIS,
\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
13 * See the License for the specific language governing permissions and
\r
14 * limitations under the License.
\r
20 * Thread and locking wrappers
\r
23 #ifndef _xmltooling_threads_h
\r
24 #define _xmltooling_threads_h
\r
26 #include <xmltooling/base.h>
\r
27 #include <xmltooling/exceptions.h>
\r
29 namespace xmltooling
\r
31 DECL_XMLTOOLING_EXCEPTION(ThreadingException,XMLTOOL_EXCEPTIONAPI(XMLTOOL_API),xmltooling,XMLToolingException,Exceptions during threading/locking operations);
\r
34 * A class for manual thread creation and synchronization.
\r
36 class XMLTOOL_API Thread
\r
38 MAKE_NONCOPYABLE(Thread);
\r
41 virtual ~Thread() {}
\r
44 * Disassociate from the thread.
\r
46 * @return 0 for success, non-zero for failure
\r
48 virtual int detach()=0;
\r
51 * Join with the thread and wait for its completion.
\r
53 * @param thread_return holds the return value of the thread routine
\r
54 * @return 0 for success, non-zero for failure
\r
56 virtual int join(void** thread_return)=0;
\r
61 * @param signo the signal to send to the thread
\r
62 * @return 0 for success, non-zero for failure
\r
64 virtual int kill(int signo)=0;
\r
67 * Creates a new thread object to run the supplied start routine.
\r
69 * @param start_routine the function to execute on the thread
\r
70 * @param arg a parameter for the start routine
\r
71 * @return the created and running thread object
\r
73 static Thread* create(void* (*start_routine)(void*), void* arg);
\r
76 * Exits a thread gracefully.
\r
78 * @param return_val the return value for the thread
\r
80 static void exit(void* return_val);
\r
84 * Masks all signals from a thread.
\r
86 static void mask_all_signals(void);
\r
89 * Masks specific signals from a thread.
\r
92 * @param newmask the new signal mask
\r
93 * @param oldmask the old signal mask
\r
94 * @return 0 for success, non-zero for failure
\r
96 static int mask_signals(int how, const sigset_t *newmask, sigset_t *oldmask);
\r
101 * A class for managing Thread Local Storage values.
\r
103 class XMLTOOL_API ThreadKey
\r
105 MAKE_NONCOPYABLE(ThreadKey);
\r
108 virtual ~ThreadKey() {}
\r
111 * Sets the value for a TLS key.
\r
113 * @param data the value to set
\r
114 * @return 0 for success, non-zero for failure
\r
116 virtual int setData(void* data)=0;
\r
119 * Returns the value for a TLS key.
\r
121 * @return the value or NULL
\r
123 virtual void* getData() const=0;
\r
126 * Creates a new TLS key.
\r
128 * @param destroy_fn a functon to cleanup key values
\r
129 * @return the new key
\r
131 static ThreadKey* create(void (*destroy_fn)(void*));
\r
135 * A class for managing exclusive access to resources.
\r
137 class XMLTOOL_API Mutex
\r
139 MAKE_NONCOPYABLE(Mutex);
\r
142 virtual ~Mutex() {}
\r
145 * Locks the mutex for exclusive access.
\r
147 * @return 0 for success, non-zero for failure
\r
149 virtual int lock()=0;
\r
152 * Unlocks the mutex for exclusive access.
\r
154 * @return 0 for success, non-zero for failure
\r
156 virtual int unlock()=0;
\r
159 * Creates a new mutex object.
\r
161 * @return the new mutex
\r
163 static Mutex* create();
\r
167 * A class for managing shared and exclusive access to resources.
\r
169 class XMLTOOL_API RWLock
\r
171 MAKE_NONCOPYABLE(RWLock);
\r
174 virtual ~RWLock() {}
\r
177 * Obtains a shared lock.
\r
179 * @return 0 for success, non-zero for failure
\r
181 virtual int rdlock()=0;
\r
184 * Obtains an exclusive lock.
\r
186 * @return 0 for success, non-zero for failure
\r
188 virtual int wrlock()=0;
\r
191 * Unlocks the lock.
\r
193 * @return 0 for success, non-zero for failure
\r
195 virtual int unlock()=0;
\r
198 * Creates a new read/write lock.
\r
200 * @return the new lock
\r
202 static RWLock* create();
\r
206 * A class for establishing queues on a mutex based on a periodic condition.
\r
208 class XMLTOOL_API CondWait
\r
210 MAKE_NONCOPYABLE(CondWait);
\r
213 virtual ~CondWait() {}
\r
216 * Waits for a condition variable using the supplied mutex as a queue.
\r
218 * @param lock mutex to queue on
\r
219 * @return 0 for success, non-zero for failure
\r
221 virtual int wait(Mutex* lock)=0;
\r
224 * Waits for a condition variable using the supplied mutex as a queue,
\r
225 * but only for a certain time limit.
\r
227 * @param lock mutex to queue on
\r
228 * @param delay_seconds maximum time to wait before waking up
\r
229 * @return 0 for success, non-zero for failure
\r
231 virtual int timedwait(Mutex* lock, int delay_seconds)=0;
\r
234 * Signal a single thread to wake up if a condition changes.
\r
236 * @return 0 for success, non-zero for failure
\r
238 virtual int signal()=0;
\r
241 * Signal all threads to wake up if a condition changes.
\r
243 * @return 0 for success, non-zero for failure
\r
245 virtual int broadcast()=0;
\r
248 * Creates a new condition variable.
\r
250 * @return the new condition variable
\r
252 static CondWait* create();
\r
256 * RAII wrapper for a mutex lock.
\r
258 class XMLTOOL_API Lock {
\r
259 MAKE_NONCOPYABLE(Lock);
\r
262 * Locks and wraps the designated mutex.
\r
264 * @param mtx mutex to lock
\r
266 Lock(Mutex* mtx) : mutex(mtx) {
\r
271 * Unlocks the wrapped mutex.
\r
282 * RAII wrapper for a shared lock.
\r
284 class XMLTOOL_API ReadLock {
\r
285 MAKE_NONCOPYABLE(ReadLock);
\r
288 * Locks and wraps the designated shared lock.
\r
290 * @param lock lock to acquire
\r
292 ReadLock(RWLock* lock) : rwlock(lock) {
\r
297 * Unlocks the wrapped shared lock.
\r
309 #endif /* _xmltooling_threads_h */
\r