19682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* 29682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL - Simple DirectMedia Layer 39682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall Copyright (C) 1997-2012 Sam Lantinga 49682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 59682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall This library is free software; you can redistribute it and/or 69682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall modify it under the terms of the GNU Library General Public 79682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall License as published by the Free Software Foundation; either 89682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall version 2 of the License, or (at your option) any later version. 99682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall This library is distributed in the hope that it will be useful, 119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall but WITHOUT ANY WARRANTY; without even the implied warranty of 129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall Library General Public License for more details. 149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall You should have received a copy of the GNU Library General Public 169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall License along with this library; if not, write to the Free 179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall Sam Lantinga 209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall slouken@libsdl.org 219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/ 229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_config.h" 239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* 259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall Note: This file hasn't been modified so technically we have to keep the disclaimer :-( 269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall Copyright: � Copyright 2002 Apple Computer, Inc. All rights reserved. 289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Computer, Inc. 309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall ("Apple") in consideration of your agreement to the following terms, and your 319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall use, installation, modification or redistribution of this Apple software 329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall constitutes acceptance of these terms. If you do not agree with these terms, 339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall please do not use, install, modify or redistribute this Apple software. 349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall In consideration of your agreement to abide by the following terms, and subject 369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall to these terms, Apple grants you a personal, non-exclusive license, under Apple�s 379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall copyrights in this original Apple software (the "Apple Software"), to use, 389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall reproduce, modify and redistribute the Apple Software, with or without 399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall modifications, in source and/or binary forms; provided that if you redistribute 409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall the Apple Software in its entirety and without modifications, you must retain 419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall this notice and the following text and disclaimers in all such redistributions of 429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall the Apple Software. Neither the name, trademarks, service marks or logos of 439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall Apple Computer, Inc. may be used to endorse or promote products derived from the 449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall Apple Software without specific prior written permission from Apple. Except as 459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall expressly stated in this notice, no other rights or licenses, express or implied, 469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall are granted by Apple herein, including but not limited to any patent rights that 479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall may be infringed by your derivative works or by other works in which the Apple 489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall Software may be incorporated. 499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO 519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED 529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR 539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN 549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall COMBINATION WITH YOUR PRODUCTS. 559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR 579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION 609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT 619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN 629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/ 649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*============================================================================= 659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall CAGuard.cp 669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall=============================================================================*/ 689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*============================================================================= 709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall Includes 719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall =============================================================================*/ 729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* 749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <stdio.h> 759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <stdlib.h> 769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <string.h> 779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/ 789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_stdinc.h" 799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*#define NDEBUG 1*/ 819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* 829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <assert.h> 839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/ 849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define assert(X) 859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDLOSXCAGuard.h" 889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*#warning Need a try-based Locker too*/ 909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*============================================================================= 919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDLOSXCAGuard 929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall =============================================================================*/ 939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int SDLOSXCAGuard_Lock(SDLOSXCAGuard *cag) 959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{ 969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall int theAnswer = 0; 979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if(pthread_self() != cag->mOwner) 999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall { 1009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall OSStatus theError = pthread_mutex_lock(&cag->mMutex); 1019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall (void)theError; 1029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall assert(theError == 0); 1039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall cag->mOwner = pthread_self(); 1049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall theAnswer = 1; 1059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 1069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return theAnswer; 1089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} 1099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void SDLOSXCAGuard_Unlock(SDLOSXCAGuard *cag) 1119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{ 1129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall OSStatus theError; 1139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall assert(pthread_self() == cag->mOwner); 1149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall cag->mOwner = 0; 1169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall theError = pthread_mutex_unlock(&cag->mMutex); 1179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall (void)theError; 1189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall assert(theError == 0); 1199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} 1209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int SDLOSXCAGuard_Try (SDLOSXCAGuard *cag, int *outWasLocked) 1229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{ 1239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall int theAnswer = 0; 1249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall *outWasLocked = 0; 1259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if (pthread_self() == cag->mOwner) { 1279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall theAnswer = 1; 1289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall *outWasLocked = 0; 1299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } else { 1309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall OSStatus theError = pthread_mutex_trylock(&cag->mMutex); 1319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if (theError == 0) { 1329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall cag->mOwner = pthread_self(); 1339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall theAnswer = 1; 1349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall *outWasLocked = 1; 1359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 1369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 1379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return theAnswer; 1399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} 1409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void SDLOSXCAGuard_Wait(SDLOSXCAGuard *cag) 1429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{ 1439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall OSStatus theError; 1449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall assert(pthread_self() == cag->mOwner); 1459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall cag->mOwner = 0; 1479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall theError = pthread_cond_wait(&cag->mCondVar, &cag->mMutex); 1499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall (void)theError; 1509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall assert(theError == 0); 1519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall cag->mOwner = pthread_self(); 1529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} 1539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void SDLOSXCAGuard_Notify(SDLOSXCAGuard *cag) 1559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{ 1569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall OSStatus theError = pthread_cond_signal(&cag->mCondVar); 1579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall (void)theError; 1589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall assert(theError == 0); 1599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} 1609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallSDLOSXCAGuard *new_SDLOSXCAGuard(void) 1639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{ 1649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall OSStatus theError; 1659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDLOSXCAGuard *cag = (SDLOSXCAGuard *) SDL_malloc(sizeof (SDLOSXCAGuard)); 1669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if (cag == NULL) 1679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return NULL; 1689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_memset(cag, '\0', sizeof (*cag)); 1699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall #define SET_SDLOSXCAGUARD_METHOD(m) cag->m = SDLOSXCAGuard_##m 1719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SET_SDLOSXCAGUARD_METHOD(Lock); 1729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SET_SDLOSXCAGUARD_METHOD(Unlock); 1739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SET_SDLOSXCAGUARD_METHOD(Try); 1749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SET_SDLOSXCAGUARD_METHOD(Wait); 1759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SET_SDLOSXCAGUARD_METHOD(Notify); 1769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall #undef SET_SDLOSXCAGUARD_METHOD 1779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall theError = pthread_mutex_init(&cag->mMutex, NULL); 1799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall (void)theError; 1809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall assert(theError == 0); 1819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall theError = pthread_cond_init(&cag->mCondVar, NULL); 1839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall (void)theError; 1849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall assert(theError == 0); 1859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall cag->mOwner = 0; 1879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return cag; 1889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} 1899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid delete_SDLOSXCAGuard(SDLOSXCAGuard *cag) 1919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{ 1929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if (cag != NULL) 1939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall { 1949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall pthread_mutex_destroy(&cag->mMutex); 1959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall pthread_cond_destroy(&cag->mCondVar); 1969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_free(cag); 1979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 1989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} 1999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 200