2 * Licensed to the University Corporation for Advanced Internet
3 * Development, Inc. (UCAID) under one or more contributor license
4 * agreements. See the NOTICE file distributed with this work for
5 * additional information regarding copyright ownership.
7 * UCAID licenses this file to you under the Apache License,
8 * Version 2.0 (the "License"); you may not use this file except
9 * in compliance with the License. You may obtain a copy of the
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
17 * either express or implied. See the License for the specific
18 * language governing permissions and limitations under the License.
22 * @file xmltooling/util/Threads.h
24 * Thread and locking wrappers.
27 #ifndef _xmltooling_threads_h
28 #define _xmltooling_threads_h
30 #include <xmltooling/exceptions.h>
37 DECL_XMLTOOLING_EXCEPTION(ThreadingException,XMLTOOL_EXCEPTIONAPI(XMLTOOL_API),xmltooling,XMLToolingException,Exceptions during threading/locking operations);
40 * A class for manual thread creation and synchronization.
42 class XMLTOOL_API Thread
44 MAKE_NONCOPYABLE(Thread);
51 * Disassociate from the thread.
53 * @return 0 for success, non-zero for failure
55 virtual int detach()=0;
58 * Join with the thread and wait for its completion.
60 * @param thread_return holds the return value of the thread routine
61 * @return 0 for success, non-zero for failure
63 virtual int join(void** thread_return)=0;
68 * @param signo the signal to send to the thread
69 * @return 0 for success, non-zero for failure
71 virtual int kill(int signo)=0;
74 * Creates a new thread object to run the supplied start routine.
76 * @param start_routine the function to execute on the thread
77 * @param arg a parameter for the start routine
78 * @param stacksize size of stack to use, or 0 for default
79 * @return the created and running thread object
81 static Thread* create(void* (*start_routine)(void*), void* arg, size_t stacksize=0);
84 * Exits a thread gracefully.
86 * @param return_val the return value for the thread
88 static void exit(void* return_val);
91 * Sleeps the current thread for the specified amount of time.
93 * @param seconds time to sleep
95 static void sleep(int seconds);
98 * Masks all signals from a thread.
100 static void mask_all_signals(void);
103 * Masks specific signals from a thread.
106 * @param newmask the new signal mask
107 * @param oldmask the old signal mask
108 * @return 0 for success, non-zero for failure
110 static int mask_signals(int how, const sigset_t *newmask, sigset_t *oldmask);
115 * A class for managing Thread Local Storage values.
117 class XMLTOOL_API ThreadKey
119 MAKE_NONCOPYABLE(ThreadKey);
123 virtual ~ThreadKey() {}
126 * Sets the value for a TLS key.
128 * @param data the value to set
129 * @return 0 for success, non-zero for failure
131 virtual int setData(void* data)=0;
134 * Returns the value for a TLS key.
136 * @return the value or nullptr
138 virtual void* getData() const=0;
141 * Creates a new TLS key.
143 * @param destroy_fn a functon to cleanup key values
144 * @return the new key
146 static ThreadKey* create(void (*destroy_fn)(void*));
150 * Allows system to notify TLS implementation when a thread completes.
152 * <p>Windows doesn't support TLS destructors, so only the DllMain detach
153 * notification can be used to trigger per-thread cleanup.
155 static void onDetach();
160 * A class for managing exclusive access to resources.
162 class XMLTOOL_API Mutex
164 MAKE_NONCOPYABLE(Mutex);
171 * Locks the mutex for exclusive access.
173 * @return 0 for success, non-zero for failure
175 virtual int lock()=0;
178 * Unlocks the mutex for exclusive access.
180 * @return 0 for success, non-zero for failure
182 virtual int unlock()=0;
185 * Creates a new mutex object.
187 * @return the new mutex
189 static Mutex* create();
193 * A class for managing shared and exclusive access to resources.
195 class XMLTOOL_API RWLock
197 MAKE_NONCOPYABLE(RWLock);
204 * Obtains a shared lock.
206 * @return 0 for success, non-zero for failure
208 virtual int rdlock()=0;
211 * Obtains an exclusive lock.
213 * @return 0 for success, non-zero for failure
215 virtual int wrlock()=0;
220 * @return 0 for success, non-zero for failure
222 virtual int unlock()=0;
225 * Creates a new read/write lock.
227 * @return the new lock
229 static RWLock* create();
233 * A class for establishing queues on a mutex based on a periodic condition.
235 class XMLTOOL_API CondWait
237 MAKE_NONCOPYABLE(CondWait);
241 virtual ~CondWait() {}
244 * Waits for a condition variable using the supplied mutex as a queue.
246 * @param lock mutex to queue on
247 * @return 0 for success, non-zero for failure
249 virtual int wait(Mutex* lock)=0;
252 * Waits for a condition variable using the supplied mutex as a queue,
253 * but only for a certain time limit.
255 * @param lock mutex to queue on
256 * @param delay_seconds maximum time to wait before waking up
257 * @return 0 for success, non-zero for failure
259 virtual int timedwait(Mutex* lock, int delay_seconds)=0;
262 * Signal a single thread to wake up if a condition changes.
264 * @return 0 for success, non-zero for failure
266 virtual int signal()=0;
269 * Signal all threads to wake up if a condition changes.
271 * @return 0 for success, non-zero for failure
273 virtual int broadcast()=0;
276 * Creates a new condition variable.
278 * @return the new condition variable
280 static CondWait* create();
284 * RAII wrapper for a mutex lock.
286 class XMLTOOL_API Lock {
287 MAKE_NONCOPYABLE(Lock);
290 * Locks and wraps the designated mutex.
292 * @param mtx mutex to lock
294 Lock(Mutex* mtx) : mutex(mtx) {
300 * Locks and wraps the designated mutex.
302 * @param mtx mutex to lock
304 Lock(Mutex& mtx) : mutex(&mtx) {
309 * Locks and wraps the designated mutex.
311 * @param mtx mutex to lock
313 Lock(std::auto_ptr<Mutex>& mtx) : mutex(mtx.get()) {
319 * Unlocks the wrapped mutex.
331 * RAII wrapper for a shared lock.
333 class XMLTOOL_API SharedLock {
334 MAKE_NONCOPYABLE(SharedLock);
337 * Locks and wraps the designated shared lock.
339 * @param lock lock to acquire
340 * @param lockit true if the lock should be acquired here, false if already acquired
342 SharedLock(RWLock* lock, bool lockit=true) : rwlock(lock) {
343 if (rwlock && lockit)
348 * Locks and wraps the designated shared lock.
350 * @param lock lock to acquire
351 * @param lockit true if the lock should be acquired here, false if already acquired
353 SharedLock(std::auto_ptr<RWLock>& lock, bool lockit=true) : rwlock(lock.get()) {
354 if (rwlock && lockit)
359 * Unlocks the wrapped shared lock.
372 #endif /* _xmltooling_threads_h */