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
289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Copyright:  � Copyright 2002 Apple Computer, Inc. All rights reserved.
299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Disclaimer: IMPORTANT:  This Apple software is supplied to you by Apple Computer, Inc.
319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            ("Apple") in consideration of your agreement to the following terms, and your
329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            use, installation, modification or redistribution of this Apple software
339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            constitutes acceptance of these terms.  If you do not agree with these terms,
349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            please do not use, install, modify or redistribute this Apple software.
359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            In consideration of your agreement to abide by the following terms, and subject
379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            to these terms, Apple grants you a personal, non-exclusive license, under Apple�s
389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            copyrights in this original Apple software (the "Apple Software"), to use,
399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            reproduce, modify and redistribute the Apple Software, with or without
409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            modifications, in source and/or binary forms; provided that if you redistribute
419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            the Apple Software in its entirety and without modifications, you must retain
429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            this notice and the following text and disclaimers in all such redistributions of
439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            the Apple Software.  Neither the name, trademarks, service marks or logos of
449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            Apple Computer, Inc. may be used to endorse or promote products derived from the
459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            Apple Software without specific prior written permission from Apple.  Except as
469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            expressly stated in this notice, no other rights or licenses, express or implied,
479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            are granted by Apple herein, including but not limited to any patent rights that
489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            may be infringed by your derivative works or by other works in which the Apple
499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            Software may be incorporated.
509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            The Apple Software is provided by Apple on an "AS IS" basis.  APPLE MAKES NO
529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            COMBINATION WITH YOUR PRODUCTS.
569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/
659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*=============================================================================
669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    CAGuard.h
679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall=============================================================================*/
699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if !defined(__CAGuard_h__)
709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define __CAGuard_h__
719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*=============================================================================
739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Includes
749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  =============================================================================*/
759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <CoreAudio/CoreAudioTypes.h>
779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <pthread.h>
789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*=============================================================================
819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    CAGuard
829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    This is your typical mutex with signalling implemented via pthreads.
849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Lock() will return true if and only if the guard is locked on that call.
859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    A thread that already has the guard will receive 'false' if it locks it
869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    again. Use of the stack-based CAGuard::Locker class is highly recommended
879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    to properly manage the recursive nesting. The Wait calls with timeouts
889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    will return true if and only if the timeout period expired. They will
899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return false if they receive notification any other way.
909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  =============================================================================*/
919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Halltypedef struct S_SDLOSXCAGuard
939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*  Construction/Destruction */
969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*public:*/
979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*  Actions */
989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*public:*/
999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int     (*Lock)(struct S_SDLOSXCAGuard *cag);
1009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    void    (*Unlock)(struct S_SDLOSXCAGuard *cag);
1019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int     (*Try)(struct S_SDLOSXCAGuard *cag, int *outWasLocked);    /* returns true if lock is free, false if not */
1029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    void    (*Wait)(struct S_SDLOSXCAGuard *cag);
1039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    void    (*Notify)(struct S_SDLOSXCAGuard *cag);
1049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*  Implementation */
1069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*protected:*/
1079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    pthread_mutex_t mMutex;
1089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    pthread_cond_t  mCondVar;
1099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    pthread_t       mOwner;
1109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} SDLOSXCAGuard;
1119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallSDLOSXCAGuard *new_SDLOSXCAGuard(void);
1139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid delete_SDLOSXCAGuard(SDLOSXCAGuard *cag);
1149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
1169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
117