include/SDL_mutex.h
author Sam Lantinga <slouken@libsdl.org>
Sat, 26 Sep 2009 23:17:08 +0000
branchSDL-1.2
changeset 4246 8b8314cc34a6
parent 4217 4c4113c2162c
child 6137 4720145f848b
permissions -rw-r--r--
Fixed bug #810

Lauri Kenttä 2009-09-26 06:42:23 PDT

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