]>
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 the SDL threads API.
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 inline int detach_(void* arg
)
56 //Function function = *(Function*)arg;
57 int code
= (*(Function
*)arg
)();
59 delete (Function
*)arg
;
63 inline Thread
detachFunction(const Function
& function
)
65 Function
* fcopy
= new Function(function
);
66 Thread thread
= SDL_CreateThread(detach_
, (void*)fcopy
);
68 if (thread
== 0) delete fcopy
;
73 inline int waitOnThread(Thread thread
)
76 SDL_WaitThread(thread
, &i
);
80 inline void killThread(Thread thread
)
82 SDL_KillThread(thread
);
87 // The identifier function returns a unique integer for the calling thread.
90 inline unsigned getThreadIdentifier()
92 return SDL_ThreadID();
95 inline unsigned getThreadIdentifier(Thread thread
)
97 return SDL_GetThreadID(thread
);
101 // =============================================================================
105 friend class Condition
;
111 mMutex
= SDL_CreateMutex();
115 SDL_DestroyMutex(mMutex
);
120 return (SDL_LockMutex(mMutex
) == 0);
124 return (SDL_UnlockMutex(mMutex
) == 0);
129 friend class Condition
;
140 if (mIsLocked
) release();
145 return (mIsLocked
= mMutex
->acquireLock());
149 return mMutex
->releaseLock();
153 bool isLocked() const
164 class ScopedLock
: public Lock
168 ScopedLock(Mutex
& mutex
) :
187 condition_
= SDL_CreateCond();
191 SDL_DestroyCond(condition_
);
194 bool wait(Mutex::Lock
& lock
)
196 return (SDL_CondWait(condition_
, lock
.mMutex
->mMutex
) == 0);
198 bool wait(Mutex::Lock
& lock
, unsigned ms
)
200 // TODO for consistency, this function should take seconds
201 return (SDL_CondWaitTimeout(condition_
, lock
.mMutex
->mMutex
, ms
) == 0);
206 return (SDL_CondSignal(condition_
) == 0);
210 return (SDL_CondBroadcast(condition_
) == 0);
215 SDL_cond
* condition_
;
223 Semaphore(unsigned int value
)
225 semaphore_
= SDL_CreateSemaphore(value
);
229 SDL_DestroySemaphore(semaphore_
);
234 return (SDL_SemWait(semaphore_
) == 0);
238 return (SDL_SemPost(semaphore_
) == 0);
243 return (SDL_SemTryWait(semaphore_
) == 0);
245 bool tryLock(unsigned ms
)
247 // TODO for consistency, this function should take seconds
248 return (SDL_SemWaitTimeout(semaphore_
, ms
) == 0);
255 Lock(Semaphore
& semaphore
)
257 semaphore_
= &semaphore
;
262 if (mIsLocked
) release();
267 return (mIsLocked
= semaphore_
->acquireLock());
271 return semaphore_
->releaseLock(); mIsLocked
= false;
274 bool isLocked() const
281 Semaphore
* semaphore_
;
285 class ScopedLock
: public Lock
289 ScopedLock(Semaphore
& semaphore
) :
305 #endif // _MOOF_THREAD_HH_
307 /** vim: set ts=4 sw=4 tw=80: *************************************************/
This page took 0.046045 seconds and 4 git commands to generate.