this repo has no description
1/* Copyright: � Copyright 2005 Apple Computer, Inc. All rights reserved.
2
3 Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Computer, Inc.
4 ("Apple") in consideration of your agreement to the following terms, and your
5 use, installation, modification or redistribution of this Apple software
6 constitutes acceptance of these terms. If you do not agree with these terms,
7 please do not use, install, modify or redistribute this Apple software.
8
9 In consideration of your agreement to abide by the following terms, and subject
10 to these terms, Apple grants you a personal, non-exclusive license, under Apple�s
11 copyrights in this original Apple software (the "Apple Software"), to use,
12 reproduce, modify and redistribute the Apple Software, with or without
13 modifications, in source and/or binary forms; provided that if you redistribute
14 the Apple Software in its entirety and without modifications, you must retain
15 this notice and the following text and disclaimers in all such redistributions of
16 the Apple Software. Neither the name, trademarks, service marks or logos of
17 Apple Computer, Inc. may be used to endorse or promote products derived from the
18 Apple Software without specific prior written permission from Apple. Except as
19 expressly stated in this notice, no other rights or licenses, express or implied,
20 are granted by Apple herein, including but not limited to any patent rights that
21 may be infringed by your derivative works or by other works in which the Apple
22 Software may be incorporated.
23
24 The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO
25 WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
26 WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
27 PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
28 COMBINATION WITH YOUR PRODUCTS.
29
30 IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
31 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
32 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
34 OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
35 (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
36 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37*/
38/*==================================================================================================
39 CAGuard.h
40
41==================================================================================================*/
42#if !defined(__CAGuard_h__)
43#define __CAGuard_h__
44
45//==================================================================================================
46// Includes
47//=============================================================================
48
49// Super Class Includes
50#include "CAMutex.h"
51
52#if CoreAudio_Debug
53// #define Log_Average_Latency 1
54#endif
55
56//==================================================================================================
57// CAGuard
58//
59// This is your typical mutex with signalling implemented via pthreads.
60// Lock() will return true if and only if the guard is locked on that call.
61// A thread that already has the guard will receive 'false' if it locks it
62// again. Use of the stack-based CAGuard::Locker class is highly recommended
63// to properly manage the recursive nesting. The Wait calls with timeouts
64// will return true if and only if the timeout period expired. They will
65// return false if they receive notification any other way.
66//==================================================================================================
67
68class CAGuard : public CAMutex
69{
70
71// Construction/Destruction
72public:
73 CAGuard(const char* inName);
74 virtual ~CAGuard();
75
76// Actions
77public:
78 virtual void Wait();
79 virtual bool WaitFor(UInt64 inNanos);
80 virtual bool WaitUntil(UInt64 inNanos);
81
82 virtual void Notify();
83 virtual void NotifyAll();
84
85// Implementation
86protected:
87#if TARGET_OS_MAC
88 pthread_cond_t mCondVar;
89#else
90 HANDLE mEvent;
91#endif
92#if Log_Average_Latency
93 Float64 mAverageLatencyAccumulator;
94 UInt32 mAverageLatencyCount;
95#endif
96
97// Helper class to manage taking and releasing recursively
98public:
99 class Locker
100 {
101
102 // Construction/Destruction
103 public:
104 Locker(CAGuard& inGuard) : mGuard(inGuard), mNeedsRelease(false) { mNeedsRelease = mGuard.Lock(); }
105 ~Locker() { if(mNeedsRelease) { mGuard.Unlock(); } }
106
107 private:
108 Locker(const Locker&);
109 Locker& operator=(const Locker&);
110
111 // Actions
112 public:
113 void Wait() { mGuard.Wait(); }
114 bool WaitFor(UInt64 inNanos) { return mGuard.WaitFor(inNanos); }
115 bool WaitUntil(UInt64 inNanos) { return mGuard.WaitUntil(inNanos); }
116
117 void Notify() { mGuard.Notify(); }
118 void NotifyAll() { mGuard.NotifyAll(); }
119
120 // Implementation
121 private:
122 CAGuard& mGuard;
123 bool mNeedsRelease;
124 };
125
126};
127
128#endif