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
54 *Retreive the current thread id.
56 static long current( void );
60 * Destroy a thread object.
61 * This is protected so that its illegal to simply delete a thread
62 * - it must return from its run() function.
67 * All threads execute by deriving the run() method of SGThread.
68 * If this function terminates then the thread also terminates.
70 virtual void run() = 0;
74 SGThread(const SGThread&);
75 SGThread& operator=(const SGThread&);
78 PrivateData* _privateData;
80 friend struct PrivateData;
83 class SGWaitCondition;
86 * A mutex is used to protect a section of code such that at any time
87 * only a single thread can execute the code.
93 * Under Linux this is a 'fast' mutex.
98 * Destroy a mutex object.
99 * Note: it is the responsibility of the caller to ensure the mutex is
100 * unlocked before destruction occurs.
106 * If the mutex is currently unlocked, it becomes locked and owned by
107 * the calling thread. If the mutex is already locked by another thread,
108 * the calling thread is suspended until the mutex is unlocked. If the
109 * mutex is already locked and owned by the calling thread, the calling
110 * thread is suspended until the mutex is unlocked, effectively causing
111 * the calling thread to deadlock.
117 * It is assumed that the mutex is locked and owned by the calling thread.
123 PrivateData* _privateData;
125 friend class SGWaitCondition;
129 * A condition variable is a synchronization device that allows threads to
130 * suspend execution until some predicate on shared data is satisfied.
131 * A condition variable is always associated with a mutex to avoid race
134 class SGWaitCondition {
137 * Create a new condition variable.
142 * Destroy the condition object.
147 * Wait for this condition variable to be signaled.
149 * @param SGMutex& reference to a locked mutex.
154 * Wait for this condition variable to be signaled for at most
157 * @param mutex reference to a locked mutex.
158 * @param ms milliseconds to wait for a signal.
162 bool wait(SGMutex& mutex, unsigned msec);
165 * Wake one thread waiting on this condition variable.
166 * Nothing happens if no threads are waiting.
167 * If several threads are waiting exactly one thread is restarted. It
168 * is not specified which.
173 * Wake all threads waiting on this condition variable.
174 * Nothing happens if no threads are waiting.
180 SGWaitCondition(const SGWaitCondition&);
181 SGWaitCondition& operator=(const SGWaitCondition&);
184 PrivateData* _privateData;
187 #endif /* SGTHREAD_HXX_INCLUDED */