2 * Copyright 2001-2009 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 * Allows system to notify TLS implementation when a thread completes.
145 * <p>Windows doesn't support TLS destructors, so only the DllMain detach
146 * notification can be used to trigger per-thread cleanup.
148 static void onDetach();
153 * A class for managing exclusive access to resources.
155 class XMLTOOL_API Mutex
157 MAKE_NONCOPYABLE(Mutex);
163 * Locks the mutex for exclusive access.
165 * @return 0 for success, non-zero for failure
167 virtual int lock()=0;
170 * Unlocks the mutex for exclusive access.
172 * @return 0 for success, non-zero for failure
174 virtual int unlock()=0;
177 * Creates a new mutex object.
179 * @return the new mutex
181 static Mutex* create();
185 * A class for managing shared and exclusive access to resources.
187 class XMLTOOL_API RWLock
189 MAKE_NONCOPYABLE(RWLock);
195 * Obtains a shared lock.
197 * @return 0 for success, non-zero for failure
199 virtual int rdlock()=0;
202 * Obtains an exclusive lock.
204 * @return 0 for success, non-zero for failure
206 virtual int wrlock()=0;
211 * @return 0 for success, non-zero for failure
213 virtual int unlock()=0;
216 * Creates a new read/write lock.
218 * @return the new lock
220 static RWLock* create();
224 * A class for establishing queues on a mutex based on a periodic condition.
226 class XMLTOOL_API CondWait
228 MAKE_NONCOPYABLE(CondWait);
231 virtual ~CondWait() {}
234 * Waits for a condition variable using the supplied mutex as a queue.
236 * @param lock mutex to queue on
237 * @return 0 for success, non-zero for failure
239 virtual int wait(Mutex* lock)=0;
242 * Waits for a condition variable using the supplied mutex as a queue,
243 * but only for a certain time limit.
245 * @param lock mutex to queue on
246 * @param delay_seconds maximum time to wait before waking up
247 * @return 0 for success, non-zero for failure
249 virtual int timedwait(Mutex* lock, int delay_seconds)=0;
252 * Signal a single thread to wake up if a condition changes.
254 * @return 0 for success, non-zero for failure
256 virtual int signal()=0;
259 * Signal all threads to wake up if a condition changes.
261 * @return 0 for success, non-zero for failure
263 virtual int broadcast()=0;
266 * Creates a new condition variable.
268 * @return the new condition variable
270 static CondWait* create();
274 * RAII wrapper for a mutex lock.
276 class XMLTOOL_API Lock {
277 MAKE_NONCOPYABLE(Lock);
280 * Locks and wraps the designated mutex.
282 * @param mtx mutex to lock
284 Lock(Mutex* mtx) : mutex(mtx) {
289 * Unlocks the wrapped mutex.
300 * RAII wrapper for a shared lock.
302 class XMLTOOL_API SharedLock {
303 MAKE_NONCOPYABLE(SharedLock);
306 * Locks and wraps the designated shared lock.
308 * @param lock lock to acquire
309 * @param lockit true if the lock should be acquired here, false if already acquired
311 SharedLock(RWLock* lock, bool lockit=true) : rwlock(lock) {
317 * Unlocks the wrapped shared lock.
329 #endif /* _xmltooling_threads_h */