include/SDL_mutex.h
author Sam Lantinga <slouken@libsdl.org>
Fri, 15 Feb 2013 08:47:44 -0800
changeset 6885 700f1b25f77f
parent 6138 4c64952a58fb
child 6966 090df160f6e0
permissions -rw-r--r--
Happy New Year!
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
/**
slouken@3407
    77
 *  Unlock the mutex.
slouken@3407
    78
 *  
slouken@3407
    79
 *  \return 0, or -1 on error.
slouken@3407
    80
 *  
slouken@3407
    81
 *  \warning It is an error to unlock a mutex that has not been locked by
slouken@3407
    82
 *           the current thread, and doing so results in undefined behavior.
slouken@417
    83
 */
slouken@0
    84
#define SDL_UnlockMutex(m)	SDL_mutexV(m)
slouken@1895
    85
extern DECLSPEC int SDLCALL SDL_mutexV(SDL_mutex * mutex);
slouken@0
    86
slouken@3407
    87
/** 
slouken@3407
    88
 *  Destroy a mutex.
slouken@3407
    89
 */
slouken@1895
    90
extern DECLSPEC void SDLCALL SDL_DestroyMutex(SDL_mutex * mutex);
slouken@0
    91
slouken@3407
    92
/*@}*//*Mutex functions*/
slouken@0
    93
slouken@3407
    94
slouken@3407
    95
/**
slouken@3407
    96
 *  \name Semaphore functions
slouken@3407
    97
 */
slouken@3407
    98
/*@{*/
slouken@0
    99
slouken@0
   100
/* The SDL semaphore structure, defined in SDL_sem.c */
slouken@0
   101
struct SDL_semaphore;
slouken@0
   102
typedef struct SDL_semaphore SDL_sem;
slouken@0
   103
slouken@3407
   104
/**
slouken@3407
   105
 *  Create a semaphore, initialized with value, returns NULL on failure.
slouken@3407
   106
 */
slouken@1895
   107
extern DECLSPEC SDL_sem *SDLCALL SDL_CreateSemaphore(Uint32 initial_value);
slouken@0
   108
slouken@3407
   109
/**
slouken@3407
   110
 *  Destroy a semaphore.
slouken@3407
   111
 */
slouken@1895
   112
extern DECLSPEC void SDLCALL SDL_DestroySemaphore(SDL_sem * sem);
slouken@0
   113
slouken@3407
   114
/**
slouken@3407
   115
 *  This function suspends the calling thread until the semaphore pointed 
slouken@3407
   116
 *  to by \c sem has a positive count. It then atomically decreases the 
slouken@3407
   117
 *  semaphore count.
slouken@0
   118
 */
slouken@1895
   119
extern DECLSPEC int SDLCALL SDL_SemWait(SDL_sem * sem);
slouken@0
   120
slouken@3407
   121
/**
slouken@3407
   122
 *  Non-blocking variant of SDL_SemWait().
slouken@3407
   123
 *  
slouken@3407
   124
 *  \return 0 if the wait succeeds, ::SDL_MUTEX_TIMEDOUT if the wait would 
slouken@3407
   125
 *          block, and -1 on error.
slouken@3407
   126
 */
slouken@1895
   127
extern DECLSPEC int SDLCALL SDL_SemTryWait(SDL_sem * sem);
slouken@0
   128
slouken@3407
   129
/**
slouken@3407
   130
 *  Variant of SDL_SemWait() with a timeout in milliseconds.
slouken@3407
   131
 *  
slouken@3407
   132
 *  \return 0 if the wait succeeds, ::SDL_MUTEX_TIMEDOUT if the wait does not 
slouken@3407
   133
 *          succeed in the allotted time, and -1 on error.
slouken@3407
   134
 *  
slouken@3407
   135
 *  \warning On some platforms this function is implemented by looping with a 
slouken@3407
   136
 *           delay of 1 ms, and so should be avoided if possible.
slouken@3407
   137
 */
slouken@1895
   138
extern DECLSPEC int SDLCALL SDL_SemWaitTimeout(SDL_sem * sem, Uint32 ms);
slouken@0
   139
slouken@3407
   140
/**
slouken@3407
   141
 *  Atomically increases the semaphore's count (not blocking).
slouken@3407
   142
 *  
slouken@3407
   143
 *  \return 0, or -1 on error.
slouken@0
   144
 */
slouken@1895
   145
extern DECLSPEC int SDLCALL SDL_SemPost(SDL_sem * sem);
slouken@0
   146
slouken@3407
   147
/**
slouken@3407
   148
 *  Returns the current count of the semaphore.
slouken@3407
   149
 */
slouken@1895
   150
extern DECLSPEC Uint32 SDLCALL SDL_SemValue(SDL_sem * sem);
slouken@0
   151
slouken@3407
   152
/*@}*//*Semaphore functions*/
slouken@0
   153
slouken@3407
   154
slouken@3407
   155
/**
slouken@3407
   156
 *  \name Condition variable functions
slouken@3407
   157
 */
slouken@3407
   158
/*@{*/
slouken@0
   159
