Jamoma API  0.6.0.a19
CAMutex.h
1 /* Copyright 2007 Apple Inc. All Rights Reserved.
2 
3  Disclaimer: IMPORTANT: This Apple software is supplied to you by
4  Apple Inc. ("Apple") in consideration of your agreement to the
5  following terms, and your use, installation, modification or
6  redistribution of this Apple software constitutes acceptance of these
7  terms. If you do not agree with these terms, please do not use,
8  install, modify or redistribute this Apple software.
9 
10  In consideration of your agreement to abide by the following terms, and
11  subject to these terms, Apple grants you a personal, non-exclusive
12  license, under Apple's copyrights in this original Apple software (the
13  "Apple Software"), to use, reproduce, modify and redistribute the Apple
14  Software, with or without modifications, in source and/or binary forms;
15  provided that if you redistribute the Apple Software in its entirety and
16  without modifications, you must retain this notice and the following
17  text and disclaimers in all such redistributions of the Apple Software.
18  Neither the name, trademarks, service marks or logos of Apple Inc.
19  may be used to endorse or promote products derived from the Apple
20  Software without specific prior written permission from Apple. Except
21  as expressly stated in this notice, no other rights or licenses, express
22  or implied, are granted by Apple herein, including but not limited to
23  any patent rights that may be infringed by your derivative works or by
24  other works in which the Apple Software may be incorporated.
25 
26  The Apple Software is provided by Apple on an "AS IS" basis. APPLE
27  MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
28  THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
29  FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
30  OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
31 
32  IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
33  OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35  INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
36  MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
37  AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
38  STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
39  POSSIBILITY OF SUCH DAMAGE.
40 */
41 #ifndef __CAMutex_h__
42 #define __CAMutex_h__
43 
44 //==================================================================================================
45 // Includes
46 //==================================================================================================
47 
48 // System Includes
49 #if !defined(__COREAUDIO_USE_FLAT_INCLUDES__)
50  #include <CoreAudio/CoreAudioTypes.h>
51 #else
52  #include <CoreAudioTypes.h>
53 #endif
54 
55 #if TARGET_OS_MAC
56  #include <pthread.h>
57 #elif TARGET_OS_WIN32
58  #include <windows.h>
59 #else
60  #error Unsupported operating system
61 #endif
62 
63 //==================================================================================================
64 // A recursive mutex.
65 //==================================================================================================
66 
67 class CAMutex
68 {
69 // Construction/Destruction
70 public:
71  CAMutex(const char* inName);
72  virtual ~CAMutex();
73 
74 // Actions
75 public:
76  virtual bool Lock();
77  virtual void Unlock();
78  virtual bool Try(bool& outWasLocked); // returns true if lock is free, false if not
79 
80  virtual bool IsFree() const;
81  virtual bool IsOwnedByCurrentThread() const;
82 
83 // Implementation
84 protected:
85  const char* mName;
86 #if TARGET_OS_MAC
87  pthread_t mOwner;
88  pthread_mutex_t mMutex;
89 #elif TARGET_OS_WIN32
90  UInt32 mOwner;
91  HANDLE mMutex;
92 #endif
93 
94 // Helper class to manage taking and releasing recursively
95 public:
96  class Locker
97  {
98 
99  // Construction/Destruction
100  public:
101  Locker(CAMutex& inMutex) : mMutex(inMutex), mNeedsRelease(false) { mNeedsRelease = mMutex.Lock(); }
102  ~Locker() { if(mNeedsRelease) { mMutex.Unlock(); } }
103 
104  private:
105  Locker(const Locker&);
106  Locker& operator=(const Locker&);
107 
108  // Implementation
109  private:
110  CAMutex& mMutex;
111  bool mNeedsRelease;
112 
113  };
114 
115  // you can use this with Try - if you take the lock in try, pass in the outWasLocked var
116  class Tryer {
117 
118  // Construction/Destruction
119  public:
120  Tryer (CAMutex &mutex) : mMutex(mutex), mNeedsRelease(false), mHasLock(false) { mHasLock = mMutex.Try (mNeedsRelease); }
121  ~Tryer () { if (mNeedsRelease) mMutex.Unlock(); }
122 
123  bool HasLock () const { return mHasLock; }
124 
125  private:
126  Tryer(const Tryer&);
127  Tryer& operator=(const Tryer&);
128 
129  // Implementation
130  private:
131  CAMutex & mMutex;
132  bool mNeedsRelease;
133  bool mHasLock;
134  };
135 };
136 
137 
138 #endif // __CAMutex_h__