include/SDL_mutex.h
author Ryan C. Gordon <icculus@icculus.org>
Fri, 13 Mar 2015 00:48:30 -0400
changeset 9381 812b2dabf8a4
parent 8149 681eb46b8ac4
child 9619 b94b6d0bff0f
permissions -rw-r--r--
Removed an unnecessary #ifdef.
slouken@0
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@8149
     3
  Copyright (C) 1997-2014 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@7191
    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@0
    37
extern "C" {
slouken@0
    38
#endif
slouken@0
    39
slouken@3407
    40
/**
slouken@3407
    41
 *  Synchronization functions which can time out return this value
slouken@3407
    42
 *  if they time out.
slouken@3407
    43
 */
slouken@7191
    44
#define SDL_MUTEX_TIMEDOUT  1
slouken@0
    45
slouken@3407
    46
/**
slouken@3407
    47
 *  This is the timeout value which corresponds to never time out.
slouken@3407
    48
 */
slouken@7191
    49
#define SDL_MUTEX_MAXWAIT   (~(Uint32)0)
slouken@0
    50
slouken@0
    51
slouken@3407
    52
/**
slouken@3407
    53
 *  \name Mutex functions
slouken@3407
    54
 */
gabomdq@7678
    55
/* @{ */
slouken@0
    56
philipp@7654
    57
/* The SDL mutex structure, defined in SDL_sysmutex.c */
slouken@0
    58
struct SDL_mutex;
slouken@0
    59
typedef struct SDL_mutex SDL_mutex;
slouken@0
    60
slouken@3407
    61
/**
slouken@3407
    62
 *  Create a mutex, initialized unlocked.
slouken@3407
    63
 */
slouken@1895
    64
extern DECLSPEC SDL_mutex *SDLCALL SDL_CreateMutex(void);
slouken@0
    65
slouken@3407
    66
/**
slouken@3407
    67
 *  Lock the mutex.
slouken@7191
    68
 *
slouken@3407
    69
 *  \return 0, or -1 on error.
slouken@3407
    70
 */
slouken@7191
    71
#define SDL_mutexP(m)   SDL_LockMutex(m)
slouken@6977
    72
extern DECLSPEC int SDLCALL SDL_LockMutex(SDL_mutex * mutex);
slouken@0
    73
slouken@3407
    74
/**
urkle@6966
    75
 *  Try to lock the mutex
slouken@7191
    76
 *
urkle@6966
    77
 *  \return 0, SDL_MUTEX_TIMEDOUT, or -1 on error
urkle@6966
    78
 */
urkle@6966
    79
extern DECLSPEC int SDLCALL SDL_TryLockMutex(SDL_mutex * mutex);
urkle@6966
    80
urkle@6966
    81
/**
slouken@3407
    82
 *  Unlock the mutex.
slouken@7191
    83
 *
slouken@3407
    84
 *  \return 0, or -1 on error.
slouken@7191
    85
 *
slouken@3407
    86
 *  \warning It is an error to unlock a mutex that has not been locked by
slouken@3407
    87
 *           the current thread, and doing so results in undefined behavior.
slouken@417
    88
 */
slouken@7191
    89
#define SDL_mutexV(m)   SDL_UnlockMutex(m)
slouken@6977
    90
extern DECLSPEC int SDLCALL SDL_UnlockMutex(SDL_mutex * mutex);
slouken@0
    91
slouken@7191
    92
/**
slouken@3407
    93
 *  Destroy a mutex.
slouken@3407
    94
 */
slouken@1895
    95
extern DECLSPEC void SDLCALL SDL_DestroyMutex(SDL_mutex * mutex);
slouken@0
    96
gabomdq@7678
    97
/* @} *//* Mutex functions */
slouken@0
    98
slouken@3407
    99
slouken@3407
   100
/**
slouken@3407
   101
 *  \name Semaphore functions
slouken@3407
   102
 */
gabomdq@7678
   103
/* @{ */
slouken@0
   104
philipp@7654
   105
/* The SDL semaphore structure, defined in SDL_syssem.c */
slouken@0
   106
struct SDL_semaphore;
slouken@0
   107
typedef struct SDL_semaphore SDL_sem;
slouken@0
   108
slouken@3407
   109
/**
slouken@3407
   110
 *  Create a semaphore, initialized with value, returns NULL on failure.
slouken@3407
   111
 */
slouken@1895
   112
extern DECLSPEC SDL_sem *SDLCALL SDL_CreateSemaphore(Uint32 initial_value);
slouken@0
   113
slouken@3407
   114
/**
slouken@3407
   115
 *  Destroy a semaphore.
slouken@3407
   116
 */
slouken@1895
   117
extern DECLSPEC void SDLCALL SDL_DestroySemaphore(SDL_sem * sem);
slouken@0
   118
slouken@3407
   119
/**
slouken@7191
   120
 *  This function suspends the calling thread until the semaphore pointed
slouken@7191
   121
 *  to by \c sem has a positive count. It then atomically decreases the
slouken@3407
   122
 *  semaphore count.
slouken@0
   123
 */
slouken@1895
   124
extern DECLSPEC int SDLCALL SDL_SemWait(SDL_sem * sem);
slouken@0
   125
slouken@3407
   126
/**
slouken@3407
   127
 *  Non-blocking variant of SDL_SemWait().
slouken@7191
   128
 *
slouken@7191
   129
 *  \return 0 if the wait succeeds, ::SDL_MUTEX_TIMEDOUT if the wait would
slouken@3407
   130
 *          block, and -1 on error.
slouken@3407
   131
 */
slouken@1895
   132
extern DECLSPEC int SDLCALL SDL_SemTryWait(SDL_sem * sem);
slouken@0
   133
slouken@3407
   134
/**
slouken@3407
   135
 *  Variant of SDL_SemWait() with a timeout in milliseconds.
slouken@7191
   136
 *
slouken@7191
   137
 *  \return 0 if the wait succeeds, ::SDL_MUTEX_TIMEDOUT if the wait does not
slouken@3407
   138
 *          succeed in the allotted time, and -1 on error.
slouken@7191
   139
 *
slouken@7191
   140
 *  \warning On some platforms this function is implemented by looping with a
slouken@3407
   141
 *           delay of 1 ms, and so should be avoided if possible.
slouken@3407
   142
 */
slouken@1895
   143
extern DECLSPEC int SDLCALL SDL_SemWaitTimeout(SDL_sem * sem, Uint32 ms);
slouken@0
   144
slouken@3407
   145
/**
slouken@3407
   146
 *  Atomically increases the semaphore's count (not blocking).
slouken@7191
   147
 *
slouken@3407
   148
 *  \return 0, or -1 on error.
slouken@0
   149
 */
slouken@1895
   150
extern DECLSPEC int SDLCALL SDL_SemPost(SDL_sem * sem);
slouken@0
   151
slouken@3407
   152
/**
slouken@3407
   153
 *  Returns the current count of the semaphore.
slouken@3407
   154
 */
slouken@1895
   155
extern DECLSPEC Uint32 SDLCALL SDL_SemValue(SDL_sem * sem);
slouken@0
   156
gabomdq@7678
   157
/* @} *//* Semaphore functions */
slouken@0
   158
slouken@3407
   159
slouken@3407
   160
/**
slouken@3407
   161
 *  \name Condition variable functions
slouken@3407
   162
 */
gabomdq@7678
   163
/* @{ */
slouken@0
   164
philipp@7654
   165
/* The SDL condition variable structure, defined in SDL_syscond.c */
slouken@0
   166
struct SDL_cond;
slouken@0
   167
typedef struct SDL_cond SDL_cond;
slouken@0
   168
slouken@3407
   169
/**
slouken@3407
   170
 *  Create a condition variable.
slouken@5110
   171
 *
slouken@5110
   172
 *  Typical use of condition variables:
slouken@5110
   173
 *
slouken@5110
   174
 *  Thread A:
slouken@5110
   175
 *    SDL_LockMutex(lock);
slouken@5110
   176
 *    while ( ! condition ) {
slouken@5110
   177
 *        SDL_CondWait(cond, lock);
slouken@5110
   178
 *    }
slouken@5110
   179
 *    SDL_UnlockMutex(lock);
slouken@5110
   180
 *
slouken@5110
   181
 *  Thread B:
slouken@5110
   182
 *    SDL_LockMutex(lock);
slouken@5110
   183
 *    ...
slouken@5110
   184
 *    condition = true;
slouken@5110
   185
 *    ...
slouken@5110
   186
 *    SDL_CondSignal(cond);
slouken@5110
   187
 *    SDL_UnlockMutex(lock);
slouken@5110
   188
 *
slouken@5110
   189
 *  There is some discussion whether to signal the condition variable
slouken@5110
   190
 *  with the mutex locked or not.  There is some potential performance
slouken@5110
   191
 *  benefit to unlocking first on some platforms, but there are some
slouken@5110
   192
 *  potential race conditions depending on how your code is structured.
slouken@5110
   193
 *
slouken@5110
   194
 *  In general it's safer to signal the condition variable while the
slouken@5110
   195
 *  mutex is locked.
slouken@3407
   196
 */
slouken@1895
   197
extern DECLSPEC SDL_cond *SDLCALL SDL_CreateCond(void);
slouken@0
   198
slouken@3407
   199
/**
slouken@3407
   200
 *  Destroy a condition variable.
slouken@3407
   201
 */
slouken@1895
   202
extern DECLSPEC void SDLCALL SDL_DestroyCond(SDL_cond * cond);
slouken@0
   203
slouken@3407
   204
/**
slouken@3407
   205
 *  Restart one of the threads that are waiting on the condition variable.
slouken@7191
   206
 *
slouken@3407
   207
 *  \return 0 or -1 on error.
slouken@0
   208
 */
slouken@1895
   209
extern DECLSPEC int SDLCALL SDL_CondSignal(SDL_cond * cond);
slouken@0
   210
slouken@3407
   211
/**
slouken@3407
   212
 *  Restart all threads that are waiting on the condition variable.
slouken@5110
   213
 *
slouken@3407
   214
 *  \return 0 or -1 on error.
slouken@0
   215
 */
slouken@1895
   216
extern DECLSPEC int SDLCALL SDL_CondBroadcast(SDL_cond * cond);
slouken@0
   217
slouken@3407
   218
/**
slouken@3407
   219
 *  Wait on the condition variable, unlocking the provided mutex.
slouken@7191
   220
 *
slouken@3407
   221
 *  \warning The mutex must be locked before entering this function!
slouken@7191
   222
 *
slouken@3407
   223
 *  The mutex is re-locked once the condition variable is signaled.
slouken@7191
   224
 *
slouken@3407
   225
 *  \return 0 when it is signaled, or -1 on error.
slouken@0
   226
 */
slouken@4469
   227
extern DECLSPEC int SDLCALL SDL_CondWait(SDL_cond * cond, SDL_mutex * mutex);
slouken@0
   228
slouken@3407
   229
/**
slouken@3407
   230
 *  Waits for at most \c ms milliseconds, and returns 0 if the condition
slouken@3407
   231
 *  variable is signaled, ::SDL_MUTEX_TIMEDOUT if the condition is not
slouken@3407
   232
 *  signaled in the allotted time, and -1 on error.
slouken@3407
   233
 *
slouken@7191
   234
 *  \warning On some platforms this function is implemented by looping with a
slouken@3407
   235
 *           delay of 1 ms, and so should be avoided if possible.
slouken@3407
   236
 */
slouken@1895
   237
extern DECLSPEC int SDLCALL SDL_CondWaitTimeout(SDL_cond * cond,
slouken@1895
   238
                                                SDL_mutex * mutex, Uint32 ms);
slouken@0
   239
gabomdq@7678
   240
/* @} *//* Condition variable functions */
slouken@3407
   241
slouken@3407
   242
slouken@0
   243
/* Ends C function definitions when using C++ */
slouken@0
   244
#ifdef __cplusplus
slouken@0
   245
}
slouken@0
   246
#endif
slouken@0
   247
#include "close_code.h"
slouken@0
   248
slouken@0
   249
#endif /* _SDL_mutex_h */
slouken@1895
   250
slouken@1895
   251
/* vi: set ts=4 sw=4 expandtab: */