Jamoma API  0.6.0.a19
CAGuard.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 #if !defined(__CAGuard_h__)
42 #define __CAGuard_h__
43 
44 //==================================================================================================
45 // Includes
46 //=============================================================================
47 
48 // Super Class Includes
49 #include "CAMutex.h"
50 
51 #if CoreAudio_Debug
52 // #define Log_Average_Latency 1
53 #endif
54 
55 //==================================================================================================
56 // CAGuard
57 //
58 // This is your typical mutex with signalling implemented via pthreads.
59 // Lock() will return true if and only if the guard is locked on that call.
60 // A thread that already has the guard will receive 'false' if it locks it
61 // again. Use of the stack-based CAGuard::Locker class is highly recommended
62 // to properly manage the recursive nesting. The Wait calls with timeouts
63 // will return true if and only if the timeout period expired. They will
64 // return false if they receive notification any other way.
65 //==================================================================================================
66 
67 class CAGuard : public CAMutex
68 {
69 
70 // Construction/Destruction
71 public:
72  CAGuard(const char* inName);
73  virtual ~CAGuard();
74 
75 // Actions
76 public:
77  virtual void Wait();
78  virtual bool WaitFor(UInt64 inNanos);
79  virtual bool WaitUntil(UInt64 inNanos);
80 
81  virtual void Notify();
82  virtual void NotifyAll();
83 
84 // Implementation
85 protected:
86 #if TARGET_OS_MAC
87  pthread_cond_t mCondVar;
88 #else
89  HANDLE mEvent;
90 #endif
91 #if Log_Average_Latency
92  Float64 mAverageLatencyAccumulator;
93  UInt32 mAverageLatencyCount;
94 #endif
95 
96 // Helper class to manage taking and releasing recursively
97 public:
98  class Locker
99  {
100 
101  // Construction/Destruction
102  public:
103  Locker(CAGuard& inGuard) : mGuard(inGuard), mNeedsRelease(false) { mNeedsRelease = mGuard.Lock(); }
104  ~Locker() { if(mNeedsRelease) { mGuard.Unlock(); } }
105 
106  private:
107  Locker(const Locker&);
108  Locker& operator=(const Locker&);
109 
110  // Actions
111  public:
112  void Wait() { mGuard.Wait(); }
113  bool WaitFor(UInt64 inNanos) { return mGuard.WaitFor(inNanos); }
114  bool WaitUntil(UInt64 inNanos) { return mGuard.WaitUntil(inNanos); }
115 
116  void Notify() { mGuard.Notify(); }
117  void NotifyAll() { mGuard.NotifyAll(); }
118 
119  // Implementation
120  private:
121  CAGuard& mGuard;
122  bool mNeedsRelease;
123  };
124 
125 };
126 
127 #endif