comparison src/cdrom/macosx/SDLOSXCAGuard.c @ 1143:71a2648acc75

Replaced Mac OS X's C++ cdrom code with almost-direct translation to C. Sam requested this effort on the mailing list, apparently because of binary compatibility issues between 10.4 and earlier systems (or gcc4 and earlier compilers?). Works fine with SDL12/test/testcdrom.c, with this command line: ./testcdrom -status -list -play -sleep 5000 -pause -sleep 3000 -resume \ -sleep 5000 -stop -sleep 3000 -play -sleep 3000 -stop \ -sleep 3000 -eject Unix Makefiles work, XCode project still need updating for new filenames.
author Ryan C. Gordon <icculus@icculus.org>
date Thu, 22 Sep 2005 08:48:16 +0000
parents
children 3692456e7b0f
comparison
equal deleted inserted replaced
1142:c7376efecdb5 1143:71a2648acc75
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 #include <stdlib.h>
73 #include <string.h>
74
75 //#define NDEBUG 1
76 #include <assert.h>
77
78
79 #include "SDLOSXCAGuard.h"
80
81 //#warning Need a try-based Locker too
82 //=============================================================================
83 // SDLOSXCAGuard
84 //=============================================================================
85
86 static int SDLOSXCAGuard_Lock(SDLOSXCAGuard *cag)
87 {
88 int theAnswer = 0;
89
90 if(pthread_self() != cag->mOwner)
91 {
92 OSStatus theError = pthread_mutex_lock(&cag->mMutex);
93 assert(theError == 0);
94 cag->mOwner = pthread_self();
95 theAnswer = 1;
96 }
97
98 return theAnswer;
99 }
100
101 static void SDLOSXCAGuard_Unlock(SDLOSXCAGuard *cag)
102 {
103 assert(pthread_self() == cag->mOwner);
104
105 cag->mOwner = 0;
106 OSStatus theError = pthread_mutex_unlock(&cag->mMutex);
107 assert(theError == 0);
108 }
109
110 static int SDLOSXCAGuard_Try (SDLOSXCAGuard *cag, int *outWasLocked)
111 {
112 int theAnswer = 0;
113 *outWasLocked = 0;
114
115 if (pthread_self() == cag->mOwner) {
116 theAnswer = 1;
117 *outWasLocked = 0;
118 } else {
119 OSStatus theError = pthread_mutex_trylock(&cag->mMutex);
120 if (theError == 0) {
121 cag->mOwner = pthread_self();
122 theAnswer = 1;
123 *outWasLocked = 1;
124 }
125 }
126
127 return theAnswer;
128 }
129
130 static void SDLOSXCAGuard_Wait(SDLOSXCAGuard *cag)
131 {
132 assert(pthread_self() == cag->mOwner);
133
134 cag->mOwner = 0;
135
136 OSStatus theError = pthread_cond_wait(&cag->mCondVar, &cag->mMutex);
137 assert(theError == 0);
138 cag->mOwner = pthread_self();
139 }
140
141 static void SDLOSXCAGuard_Notify(SDLOSXCAGuard *cag)
142 {
143 OSStatus theError = pthread_cond_signal(&cag->mCondVar);
144 assert(theError == 0);
145 }
146
147
148 SDLOSXCAGuard *new_SDLOSXCAGuard(void)
149 {
150 SDLOSXCAGuard *cag = (SDLOSXCAGuard *) malloc(sizeof (SDLOSXCAGuard));
151 if (cag == NULL)
152 return NULL;
153 memset(cag, '\0', sizeof (*cag));
154
155 #define SET_SDLOSXCAGUARD_METHOD(m) cag->m = SDLOSXCAGuard_##m
156 SET_SDLOSXCAGUARD_METHOD(Lock);
157 SET_SDLOSXCAGUARD_METHOD(Unlock);
158 SET_SDLOSXCAGUARD_METHOD(Try);
159 SET_SDLOSXCAGUARD_METHOD(Wait);
160 SET_SDLOSXCAGUARD_METHOD(Notify);
161 #undef SET_SDLOSXCAGUARD_METHOD
162
163 OSStatus theError = pthread_mutex_init(&cag->mMutex, NULL);
164 assert(theError == 0);
165
166 theError = pthread_cond_init(&cag->mCondVar, NULL);
167 assert(theError == 0);
168
169 cag->mOwner = 0;
170 return cag;
171 }
172
173 void delete_SDLOSXCAGuard(SDLOSXCAGuard *cag)
174 {
175 if (cag != NULL)
176 {
177 pthread_mutex_destroy(&cag->mMutex);
178 pthread_cond_destroy(&cag->mCondVar);
179 free(cag);
180 }
181 }
182