include/SDL_mutex.h
author Bob Pendleton <bob@pendleton.com>
Thu, 09 Jul 2009 21:31:27 +0000
changeset 3216 48a80f2a7ff2
parent 2859 99210400e8b9
child 3407 d3baf5ac4e37
permissions -rw-r--r--
volitile... duh, yeah the variable need to be labeled volitile
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@2859
     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@1895
    26
/**
slouken@1895
    27
 * \file SDL_mutex.h
slouken@1895
    28
 *
slouken@1895
    29
 * Functions to provide thread synchronization primitives
slouken@1895
    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@1895
    38
/* *INDENT-OFF* */
slouken@0
    39
extern "C" {
slouken@1895
    40
/* *INDENT-ON* */
slouken@0
    41
#endif
slouken@0
    42
slouken@0
    43
/* Synchronization functions which can time out return this value
slouken@0
    44
   if they time out.
slouken@0
    45
*/
slouken@0
    46
#define SDL_MUTEX_TIMEDOUT	1
slouken@0
    47
slouken@0
    48
/* This is the timeout value which corresponds to never time out */
slouken@0
    49
#define SDL_MUTEX_MAXWAIT	(~(Uint32)0)
slouken@0
    50
slouken@0
    51
slouken@0
    52
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
slouken@0
    53
/* Mutex functions                                               */
slouken@0
    54
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
slouken@0
    55
slouken@0
    56
/* The SDL mutex structure, defined in SDL_mutex.c */
slouken@0
    57
struct SDL_mutex;
slouken@0
    58
typedef struct SDL_mutex SDL_mutex;
slouken@0
    59
slouken@0
    60
/* Create a mutex, initialized unlocked */
slouken@1895
    61
extern DECLSPEC SDL_mutex *SDLCALL SDL_CreateMutex(void);
slouken@0
    62
slouken@0
    63
/* Lock the mutex  (Returns 0, or -1 on error) */
slouken@0
    64
#define SDL_LockMutex(m)	SDL_mutexP(m)
slouken@1895
    65
extern DECLSPEC int SDLCALL SDL_mutexP(SDL_mutex * mutex);
slouken@0
    66
slouken@417
    67
/* Unlock the mutex  (Returns 0, or -1 on error)
slouken@417
    68
   It is an error to unlock a mutex that has not been locked by
slouken@417
    69
   the current thread, and doing so results in undefined behavior.
slouken@417
    70
 */
slouken@0
    71
#define SDL_UnlockMutex(m)	SDL_mutexV(m)
slouken@1895
    72
extern DECLSPEC int SDLCALL SDL_mutexV(SDL_mutex * mutex);
slouken@0
    73
slouken@0
    74
/* Destroy a mutex */
slouken@1895
    75
extern DECLSPEC void SDLCALL SDL_DestroyMutex(SDL_mutex * mutex);
slouken@0
    76
slouken@0
    77
slouken@0
    78
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
slouken@0
    79
/* Semaphore functions                                           */
slouken@0
    80
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
slouken@0
    81
slouken@0
    82
/* The SDL semaphore structure, defined in SDL_sem.c */
slouken@0
    83
struct SDL_semaphore;
slouken@0
    84
typedef struct SDL_semaphore SDL_sem;
slouken@0
    85
slouken@0
    86
/* Create a semaphore, initialized with value, returns NULL on failure. */
slouken@1895
    87
extern DECLSPEC SDL_sem *SDLCALL SDL_CreateSemaphore(Uint32 initial_value);
slouken@0
    88
slouken@0
    89
/* Destroy a semaphore */
slouken@1895
    90
extern DECLSPEC void SDLCALL SDL_DestroySemaphore(SDL_sem * sem);
slouken@0
    91
slouken@0
    92
/* This function suspends the calling thread until the semaphore pointed 
slouken@0
    93
 * to by sem has a positive count. It then atomically decreases the semaphore
slouken@0
    94
 * count.
slouken@0
    95
 */
slouken@1895
    96
extern DECLSPEC int SDLCALL SDL_SemWait(SDL_sem * sem);
slouken@0
    97
slouken@0
    98
/* Non-blocking variant of SDL_SemWait(), returns 0 if the wait succeeds,
slouken@0
    99
   SDL_MUTEX_TIMEDOUT if the wait would block, and -1 on error.
slouken@0
   100
*/
slouken@1895
   101
extern DECLSPEC int SDLCALL SDL_SemTryWait(SDL_sem * sem);
slouken@0
   102
slouken@0
   103
/* Variant of SDL_SemWait() with a timeout in milliseconds, returns 0 if
slouken@0
   104
   the wait succeeds, SDL_MUTEX_TIMEDOUT if the wait does not succeed in
slouken@0
   105
   the allotted time, and -1 on error.
slouken@0
   106
   On some platforms this function is implemented by looping with a delay
slouken@0
   107
   of 1 ms, and so should be avoided if possible.
slouken@0
   108
*/
slouken@1895
   109
extern DECLSPEC int SDLCALL SDL_SemWaitTimeout(SDL_sem * sem, Uint32 ms);
slouken@0
   110
slouken@0
   111
/* Atomically increases the semaphore's count (not blocking), returns 0,
slouken@0
   112
   or -1 on error.
slouken@0
   113
 */
slouken@1895
   114
extern DECLSPEC int SDLCALL SDL_SemPost(SDL_sem * sem);
slouken@0
   115
slouken@0
   116
/* Returns the current count of the semaphore */
slouken@1895
   117
extern DECLSPEC Uint32 SDLCALL SDL_SemValue(SDL_sem * sem);
slouken@0
   118
slouken@0
   119
slouken@0
   120
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
slouken@0
   121
/* Condition variable functions                                  */
slouken@0
   122
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
slouken@0
   123
slouken@0
   124
/* The SDL condition variable structure, defined in SDL_cond.c */
slouken@0
   125
struct SDL_cond;
slouken@0
   126
typedef struct SDL_cond SDL_cond;
slouken@0
   127
slouken@0
   128
/* Create a condition variable */
slouken@1895
   129
extern DECLSPEC SDL_cond *SDLCALL SDL_CreateCond(void);
slouken@0
   130
slouken@0
   131
/* Destroy a condition variable */
slouken@1895
   132
extern DECLSPEC void SDLCALL SDL_DestroyCond(SDL_cond * cond);
slouken@0
   133
slouken@0
   134
/* Restart one of the threads that are waiting on the condition variable,
slouken@0
   135
   returns 0 or -1 on error.
slouken@0
   136
 */
slouken@1895
   137
extern DECLSPEC int SDLCALL SDL_CondSignal(SDL_cond * cond);
slouken@0
   138
slouken@0
   139
/* Restart all threads that are waiting on the condition variable,
slouken@0
   140
   returns 0 or -1 on error.
slouken@0
   141
 */
slouken@1895
   142
extern DECLSPEC int SDLCALL SDL_CondBroadcast(SDL_cond * cond);
slouken@0
   143
slouken@0
   144
/* Wait on the condition variable, unlocking the provided mutex.
slouken@0
   145
   The mutex must be locked before entering this function!
slouken@1263
   146
   The mutex is re-locked once the condition variable is signaled.
slouken@0
   147
   Returns 0 when it is signaled, or -1 on error.
slouken@0
   148
 */
slouken@1895
   149
extern DECLSPEC int SDLCALL SDL_CondWait(SDL_cond * cond, SDL_mutex * mut);
slouken@0
   150
slouken@0
   151
/* Waits for at most 'ms' milliseconds, and returns 0 if the condition
slouken@0
   152
   variable is signaled, SDL_MUTEX_TIMEDOUT if the condition is not
slouken@0
   153
   signaled in the allotted time, and -1 on error.
slouken@0
   154
   On some platforms this function is implemented by looping with a delay
slouken@0
   155
   of 1 ms, and so should be avoided if possible.
slouken@0
   156
*/
slouken@1895
   157
extern DECLSPEC int SDLCALL SDL_CondWaitTimeout(SDL_cond * cond,
slouken@1895
   158
                                                SDL_mutex * mutex, Uint32 ms);
slouken@0
   159
slouken@0
   160
/* Ends C function definitions when using C++ */
slouken@0
   161
#ifdef __cplusplus
slouken@1895
   162
/* *INDENT-OFF* */
slouken@0
   163
}
slouken@1895
   164
/* *INDENT-ON* */
slouken@0
   165
#endif
slouken@0
   166
#include "close_code.h"
slouken@0
   167
slouken@0
   168
#endif /* _SDL_mutex_h */
slouken@1895
   169
slouken@1895
   170
/* vi: set ts=4 sw=4 expandtab: */