2 * Copyright (c) 2005, Eric Crahen
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is furnished
9 * to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in all
12 * copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
18 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 #ifndef __ZTFASTLOCK_H__
24 #define __ZTFASTLOCK_H__
26 #include "zthread/NonCopyable.h"
27 #include "zthread/Exceptions.h"
30 #include <CoreServices/CoreServices.h>
31 //#include <Multiprocessing.h>
38 * @author Eric Crahen <http://www.code-foo.com>
39 * @date <2003-07-16T23:25:31-0400>
43 class FastLock
: private NonCopyable
{
45 MPCriticalRegionID _mtx
;
50 * Create a new FastLock. No safety or state checks are performed.
52 * @exception Initialization_Exception - not thrown
57 static bool init
= MPLibraryIsLoaded();
59 if(!init
|| MPCreateCriticalRegion(&_mtx
) != noErr
) {
61 throw Initialization_Exception();
67 * Destroy a FastLock. No safety or state checks are performed.
69 inline ~FastLock() throw () {
71 OSStatus status
= MPDeleteCriticalRegion(_mtx
);
78 * Acquire an exclusive lock. No safety or state checks are performed.
80 * @exception Synchronization_Exception - not thrown
82 inline void acquire() {
84 if(MPEnterCriticalRegion(_mtx
, kDurationForever
) != noErr
)
85 throw Synchronization_Exception();
90 * Try to acquire an exclusive lock. No safety or state checks are performed.
91 * This function returns immediately regardless of the value of the timeout
93 * @param timeout Unused
95 * @exception Synchronization_Exception - not thrown
97 inline bool tryAcquire(unsigned long timeout
=0) {
100 MPEnterCriticalRegion(_mtx
, kDurationMillisecond
* timeout
);
112 throw Synchronization_Exception();
117 * Release an exclusive lock. No safety or state checks are performed.
118 * The caller should have already acquired the lock, and release it
121 * @exception Synchronization_Exception - not thrown
123 inline void release() {
125 if(MPExitCriticalRegion(_mtx
) != noErr
)
126 throw Synchronization_Exception();