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/exceptions.h>
32 DECL_XMLTOOLING_EXCEPTION(ThreadingException,XMLTOOL_EXCEPTIONAPI(XMLTOOL_API),xmltooling,XMLToolingException,Exceptions during threading/locking operations);
35 * A class for manual thread creation and synchronization.
37 class XMLTOOL_API Thread
39 MAKE_NONCOPYABLE(Thread);
45 * Disassociate from the thread.
47 * @return 0 for success, non-zero for failure
49 virtual int detach()=0;
52 * Join with the thread and wait for its completion.
54 * @param thread_return holds the return value of the thread routine
55 * @return 0 for success, non-zero for failure
57 virtual int join(void** thread_return)=0;
62 * @param signo the signal to send to the thread
63 * @return 0 for success, non-zero for failure
65 virtual int kill(int signo)=0;
68 * Creates a new thread object to run the supplied start routine.
70 * @param start_routine the function to execute on the thread
71 * @param arg a parameter for the start routine
72 * @return the created and running thread object
74 static Thread* create(void* (*start_routine)(void*), void* arg);
77 * Exits a thread gracefully.
79 * @param return_val the return value for the thread
81 static void exit(void* return_val);
84 * Sleeps the current thread for the specified amount of time.
86 * @param seconds time to sleep
88 static void sleep(int seconds);
91 * Masks all signals from a thread.
93 static void mask_all_signals(void);
96 * Masks specific signals from a thread.
99 * @param newmask the new signal mask
100 * @param oldmask the old signal mask
101 * @return 0 for success, non-zero for failure
103 static int mask_signals(int how, const sigset_t *newmask, sigset_t *oldmask);
108 * A class for managing Thread Local Storage values.
110 class XMLTOOL_API ThreadKey
112 MAKE_NONCOPYABLE(ThreadKey);
115 virtual ~ThreadKey() {}
118 * Sets the value for a TLS key.
120 * @param data the value to set
121 * @return 0 for success, non-zero for failure
123 virtual int setData(void* data)=0;
126 * Returns the value for a TLS key.
128 * @return the value or NULL
130 virtual void* getData() const=0;
133 * Creates a new TLS key.
135 * @param destroy_fn a functon to cleanup key values
136 * @return the new key
138 static ThreadKey* create(void (*destroy_fn)(void*));
142 * Allows system to notify TLS implementation when a thread completes.
144 * <p>Windows doesn't support TLS destructors, so only the DllMain detach
145 * notification can be used to trigger per-thread cleanup.
147 static void onDetach();
152 * A class for managing exclusive access to resources.
154 class XMLTOOL_API Mutex
156 MAKE_NONCOPYABLE(Mutex);
162 * Locks the mutex for exclusive access.
164 * @return 0 for success, non-zero for failure
166 virtual int lock()=0;
169 * Unlocks the mutex for exclusive access.
171 * @return 0 for success, non-zero for failure
173 virtual int unlock()=0;
176 * Creates a new mutex object.
178 * @return the new mutex
180 static Mutex* create();
184 * A class for managing shared and exclusive access to resources.
186 class XMLTOOL_API RWLock
188 MAKE_NONCOPYABLE(RWLock);
194 * Obtains a shared lock.
196 * @return 0 for success, non-zero for failure
198 virtual int rdlock()=0;
201 * Obtains an exclusive lock.
203 * @return 0 for success, non-zero for failure
205 virtual int wrlock()=0;
210 * @return 0 for success, non-zero for failure
212 virtual int unlock()=0;
215 * Creates a new read/write lock.
217 * @return the new lock
219 static RWLock* create();
223 * A class for establishing queues on a mutex based on a periodic condition.
225 class XMLTOOL_API CondWait
227 MAKE_NONCOPYABLE(CondWait);
230 virtual ~CondWait() {}
233 * Waits for a condition variable using the supplied mutex as a queue.
235 * @param lock mutex to queue on
236 * @return 0 for success, non-zero for failure
238 virtual int wait(Mutex* lock)=0;
241 * Waits for a condition variable using the supplied mutex as a queue,
242 * but only for a certain time limit.
244 * @param lock mutex to queue on
245 * @param delay_seconds maximum time to wait before waking up
246 * @return 0 for success, non-zero for failure
248 virtual int timedwait(Mutex* lock, int delay_seconds)=0;
251 * Signal a single thread to wake up if a condition changes.
253 * @return 0 for success, non-zero for failure
255 virtual int signal()=0;
258 * Signal all threads to wake up if a condition changes.
260 * @return 0 for success, non-zero for failure
262 virtual int broadcast()=0;
265 * Creates a new condition variable.
267 * @return the new condition variable
269 static CondWait* create();
273 * RAII wrapper for a mutex lock.
275 class XMLTOOL_API Lock {
276 MAKE_NONCOPYABLE(Lock);
279 * Locks and wraps the designated mutex.
281 * @param mtx mutex to lock
283 Lock(Mutex* mtx) : mutex(mtx) {
288 * Unlocks the wrapped mutex.
299 * RAII wrapper for a shared lock.
301 class XMLTOOL_API SharedLock {
302 MAKE_NONCOPYABLE(SharedLock);
305 * Locks and wraps the designated shared lock.
307 * @param lock lock to acquire
308 * @param lockit true if the lock should be acquired here, false if already acquired
310 SharedLock(RWLock* lock, bool lockit=true) : rwlock(lock) {
316 * Unlocks the wrapped shared lock.
328 #endif /* _xmltooling_threads_h */