src/cdrom/macosx/SDLOSXCAGuard.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 29 May 2006 04:04:35 +0000
branchSDL-1.3
changeset 1668 4da1ee79c9af
parent 1662 782fd950bd46
permissions -rw-r--r--
more tweaking indent options
icculus@1143
     1
/*
icculus@1143
     2
    SDL - Simple DirectMedia Layer
icculus@1143
     3
    Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002  Sam Lantinga
icculus@1143
     4
icculus@1143
     5
    This library is free software; you can redistribute it and/or
icculus@1143
     6
    modify it under the terms of the GNU Library General Public
icculus@1143
     7
    License as published by the Free Software Foundation; either
icculus@1143
     8
    version 2 of the License, or (at your option) any later version.
icculus@1143
     9
icculus@1143
    10
    This library is distributed in the hope that it will be useful,
icculus@1143
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
icculus@1143
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
icculus@1143
    13
    Library General Public License for more details.
icculus@1143
    14
icculus@1143
    15
    You should have received a copy of the GNU Library General Public
icculus@1143
    16
    License along with this library; if not, write to the Free
icculus@1143
    17
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
icculus@1143
    18
icculus@1143
    19
    Sam Lantinga
icculus@1143
    20
    slouken@libsdl.org
icculus@1143
    21
*/
slouken@1402
    22
#include "SDL_config.h"
slouken@1402
    23
icculus@1143
    24
/*  
icculus@1143
    25
    Note: This file hasn't been modified so technically we have to keep the disclaimer :-(
icculus@1143
    26
    
icculus@1143
    27
    Copyright:  © Copyright 2002 Apple Computer, Inc. All rights reserved.
icculus@1143
    28
icculus@1143
    29
    Disclaimer: IMPORTANT:  This Apple software is supplied to you by Apple Computer, Inc.
icculus@1143
    30
            ("Apple") in consideration of your agreement to the following terms, and your
icculus@1143
    31
            use, installation, modification or redistribution of this Apple software
icculus@1143
    32
            constitutes acceptance of these terms.  If you do not agree with these terms,
icculus@1143
    33
            please do not use, install, modify or redistribute this Apple software.
icculus@1143
    34
icculus@1143
    35
            In consideration of your agreement to abide by the following terms, and subject
icculus@1143
    36
            to these terms, Apple grants you a personal, non-exclusive license, under Apple’s
icculus@1143
    37
            copyrights in this original Apple software (the "Apple Software"), to use,
icculus@1143
    38
            reproduce, modify and redistribute the Apple Software, with or without
icculus@1143
    39
            modifications, in source and/or binary forms; provided that if you redistribute
icculus@1143
    40
            the Apple Software in its entirety and without modifications, you must retain
icculus@1143
    41
            this notice and the following text and disclaimers in all such redistributions of
icculus@1143
    42
            the Apple Software.  Neither the name, trademarks, service marks or logos of
icculus@1143
    43
            Apple Computer, Inc. may be used to endorse or promote products derived from the
icculus@1143
    44
            Apple Software without specific prior written permission from Apple.  Except as
icculus@1143
    45
            expressly stated in this notice, no other rights or licenses, express or implied,
icculus@1143
    46
            are granted by Apple herein, including but not limited to any patent rights that
icculus@1143
    47
            may be infringed by your derivative works or by other works in which the Apple
icculus@1143
    48
            Software may be incorporated.
icculus@1143
    49
icculus@1143
    50
            The Apple Software is provided by Apple on an "AS IS" basis.  APPLE MAKES NO
icculus@1143
    51
            WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
icculus@1143
    52
            WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
icculus@1143
    53
            PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
icculus@1143
    54
            COMBINATION WITH YOUR PRODUCTS.
icculus@1143
    55
icculus@1143
    56
            IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
icculus@1143
    57
            CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
icculus@1143
    58
            GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
icculus@1143
    59
            ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
icculus@1143
    60
            OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
icculus@1143
    61
            (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
icculus@1143
    62
            ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
icculus@1143
    63
*/
icculus@1143
    64
/*=============================================================================
icculus@1143
    65
    CAGuard.cp
icculus@1143
    66
icculus@1143
    67
=============================================================================*/
icculus@1143
    68
slouken@1487
    69
/*=============================================================================
slouken@1487
    70
    Includes
slouken@1487
    71
  =============================================================================*/
icculus@1143
    72
slouken@1338
    73
/*
icculus@1143
    74
#include <stdio.h>
icculus@1143
    75
#include <stdlib.h>
icculus@1143
    76
#include <string.h>
slouken@1338
    77
*/
slouken@1358
    78
#include "SDL_stdinc.h"
icculus@1143
    79
slouken@1487
    80
/*#define NDEBUG 1*/
slouken@1338
    81
/*
icculus@1143
    82
#include <assert.h>
slouken@1338
    83
*/
slouken@1338
    84
#define assert(X)
icculus@1143
    85
icculus@1143
    86
icculus@1143
    87
#include "SDLOSXCAGuard.h"
icculus@1143
    88
slouken@1487
    89
/*#warning      Need a try-based Locker too*/
slouken@1487
    90
/*=============================================================================
slouken@1487
    91
    SDLOSXCAGuard
slouken@1487
    92
  =============================================================================*/
icculus@1143
    93
slouken@1662
    94
static int
slouken@1668
    95
SDLOSXCAGuard_Lock(SDLOSXCAGuard * cag)
icculus@1143
    96
{
icculus@1143
    97
    int theAnswer = 0;
slouken@1662
    98
slouken@1668
    99
    if (pthread_self() != cag->mOwner) {
slouken@1668
   100
        OSStatus theError = pthread_mutex_lock(&cag->mMutex);
slouken@1662
   101
        (void) theError;
slouken@1668
   102
        assert(theError == 0);
slouken@1668
   103
        cag->mOwner = pthread_self();
icculus@1143
   104
        theAnswer = 1;
icculus@1143
   105
    }
icculus@1143
   106
icculus@1143
   107
    return theAnswer;
icculus@1143
   108
}
icculus@1143
   109
