include/SDL_mutex.h
author Sam Lantinga <slouken@libsdl.org>
Mon, 08 Dec 2008 00:25:42 +0000
branchSDL-1.2
changeset 4159 a1b03ba2fcd0
parent 1358 c71e05b4dc2e
child 4217 4c4113c2162c
permissions -rw-r--r--
Updated copyright date
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@4159
     3
    Copyright (C) 1997-2009 Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@1312
     6
    modify it under the terms of the GNU Lesser General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@1312
     8
    version 2.1 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@1312
    13
    Lesser General Public License for more details.
slouken@0
    14
slouken@1312
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1312
    16
    License along with this library; if not, write to the Free Software
slouken@1312
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@251
    20
    slouken@libsdl.org
slouken@0
    21
*/
slouken@0
    22
slouken@0
    23
#ifndef _SDL_mutex_h
slouken@0
    24
#define _SDL_mutex_h
slouken@0
    25
slouken@0
    26
/* Functions to provide thread synchronization primitives
slouken@0
    27
slouken@0
    28
	These are independent of the other SDL routines.
slouken@0
    29
*/
slouken@0
    30
slouken@1356
    31
#include "SDL_stdinc.h"
slouken@1358
    32
#include "SDL_error.h"
slouken@0
    33
slouken@0
    34
#include "begin_code.h"
slouken@0
    35
/* Set up for C function definitions, even when using C++ */
slouken@0
    36
#ifdef __cplusplus
slouken@0
    37
