| 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 | CAMutex.h |
|---|
| 40 | |
|---|
| 41 | ==================================================================================================*/ |
|---|
| 42 | |
|---|
| 43 | #ifndef __CAMutex_h__ |
|---|
| 44 | #define __CAMutex_h__ |
|---|
| 45 | |
|---|
| 46 | //================================================================================================== |
|---|
| 47 | // Includes |
|---|
| 48 | //================================================================================================== |
|---|
| 49 | |
|---|
| 50 | // System Includes |
|---|
| 51 | #if !defined(__COREAUDIO_USE_FLAT_INCLUDES__) |
|---|
| 52 | #include <CoreAudio/CoreAudioTypes.h> |
|---|
| 53 | #else |
|---|
| 54 | #include <CoreAudioTypes.h> |
|---|
| 55 | #endif |
|---|
| 56 | |
|---|
| 57 | #if TARGET_OS_MAC |
|---|
| 58 | #include <pthread.h> |
|---|
| 59 | #elif TARGET_OS_WIN32 |
|---|
| 60 | #include <windows.h> |
|---|
| 61 | #else |
|---|
| 62 | #error Unsupported operating system |
|---|
| 63 | #endif |
|---|
| 64 | |
|---|
| 65 | //================================================================================================== |
|---|
| 66 | // A recursive mutex. |
|---|
| 67 | //================================================================================================== |
|---|
| 68 | |
|---|
| 69 | class CAMutex |
|---|
| 70 | { |
|---|
| 71 | // Construction/Destruction |
|---|
| 72 | public: |
|---|
| 73 | CAMutex(const char* inName); |
|---|
| 74 | virtual ~CAMutex(); |
|---|
| 75 | |
|---|
| 76 | // Actions |
|---|
| 77 | public: |
|---|
| 78 | virtual bool Lock(); |
|---|
| 79 | virtual void Unlock(); |
|---|
| 80 | virtual bool Try(bool& outWasLocked); // returns true if lock is free, false if not |
|---|
| 81 | |
|---|
| 82 | // Implementation |
|---|
| 83 | protected: |
|---|
| 84 | const char* mName; |
|---|
| 85 | #if TARGET_OS_MAC |
|---|
| 86 | pthread_t mOwner; |
|---|
| 87 | pthread_mutex_t mMutex; |
|---|
| 88 | #elif TARGET_OS_WIN32 |
|---|
| 89 | UInt32 mOwner; |
|---|
| 90 | HANDLE mMutex; |
|---|
| 91 | #endif |
|---|
| 92 | |
|---|
| 93 | // Helper class to manage taking and releasing recursively |
|---|
| 94 | public: |
|---|
| 95 | class Locker |
|---|
| 96 | { |
|---|
| 97 | |
|---|
| 98 | // Construction/Destruction |
|---|
| 99 | public: |
|---|
| 100 | Locker(CAMutex& inMutex) : mMutex(inMutex), mNeedsRelease(false) { mNeedsRelease = mMutex.Lock(); } |
|---|
| 101 | ~Locker() { if(mNeedsRelease) { mMutex.Unlock(); } } |
|---|
| 102 | |
|---|
| 103 | private: |
|---|
| 104 | Locker(const Locker&); |
|---|
| 105 | Locker& operator=(const Locker&); |
|---|
| 106 | |
|---|
| 107 | // Implementation |
|---|
| 108 | private: |
|---|
| 109 | CAMutex& mMutex; |
|---|
| 110 | bool mNeedsRelease; |
|---|
| 111 | |
|---|
| 112 | }; |
|---|
| 113 | }; |
|---|
| 114 | |
|---|
| 115 | |
|---|
| 116 | #endif // __CAMutex_h__ |
|---|