include/SDL_mutex.h
author Sam Lantinga <slouken@libsdl.org>
Fri, 10 Feb 2006 03:19:02 +0000
changeset 1356 67114343400d
parent 1312 c9b51268668f
child 1358 c71e05b4dc2e
permissions -rw-r--r--
*** empty log message ***
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@1312
     3
    Copyright (C) 1997-2006 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@0
    32
slouken@0
    33
#include "begin_code.h"
slouken@0
    34
/* Set up for C function definitions, even when using C++ */
slouken@0
    35
#ifdef __cplusplus
slouken@0
    36
extern "C" {
slouken@0
    37
#endif
slouken@0
    38
slouken@0
    39
/* Synchronization functions which can time out return this value
slouken@0
    40
   if they time out.
slouken@0
    41
*/
slouken@0
    42
#define SDL_MUTEX_TIMEDOUT	1
slouken@0
    43
slouken@0
    44
/* This is the timeout value which corresponds to never time out */
slouken@0
    45
#define SDL_MUTEX_MAXWAIT	(~(Uint32)0)
slouken@0
    46
slouken@0
    47
slouken@0
    48
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
slouken@0
    49
/* Mutex functions                                               */
slouken@0
    50
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
slouken@0
    51
slouken@0
    52
/* The SDL mutex structure, defined in SDL_mutex.c */
slouken@0
    53
struct SDL_mutex;
slouken@0
    54
typedef struct SDL_mutex SDL_mutex;
slouken@0
    55
slouken@0
    56
/* Create a mutex, initialized unlocked */
slouken@337
    57
extern DECLSPEC SDL_mutex * SDLCALL SDL_CreateMutex(void);
slouken@0
    58
slouken@0
    59
/* Lock the mutex  (Returns 0, or -1 on error) */
slouken@0
    60
#define SDL_LockMutex(m)	SDL_mutexP(m)
slouken@337
    61
extern DECLSPEC int SDLCALL SDL_mutexP(SDL_mutex *mutex);
slouken@0
    62
slouken@417
    63
/* Unlock the mutex  (Returns 0, or -1 on error)
slouken@417
    64
   It is an error to unlock a mutex that has not been locked by
slouken@417
    65
   the current thread, and doing so results in undefined behavior.
slouken@417
    66
 */
slouken@0
    67
#define SDL_UnlockMutex(m)	SDL_mutexV(m)
slouken@337
    68
extern DECLSPEC int SDLCALL SDL_mutexV(SDL_mutex *mutex);
slouken@0
    69
slouken@0
    70
/* Destroy a mutex */
slouken@337
    71
extern DECLSPEC void SDLCALL SDL_DestroyMutex(SDL_mutex *mutex);
slouken@0
    72
slouken@0
    73
slouken@0
    74
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
slouken@0
    75
/* Semaphore functions                                           */
slouken@0
    76
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
slouken@0
    77
slouken@0
    78
/* The SDL semaphore structure, defined in SDL_sem.c */
slouken@0
    79
struct SDL_semaphore;
slouken@0
    80
typedef struct SDL_semaphore SDL_sem;
slouken@0
    81
slouken@0
    82
/* Create a semaphore, initialized with value, returns NULL on failure. */
slouken@337
    83
extern DECLSPEC SDL_sem * SDLCALL SDL_CreateSemaphore(Uint32 initial_value);
slouken@0
    84
slouken@0
    85
/* Destroy a semaphore */
slouken@337
    86
extern DECLSPEC void SDLCALL SDL_DestroySemaphore(SDL_sem *sem);
slouken@0
    87
slouken@0
    88
/* This function suspends the calling thread until the semaphore pointed 
slouken@0
    89
 * to by sem has a positive count. It then atomically decreases the semaphore
slouken@0
    90
 * count.
slouken@0
    91
 */
slouken@337
    92
extern DECLSPEC int SDLCALL SDL_SemWait(SDL_sem *sem);
slouken@0
    93
slouken@0
    94
/* Non-blocking variant of SDL_SemWait(), returns 0 if the wait succeeds,
slouken@0
    95
   SDL_MUTEX_TIMEDOUT if the wait would block, and -1 on error.
slouken@0
    96
*/
slouken@337
    97
extern DECLSPEC int SDLCALL SDL_SemTryWait(SDL_sem *sem);
slouken@0
    98
slouken@0
    99
/* Variant of SDL_SemWait() with a timeout in milliseconds, returns 0 if
slouken@0
   100
   the wait succeeds, SDL_MUTEX_TIMEDOUT if the wait does not succeed in
slouken@0
   101
   the allotted time, and -1 on error.
slouken@0
   102
   On some platforms this function is implemented by looping with a delay
slouken@0
   103
   of 1 ms, and so should be avoided if possible.
slouken@0
   104
*/
slouken@337
   105
extern DECLSPEC int SDLCALL SDL_SemWaitTimeout(SDL_sem *sem, Uint32 ms);
slouken@0
   106
slouken@0
   107
/* Atomically increases the semaphore's count (not blocking), returns 0,
slouken@0
   108
   or -1 on error.
slouken@0
   109
 */
slouken@337
   110
extern DECLSPEC int SDLCALL SDL_SemPost(SDL_sem *sem);
slouken@0
   111
slouken@0
   112
/* Returns the current count of the semaphore */
slouken@337
   113
extern DECLSPEC Uint32 SDLCALL SDL_SemValue(SDL_sem *sem);
slouken@0
   114
slouken@0
   115
slouken@0
   116
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
slouken@0
   117
/* Condition variable functions                                  */
slouken@0
   118
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
slouken@0
   119
slouken@0
   120
/* The SDL condition variable structure, defined in SDL_cond.c */
slouken@0
   121
struct SDL_cond;
slouken@0
   122
typedef struct SDL_cond SDL_cond;
slouken@0
   123
slouken@0
   124
/* Create a condition variable */
slouken@337
   125
extern DECLSPEC SDL_cond * SDLCALL SDL_CreateCond(void);
slouken@0
   126
slouken@0
   127
/* Destroy a condition variable */
slouken@337
   128
extern DECLSPEC void SDLCALL SDL_DestroyCond(SDL_cond *cond);
slouken@0
   129
slouken@0
   130
/* Restart one of the threads that are waiting on the condition variable,
slouken@0
   131
   returns 0 or -1 on error.
slouken@0
   132
 */
slouken@337
   133
extern DECLSPEC int SDLCALL SDL_CondSignal(SDL_cond *cond);
slouken@0
   134
slouken@0
   135
/* Restart all threads that are waiting on the condition variable,
slouken@0
   136
   returns 0 or -1 on error.
slouken@0
   137
 */
slouken@337
   138
extern DECLSPEC int SDLCALL SDL_CondBroadcast(SDL_cond *cond);
slouken@0
   139
slouken@0
   140
/* Wait on the condition variable, unlocking the provided mutex.
slouken@0
   141
   The mutex must be locked before entering this function!
slouken@1263
   142
   The mutex is re-locked once the condition variable is signaled.
slouken@0
   143
   Returns 0 when it is signaled, or -1 on error.
slouken@0
   144
 */
slouken@337
   145
extern DECLSPEC int SDLCALL SDL_CondWait(SDL_cond *cond, SDL_mutex *mut);
slouken@0
   146
slouken@0
   147
/* Waits for at most 'ms' milliseconds, and returns 0 if the condition
slouken@0
   148
   variable is signaled, SDL_MUTEX_TIMEDOUT if the condition is not
slouken@0
   149
   signaled in the allotted time, and -1 on error.
slouken@0
   150
   On some platforms this function is implemented by looping with a delay
slouken@0
   151
   of 1 ms, and so should be avoided if possible.
slouken@0
   152
*/
slouken@337
   153
extern DECLSPEC int SDLCALL SDL_CondWaitTimeout(SDL_cond *cond, SDL_mutex *mutex, Uint32 ms);
slouken@0
   154
slouken@0
   155
/* Ends C function definitions when using C++ */
slouken@0
   156
#ifdef __cplusplus
slouken@0
   157
}
slouken@0
   158
#endif
slouken@0
   159
#include "close_code.h"
slouken@0
   160
slouken@0
   161
#endif /* _SDL_mutex_h */