Home | History | Annotate | Download | only in macosx
      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 (at) libsdl.org
     21 */
     22 #include "SDL_config.h"
     23 
     24 /*
     25     Note: This file hasn't been modified so technically we have to keep the disclaimer :-(
     26 
     27     Copyright:   Copyright 2002 Apple Computer, Inc. All rights reserved.
     28 
     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.
     34 
     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 Apples
     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.
     49 
     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.
     55 
     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.
     63 */
     64 /*=============================================================================
     65     CAGuard.cp
     66 
     67 =============================================================================*/
     68 
     69 /*=============================================================================
     70     Includes
     71   =============================================================================*/
     72 
     73 /*
     74 #include <stdio.h>
     75 #include <stdlib.h>
     76 #include <string.h>
     77 */
     78 #include "SDL_stdinc.h"
     79 
     80 /*#define NDEBUG 1*/
     81 /*
     82 #include <assert.h>
     83 */
     84 #define assert(X)
     85 
     86 
     87 #include "SDLOSXCAGuard.h"
     88 
     89 /*#warning      Need a try-based Locker too*/
     90 /*=============================================================================
     91     SDLOSXCAGuard
     92   =============================================================================*/
     93 
     94 static int SDLOSXCAGuard_Lock(SDLOSXCAGuard *cag)
     95 {
     96     int theAnswer = 0;
     97 
     98     if(pthread_self() != cag->mOwner)
     99     {
    100         OSStatus theError = pthread_mutex_lock(&cag->mMutex);
    101         (void)theError;
    102         assert(theError == 0);
    103         cag->mOwner = pthread_self();
    104         theAnswer = 1;
    105     }
    106 
    107     return theAnswer;
    108 }
    109 
    110 static void    SDLOSXCAGuard_Unlock(SDLOSXCAGuard *cag)
    111 {
    112     OSStatus theError;
    113     assert(pthread_self() == cag->mOwner);
    114 
    115     cag->mOwner = 0;
    116     theError = pthread_mutex_unlock(&cag->mMutex);
    117     (void)theError;
    118     assert(theError == 0);
    119 }
    120 
    121 static int SDLOSXCAGuard_Try (SDLOSXCAGuard *cag, int *outWasLocked)
    122 {
    123     int theAnswer = 0;
    124     *outWasLocked = 0;
    125 
    126     if (pthread_self() == cag->mOwner) {
    127         theAnswer = 1;
    128         *outWasLocked = 0;
    129     } else {
    130         OSStatus theError = pthread_mutex_trylock(&cag->mMutex);
    131         if (theError == 0) {
    132             cag->mOwner = pthread_self();
    133             theAnswer = 1;
    134             *outWasLocked = 1;
    135         }
    136     }
    137 
    138     return theAnswer;
    139 }
    140 
    141 static void    SDLOSXCAGuard_Wait(SDLOSXCAGuard *cag)
    142 {
    143     OSStatus theError;
    144     assert(pthread_self() == cag->mOwner);
    145 
    146     cag->mOwner = 0;
    147 
    148     theError = pthread_cond_wait(&cag->mCondVar, &cag->mMutex);
    149     (void)theError;
    150     assert(theError == 0);
    151     cag->mOwner = pthread_self();
    152 }
    153 
    154 static void    SDLOSXCAGuard_Notify(SDLOSXCAGuard *cag)
    155 {
    156     OSStatus theError = pthread_cond_signal(&cag->mCondVar);
    157     (void)theError;
    158     assert(theError == 0);
    159 }
    160 
    161 
    162 SDLOSXCAGuard *new_SDLOSXCAGuard(void)
    163 {
    164     OSStatus theError;
    165     SDLOSXCAGuard *cag = (SDLOSXCAGuard *) SDL_malloc(sizeof (SDLOSXCAGuard));
    166     if (cag == NULL)
    167         return NULL;
    168     SDL_memset(cag, '\0', sizeof (*cag));
    169 
    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
    177 
    178     theError = pthread_mutex_init(&cag->mMutex, NULL);
    179     (void)theError;
    180     assert(theError == 0);
    181 
    182     theError = pthread_cond_init(&cag->mCondVar, NULL);
    183     (void)theError;
    184     assert(theError == 0);
    185 
    186     cag->mOwner = 0;
    187     return cag;
    188 }
    189 
    190 void delete_SDLOSXCAGuard(SDLOSXCAGuard *cag)
    191 {
    192     if (cag != NULL)
    193     {
    194         pthread_mutex_destroy(&cag->mMutex);
    195         pthread_cond_destroy(&cag->mCondVar);
    196         SDL_free(cag);
    197     }
    198 }
    199 
    200