root/trunk/dep/src/zthread/macos/FastLock.h @ 2

Revision 2, 3.4 kB (checked in by yumileroy, 17 years ago)

[svn] * Proper SVN structure

Original author: Neo2003
Date: 2008-10-02 16:23:55-05:00

Line 
1/*
2 * Copyright (c) 2005, Eric Crahen
3 *
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:
10 *
11 * The above copyright notice and this permission notice shall be included in all
12 * copies or substantial portions of the Software.
13 *
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.
20 *
21 */
22
23#ifndef __ZTFASTLOCK_H__
24#define __ZTFASTLOCK_H__
25
26#include "zthread/NonCopyable.h"
27#include "zthread/Exceptions.h"
28
29#include <assert.h>
30#include <CoreServices/CoreServices.h>
31//#include <Multiprocessing.h>
32
33namespace ZThread {
34
35/**
36 * @class FastLock
37 *
38 * @author Eric Crahen <http://www.code-foo.com>
39 * @date <2003-07-16T23:25:31-0400>
40 * @version 2.1.6
41 *
42 */ 
43class FastLock : private NonCopyable {
44   
45  MPCriticalRegionID _mtx;
46
47 public:
48 
49  /**
50   * Create a new FastLock. No safety or state checks are performed.
51   *
52   * @exception Initialization_Exception - not thrown
53   */
54  inline FastLock() {
55   
56    // Apple TN1071
57    static bool init = MPLibraryIsLoaded();
58   
59    if(!init || MPCreateCriticalRegion(&_mtx) != noErr) {
60      assert(0);
61      throw Initialization_Exception();
62    }
63
64  }
65 
66  /**
67   * Destroy a FastLock. No safety or state checks are performed.
68   */
69  inline ~FastLock() throw () {
70
71    OSStatus status = MPDeleteCriticalRegion(_mtx);
72    if(status != noErr) 
73      assert(false);
74
75  }
76 
77  /**
78   * Acquire an exclusive lock. No safety or state checks are performed.
79   *
80   * @exception Synchronization_Exception - not thrown
81   */
82  inline void acquire() {
83   
84    if(MPEnterCriticalRegion(_mtx, kDurationForever) != noErr)
85      throw Synchronization_Exception();
86
87  }
88
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
92   *
93   * @param timeout Unused
94   * @return bool
95   * @exception Synchronization_Exception - not thrown
96   */
97  inline bool tryAcquire(unsigned long timeout=0) {
98
99    OSStatus status = 
100      MPEnterCriticalRegion(_mtx, kDurationMillisecond * timeout);
101
102    switch(status) {
103      case kMPTimeoutErr:
104        return false;
105       
106      case noErr:
107        return true;
108       
109    } 
110   
111    assert(0);
112    throw Synchronization_Exception();
113
114  }
115 
116  /**
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.
120   *
121   * @exception Synchronization_Exception - not thrown
122   */
123  inline void release() {
124   
125    if(MPExitCriticalRegion(_mtx) != noErr)
126      throw Synchronization_Exception();
127
128  }
129 
130 
131}; /* FastLock */
132
133
134};
135
136#endif
137
138
139
Note: See TracBrowser for help on using the browser.