include/SDL_mutex.h
author Edward Rudd <urkle@outoforder.cc>
Tue, 05 Mar 2013 18:54:55 -0500
changeset 6966 090df160f6e0
parent 6885 700f1b25f77f
child 6977 b73d51026c68
permissions -rw-r--r--
Add SDL_TryLockMutex and implementations for all platforms
slouken@0
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@6885
     3
  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
slouken@0
     4
slouken@5535
     5
  This software is provided 'as-is', without any express or implied
slouken@5535
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     7
  arising from the use of this software.
slouken@0
     8
slouken@5535
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
    10
  including commercial applications, and to alter it and redistribute it
slouken@5535
    11
  freely, subject to the following restrictions:
slouken@0
    12
slouken@5535
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@5535
    14
     claim that you wrote the original software. If you use this software
slouken@5535
    15
     in a product, an acknowledgment in the product documentation would be
slouken@5535
    16
     appreciated but is not required.
slouken@5535
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@5535
    18
     misrepresented as being the original software.
slouken@5535
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@0
    20
*/
slouken@0
    21
slouken@0
    22
#ifndef _SDL_mutex_h
slouken@0
    23
#define _SDL_mutex_h
slouken@0
    24
slouken@1895
    25
/**
slouken@3407
    26
 *  \file SDL_mutex.h
slouken@3407
    27
 *  
slouken@3407
    28
 *  Functions to provide thread synchronization primitives.
slouken@1895
    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@1895
    37
/* *INDENT-OFF* */
slouken@0
    38
