]>
Dogcows Code - chaz/yoink/blob - src/Moof/Thread.hh
2 /*******************************************************************************
4 Copyright (c) 2009, Charles McGarvey
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
10 * Redistributions of source code must retain the above copyright notice,
11 this list of conditions and the following disclaimer.
12 * Redistributions in binary form must reproduce the above copyright notice,
13 this list of conditions and the following disclaimer in the documentation
14 and/or other materials provided with the distribution.
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
20 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *******************************************************************************/
29 #ifndef _MOOF_THREAD_HH_
30 #define _MOOF_THREAD_HH_
34 * Light C++ wrapper around SDL threads.
37 #include <boost/function.hpp>
45 // The detach function detaches a separate thread by calling 'func' with
46 // the 'arg' parameter.
49 typedef SDL_Thread
* Thread
;
51 typedef boost::function
<int(void)> Function
;
54 int detach_(void* arg
)
56 Function function
= *(Function
*)arg
;
59 delete (Function
*)arg
;
63 Thread
detachFunction(const Function
& function
)
66 Function
* fcopy
= new Function(function
);
68 thread
= SDL_CreateThread(detach_
, (void*)fcopy
);
69 if (thread
== 0) delete fcopy
;
74 int waitOnThread(Thread thread
)
77 SDL_WaitThread(thread
, &i
);
81 void killThread(Thread thread
)
83 SDL_KillThread(thread
);
88 // The identifier function returns a unique integer for the calling thread.
91 unsigned int getThreadIdentifier()
93 return SDL_ThreadID();
96 unsigned int getThreadIdentifier(Thread thread
)
98 return SDL_GetThreadID(thread
);
102 // =============================================================================
106 friend class Condition
;
111 mutex_
= SDL_CreateMutex();
115 SDL_DestroyMutex(mutex_
);
120 return (SDL_LockMutex(mutex_
) == 0);
124 return (SDL_UnlockMutex(mutex_
) == 0);
129 friend class Condition
;
139 if (isLocked_
) release();
144 return (isLocked_
= mutex_
->acquireLock());
148 return mutex_
->releaseLock();
152 bool isLocked() const
162 class ScopedLock
: public Lock
165 ScopedLock(Mutex
& mutex
) :
182 condition_
= SDL_CreateCond();
186 SDL_DestroyCond(condition_
);
189 bool wait(Mutex::Lock
& lock
)
191 return (SDL_CondWait(condition_
, lock
.mutex_
->mutex_
) == 0);
193 bool wait(Mutex::Lock
& lock
, unsigned ms
)
195 // TODO for consistency, this function should take seconds
196 return (SDL_CondWaitTimeout(condition_
, lock
.mutex_
->mutex_
, ms
) == 0);
201 return (SDL_CondSignal(condition_
) == 0);
205 return (SDL_CondBroadcast(condition_
) == 0);
209 SDL_cond
* condition_
;
216 Semaphore(unsigned int value
)
218 semaphore_
= SDL_CreateSemaphore(value
);
222 SDL_DestroySemaphore(semaphore_
);
227 return (SDL_SemWait(semaphore_
) == 0);
231 return (SDL_SemPost(semaphore_
) == 0);
236 return (SDL_SemTryWait(semaphore_
) == 0);
238 bool tryLock(unsigned ms
)
240 // TODO for consistency, this function should take seconds
241 return (SDL_SemWaitTimeout(semaphore_
, ms
) == 0);
247 Lock(Semaphore
& semaphore
)
249 semaphore_
= &semaphore
;
254 if (isLocked_
) release();
259 return (isLocked_
= semaphore_
->acquireLock());
263 return semaphore_
->releaseLock(); isLocked_
= false;
266 bool isLocked() const
272 Semaphore
* semaphore_
;
276 class ScopedLock
: public Lock
279 ScopedLock(Semaphore
& semaphore
) :
294 #endif // _MOOF_THREAD_HH_
296 /** vim: set ts=4 sw=4 tw=80: *************************************************/
This page took 0.051696 seconds and 4 git commands to generate.