Mercurial > sdl-ios-xcode
comparison src/cdrom/macosx/CAGuard.cpp @ 613:9c6717a1c66f
Added MacOS X CD-ROM audio support (thanks Max and Darrell)
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Tue, 15 Apr 2003 16:33:56 +0000 |
parents | |
children | de1b2c3063b9 |
comparison
equal
deleted
inserted
replaced
612:0648505b1f8b | 613:9c6717a1c66f |
---|---|
1 /* | |
2 SDL - Simple DirectMedia Layer | |
3 Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002 Sam Lantinga | |
4 | |
5 This library is free software; you can redistribute it and/or | |
6 modify it under the terms of the GNU Library General Public | |
7 License as published by the Free Software Foundation; either | |
8 version 2 of the License, or (at your option) any later version. | |
9 | |
10 This library is distributed in the hope that it will be useful, | |
11 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 Library General Public License for more details. | |
14 | |
15 You should have received a copy of the GNU Library General Public | |
16 License along with this library; if not, write to the Free | |
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
18 | |
19 Sam Lantinga | |
20 slouken@libsdl.org | |
21 */ | |
22 /* | |
23 Note: This file hasn't been modified so technically we have to keep the disclaimer :-( | |
24 | |
25 Copyright: © Copyright 2002 Apple Computer, Inc. All rights reserved. | |
26 | |
27 Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Computer, Inc. | |
28 ("Apple") in consideration of your agreement to the following terms, and your | |
29 use, installation, modification or redistribution of this Apple software | |
30 constitutes acceptance of these terms. If you do not agree with these terms, | |
31 please do not use, install, modify or redistribute this Apple software. | |
32 | |
33 In consideration of your agreement to abide by the following terms, and subject | |
34 to these terms, Apple grants you a personal, non-exclusive license, under AppleÕs | |
35 copyrights in this original Apple software (the "Apple Software"), to use, | |
36 reproduce, modify and redistribute the Apple Software, with or without | |
37 modifications, in source and/or binary forms; provided that if you redistribute | |
38 the Apple Software in its entirety and without modifications, you must retain | |
39 this notice and the following text and disclaimers in all such redistributions of | |
40 the Apple Software. Neither the name, trademarks, service marks or logos of | |
41 Apple Computer, Inc. may be used to endorse or promote products derived from the | |
42 Apple Software without specific prior written permission from Apple. Except as | |
43 expressly stated in this notice, no other rights or licenses, express or implied, | |
44 are granted by Apple herein, including but not limited to any patent rights that | |
45 may be infringed by your derivative works or by other works in which the Apple | |
46 Software may be incorporated. | |
47 | |
48 The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO | |
49 WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED | |
50 WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
51 PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN | |
52 COMBINATION WITH YOUR PRODUCTS. | |
53 | |
54 IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR | |
55 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE | |
56 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
57 ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION | |
58 OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT | |
59 (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN | |
60 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
61 */ | |
62 /*============================================================================= | |
63 CAGuard.cp | |
64 | |
65 =============================================================================*/ | |
66 | |
67 //============================================================================= | |
68 // Includes | |
69 //============================================================================= | |
70 | |
71 #include <stdio.h> | |
72 | |
73 #define NDEBUG 1 | |
74 #include <assert.h> | |
75 | |
76 | |
77 #include "CAGuard.h" | |
78 | |
79 //#warning Need a try-based Locker too | |
80 //============================================================================= | |
81 // CAGuard | |
82 //============================================================================= | |
83 | |
84 CAGuard::CAGuard() | |
85 { | |
86 OSStatus theError = pthread_mutex_init(&mMutex, NULL); | |
87 assert(theError == 0); | |
88 | |
89 theError = pthread_cond_init(&mCondVar, NULL); | |
90 assert(theError == 0); | |
91 | |
92 mOwner = 0; | |
93 } | |
94 | |
95 CAGuard::~CAGuard() | |
96 { | |
97 pthread_mutex_destroy(&mMutex); | |
98 pthread_cond_destroy(&mCondVar); | |
99 } | |
100 | |
101 bool CAGuard::Lock() | |
102 { | |
103 bool theAnswer = false; | |
104 | |
105 if(pthread_self() != mOwner) | |
106 { | |
107 OSStatus theError = pthread_mutex_lock(&mMutex); | |
108 assert(theError == 0); | |
109 mOwner = pthread_self(); | |
110 theAnswer = true; | |
111 } | |
112 | |
113 return theAnswer; | |
114 } | |
115 | |
116 void CAGuard::Unlock() | |
117 { | |
118 assert(pthread_self() == mOwner); | |
119 | |
120 mOwner = 0; | |
121 OSStatus theError = pthread_mutex_unlock(&mMutex); | |
122 assert(theError == 0); | |
123 } | |
124 | |
125 bool CAGuard::Try (bool& outWasLocked) | |
126 { | |
127 bool theAnswer = false; | |
128 outWasLocked = false; | |
129 | |
130 if (pthread_self() == mOwner) { | |
131 theAnswer = true; | |
132 outWasLocked = false; | |
133 } else { | |
134 OSStatus theError = pthread_mutex_trylock(&mMutex); | |
135 if (theError == 0) { | |
136 mOwner = pthread_self(); | |
137 theAnswer = true; | |
138 outWasLocked = true; | |
139 } | |
140 } | |
141 | |
142 return theAnswer; | |
143 } | |
144 | |
145 void CAGuard::Wait() | |
146 { | |
147 assert(pthread_self() == mOwner); | |
148 | |
149 mOwner = 0; | |
150 | |
151 OSStatus theError = pthread_cond_wait(&mCondVar, &mMutex); | |
152 assert(theError == 0); | |
153 mOwner = pthread_self(); | |
154 } | |
155 | |
156 void CAGuard::Notify() | |
157 { | |
158 OSStatus theError = pthread_cond_signal(&mCondVar); | |
159 assert(theError == 0); | |
160 } |