1b565d46b836401fa5dac23f9f8f0841c7a41e58eXavier Ducrohet/*
2b565d46b836401fa5dac23f9f8f0841c7a41e58eXavier Ducrohet    SDL - Simple DirectMedia Layer
34458c4364a99c5d8d124b19eec146b0998c4895aJesse Hall    Copyright (C) 1997-2012 Sam Lantinga
4b565d46b836401fa5dac23f9f8f0841c7a41e58eXavier Ducrohet
5b565d46b836401fa5dac23f9f8f0841c7a41e58eXavier Ducrohet    This library is free software; you can redistribute it and/or
6b565d46b836401fa5dac23f9f8f0841c7a41e58eXavier Ducrohet    modify it under the terms of the GNU Lesser General Public
7b565d46b836401fa5dac23f9f8f0841c7a41e58eXavier Ducrohet    License as published by the Free Software Foundation; either
8b565d46b836401fa5dac23f9f8f0841c7a41e58eXavier Ducrohet    version 2.1 of the License, or (at your option) any later version.
9b565d46b836401fa5dac23f9f8f0841c7a41e58eXavier Ducrohet
10b565d46b836401fa5dac23f9f8f0841c7a41e58eXavier Ducrohet    This library is distributed in the hope that it will be useful,
11b565d46b836401fa5dac23f9f8f0841c7a41e58eXavier Ducrohet    but WITHOUT ANY WARRANTY; without even the implied warranty of
12b565d46b836401fa5dac23f9f8f0841c7a41e58eXavier Ducrohet    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13b565d46b836401fa5dac23f9f8f0841c7a41e58eXavier Ducrohet    Lesser General Public License for more details.
14b565d46b836401fa5dac23f9f8f0841c7a41e58eXavier Ducrohet
15b565d46b836401fa5dac23f9f8f0841c7a41e58eXavier Ducrohet    You should have received a copy of the GNU Lesser General Public
16b565d46b836401fa5dac23f9f8f0841c7a41e58eXavier Ducrohet    License along with this library; if not, write to the Free Software
17b565d46b836401fa5dac23f9f8f0841c7a41e58eXavier Ducrohet    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18b565d46b836401fa5dac23f9f8f0841c7a41e58eXavier Ducrohet
19b565d46b836401fa5dac23f9f8f0841c7a41e58eXavier Ducrohet    Sam Lantinga
20b565d46b836401fa5dac23f9f8f0841c7a41e58eXavier Ducrohet    slouken@libsdl.org
21b565d46b836401fa5dac23f9f8f0841c7a41e58eXavier Ducrohet*/
22b565d46b836401fa5dac23f9f8f0841c7a41e58eXavier Ducrohet
23b565d46b836401fa5dac23f9f8f0841c7a41e58eXavier Ducrohet#ifndef _SDL_mutex_h
24b565d46b836401fa5dac23f9f8f0841c7a41e58eXavier Ducrohet#define _SDL_mutex_h
25b565d46b836401fa5dac23f9f8f0841c7a41e58eXavier Ducrohet
264458c4364a99c5d8d124b19eec146b0998c4895aJesse Hall/** @file SDL_mutex.h
274458c4364a99c5d8d124b19eec146b0998c4895aJesse Hall *  Functions to provide thread synchronization primitives
284458c4364a99c5d8d124b19eec146b0998c4895aJesse Hall *
294458c4364a99c5d8d124b19eec146b0998c4895aJesse Hall *  @note These are independent of the other SDL routines.
304458c4364a99c5d8d124b19eec146b0998c4895aJesse Hall */
31b565d46b836401fa5dac23f9f8f0841c7a41e58eXavier Ducrohet
32b565d46b836401fa5dac23f9f8f0841c7a41e58eXavier Ducrohet#include "SDL_stdinc.h"
33b565d46b836401fa5dac23f9f8f0841c7a41e58eXavier Ducrohet#include "SDL_error.h"
34b565d46b836401fa5dac23f9f8f0841c7a41e58eXavier Ducrohet
35b565d46b836401fa5dac23f9f8f0841c7a41e58eXavier Ducrohet#include "begin_code.h"
36b565d46b836401fa5dac23f9f8f0841c7a41e58eXavier Ducrohet/* Set up for C function definitions, even when using C++ */
37b565d46b836401fa5dac23f9f8f0841c7a41e58eXavier Ducrohet#ifdef __cplusplus
38b565d46b836401fa5dac23f9f8f0841c7a41e58eXavier Ducrohetextern "C" {
39b565d46b836401fa5dac23f9f8f0841c7a41e58eXavier Ducrohet#endif
40b565d46b836401fa5dac23f9f8f0841c7a41e58eXavier Ducrohet
414458c4364a99c5d8d124b19eec146b0998c4895aJesse Hall/** Synchronization functions which can time out return this value
424458c4364a99c5d8d124b19eec146b0998c4895aJesse Hall *  if they time out.
434458c4364a99c5d8d124b19eec146b0998c4895aJesse Hall */
44b565d46b836401fa5dac23f9f8f0841c7a41e58eXavier Ducrohet#define SDL_MUTEX_TIMEDOUT	1
45b565d46b836401fa5dac23f9f8f0841c7a41e58eXavier Ducrohet
464458c4364a99c5d8d124b19eec146b0998c4895aJesse Hall/** This is the timeout value which corresponds to never time out */
47b565d46b836401fa5dac23f9f8f0841c7a41e58eXavier Ducrohet#define SDL_MUTEX_MAXWAIT	(~(Uint32)0)
48b565d46b836401fa5dac23f9f8f0841c7a41e58eXavier Ducrohet
49b565d46b836401fa5dac23f9f8f0841c7a41e58eXavier Ducrohet
50b565d46b836401fa5dac23f9f8f0841c7a41e58eXavier Ducrohet/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
514458c4364a99c5d8d124b19eec146b0998c4895aJesse Hall/** @name Mutex functions                                        */ /*@{*/
52b565d46b836401fa5dac23f9f8f0841c7a41e58eXavier Ducrohet/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
53b565d46b836401fa5dac23f9f8f0841c7a41e58eXavier Ducrohet
544458c4364a99c5d8d124b19eec146b0998c4895aJesse Hall/** The SDL mutex structure, defined in SDL_mutex.c */
55b565d46b836401fa5dac23f9f8f0841c7a41e58eXavier Ducrohetstruct SDL_mutex;
56b565d46b836401fa5dac23f9f8f0841c7a41e58eXavier Ducrohettypedef struct SDL_mutex SDL_mutex;
57b565d46b836401fa5dac23f9f8f0841c7a41e58eXavier Ducrohet
584458c4364a99c5d8d124b19eec146b0998c4895aJesse Hall/** Create a mutex, initialized unlocked */
59b565d46b836401fa5dac23f9f8f0841c7a41e58eXavier Ducrohetextern DECLSPEC SDL_mutex * SDLCALL SDL_CreateMutex(void);
60b565d46b836401fa5dac23f9f8f0841c7a41e58eXavier Ducrohet
61b565d46b836401fa5dac23f9f8f0841c7a41e58eXavier Ducrohet#define SDL_LockMutex(m)	SDL_mutexP(m)
624458c4364a99c5d8d124b19eec146b0998c4895aJesse Hall/** Lock the mutex
634458c4364a99c5d8d124b19eec146b0998c4895aJesse Hall *  @return 0, or -1 on error
644458c4364a99c5d8d124b19eec146b0998c4895aJesse Hall */
65b565d46b836401fa5dac23f9f8f0841c7a41e58eXavier Ducrohetextern DECLSPEC int SDLCALL SDL_mutexP(SDL_mutex *mutex);
66b565d46b836401fa5dac23f9f8f0841c7a41e58eXavier Ducrohet
67b565d46b836401fa5dac23f9f8f0841c7a41e58eXavier Ducrohet#define SDL_UnlockMutex(m)	SDL_mutexV(m)
684458c4364a99c5d8d124b19eec146b0998c4895aJesse Hall/** Unlock the mutex
694458c4364a99c5d8d124b19eec146b0998c4895aJesse Hall *  @return 0, or -1 on error
704458c4364a99c5d8d124b19eec146b0998c4895aJesse Hall *
714458c4364a99c5d8d124b19eec146b0998c4895aJesse Hall *  It is an error to unlock a mutex that has not been locked by
724458c4364a99c5d8d124b19eec146b0998c4895aJesse Hall *  the current thread, and doing so results in undefined behavior.
734458c4364a99c5d8d124b19eec146b0998c4895aJesse Hall */
74b565d46b836401fa5dac23f9f8f0841c7a41e58eXavier Ducrohetextern DECLSPEC int SDLCALL SDL_mutexV(SDL_mutex *mutex);
75b565d46b836401fa5dac23f9f8f0841c7a41e58eXavier Ducrohet
764458c4364a99c5d8d124b19eec146b0998c4895aJesse Hall/** Destroy a mutex */
77b565d46b836401fa5dac23f9f8f0841c7a41e58eXavier Ducrohetextern DECLSPEC void SDLCALL SDL_DestroyMutex(SDL_mutex *mutex);
78b565d46b836401fa5dac23f9f8f0841c7a41e58eXavier Ducrohet
794458c4364a99c5d8d124b19eec146b0998c4895aJesse Hall/*@}*/
80b565d46b836401fa5dac23f9f8f0841c7a41e58eXavier Ducrohet
81b565d46b836401fa5dac23f9f8f0841c7a41e58eXavier Ducrohet/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
824458c4364a99c5d8d124b19eec146b0998c4895aJesse Hall/** @name Semaphore functions                                    */ /*@{*/
83b565d46b836401fa5dac23f9f8f0841c7a41e58eXavier Ducrohet/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
84b565d46b836401fa5dac23f9f8f0841c7a41e58eXavier Ducrohet
854458c4364a99c5d8d124b19eec146b0998c4895aJesse Hall/** The SDL semaphore structure, defined in SDL_sem.c */
86b565d46b836401fa5dac23f9f8f0841c7a41e58eXavier Ducrohetstruct SDL_semaphore;
87b565d46b836401fa5dac23f9f8f0841c7a41e58eXavier Ducrohettypedef struct SDL_semaphore SDL_sem;
88b565d46b836401fa5dac23f9f8f0841c7a41e58eXavier Ducrohet
894458c4364a99c5d8d124b19eec146b0998c4895aJesse Hall/** Create a semaphore, initialized with value, returns NULL on failure. */
90b565d46b836401fa5dac23f9f8f0841c7a41e58eXavier Ducrohetextern DECLSPEC SDL_sem * SDLCALL SDL_CreateSemaphore(Uint32 initial_value);
91b565d46b836401fa5dac23f9f8f0841c7a41e58eXavier Ducrohet
924458c4364a99c5d8d124b19eec146b0998c4895aJesse Hall/** Destroy a semaphore */
93b565d46b836401fa5dac23f9f8f0841c7a41e58eXavier Ducrohetextern DECLSPEC void SDLCALL SDL_DestroySemaphore(SDL_sem *sem);
94b565d46b836401fa5dac23f9f8f0841c7a41e58eXavier Ducrohet
954458c4364a99c5d8d124b19eec146b0998c4895aJesse Hall/**
964458c4364a99c5d8d124b19eec146b0998c4895aJesse Hall * This function suspends the calling thread until the semaphore pointed
97b565d46b836401fa5dac23f9f8f0841c7a41e58eXavier Ducrohet * to by sem has a positive count. It then atomically decreases the semaphore
98b565d46b836401fa5dac23f9f8f0841c7a41e58eXavier Ducrohet * count.
99b565d46b836401fa5dac23f9f8f0841c7a41e58eXavier Ducrohet */
100b565d46b836401fa5dac23f9f8f0841c7a41e58eXavier Ducrohetextern DECLSPEC int SDLCALL SDL_SemWait(SDL_sem *sem);
101b565d46b836401fa5dac23f9f8f0841c7a41e58eXavier Ducrohet
1024458c4364a99c5d8d124b19eec146b0998c4895aJesse Hall/** Non-blocking variant of SDL_SemWait().
1034458c4364a99c5d8d124b19eec146b0998c4895aJesse Hall *  @return 0 if the wait succeeds,
1044458c4364a99c5d8d124b19eec146b0998c4895aJesse Hall *  SDL_MUTEX_TIMEDOUT if the wait would block, and -1 on error.
1054458c4364a99c5d8d124b19eec146b0998c4895aJesse Hall */
106b565d46b836401fa5dac23f9f8f0841c7a41e58eXavier Ducrohetextern DECLSPEC int SDLCALL SDL_SemTryWait(SDL_sem *sem);
107b565d46b836401fa5dac23f9f8f0841c7a41e58eXavier Ducrohet
1084458c4364a99c5d8d124b19eec146b0998c4895aJesse Hall/** Variant of SDL_SemWait() with a timeout in milliseconds, returns 0 if
1094458c4364a99c5d8d124b19eec146b0998c4895aJesse Hall *  the wait succeeds, SDL_MUTEX_TIMEDOUT if the wait does not succeed in
1104458c4364a99c5d8d124b19eec146b0998c4895aJesse Hall *  the allotted time, and -1 on error.
1114458c4364a99c5d8d124b19eec146b0998c4895aJesse Hall *
1124458c4364a99c5d8d124b19eec146b0998c4895aJesse Hall *  On some platforms this function is implemented by looping with a delay
1134458c4364a99c5d8d124b19eec146b0998c4895aJesse Hall *  of 1 ms, and so should be avoided if possible.
1144458c4364a99c5d8d124b19eec146b0998c4895aJesse Hall */
115b565d46b836401fa5dac23f9f8f0841c7a41e58eXavier Ducrohetextern DECLSPEC int SDLCALL SDL_SemWaitTimeout(SDL_sem *sem, Uint32 ms);
116b565d46b836401fa5dac23f9f8f0841c7a41e58eXavier Ducrohet
1174458c4364a99c5d8d124b19eec146b0998c4895aJesse Hall/** Atomically increases the semaphore's count (not blocking).
1184458c4364a99c5d8d124b19eec146b0998c4895aJesse Hall *  @return 0, or -1 on error.
119b565d46b836401fa5dac23f9f8f0841c7a41e58eXavier Ducrohet */
120b565d46b836401fa5dac23f9f8f0841c7a41e58eXavier Ducrohetextern DECLSPEC int SDLCALL SDL_SemPost(SDL_sem *sem);
121b565d46b836401fa5dac23f9f8f0841c7a41e58eXavier Ducrohet
1224458c4364a99c5d8d124b19eec146b0998c4895aJesse Hall/** Returns the current count of the semaphore */
123b565d46b836401fa5dac23f9f8f0841c7a41e58eXavier Ducrohetextern DECLSPEC Uint32 SDLCALL SDL_SemValue(SDL_sem *sem);
124b565d46b836401fa5dac23f9f8f0841c7a41e58eXavier Ducrohet
1254458c4364a99c5d8d124b19eec146b0998c4895aJesse Hall/*@}*/
126b565d46b836401fa5dac23f9f8f0841c7a41e58eXavier Ducrohet
127b565d46b836401fa5dac23f9f8f0841c7a41e58eXavier Ducrohet/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1284458c4364a99c5d8d124b19eec146b0998c4895aJesse Hall/** @name Condition_variable_functions                           */ /*@{*/
129b565d46b836401fa5dac23f9f8f0841c7a41e58eXavier Ducrohet/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
130b565d46b836401fa5dac23f9f8f0841c7a41e58eXavier Ducrohet
1314458c4364a99c5d8d124b19eec146b0998c4895aJesse Hall/*@{*/
1324458c4364a99c5d8d124b19eec146b0998c4895aJesse Hall/** The SDL condition variable structure, defined in SDL_cond.c */
133b565d46b836401fa5dac23f9f8f0841c7a41e58eXavier Ducrohetstruct SDL_cond;
134b565d46b836401fa5dac23f9f8f0841c7a41e58eXavier Ducrohettypedef struct SDL_cond SDL_cond;
1354458c4364a99c5d8d124b19eec146b0998c4895aJesse Hall/*@}*/
136b565d46b836401fa5dac23f9f8f0841c7a41e58eXavier Ducrohet
1374458c4364a99c5d8d124b19eec146b0998c4895aJesse Hall/** Create a condition variable */
138b565d46b836401fa5dac23f9f8f0841c7a41e58eXavier Ducrohetextern DECLSPEC SDL_cond * SDLCALL SDL_CreateCond(void);
139b565d46b836401fa5dac23f9f8f0841c7a41e58eXavier Ducrohet
1404458c4364a99c5d8d124b19eec146b0998c4895aJesse Hall/** Destroy a condition variable */
141b565d46b836401fa5dac23f9f8f0841c7a41e58eXavier Ducrohetextern DECLSPEC void SDLCALL SDL_DestroyCond(SDL_cond *cond);
142b565d46b836401fa5dac23f9f8f0841c7a41e58eXavier Ducrohet
1434458c4364a99c5d8d124b19eec146b0998c4895aJesse Hall/** Restart one of the threads that are waiting on the condition variable,
1444458c4364a99c5d8d124b19eec146b0998c4895aJesse Hall *  @return 0 or -1 on error.
145b565d46b836401fa5dac23f9f8f0841c7a41e58eXavier Ducrohet */
146b565d46b836401fa5dac23f9f8f0841c7a41e58eXavier Ducrohetextern DECLSPEC int SDLCALL SDL_CondSignal(SDL_cond *cond);
147b565d46b836401fa5dac23f9f8f0841c7a41e58eXavier Ducrohet
1484458c4364a99c5d8d124b19eec146b0998c4895aJesse Hall/** Restart all threads that are waiting on the condition variable,
1494458c4364a99c5d8d124b19eec146b0998c4895aJesse Hall *  @return 0 or -1 on error.
150b565d46b836401fa5dac23f9f8f0841c7a41e58eXavier Ducrohet */
151b565d46b836401fa5dac23f9f8f0841c7a41e58eXavier Ducrohetextern DECLSPEC int SDLCALL SDL_CondBroadcast(SDL_cond *cond);
152b565d46b836401fa5dac23f9f8f0841c7a41e58eXavier Ducrohet
1534458c4364a99c5d8d124b19eec146b0998c4895aJesse Hall/** Wait on the condition variable, unlocking the provided mutex.
1544458c4364a99c5d8d124b19eec146b0998c4895aJesse Hall *  The mutex must be locked before entering this function!
1554458c4364a99c5d8d124b19eec146b0998c4895aJesse Hall *  The mutex is re-locked once the condition variable is signaled.
1564458c4364a99c5d8d124b19eec146b0998c4895aJesse Hall *  @return 0 when it is signaled, or -1 on error.
157b565d46b836401fa5dac23f9f8f0841c7a41e58eXavier Ducrohet */
158b565d46b836401fa5dac23f9f8f0841c7a41e58eXavier Ducrohetextern DECLSPEC int SDLCALL SDL_CondWait(SDL_cond *cond, SDL_mutex *mut);
159b565d46b836401fa5dac23f9f8f0841c7a41e58eXavier Ducrohet
1604458c4364a99c5d8d124b19eec146b0998c4895aJesse Hall/** Waits for at most 'ms' milliseconds, and returns 0 if the condition
1614458c4364a99c5d8d124b19eec146b0998c4895aJesse Hall *  variable is signaled, SDL_MUTEX_TIMEDOUT if the condition is not
1624458c4364a99c5d8d124b19eec146b0998c4895aJesse Hall *  signaled in the allotted time, and -1 on error.
1634458c4364a99c5d8d124b19eec146b0998c4895aJesse Hall *  On some platforms this function is implemented by looping with a delay
1644458c4364a99c5d8d124b19eec146b0998c4895aJesse Hall *  of 1 ms, and so should be avoided if possible.
1654458c4364a99c5d8d124b19eec146b0998c4895aJesse Hall */
166b565d46b836401fa5dac23f9f8f0841c7a41e58eXavier Ducrohetextern DECLSPEC int SDLCALL SDL_CondWaitTimeout(SDL_cond *cond, SDL_mutex *mutex, Uint32 ms);
167b565d46b836401fa5dac23f9f8f0841c7a41e58eXavier Ducrohet
1684458c4364a99c5d8d124b19eec146b0998c4895aJesse Hall/*@}*/
1694458c4364a99c5d8d124b19eec146b0998c4895aJesse Hall
170b565d46b836401fa5dac23f9f8f0841c7a41e58eXavier Ducrohet/* Ends C function definitions when using C++ */
171b565d46b836401fa5dac23f9f8f0841c7a41e58eXavier Ducrohet#ifdef __cplusplus
172b565d46b836401fa5dac23f9f8f0841c7a41e58eXavier Ducrohet}
173b565d46b836401fa5dac23f9f8f0841c7a41e58eXavier Ducrohet#endif
174b565d46b836401fa5dac23f9f8f0841c7a41e58eXavier Ducrohet#include "close_code.h"
175b565d46b836401fa5dac23f9f8f0841c7a41e58eXavier Ducrohet
176b565d46b836401fa5dac23f9f8f0841c7a41e58eXavier Ducrohet#endif /* _SDL_mutex_h */
1774458c4364a99c5d8d124b19eec146b0998c4895aJesse Hall
178