SDLOSXCAGuard.c revision 9682c8870b8ff5e4ac2e4c70b759f791c6f38c1f
1411bb7237e5148dc398ae0f3e53ea5f2eb4402decaryclark@google.com/* 2451bb9f801d668275394ca5bd57f238e13cf3d17keyar@chromium.org SDL - Simple DirectMedia Layer 3451bb9f801d668275394ca5bd57f238e13cf3d17keyar@chromium.org Copyright (C) 1997-2012 Sam Lantinga 4451bb9f801d668275394ca5bd57f238e13cf3d17keyar@chromium.org 5451bb9f801d668275394ca5bd57f238e13cf3d17keyar@chromium.org This library is free software; you can redistribute it and/or 6451bb9f801d668275394ca5bd57f238e13cf3d17keyar@chromium.org modify it under the terms of the GNU Library General Public 7451bb9f801d668275394ca5bd57f238e13cf3d17keyar@chromium.org License as published by the Free Software Foundation; either 8451bb9f801d668275394ca5bd57f238e13cf3d17keyar@chromium.org version 2 of the License, or (at your option) any later version. 9451bb9f801d668275394ca5bd57f238e13cf3d17keyar@chromium.org 10a62da2fee72172a630c2d1dba0e529b357743662scroggo@google.com This library is distributed in the hope that it will be useful, 11161e1ba4c0a82e21c7d68808529699fd2394ad6cscroggo@google.com but WITHOUT ANY WARRANTY; without even the implied warranty of 12a36223764466c964ed1035bf6f152ba55168f61ecaryclark@google.com MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 134d1a364e391a8fd79c9e133d27957761d35068d0kelvinly Library General Public License for more details. 14f4959ab11827bef99e8985031feb457cae1f987akeyar@chromium.org 15ea6a306457e23d579ee86cd705c83e4e1e0693c3reed@google.com You should have received a copy of the GNU Library General Public 169a4125283ad56cea3b986337cb669dde14bf0ed8scroggo@google.com License along with this library; if not, write to the Free 17770963f23f4fc313db0fa3bac18b1b8aafb55f17robertphillips@google.com Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18acfb30e5bb797249f4ec41edda4c4d4b86e0e5d0scroggo@google.com 19451bb9f801d668275394ca5bd57f238e13cf3d17keyar@chromium.org Sam Lantinga 209a4125283ad56cea3b986337cb669dde14bf0ed8scroggo@google.com slouken@libsdl.org 21a62da2fee72172a630c2d1dba0e529b357743662scroggo@google.com*/ 22a62da2fee72172a630c2d1dba0e529b357743662scroggo@google.com#include "SDL_config.h" 23451bb9f801d668275394ca5bd57f238e13cf3d17keyar@chromium.org 240612564bb76ec533d26d87e03368fd81ef97831ekeyar@chromium.org/* 250612564bb76ec533d26d87e03368fd81ef97831ekeyar@chromium.org Note: This file hasn't been modified so technically we have to keep the disclaimer :-( 260612564bb76ec533d26d87e03368fd81ef97831ekeyar@chromium.org 270612564bb76ec533d26d87e03368fd81ef97831ekeyar@chromium.org Copyright: � Copyright 2002 Apple Computer, Inc. All rights reserved. 280612564bb76ec533d26d87e03368fd81ef97831ekeyar@chromium.org 2990c0fbd34947b6b644e90c5b2253752035f65b64commit-bot@chromium.org Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Computer, Inc. 3090c0fbd34947b6b644e90c5b2253752035f65b64commit-bot@chromium.org ("Apple") in consideration of your agreement to the following terms, and your 31451bb9f801d668275394ca5bd57f238e13cf3d17keyar@chromium.org use, installation, modification or redistribution of this Apple software 32451bb9f801d668275394ca5bd57f238e13cf3d17keyar@chromium.org constitutes acceptance of these terms. If you do not agree with these terms, 336177e6999d23a4268ffd98dedfb1da00e272a89brobertphillips@google.com please do not use, install, modify or redistribute this Apple software. 34a62da2fee72172a630c2d1dba0e529b357743662scroggo@google.com 35451bb9f801d668275394ca5bd57f238e13cf3d17keyar@chromium.org In consideration of your agreement to abide by the following terms, and subject 36451bb9f801d668275394ca5bd57f238e13cf3d17keyar@chromium.org to these terms, Apple grants you a personal, non-exclusive license, under Apple�s 37451bb9f801d668275394ca5bd57f238e13cf3d17keyar@chromium.org copyrights in this original Apple software (the "Apple Software"), to use, 38cbcef708914f3b5e9bb63ad3d87261378026e29bscroggo@google.com reproduce, modify and redistribute the Apple Software, with or without 39cbcef708914f3b5e9bb63ad3d87261378026e29bscroggo@google.com modifications, in source and/or binary forms; provided that if you redistribute 40451bb9f801d668275394ca5bd57f238e13cf3d17keyar@chromium.org the Apple Software in its entirety and without modifications, you must retain 41cbcef708914f3b5e9bb63ad3d87261378026e29bscroggo@google.com this notice and the following text and disclaimers in all such redistributions of 42451bb9f801d668275394ca5bd57f238e13cf3d17keyar@chromium.org the Apple Software. Neither the name, trademarks, service marks or logos of 43c81686cd6edba2cfedcfd5ad6a164f160ddfb49fkeyar@chromium.org Apple Computer, Inc. may be used to endorse or promote products derived from the 440556ea0ede7066b9b3ed9f03f5a6eb72cd718897scroggo@google.com Apple Software without specific prior written permission from Apple. Except as 450556ea0ede7066b9b3ed9f03f5a6eb72cd718897scroggo@google.com expressly stated in this notice, no other rights or licenses, express or implied, 460556ea0ede7066b9b3ed9f03f5a6eb72cd718897scroggo@google.com are granted by Apple herein, including but not limited to any patent rights that 476ab96733b85cebac99b378f998cd7dc10b286395rmistry@google.com may be infringed by your derivative works or by other works in which the Apple 486ab96733b85cebac99b378f998cd7dc10b286395rmistry@google.com Software may be incorporated. 496ab96733b85cebac99b378f998cd7dc10b286395rmistry@google.com 50c81686cd6edba2cfedcfd5ad6a164f160ddfb49fkeyar@chromium.org The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO 51c81686cd6edba2cfedcfd5ad6a164f160ddfb49fkeyar@chromium.org WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED 520556ea0ede7066b9b3ed9f03f5a6eb72cd718897scroggo@google.com WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR 530fd5270ad69f5b99852d544158c989d2deda2c11commit-bot@chromium.org PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN 54c81686cd6edba2cfedcfd5ad6a164f160ddfb49fkeyar@chromium.org COMBINATION WITH YOUR PRODUCTS. 55c81686cd6edba2cfedcfd5ad6a164f160ddfb49fkeyar@chromium.org 56c81686cd6edba2cfedcfd5ad6a164f160ddfb49fkeyar@chromium.org IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR 579313ca4bde606a18f29214eea8b8e47312b8fd9cjunov@chromium.org CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 589313ca4bde606a18f29214eea8b8e47312b8fd9cjunov@chromium.org GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 599313ca4bde606a18f29214eea8b8e47312b8fd9cjunov@chromium.org ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION 607b53706a7d596a2d8dce6cfe5b543264e5a37239junov@chromium.org OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT 61cdd0f92b1aca0c2164ac1a24d7f14a6ad3cc10f5commit-bot@chromium.org (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN 62cdd0f92b1aca0c2164ac1a24d7f14a6ad3cc10f5commit-bot@chromium.org ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 639313ca4bde606a18f29214eea8b8e47312b8fd9cjunov@chromium.org*/ 649313ca4bde606a18f29214eea8b8e47312b8fd9cjunov@chromium.org/*============================================================================= 65a36223764466c964ed1035bf6f152ba55168f61ecaryclark@google.com CAGuard.cp 66a36223764466c964ed1035bf6f152ba55168f61ecaryclark@google.com 67a36223764466c964ed1035bf6f152ba55168f61ecaryclark@google.com=============================================================================*/ 68881b10b023bcba3c81f1d9e98e62b7b6855b0f11reed@google.com 69881b10b023bcba3c81f1d9e98e62b7b6855b0f11reed@google.com/*============================================================================= 70881b10b023bcba3c81f1d9e98e62b7b6855b0f11reed@google.com Includes 71387db0a2e516ca01508f7d16433f84da2ea3b93bhumper@google.com =============================================================================*/ 72a36223764466c964ed1035bf6f152ba55168f61ecaryclark@google.com 73a36223764466c964ed1035bf6f152ba55168f61ecaryclark@google.com/* 74491493119c11206c5823b76eb6420a705be243a1robertphillips@google.com#include <stdio.h> 75a36223764466c964ed1035bf6f152ba55168f61ecaryclark@google.com#include <stdlib.h> 76a36223764466c964ed1035bf6f152ba55168f61ecaryclark@google.com#include <string.h> 77a36223764466c964ed1035bf6f152ba55168f61ecaryclark@google.com*/ 78a36223764466c964ed1035bf6f152ba55168f61ecaryclark@google.com#include "SDL_stdinc.h" 79a36223764466c964ed1035bf6f152ba55168f61ecaryclark@google.com 80a62da2fee72172a630c2d1dba0e529b357743662scroggo@google.com/*#define NDEBUG 1*/ 81a62da2fee72172a630c2d1dba0e529b357743662scroggo@google.com/* 82f5e315ccf1ae2941f7cf53fa53e5c8c4bb665fe1commit-bot@chromium.org#include <assert.h> 83f5e315ccf1ae2941f7cf53fa53e5c8c4bb665fe1commit-bot@chromium.org*/ 843f0451772109959fcb79bacf2c9a03e0eb39ff27commit-bot@chromium.org#define assert(X) 853f0451772109959fcb79bacf2c9a03e0eb39ff27commit-bot@chromium.org 863f0451772109959fcb79bacf2c9a03e0eb39ff27commit-bot@chromium.org 873f0451772109959fcb79bacf2c9a03e0eb39ff27commit-bot@chromium.org#include "SDLOSXCAGuard.h" 88f5e315ccf1ae2941f7cf53fa53e5c8c4bb665fe1commit-bot@chromium.org 89f5e315ccf1ae2941f7cf53fa53e5c8c4bb665fe1commit-bot@chromium.org/*#warning Need a try-based Locker too*/ 90f5e315ccf1ae2941f7cf53fa53e5c8c4bb665fe1commit-bot@chromium.org/*============================================================================= 91a62da2fee72172a630c2d1dba0e529b357743662scroggo@google.com SDLOSXCAGuard 922a65a238b0e688356aee09d08f3f8a317359b4d2mtklein =============================================================================*/ 932a65a238b0e688356aee09d08f3f8a317359b4d2mtklein 94ce4dd3de38cd7c29bf5b9d8a8efb55c08ec9be47robertphillipsstatic int SDLOSXCAGuard_Lock(SDLOSXCAGuard *cag) 952a65a238b0e688356aee09d08f3f8a317359b4d2mtklein{ 96ce4dd3de38cd7c29bf5b9d8a8efb55c08ec9be47robertphillips int theAnswer = 0; 979a4125283ad56cea3b986337cb669dde14bf0ed8scroggo@google.com 989a4125283ad56cea3b986337cb669dde14bf0ed8scroggo@google.com if(pthread_self() != cag->mOwner) 998991c67f0c1b9364bb12dfb0f32a53bd5a3357f3commit-bot@chromium.org { 1008991c67f0c1b9364bb12dfb0f32a53bd5a3357f3commit-bot@chromium.org OSStatus theError = pthread_mutex_lock(&cag->mMutex); 1018991c67f0c1b9364bb12dfb0f32a53bd5a3357f3commit-bot@chromium.org (void)theError; 1028991c67f0c1b9364bb12dfb0f32a53bd5a3357f3commit-bot@chromium.org assert(theError == 0); 1038991c67f0c1b9364bb12dfb0f32a53bd5a3357f3commit-bot@chromium.org cag->mOwner = pthread_self(); 1048991c67f0c1b9364bb12dfb0f32a53bd5a3357f3commit-bot@chromium.org theAnswer = 1; 1058991c67f0c1b9364bb12dfb0f32a53bd5a3357f3commit-bot@chromium.org } 1068991c67f0c1b9364bb12dfb0f32a53bd5a3357f3commit-bot@chromium.org 107c0d5e549ab8d594a5da8db417db39622e9491fffscroggo@google.com return theAnswer; 108c0d5e549ab8d594a5da8db417db39622e9491fffscroggo@google.com} 109c0d5e549ab8d594a5da8db417db39622e9491fffscroggo@google.com 110c0d5e549ab8d594a5da8db417db39622e9491fffscroggo@google.comstatic void SDLOSXCAGuard_Unlock(SDLOSXCAGuard *cag) 111c0d5e549ab8d594a5da8db417db39622e9491fffscroggo@google.com{ 11282ec0b00f380906c1cdeb4b4cc4a355264ab3882scroggo@google.com OSStatus theError; 11382ec0b00f380906c1cdeb4b4cc4a355264ab3882scroggo@google.com assert(pthread_self() == cag->mOwner); 11482ec0b00f380906c1cdeb4b4cc4a355264ab3882scroggo@google.com 11582ec0b00f380906c1cdeb4b4cc4a355264ab3882scroggo@google.com cag->mOwner = 0; 11682ec0b00f380906c1cdeb4b4cc4a355264ab3882scroggo@google.com theError = pthread_mutex_unlock(&cag->mMutex); 1179a4125283ad56cea3b986337cb669dde14bf0ed8scroggo@google.com (void)theError; 1189a4125283ad56cea3b986337cb669dde14bf0ed8scroggo@google.com assert(theError == 0); 1199a4125283ad56cea3b986337cb669dde14bf0ed8scroggo@google.com} 1209a4125283ad56cea3b986337cb669dde14bf0ed8scroggo@google.com 1219a4125283ad56cea3b986337cb669dde14bf0ed8scroggo@google.comstatic int SDLOSXCAGuard_Try (SDLOSXCAGuard *cag, int *outWasLocked) 1229a4125283ad56cea3b986337cb669dde14bf0ed8scroggo@google.com{ 123f5e315ccf1ae2941f7cf53fa53e5c8c4bb665fe1commit-bot@chromium.org int theAnswer = 0; 124f5e315ccf1ae2941f7cf53fa53e5c8c4bb665fe1commit-bot@chromium.org *outWasLocked = 0; 125f5e315ccf1ae2941f7cf53fa53e5c8c4bb665fe1commit-bot@chromium.org 126f5e315ccf1ae2941f7cf53fa53e5c8c4bb665fe1commit-bot@chromium.org if (pthread_self() == cag->mOwner) { 127f5e315ccf1ae2941f7cf53fa53e5c8c4bb665fe1commit-bot@chromium.org theAnswer = 1; 128f5e315ccf1ae2941f7cf53fa53e5c8c4bb665fe1commit-bot@chromium.org *outWasLocked = 0; 1293f0451772109959fcb79bacf2c9a03e0eb39ff27commit-bot@chromium.org } else { 1303f0451772109959fcb79bacf2c9a03e0eb39ff27commit-bot@chromium.org OSStatus theError = pthread_mutex_trylock(&cag->mMutex); 1313f0451772109959fcb79bacf2c9a03e0eb39ff27commit-bot@chromium.org if (theError == 0) { 132f5e315ccf1ae2941f7cf53fa53e5c8c4bb665fe1commit-bot@chromium.org cag->mOwner = pthread_self(); 133f5e315ccf1ae2941f7cf53fa53e5c8c4bb665fe1commit-bot@chromium.org theAnswer = 1; 134f5e315ccf1ae2941f7cf53fa53e5c8c4bb665fe1commit-bot@chromium.org *outWasLocked = 1; 1353f0451772109959fcb79bacf2c9a03e0eb39ff27commit-bot@chromium.org } 136f5e315ccf1ae2941f7cf53fa53e5c8c4bb665fe1commit-bot@chromium.org } 1379a4125283ad56cea3b986337cb669dde14bf0ed8scroggo@google.com 138f5e315ccf1ae2941f7cf53fa53e5c8c4bb665fe1commit-bot@chromium.org return theAnswer; 1399a4125283ad56cea3b986337cb669dde14bf0ed8scroggo@google.com} 140a62da2fee72172a630c2d1dba0e529b357743662scroggo@google.com 141a62da2fee72172a630c2d1dba0e529b357743662scroggo@google.comstatic void SDLOSXCAGuard_Wait(SDLOSXCAGuard *cag) 142a62da2fee72172a630c2d1dba0e529b357743662scroggo@google.com{ 143a62da2fee72172a630c2d1dba0e529b357743662scroggo@google.com OSStatus theError; 1449d696c0d04548520af08252e577d2ca88012a239keyar@chromium.org assert(pthread_self() == cag->mOwner); 145a62da2fee72172a630c2d1dba0e529b357743662scroggo@google.com 146cbcef708914f3b5e9bb63ad3d87261378026e29bscroggo@google.com cag->mOwner = 0; 147cbcef708914f3b5e9bb63ad3d87261378026e29bscroggo@google.com 148cbcef708914f3b5e9bb63ad3d87261378026e29bscroggo@google.com theError = pthread_cond_wait(&cag->mCondVar, &cag->mMutex); 149cbcef708914f3b5e9bb63ad3d87261378026e29bscroggo@google.com (void)theError; 150cbcef708914f3b5e9bb63ad3d87261378026e29bscroggo@google.com assert(theError == 0); 151cbcef708914f3b5e9bb63ad3d87261378026e29bscroggo@google.com cag->mOwner = pthread_self(); 15208085f808b808bfb14fcb247bb8cb1a5e8d43dccscroggo@google.com} 15308085f808b808bfb14fcb247bb8cb1a5e8d43dccscroggo@google.com 15451c040ed1e8c50bec4297bbe25242f644b40b0b7commit-bot@chromium.orgstatic void SDLOSXCAGuard_Notify(SDLOSXCAGuard *cag) 15508085f808b808bfb14fcb247bb8cb1a5e8d43dccscroggo@google.com{ 15608085f808b808bfb14fcb247bb8cb1a5e8d43dccscroggo@google.com OSStatus theError = pthread_cond_signal(&cag->mCondVar); 15708085f808b808bfb14fcb247bb8cb1a5e8d43dccscroggo@google.com (void)theError; 1589d696c0d04548520af08252e577d2ca88012a239keyar@chromium.org assert(theError == 0); 1590556ea0ede7066b9b3ed9f03f5a6eb72cd718897scroggo@google.com} 16094d8f1ed6116780346ffa39ed683f1977786434arobertphillips@google.com 16194d8f1ed6116780346ffa39ed683f1977786434arobertphillips@google.com 16294d8f1ed6116780346ffa39ed683f1977786434arobertphillips@google.comSDLOSXCAGuard *new_SDLOSXCAGuard(void) 16394d8f1ed6116780346ffa39ed683f1977786434arobertphillips@google.com{ 16494d8f1ed6116780346ffa39ed683f1977786434arobertphillips@google.com OSStatus theError; 16594d8f1ed6116780346ffa39ed683f1977786434arobertphillips@google.com SDLOSXCAGuard *cag = (SDLOSXCAGuard *) SDL_malloc(sizeof (SDLOSXCAGuard)); 1660556ea0ede7066b9b3ed9f03f5a6eb72cd718897scroggo@google.com if (cag == NULL) 1670556ea0ede7066b9b3ed9f03f5a6eb72cd718897scroggo@google.com return NULL; 16880549fcdd50269d7e069d6db02b395fca128056ckkinnunen SDL_memset(cag, '\0', sizeof (*cag)); 16980549fcdd50269d7e069d6db02b395fca128056ckkinnunen 17080549fcdd50269d7e069d6db02b395fca128056ckkinnunen #define SET_SDLOSXCAGUARD_METHOD(m) cag->m = SDLOSXCAGuard_##m 1710556ea0ede7066b9b3ed9f03f5a6eb72cd718897scroggo@google.com SET_SDLOSXCAGUARD_METHOD(Lock); 17280549fcdd50269d7e069d6db02b395fca128056ckkinnunen SET_SDLOSXCAGUARD_METHOD(Unlock); 173c81686cd6edba2cfedcfd5ad6a164f160ddfb49fkeyar@chromium.org SET_SDLOSXCAGUARD_METHOD(Try); 1740556ea0ede7066b9b3ed9f03f5a6eb72cd718897scroggo@google.com SET_SDLOSXCAGUARD_METHOD(Wait); 1750556ea0ede7066b9b3ed9f03f5a6eb72cd718897scroggo@google.com SET_SDLOSXCAGUARD_METHOD(Notify); 1760556ea0ede7066b9b3ed9f03f5a6eb72cd718897scroggo@google.com #undef SET_SDLOSXCAGUARD_METHOD 1770556ea0ede7066b9b3ed9f03f5a6eb72cd718897scroggo@google.com 1780556ea0ede7066b9b3ed9f03f5a6eb72cd718897scroggo@google.com theError = pthread_mutex_init(&cag->mMutex, NULL); 1790556ea0ede7066b9b3ed9f03f5a6eb72cd718897scroggo@google.com (void)theError; 1800556ea0ede7066b9b3ed9f03f5a6eb72cd718897scroggo@google.com assert(theError == 0); 1810556ea0ede7066b9b3ed9f03f5a6eb72cd718897scroggo@google.com 1820556ea0ede7066b9b3ed9f03f5a6eb72cd718897scroggo@google.com theError = pthread_cond_init(&cag->mCondVar, NULL); 1830556ea0ede7066b9b3ed9f03f5a6eb72cd718897scroggo@google.com (void)theError; 1840556ea0ede7066b9b3ed9f03f5a6eb72cd718897scroggo@google.com assert(theError == 0); 1850556ea0ede7066b9b3ed9f03f5a6eb72cd718897scroggo@google.com 1860556ea0ede7066b9b3ed9f03f5a6eb72cd718897scroggo@google.com cag->mOwner = 0; 1870556ea0ede7066b9b3ed9f03f5a6eb72cd718897scroggo@google.com return cag; 1880fd5270ad69f5b99852d544158c989d2deda2c11commit-bot@chromium.org} 1890fd5270ad69f5b99852d544158c989d2deda2c11commit-bot@chromium.org 1900fd5270ad69f5b99852d544158c989d2deda2c11commit-bot@chromium.orgvoid delete_SDLOSXCAGuard(SDLOSXCAGuard *cag) 1910556ea0ede7066b9b3ed9f03f5a6eb72cd718897scroggo@google.com{ 1920556ea0ede7066b9b3ed9f03f5a6eb72cd718897scroggo@google.com if (cag != NULL) 1930556ea0ede7066b9b3ed9f03f5a6eb72cd718897scroggo@google.com { 1940556ea0ede7066b9b3ed9f03f5a6eb72cd718897scroggo@google.com pthread_mutex_destroy(&cag->mMutex); 1950556ea0ede7066b9b3ed9f03f5a6eb72cd718897scroggo@google.com pthread_cond_destroy(&cag->mCondVar); 1966ab96733b85cebac99b378f998cd7dc10b286395rmistry@google.com SDL_free(cag); 1976ab96733b85cebac99b378f998cd7dc10b286395rmistry@google.com } 1986ab96733b85cebac99b378f998cd7dc10b286395rmistry@google.com} 1996ab96733b85cebac99b378f998cd7dc10b286395rmistry@google.com 2006ab96733b85cebac99b378f998cd7dc10b286395rmistry@google.com