2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997-2009 Sam Lantinga
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.
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.
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
22 #include "SDL_config.h"
25 Note: This file hasn't been modified so technically we have to keep the disclaimer :-(
27 Copyright: © Copyright 2002 Apple Computer, Inc. All rights reserved.
29 Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Computer, Inc.
30 ("Apple") in consideration of your agreement to the following terms, and your
31 use, installation, modification or redistribution of this Apple software
32 constitutes acceptance of these terms. If you do not agree with these terms,
33 please do not use, install, modify or redistribute this Apple software.
35 In consideration of your agreement to abide by the following terms, and subject
36 to these terms, Apple grants you a personal, non-exclusive license, under AppleÕs
37 copyrights in this original Apple software (the "Apple Software"), to use,
38 reproduce, modify and redistribute the Apple Software, with or without
39 modifications, in source and/or binary forms; provided that if you redistribute
40 the Apple Software in its entirety and without modifications, you must retain
41 this notice and the following text and disclaimers in all such redistributions of
42 the Apple Software. Neither the name, trademarks, service marks or logos of
43 Apple Computer, Inc. may be used to endorse or promote products derived from the
44 Apple Software without specific prior written permission from Apple. Except as
45 expressly stated in this notice, no other rights or licenses, express or implied,
46 are granted by Apple herein, including but not limited to any patent rights that
47 may be infringed by your derivative works or by other works in which the Apple
48 Software may be incorporated.
50 The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO
51 WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
52 WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
53 PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
54 COMBINATION WITH YOUR PRODUCTS.
56 IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
57 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
58 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59 ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
60 OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
61 (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
62 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
64 /*=============================================================================
67 =============================================================================*/
69 /*=============================================================================
71 =============================================================================*/
78 #include "SDL_stdinc.h"
87 #include "SDLOSXCAGuard.h"
89 /*#warning Need a try-based Locker too*/
90 /*=============================================================================
92 =============================================================================*/
94 static int SDLOSXCAGuard_Lock(SDLOSXCAGuard *cag)
98 if(pthread_self() != cag->mOwner)
100 OSStatus theError = pthread_mutex_lock(&cag->mMutex);
102 assert(theError == 0);
103 cag->mOwner = pthread_self();
110 static void SDLOSXCAGuard_Unlock(SDLOSXCAGuard *cag)
113 assert(pthread_self() == cag->mOwner);
116 theError = pthread_mutex_unlock(&cag->mMutex);
118 assert(theError == 0);
121 static int SDLOSXCAGuard_Try (SDLOSXCAGuard *cag, int *outWasLocked)
126 if (pthread_self() == cag->mOwner) {
130 OSStatus theError = pthread_mutex_trylock(&cag->mMutex);
132 cag->mOwner = pthread_self();
141 static void SDLOSXCAGuard_Wait(SDLOSXCAGuard *cag)
144 assert(pthread_self() == cag->mOwner);
148 theError = pthread_cond_wait(&cag->mCondVar, &cag->mMutex);
150 assert(theError == 0);
151 cag->mOwner = pthread_self();
154 static void SDLOSXCAGuard_Notify(SDLOSXCAGuard *cag)
156 OSStatus theError = pthread_cond_signal(&cag->mCondVar);
158 assert(theError == 0);
162 SDLOSXCAGuard *new_SDLOSXCAGuard(void)
165 SDLOSXCAGuard *cag = (SDLOSXCAGuard *) SDL_malloc(sizeof (SDLOSXCAGuard));
168 SDL_memset(cag, '\0', sizeof (*cag));
170 #define SET_SDLOSXCAGUARD_METHOD(m) cag->m = SDLOSXCAGuard_##m
171 SET_SDLOSXCAGUARD_METHOD(Lock);
172 SET_SDLOSXCAGUARD_METHOD(Unlock);
173 SET_SDLOSXCAGUARD_METHOD(Try);
174 SET_SDLOSXCAGUARD_METHOD(Wait);
175 SET_SDLOSXCAGUARD_METHOD(Notify);
176 #undef SET_SDLOSXCAGUARD_METHOD
178 theError = pthread_mutex_init(&cag->mMutex, NULL);
180 assert(theError == 0);
182 theError = pthread_cond_init(&cag->mCondVar, NULL);
184 assert(theError == 0);
190 void delete_SDLOSXCAGuard(SDLOSXCAGuard *cag)
194 pthread_mutex_destroy(&cag->mMutex);
195 pthread_cond_destroy(&cag->mCondVar);