slouken@0
   160
/* The SDL condition variable structure, defined in SDL_cond.c */
slouken@0
   161
struct SDL_cond;
slouken@0
   162
typedef struct SDL_cond SDL_cond;
slouken@0
   163
slouken@3407
   164
/**
slouken@3407
   165
 *  Create a condition variable.
slouken@5110
   166
 *
slouken@5110
   167
 *  Typical use of condition variables:
slouken@5110
   168
 *
slouken@5110
   169
 *  Thread A:
slouken@5110
   170
 *    SDL_LockMutex(lock);
slouken@5110
   171
 *    while ( ! condition ) {
slouken@5110
   172
 *        SDL_CondWait(cond, lock);
slouken@5110
   173
 *    }
slouken@5110
   174
 *    SDL_UnlockMutex(lock);
slouken@5110
   175
 *
slouken@5110
   176
 *  Thread B:
slouken@5110
   177
 *    SDL_LockMutex(lock);
slouken@5110
   178
 *    ...
slouken@5110
   179
 *    condition = true;
slouken@5110
   180
 *    ...
slouken@5110
   181
 *    SDL_CondSignal(cond);
slouken@5110
   182
 *    SDL_UnlockMutex(lock);
slouken@5110
   183
 *
slouken@5110
   184
 *  There is some discussion whether to signal the condition variable
slouken@5110
   185
 *  with the mutex locked or not.  There is some potential performance
slouken@5110
   186
 *  benefit to unlocking first on some platforms, but there are some
slouken@5110
   187
 *  potential race conditions depending on how your code is structured.
slouken@5110
   188
 *
slouken@5110
   189
 *  In general it's safer to signal the condition variable while the
slouken@5110
   190
 *  mutex is locked.
slouken@3407
   191
 */
slouken@1895
   192
extern DECLSPEC SDL_cond *SDLCALL SDL_CreateCond(void);
slouken@0
   193
slouken@3407
   194
/**
slouken@3407
   195
 *  Destroy a condition variable.
slouken@3407
   196
 */
slouken@1895
   197
extern DECLSPEC void SDLCALL SDL_DestroyCond(SDL_cond * cond);
slouken@0
   198
slouken@3407
   199
/**
slouken@3407
   200
 *  Restart one of the threads that are waiting on the condition variable.
slouken@3407
   201
 *  
slouken@3407
   202
 *  \return 0 or -1 on error.
slouken@0
   203
 */
slouken@1895
   204
extern DECLSPEC int SDLCALL SDL_CondSignal(SDL_cond * cond);
slouken@0
   205
slouken@3407
   206
/**
slouken@3407
   207
 *  Restart all threads that are waiting on the condition variable.
slouken@5110
   208
 *
slouken@3407
   209
 *  \return 0 or -1 on error.
slouken@0
   210
 */
slouken@1895
   211
extern DECLSPEC int SDLCALL SDL_CondBroadcast(SDL_cond * cond);
slouken@0
   212
slouken@3407
   213
/**
slouken@3407
   214
 *  Wait on the condition variable, unlocking the provided mutex.
slouken@3407
   215
 *  
slouken@3407
   216
 *  \warning The mutex must be locked before entering this function!
slouken@3407
   217
 *  
slouken@3407
   218
 *  The mutex is re-locked once the condition variable is signaled.
slouken@3407
   219
 *  
slouken@3407
   220
 *  \return 0 when it is signaled, or -1 on error.
slouken@0
   221
 */
slouken@4469
   222
extern DECLSPEC int SDLCALL SDL_CondWait(SDL_cond * cond, SDL_mutex * mutex);
slouken@0
   223
slouken@3407
   224
/**
slouken@3407
   225
 *  Waits for at most \c ms milliseconds, and returns 0 if the condition
slouken@3407
   226
 *  variable is signaled, ::SDL_MUTEX_TIMEDOUT if the condition is not
slouken@3407
   227
 *  signaled in the allotted time, and -1 on error.
slouken@3407
   228
 *
slouken@3407
   229
 *  \warning On some platforms this function is implemented by looping with a 
slouken@3407
   230
 *           delay of 1 ms, and so should be avoided if possible.
slouken@3407
   231
 */
slouken@1895
   232
extern DECLSPEC int SDLCALL SDL_CondWaitTimeout(SDL_cond * cond,
slouken@1895
   233
                                                SDL_mutex * mutex, Uint32 ms);
slouken@0
   234
slouken@3407
   235
/*@}*//*Condition variable functions*/
slouken@3407
   236
slouken@3407
   237
slouken@0
   238
/* Ends C function definitions when using C++ */
slouken@0
   239
#ifdef __cplusplus
slouken@1895
   240
/* *INDENT-OFF* */
slouken@0
   241
}
slouken@1895
   242
/* *INDENT-ON* */
slouken@0
   243
#endif
slouken@0
   244
#include "close_code.h"
slouken@0
   245
slouken@0
   246
#endif /* _SDL_mutex_h */
slouken@1895
   247
slouken@1895
   248
/* vi: set ts=4 sw=4 expandtab: */