slouken@1662
   110
static void
slouken@1668
   111
SDLOSXCAGuard_Unlock(SDLOSXCAGuard * cag)
icculus@1143
   112
{
slouken@1487
   113
    OSStatus theError;
slouken@1668
   114
    assert(pthread_self() == cag->mOwner);
icculus@1143
   115
icculus@1143
   116
    cag->mOwner = 0;
slouken@1668
   117
    theError = pthread_mutex_unlock(&cag->mMutex);
slouken@1662
   118
    (void) theError;
slouken@1668
   119
    assert(theError == 0);
icculus@1143
   120
}
icculus@1143
   121
slouken@1662
   122
static int
slouken@1668
   123
SDLOSXCAGuard_Try(SDLOSXCAGuard * cag, int *outWasLocked)
icculus@1143
   124
{
icculus@1143
   125
    int theAnswer = 0;
icculus@1143
   126
    *outWasLocked = 0;
slouken@1662
   127
slouken@1668
   128
    if (pthread_self() == cag->mOwner) {
icculus@1143
   129
        theAnswer = 1;
icculus@1143
   130
        *outWasLocked = 0;
icculus@1143
   131
    } else {
slouken@1668
   132
        OSStatus theError = pthread_mutex_trylock(&cag->mMutex);
icculus@1143
   133
        if (theError == 0) {
slouken@1668
   134
            cag->mOwner = pthread_self();
icculus@1143
   135
            theAnswer = 1;
icculus@1143
   136
            *outWasLocked = 1;
icculus@1143
   137
        }
icculus@1143
   138
    }
slouken@1662
   139
icculus@1143
   140
    return theAnswer;
icculus@1143
   141
}
icculus@1143
   142
slouken@1662
   143
static void
slouken@1668
   144
SDLOSXCAGuard_Wait(SDLOSXCAGuard * cag)
icculus@1143
   145
{
slouken@1487
   146
    OSStatus theError;
slouken@1668
   147
    assert(pthread_self() == cag->mOwner);
icculus@1143
   148
icculus@1143
   149
    cag->mOwner = 0;
icculus@1143
   150
slouken@1668
   151
    theError = pthread_cond_wait(&cag->mCondVar, &cag->mMutex);
slouken@1662
   152
    (void) theError;
slouken@1668
   153
    assert(theError == 0);
slouken@1668
   154
    cag->mOwner = pthread_self();
icculus@1143
   155
}
icculus@1143
   156
slouken@1662
   157
static void
slouken@1668
   158
SDLOSXCAGuard_Notify(SDLOSXCAGuard * cag)
icculus@1143
   159
{
slouken@1668
   160
    OSStatus theError = pthread_cond_signal(&cag->mCondVar);
slouken@1662
   161
    (void) theError;
slouken@1668
   162
    assert(theError == 0);
icculus@1143
   163
}
icculus@1143
   164
icculus@1143
   165
slouken@1662
   166
SDLOSXCAGuard *
slouken@1668
   167
new_SDLOSXCAGuard(void)
icculus@1143
   168
{
slouken@1487
   169
    OSStatus theError;
slouken@1668
   170
    SDLOSXCAGuard *cag = (SDLOSXCAGuard *) SDL_malloc(sizeof(SDLOSXCAGuard));
icculus@1143
   171
    if (cag == NULL)
icculus@1143
   172
        return NULL;
slouken@1668
   173
    SDL_memset(cag, '\0', sizeof(*cag));
icculus@1143
   174
slouken@1662
   175
#define SET_SDLOSXCAGUARD_METHOD(m) cag->m = SDLOSXCAGuard_##m
slouken@1668
   176
    SET_SDLOSXCAGUARD_METHOD(Lock);
slouken@1668
   177
    SET_SDLOSXCAGUARD_METHOD(Unlock);
slouken@1668
   178
    SET_SDLOSXCAGUARD_METHOD(Try);
slouken@1668
   179
    SET_SDLOSXCAGUARD_METHOD(Wait);
slouken@1668
   180
    SET_SDLOSXCAGUARD_METHOD(Notify);
slouken@1662
   181
#undef SET_SDLOSXCAGUARD_METHOD
icculus@1143
   182
slouken@1668
   183
    theError = pthread_mutex_init(&cag->mMutex, NULL);
slouken@1662
   184
    (void) theError;
slouken@1668
   185
    assert(theError == 0);
slouken@1662
   186
slouken@1668
   187
    theError = pthread_cond_init(&cag->mCondVar, NULL);
slouken@1662
   188
    (void) theError;
slouken@1668
   189
    assert(theError == 0);
slouken@1662
   190
icculus@1143
   191
    cag->mOwner = 0;
icculus@1143
   192
    return cag;
icculus@1143
   193
}
icculus@1143
   194
slouken@1662
   195
void
slouken@1668
   196
delete_SDLOSXCAGuard(SDLOSXCAGuard * cag)
icculus@1143
   197
{
slouken@1662
   198
    if (cag != NULL) {
slouken@1668
   199
        pthread_mutex_destroy(&cag->mMutex);
slouken@1668
   200
        pthread_cond_destroy(&cag->mCondVar);
slouken@1668
   201
        SDL_free(cag);
icculus@1143
   202
    }
icculus@1143
   203
}
icculus@1143
   204
slouken@1662
   205
/* vi: set ts=4 sw=4 expandtab: */