extern "C" {
slouken@1895
    39
/* *INDENT-ON* */
slouken@0
    40
#endif
slouken@0
    41
slouken@3407
    42
/**
slouken@3407
    43
 *  Synchronization functions which can time out return this value
slouken@3407
    44
 *  if they time out.
slouken@3407
    45
 */
slouken@0
    46
#define SDL_MUTEX_TIMEDOUT	1
slouken@0
    47
slouken@3407
    48
/**
slouken@3407
    49
 *  This is the timeout value which corresponds to never time out.
slouken@3407
    50
 */
slouken@0
    51
#define SDL_MUTEX_MAXWAIT	(~(Uint32)0)
slouken@0
    52
slouken@0
    53
slouken@3407
    54
/**
slouken@3407
    55
 *  \name Mutex functions
slouken@3407
    56
 */
slouken@3407
    57
/*@{*/
slouken@0
    58
slouken@0
    59
/* The SDL mutex structure, defined in SDL_mutex.c */
slouken@0
    60
struct SDL_mutex;
slouken@0
    61
typedef struct SDL_mutex SDL_mutex;
slouken@0
    62
slouken@3407
    63
/**
slouken@3407
    64
 *  Create a mutex, initialized unlocked.
slouken@3407
    65
 */
slouken@1895
    66
extern DECLSPEC SDL_mutex *SDLCALL SDL_CreateMutex(void);
slouken@0
    67
slouken@3407
    68
/**
slouken@3407
    69
 *  Lock the mutex.
slouken@3407
    70
 *  
slouken@3407
    71
 *  \return 0, or -1 on error.
slouken@3407
    72
 */
slouken@0
    73
#define SDL_LockMutex(m)	SDL_mutexP(m)
slouken@1895
    74
extern DECLSPEC int SDLCALL SDL_mutexP(SDL_mutex * mutex);
slouken@0
    75
slouken@3407
    76
/**
urkle@6966
    77
 *  Try to lock the mutex
urkle@6966
    78
 *  
urkle@6966
    79
 *  \return 0, SDL_MUTEX_TIMEDOUT, or -1 on error
urkle@6966
    80
 */
urkle@6966
    81
extern DECLSPEC int SDLCALL SDL_TryLockMutex(SDL_mutex * mutex);
urkle@6966
    82
urkle@6966
    83
/**
slouken@3407
    84
 *  Unlock the mutex.
slouken@3407
    85
 *  
slouken@3407
    86
 *  \return 0, or -1 on error.
slouken@3407
    87
 *  
slouken@3407
    88
 *  \warning It is an error to unlock a mutex that has not been locked by
slouken@3407
    89
 *           the current thread, and doing so results in undefined behavior.
slouken@417
    90
 */
slouken@0
    91
#define SDL_UnlockMutex(m)	SDL_mutexV(m)
slouken@1895
    92
extern DECLSPEC int SDLCALL SDL_mutexV(SDL_mutex * mutex);
slouken@0
    93
slouken@3407
    94
/** 
slouken@3407
    95
 *  Destroy a mutex.
slouken@3407
    96
 */
slouken@1895
    97
extern DECLSPEC void SDLCALL SDL_DestroyMutex(SDL_mutex * mutex);
slouken@0
    98
slouken@3407
    99
/*@}*//*Mutex functions*/
slouken@0
   100
slouken@3407
   101
slouken@3407
   102
/**
slouken@3407
   103
 *  \name Semaphore functions
slouken@3407
   104
 */
slouken@3407
   105
/*@{*/
slouken@0
   106
slouken@0
   107
/* The SDL semaphore structure, defined in SDL_sem.c */
slouken@0
   108
struct SDL_semaphore;
slouken@0
   109
typedef struct SDL_semaphore SDL_sem;
slouken@0
   110
slouken@3407
   111
/**
slouken@3407
   112
 *  Create a semaphore, initialized with value, returns NULL on failure.
slouken@3407
   113
 */
slouken@1895
   114
extern DECLSPEC SDL_sem *SDLCALL SDL_CreateSemaphore(Uint32 initial_value);
slouken@0
   115
slouken@3407
   116
/**
slouken@3407
   117
 *  Destroy a semaphore.
slouken@3407
   118
 */
slouken@1895
   119
extern DECLSPEC void SDLCALL SDL_DestroySemaphore(SDL_sem * sem);
slouken@0
   120
slouken@3407
   121
/**
slouken@3407
   122
 *  This function suspends the calling thread until the semaphore pointed 
slouken@3407
   123
 *  to by \c sem has a positive count. It then atomically decreases the 
slouken@3407
   124
 *  semaphore count.
slouken@0
   125
 */
slouken@1895
   126
extern DECLSPEC int SDLCALL SDL_SemWait(SDL_sem * sem);
slouken@0
   127
slouken@3407
   128
/**
slouken@3407
   129
 *  Non-blocking variant of SDL_SemWait().
slouken@3407
   130
 *  
slouken@3407
   131
 *  \return 0 if the wait succeeds, ::SDL_MUTEX_TIMEDOUT if the wait would 
slouken@3407
   132
 *          block, and -1 on error.
slouken@3407
   133
 */
slouken@1895
   134
extern DECLSPEC int SDLCALL SDL_SemTryWait(SDL_sem * sem);
slouken@0
   135
slouken@3407
   136
/**
slouken@3407
   137
 *  Variant of SDL_SemWait() with a timeout in milliseconds.
slouken@3407
   138
 *  
slouken@3407
   139
 *  \return 0 if the wait succeeds, ::SDL_MUTEX_TIMEDOUT if the wait does not 
slouken@3407
   140
 *          succeed in the allotted time, and -1 on error.
slouken@3407
   141
 *  
slouken@3407
   142
 *  \warning On some platforms this function is implemented by looping with a 
slouken@3407
   143
 *           delay of 1 ms, and so should be avoided if possible.
slouken@3407
   144
 */
slouken@1895
   145
extern DECLSPEC int SDLCALL SDL_SemWaitTimeout(SDL_sem * sem, Uint32 ms);
slouken@0
   146
slouken@3407
   147
/**
slouken@3407
   148
 *  Atomically increases the semaphore's count (not blocking).
slouken@3407
   149
 *  
slouken@3407
   150
 *  \return 0, or -1 on error.
slouken@0
   151
 */
slouken@1895
   152
extern DECLSPEC int SDLCALL SDL_SemPost(SDL_sem * sem);
slouken@0
   153
slouken@3407
   154
/**
slouken@3407
   155
 *  Returns the current count of the semaphore.
slouken@3407
   156
 */
slouken@1895
   157
extern DECLSPEC Uint32 SDLCALL SDL_SemValue(SDL_sem * sem);
slouken@0
   158
slouken@3407
   159
/*@}*//*Semaphore functions*/
slouken@0
   160
slouken@3407
   161
slouken@3407
   162
/**
slouken@3407
   163
 *  \name Condition variable functions
slouken@3407
   164
 */
slouken@3407
   165
/*@{*/
slouken@0
   166
slouken@0
   167
/* The SDL condition variable structure, defined in SDL_cond.c */
slouken@0
   168
struct SDL_cond;
slouken@0
   169
typedef struct SDL_cond SDL_cond;
slouken@0
   170
slouken@3407
   171
/**
slouken@3407
   172
 *  Create a condition variable.
slouken@5110
   173
 *
slouken@5110
   174
 *  Typical use of condition variables:
slouken@5110
   175
 *
slouken@5110
   176
 *  Thread A:
slouken@5110
   177
 *    SDL_LockMutex(lock);
slouken@5110
   178
 *    while ( ! condition ) {
slouken@5110
   179
 *        SDL_CondWait(cond, lock);
slouken@5110
   180
 *    }
slouken@5110
   181
 *    SDL_UnlockMutex(lock);
slouken@5110
   182
 *
slouken@5110
   183
 *  Thread B:
slouken@5110
   184
 *    SDL_LockMutex(lock);
slouken@5110
   185
 *    ...
slouken@5110
   186
 *    condition = true;
slouken@5110
   187
 *    ...
slouken@5110
   188
 *    SDL_CondSignal(cond);
slouken@5110
   189
 *    SDL_UnlockMutex(lock);
slouken@5110
   190
 *
slouken@5110
   191
 *  There is some discussion whether to signal the condition variable
slouken@5110
   192
 *  with the mutex locked or not.  There is some potential performance
slouken@5110
   193
 *  benefit to unlocking first on some platforms, but there are some
slouken@5110
   194
 *  potential race conditions depending on how your code is structured.
slouken@5110
   195
 *
slouken@5110
   196
 *  In general it's safer to signal the condition variable while the
slouken@5110
   197
 *  mutex is locked.
slouken@3407
   198
 */
slouken@1895
   199
extern DECLSPEC SDL_cond *SDLCALL SDL_CreateCond(void);
slouken@0
   200
slouken@3407
   201
/**
slouken@3407
   202
 *  Destroy a condition variable.
slouken@3407
   203
 */
slouken@1895
   204
extern DECLSPEC void SDLCALL SDL_DestroyCond(SDL_cond * cond);
slouken@0
   205
slouken@3407
   206
/**
slouken@3407
   207
 *  Restart one of the threads that are waiting on the condition variable.
slouken@3407
   208
 *  
slouken@3407
   209
 *  \return 0 or -1 on error.
slouken@0
   210
 */
slouken@1895
   211
extern DECLSPEC int SDLCALL SDL_CondSignal(SDL_cond * cond);
slouken@0
   212
slouken@3407
   213
/**
slouken@3407
   214
 *  Restart all threads that are waiting on the condition variable.
slouken@5110
   215
 *
slouken@3407
   216
 *  \return 0 or -1 on error.
slouken@0
   217
 */
slouken@1895
   218
extern DECLSPEC int SDLCALL SDL_CondBroadcast(SDL_cond * cond);
slouken@0
   219
slouken@3407
   220
/**
slouken@3407
   221
 *  Wait on the condition variable, unlocking the provided mutex.
slouken@3407
   222
 *  
slouken@3407
   223
 *  \warning The mutex must be locked before entering this function!
slouken@3407
   224
 *  
slouken@3407
   225
 *  The mutex is re-locked once the condition variable is signaled.
slouken@3407
   226
 *  
slouken@3407
   227
 *  \return 0 when it is signaled, or -1 on error.
slouken@0
   228
 */
slouken@4469
   229
extern DECLSPEC int SDLCALL SDL_CondWait(SDL_cond * cond, SDL_mutex * mutex);
slouken@0
   230
slouken@3407
   231
/**
slouken@3407
   232
 *  Waits for at most \c ms milliseconds, and returns 0 if the condition
slouken@3407
   233
 *  variable is signaled, ::SDL_MUTEX_TIMEDOUT if the condition is not
slouken@3407
   234
 *  signaled in the allotted time, and -1 on error.
slouken@3407
   235
 *
slouken@3407
   236
 *  \warning On some platforms this function is implemented by looping with a 
slouken@3407
   237
 *           delay of 1 ms, and so should be avoided if possible.
slouken@3407
   238
 */
slouken@1895
   239
extern DECLSPEC int SDLCALL SDL_CondWaitTimeout(SDL_cond * cond,
slouken@1895
   240
                                                SDL_mutex * mutex, Uint32 ms);
slouken@0
   241
slouken@3407
   242
/*@}*//*Condition variable functions*/
slouken@3407
   243
slouken@3407
   244
slouken@0
   245
/* Ends C function definitions when using C++ */
slouken@0
   246
#ifdef __cplusplus
slouken@1895
   247
/* *INDENT-OFF* */
slouken@0
   248
}
slouken@1895
   249
/* *INDENT-ON* */
slouken@0
   250
#endif
slouken@0
   251
#include "close_code.h"
slouken@0
   252
slouken@0
   253
#endif /* _SDL_mutex_h */
slouken@1895
   254
slouken@1895
   255
/* vi: set ts=4 sw=4 expandtab: */