extern "C" {
slouken@0
    38
#endif
slouken@0
    39
slouken@0
    40
/* Synchronization functions which can time out return this value
slouken@0
    41
   if they time out.
slouken@0
    42
*/
slouken@0
    43
#define SDL_MUTEX_TIMEDOUT	1
slouken@0
    44
slouken@0
    45
/* This is the timeout value which corresponds to never time out */
slouken@0
    46
#define SDL_MUTEX_MAXWAIT	(~(Uint32)0)
slouken@0
    47
slouken@0
    48
slouken@0
    49
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
slouken@0
    50
/* Mutex functions                                               */
slouken@0
    51
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
slouken@0
    52
slouken@0
    53
/* The SDL mutex structure, defined in SDL_mutex.c */
slouken@0
    54
struct SDL_mutex;
slouken@0
    55
typedef struct SDL_mutex SDL_mutex;
slouken@0
    56
slouken@0
    57
/* Create a mutex, initialized unlocked */
slouken@337
    58
extern DECLSPEC SDL_mutex * SDLCALL SDL_CreateMutex(void);
slouken@0
    59
slouken@0
    60
/* Lock the mutex  (Returns 0, or -1 on error) */
slouken@0
    61
#define SDL_LockMutex(m)	SDL_mutexP(m)
slouken@337
    62
extern DECLSPEC int SDLCALL SDL_mutexP(SDL_mutex *mutex);
slouken@0
    63
slouken@417
    64
/* Unlock the mutex  (Returns 0, or -1 on error)
slouken@417
    65
   It is an error to unlock a mutex that has not been locked by
slouken@417
    66
   the current thread, and doing so results in undefined behavior.
slouken@417
    67
 */
slouken@0
    68
#define SDL_UnlockMutex(m)	SDL_mutexV(m)
slouken@337
    69
extern DECLSPEC int SDLCALL SDL_mutexV(SDL_mutex *mutex);
slouken@0
    70
slouken@0
    71
/* Destroy a mutex */
slouken@337
    72
extern DECLSPEC void SDLCALL SDL_DestroyMutex(SDL_mutex *mutex);
slouken@0
    73
slouken@0
    74
slouken@0
    75
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
slouken@0
    76
/* Semaphore functions                                           */
slouken@0
    77
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
slouken@0
    78
slouken@0
    79
/* The SDL semaphore structure, defined in SDL_sem.c */
slouken@0
    80
struct SDL_semaphore;
slouken@0
    81
typedef struct SDL_semaphore SDL_sem;
slouken@0
    82
slouken@0
    83
/* Create a semaphore, initialized with value, returns NULL on failure. */
slouken@337
    84
extern DECLSPEC SDL_sem * SDLCALL SDL_CreateSemaphore(Uint32 initial_value);
slouken@0
    85
slouken@0
    86
/* Destroy a semaphore */
slouken@337
    87
extern DECLSPEC void SDLCALL SDL_DestroySemaphore(SDL_sem *sem);
slouken@0
    88
slouken@0
    89
/* This function suspends the calling thread until the semaphore pointed 
slouken@0
    90
 * to by sem has a positive count. It then atomically decreases the semaphore
slouken@0
    91
 * count.
slouken@0
    92
 */
slouken@337
    93
extern DECLSPEC int SDLCALL SDL_SemWait(SDL_sem *sem);
slouken@0
    94
slouken@0
    95
/* Non-blocking variant of SDL_SemWait(), returns 0 if the wait succeeds,
slouken@0
    96
   SDL_MUTEX_TIMEDOUT if the wait would block, and -1 on error.
slouken@0
    97
*/
slouken@337
    98
extern DECLSPEC int SDLCALL SDL_SemTryWait(SDL_sem *sem);
slouken@0
    99
slouken@0
   100
/* Variant of SDL_SemWait() with a timeout in milliseconds, returns 0 if
slouken@0
   101
   the wait succeeds, SDL_MUTEX_TIMEDOUT if the wait does not succeed in
slouken@0
   102
   the allotted time, and -1 on error.
slouken@0
   103
   On some platforms this function is implemented by looping with a delay
slouken@0
   104
   of 1 ms, and so should be avoided if possible.
slouken@0
   105
*/
slouken@337
   106
extern DECLSPEC int SDLCALL SDL_SemWaitTimeout(SDL_sem *sem, Uint32 ms);
slouken@0
   107
slouken@0
   108
/* Atomically increases the semaphore's count (not blocking), returns 0,
slouken@0
   109
   or -1 on error.
slouken@0
   110
 */
slouken@337
   111
extern DECLSPEC int SDLCALL SDL_SemPost(SDL_sem *sem);
slouken@0
   112
slouken@0
   113
/* Returns the current count of the semaphore */
slouken@337
   114
extern DECLSPEC Uint32 SDLCALL SDL_SemValue(SDL_sem *sem);
slouken@0
   115
slouken@0
   116
slouken@0
   117
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
slouken@0
   118
/* Condition variable functions                                  */
slouken@0
   119
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
slouken@0
   120
slouken@0
   121
/* The SDL condition variable structure, defined in SDL_cond.c */
slouken@0
   122
struct SDL_cond;
slouken@0
   123
typedef struct SDL_cond SDL_cond;
slouken@0
   124
slouken@0
   125
/* Create a condition variable */
slouken@337
   126
extern DECLSPEC SDL_cond * SDLCALL SDL_CreateCond(void);
slouken@0
   127
slouken@0
   128
/* Destroy a condition variable */
slouken@337
   129
extern DECLSPEC void SDLCALL SDL_DestroyCond(SDL_cond *cond);
slouken@0
   130
slouken@0
   131
/* Restart one of the threads that are waiting on the condition variable,
slouken@0
   132
   returns 0 or -1 on error.
slouken@0
   133
 */
slouken@337
   134
extern DECLSPEC int SDLCALL SDL_CondSignal(SDL_cond *cond);
slouken@0
   135
slouken@0
   136
/* Restart all 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_CondBroadcast(SDL_cond *cond);
slouken@0
   140
slouken@0
   141
/* Wait on the condition variable, unlocking the provided mutex.
slouken@0
   142
   The mutex must be locked before entering this function!
slouken@1263
   143
   The mutex is re-locked once the condition variable is signaled.
slouken@0
   144
   Returns 0 when it is signaled, or -1 on error.
slouken@0
   145
 */
slouken@337
   146
extern DECLSPEC int SDLCALL SDL_CondWait(SDL_cond *cond, SDL_mutex *mut);
slouken@0
   147
slouken@0
   148
/* Waits for at most 'ms' milliseconds, and returns 0 if the condition
slouken@0
   149
   variable is signaled, SDL_MUTEX_TIMEDOUT if the condition is not
slouken@0
   150
   signaled in the allotted time, and -1 on error.
slouken@0
   151
   On some platforms this function is implemented by looping with a delay
slouken@0
   152
   of 1 ms, and so should be avoided if possible.
slouken@0
   153
*/
slouken@337
   154
extern DECLSPEC int SDLCALL SDL_CondWaitTimeout(SDL_cond *cond, SDL_mutex *mutex, Uint32 ms);
slouken@0
   155
slouken@0
   156
/* Ends C function definitions when using C++ */
slouken@0
   157
#ifdef __cplusplus
slouken@0
   158
}
slouken@0
   159
#endif
slouken@0
   160
#include "close_code.h"
slouken@0
   161
slouken@0
   162
#endif /* _SDL_mutex_h */