1 // SGThread - Simple pthread class wrappers.
3 // Written by Bernie Bright, started April 2001.
5 // Copyright (C) 2001 Bernard Bright - bbright@bigpond.net.au
6 // Copyright (C) 2011 Mathias Froehlich
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU General Public License as
10 // published by the Free Software Foundation; either version 2 of the
11 // License, or (at your option) any later version.
13 // This program is distributed in the hope that it will be useful, but
14 // WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 #ifndef SGTHREAD_HXX_INCLUDED
24 #define SGTHREAD_HXX_INCLUDED 1
26 #include <simgear/compiler.h>
29 * Encapsulate generic threading methods.
30 * Users derive a class from SGThread and implement the run() member function.
35 * Create a new thread object.
36 * When a SGThread object is created it does not begin execution
37 * immediately. It is started by calling the start() member function.
42 * Start the underlying thread of execution.
43 * @return Pthread error code if execution fails, otherwise returns 0.
48 * Suspends the exection of the calling thread until this thread
55 * Destroy a thread object.
56 * This is protected so that its illegal to simply delete a thread
57 * - it must return from its run() function.
62 * All threads execute by deriving the run() method of SGThread.
63 * If this function terminates then the thread also terminates.
65 virtual void run() = 0;
69 SGThread(const SGThread&);
70 SGThread& operator=(const SGThread&);
73 PrivateData* _privateData;
75 friend struct PrivateData;
78 class SGWaitCondition;
81 * A mutex is used to protect a section of code such that at any time
82 * only a single thread can execute the code.
88 * Under Linux this is a 'fast' mutex.
93 * Destroy a mutex object.
94 * Note: it is the responsibility of the caller to ensure the mutex is
95 * unlocked before destruction occurs.
101 * If the mutex is currently unlocked, it becomes locked and owned by
102 * the calling thread. If the mutex is already locked by another thread,
103 * the calling thread is suspended until the mutex is unlocked. If the
104 * mutex is already locked and owned by the calling thread, the calling
105 * thread is suspended until the mutex is unlocked, effectively causing
106 * the calling thread to deadlock.
112 * It is assumed that the mutex is locked and owned by the calling thread.
118 PrivateData* _privateData;
120 friend class SGWaitCondition;
124 * A condition variable is a synchronization device that allows threads to
125 * suspend execution until some predicate on shared data is satisfied.
126 * A condition variable is always associated with a mutex to avoid race
129 class SGWaitCondition {
132 * Create a new condition variable.
137 * Destroy the condition object.
142 * Wait for this condition variable to be signaled.
144 * @param SGMutex& reference to a locked mutex.
149 * Wait for this condition variable to be signaled for at most
152 * @param mutex reference to a locked mutex.
153 * @param ms milliseconds to wait for a signal.
157 bool wait(SGMutex& mutex, unsigned msec);
160 * Wake one thread waiting on this condition variable.
161 * Nothing happens if no threads are waiting.
162 * If several threads are waiting exactly one thread is restarted. It
163 * is not specified which.
168 * Wake all threads waiting on this condition variable.
169 * Nothing happens if no threads are waiting.
175 SGWaitCondition(const SGWaitCondition&);
176 SGWaitCondition& operator=(const SGWaitCondition&);
179 PrivateData* _privateData;
182 #endif /* SGTHREAD_HXX_INCLUDED */