include/SDL_mutex.h
author Sam Lantinga <slouken@libsdl.org>
Fri, 20 Aug 2004 22:32:05 +0000
changeset 932 66761191fc11
parent 769 b8d311d90021
child 1263 3bdcef7e1c90
permissions -rw-r--r--
Use the canonical glext.h on MacOS X as well (#define NO_SDL_GLEXT if you don't want this)
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@769
     3
    Copyright (C) 1997-2004 Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@0
     6
    modify it under the terms of the GNU Library General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@0
     8
    version 2 of the License, or (at your option) any later version.
slouken@0
     9
slouken@0
    10
    This library is distributed in the hope that it will be useful,
slouken@0
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@0
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@0
    13
    Library General Public License for more details.
slouken@0
    14
slouken@0
    15
    You should have received a copy of the GNU Library General Public
slouken@0
    16
    License along with this library; if not, write to the Free
slouken@0
    17
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@251
    20
    slouken@libsdl.org
slouken@0
    21
*/
slouken@0
    22
slouken@0
    23
#ifdef SAVE_RCSID
slouken@0
    24
static char rcsid =
slouken@0
    25
 "@(#) $Id$";
slouken@0
    26
#endif
slouken@0
    27
slouken@0
    28
#ifndef _SDL_mutex_h
slouken@0
    29
#define _SDL_mutex_h
slouken@0
    30
slouken@0
    31
/* Functions to provide thread synchronization primitives
slouken@0
    32
slouken@0
    33
	These are independent of the other SDL routines.
slouken@0
    34
*/
slouken@0
    35
slouken@0
    36
#include "SDL_main.h"
slouken@0
    37
#include "SDL_types.h"
slouken@0
    38
slouken@0
    39
#include "begin_code.h"
slouken@0
    40
/* Set up for C function definitions, even when using C++ */
slouken@0
    41
#ifdef __cplusplus
slouken@0
    42
extern "C" {
slouken@0
    43
#endif
slouken@0
    44
slouken@0
    45
/* Synchronization functions which can time out return this value
slouken@0
    46
   if they time out.
slouken@0
    47
*/
slouken@0
    48
#define SDL_MUTEX_TIMEDOUT	1
slouken@0
    49
slouken@0
    50
/* This is the timeout value which corresponds to never time out */
slouken@0
    51
#define SDL_MUTEX_MAXWAIT	(~(Uint32)0)
slouken@0
    52
slouken@0
    53
slouken@0
    54
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
slouken@0
    55
/* Mutex functions                                               */
slouken@0
    56
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
slouken@0
    57
slouken@0
    58
/* The SDL mutex structure, defined in SDL_mutex.c */
slouken@0
    59
struct SDL_mutex;
slouken@0
    60
typedef struct SDL_mutex SDL_mutex;
slouken@0
    61
slouken@0
    62
/* Create a mutex, initialized unlocked */
slouken@337
    63
extern DECLSPEC SDL_mutex * SDLCALL SDL_CreateMutex(void);
slouken@0
    64
slouken@0
    65
/* Lock the mutex  (Returns 0, or -1 on error) */
slouken@0
    66
#define SDL_LockMutex(m)	SDL_mutexP(m)
slouken@337
    67
extern DECLSPEC int SDLCALL SDL_mutexP(SDL_mutex *mutex);
slouken@0
    68
slouken@417
    69
/* Unlock the mutex  (Returns 0, or -1 on error)
slouken@417
    70
   It is an error to unlock a mutex that has not been locked by
slouken@417
    71
   the current thread, and doing so results in undefined behavior.
slouken@417
    72
 */
slouken@0
    73
#define SDL_UnlockMutex(m)	SDL_mutexV(m)
slouken@337
    74
extern DECLSPEC int SDLCALL SDL_mutexV(SDL_mutex *mutex);
slouken@0
    75
slouken@0
    76
/* Destroy a mutex */
slouken@337
    77
extern DECLSPEC void SDLCALL SDL_DestroyMutex(SDL_mutex *mutex);
slouken@0
    78
slouken@0
    79
slouken@0
    80
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
slouken@0
    81
/* Semaphore functions                                           */
slouken@0
    82
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
slouken@0
    83
slouken@0
    84
/* The SDL semaphore structure, defined in SDL_sem.c */
slouken@0
    85
struct SDL_semaphore;
slouken@0
    86
typedef struct SDL_semaphore SDL_sem;
slouken@0
    87
slouken@0
    88
/* Create a semaphore, initialized with value, returns NULL on failure. */
slouken@337
    89
extern DECLSPEC SDL_sem * SDLCALL SDL_CreateSemaphore(Uint32 initial_value);
slouken@0
    90
slouken@0
    91
/* Destroy a semaphore */
slouken@337
    92
extern DECLSPEC void SDLCALL SDL_DestroySemaphore(SDL_sem *sem);
slouken@0
    93
slouken@0
    94
/* This function suspends the calling thread until the semaphore pointed 
slouken@0
    95
 * to by sem has a positive count. It then atomically decreases the semaphore
slouken@0
    96
 * count.
slouken@0
    97
 */
slouken@337
    98
extern DECLSPEC int SDLCALL SDL_SemWait(SDL_sem *sem);
slouken@0
    99
slouken@0
   100
/* Non-blocking variant of SDL_SemWait(), returns 0 if the wait succeeds,
slouken@0
   101
   SDL_MUTEX_TIMEDOUT if the wait would block, and -1 on error.
slouken@0
   102
*/
slouken@337
   103
extern DECLSPEC int SDLCALL SDL_SemTryWait(SDL_sem *sem);
slouken@0
   104
slouken@0
   105
/* Variant of SDL_SemWait() with a timeout in milliseconds, returns 0 if
slouken@0
   106
   the wait succeeds, SDL_MUTEX_TIMEDOUT if the wait does not succeed in
slouken@0
   107
   the allotted time, and -1 on error.
slouken@0
   108
   On some platforms this function is implemented by looping with a delay
slouken@0
   109
   of 1 ms, and so should be avoided if possible.
slouken@0
   110
*/
slouken@337
   111
extern DECLSPEC int SDLCALL SDL_SemWaitTimeout(SDL_sem *sem, Uint32 ms);
slouken@0
   112
slouken@0
   113
/* Atomically increases the semaphore's count (not blocking), returns 0,
slouken@0
   114
   or -1 on error.
slouken@0
   115
 */
slouken@337
   116
extern DECLSPEC int SDLCALL SDL_SemPost(SDL_sem *sem);
slouken@0
   117
slouken@0
   118
/* Returns the current count of the semaphore */
slouken@337
   119
extern DECLSPEC Uint32 SDLCALL SDL_SemValue(SDL_sem *sem);
slouken@0
   120
slouken@0
   121
slouken@0
   122
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
slouken@0
   123
/* Condition variable functions                                  */
slouken@0
   124
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
slouken@0
   125
slouken@0
   126
/* The SDL condition variable structure, defined in SDL_cond.c */
slouken@0
   127
struct SDL_cond;
slouken@0
   128
typedef struct SDL_cond SDL_cond;
slouken@0
   129
slouken@0
   130
/* Create a condition variable */
slouken@337
   131
extern DECLSPEC SDL_cond * SDLCALL SDL_CreateCond(void);
slouken@0
   132
slouken@0
   133
/* Destroy a condition variable */
slouken@337
   134
extern DECLSPEC void SDLCALL SDL_DestroyCond(SDL_cond *cond);
slouken@0
   135
slouken@0
   136
/* Restart one of the threads that are waiting on the condition variable,
slouken@0
   137
   returns 0 or -1 on error.
slouken@0
   138
 */
slouken@337
   139
extern DECLSPEC int SDLCALL SDL_CondSignal(SDL_cond *cond);
slouken@0
   140
slouken@0
   141
/* Restart all threads that are waiting on the condition variable,
slouken@0
   142
   returns 0 or -1 on error.
slouken@0
   143
 */
slouken@337
   144
extern DECLSPEC int SDLCALL SDL_CondBroadcast(SDL_cond *cond);
slouken@0
   145
slouken@0
   146
/* Wait on the condition variable, unlocking the provided mutex.
slouken@0
   147
   The mutex must be locked before entering this function!
slouken@0
   148
   Returns 0 when it is signaled, or -1 on error.
slouken@0
   149
 */
slouken@337
   150
extern DECLSPEC int SDLCALL SDL_CondWait(SDL_cond *cond, SDL_mutex *mut);
slouken@0
   151
slouken@0
   152
/* Waits for at most 'ms' milliseconds, and returns 0 if the condition
slouken@0
   153
   variable is signaled, SDL_MUTEX_TIMEDOUT if the condition is not
slouken@0
   154
   signaled in the allotted time, and -1 on error.
slouken@0
   155
   On some platforms this function is implemented by looping with a delay
slouken@0
   156
   of 1 ms, and so should be avoided if possible.
slouken@0
   157
*/
slouken@337
   158
extern DECLSPEC int SDLCALL SDL_CondWaitTimeout(SDL_cond *cond, SDL_mutex *mutex, Uint32 ms);
slouken@0
   159
slouken@0
   160
/* Ends C function definitions when using C++ */
slouken@0
   161
#ifdef __cplusplus
slouken@0
   162
}
slouken@0
   163
#endif
slouken@0
   164
#include "close_code.h"
slouken@0
   165
slouken@0
   166
#endif /* _SDL_mutex_h */