Initial Patch of Auction House bot rev. 135
[auctionmangos.git] / dep / src / zthread / macos / FastLock.h
blobbae5c482903bff726cd7da191c5c846a415f169b
1 /*
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"
29 #include <assert.h>
30 #include <CoreServices/CoreServices.h>
31 //#include <Multiprocessing.h>
33 namespace ZThread {
35 /**
36 * @class FastLock
38 * @author Eric Crahen <http://www.code-foo.com>
39 * @date <2003-07-16T23:25:31-0400>
40 * @version 2.1.6
42 */
43 class FastLock : private NonCopyable {
45 MPCriticalRegionID _mtx;
47 public:
49 /**
50 * Create a new FastLock. No safety or state checks are performed.
52 * @exception Initialization_Exception - not thrown
54 inline FastLock() {
56 // Apple TN1071
57 static bool init = MPLibraryIsLoaded();
59 if(!init || MPCreateCriticalRegion(&_mtx) != noErr) {
60 assert(0);
61 throw Initialization_Exception();
66 /**
67 * Destroy a FastLock. No safety or state checks are performed.
69 inline ~FastLock() throw () {
71 OSStatus status = MPDeleteCriticalRegion(_mtx);
72 if(status != noErr)
73 assert(false);
77 /**
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();
89 /**
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
94 * @return bool
95 * @exception Synchronization_Exception - not thrown
97 inline bool tryAcquire(unsigned long timeout=0) {
99 OSStatus status =
100 MPEnterCriticalRegion(_mtx, kDurationMillisecond * timeout);
102 switch(status) {
103 case kMPTimeoutErr:
104 return false;
106 case noErr:
107 return true;
111 assert(0);
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
119 * only once.
121 * @exception Synchronization_Exception - not thrown
123 inline void release() {
125 if(MPExitCriticalRegion(_mtx) != noErr)
126 throw Synchronization_Exception();
131 }; /* FastLock */
136 #endif