Path: blob/main/system/include/SDL/SDL_mutex.h
6169 views
/*1Simple DirectMedia Layer2Copyright (C) 1997-2011 Sam Lantinga <[email protected]>34This software is provided 'as-is', without any express or implied5warranty. In no event will the authors be held liable for any damages6arising from the use of this software.78Permission is granted to anyone to use this software for any purpose,9including commercial applications, and to alter it and redistribute it10freely, subject to the following restrictions:11121. The origin of this software must not be misrepresented; you must not13claim that you wrote the original software. If you use this software14in a product, an acknowledgment in the product documentation would be15appreciated but is not required.162. Altered source versions must be plainly marked as such, and must not be17misrepresented as being the original software.183. This notice may not be removed or altered from any source distribution.19*/2021#ifndef _SDL_mutex_h22#define _SDL_mutex_h2324/**25* \file SDL_mutex.h26*27* Functions to provide thread synchronization primitives.28*/2930#include "SDL_stdinc.h"31#include "SDL_error.h"3233#include "begin_code.h"34/* Set up for C function definitions, even when using C++ */35#ifdef __cplusplus36/* *INDENT-OFF* */37extern "C" {38/* *INDENT-ON* */39#endif4041/**42* Synchronization functions which can time out return this value43* if they time out.44*/45#define SDL_MUTEX_TIMEDOUT 14647/**48* This is the timeout value which corresponds to never time out.49*/50#define SDL_MUTEX_MAXWAIT (~(Uint32)0)515253/**54* \name Mutex functions55*/56/*@{*/5758/* The SDL mutex structure, defined in SDL_mutex.c */59struct SDL_mutex;60typedef struct SDL_mutex SDL_mutex;6162/**63* Create a mutex, initialized unlocked.64*/65extern DECLSPEC SDL_mutex *SDLCALL SDL_CreateMutex(void);6667/**68* Lock the mutex.69*70* \return 0, or -1 on error.71*/72#define SDL_LockMutex(m) SDL_mutexP(m)73extern DECLSPEC int SDLCALL SDL_mutexP(SDL_mutex * mutex);7475/**76* Unlock the mutex.77*78* \return 0, or -1 on error.79*80* \warning It is an error to unlock a mutex that has not been locked by81* the current thread, and doing so results in undefined behavior.82*/83#define SDL_UnlockMutex(m) SDL_mutexV(m)84extern DECLSPEC int SDLCALL SDL_mutexV(SDL_mutex * mutex);8586/**87* Destroy a mutex.88*/89extern DECLSPEC void SDLCALL SDL_DestroyMutex(SDL_mutex * mutex);9091/*@}*//*Mutex functions*/929394/**95* \name Semaphore functions96*/97/*@{*/9899/* The SDL semaphore structure, defined in SDL_sem.c */100struct SDL_semaphore;101typedef struct SDL_semaphore SDL_sem;102103/**104* Create a semaphore, initialized with value, returns NULL on failure.105*/106extern DECLSPEC SDL_sem *SDLCALL SDL_CreateSemaphore(Uint32 initial_value);107108/**109* Destroy a semaphore.110*/111extern DECLSPEC void SDLCALL SDL_DestroySemaphore(SDL_sem * sem);112113/**114* This function suspends the calling thread until the semaphore pointed115* to by \c sem has a positive count. It then atomically decreases the116* semaphore count.117*/118extern DECLSPEC int SDLCALL SDL_SemWait(SDL_sem * sem);119120/**121* Non-blocking variant of SDL_SemWait().122*123* \return 0 if the wait succeeds, ::SDL_MUTEX_TIMEDOUT if the wait would124* block, and -1 on error.125*/126extern DECLSPEC int SDLCALL SDL_SemTryWait(SDL_sem * sem);127128/**129* Variant of SDL_SemWait() with a timeout in milliseconds.130*131* \return 0 if the wait succeeds, ::SDL_MUTEX_TIMEDOUT if the wait does not132* succeed in the allotted time, and -1 on error.133*134* \warning On some platforms this function is implemented by looping with a135* delay of 1 ms, and so should be avoided if possible.136*/137extern DECLSPEC int SDLCALL SDL_SemWaitTimeout(SDL_sem * sem, Uint32 ms);138139/**140* Atomically increases the semaphore's count (not blocking).141*142* \return 0, or -1 on error.143*/144extern DECLSPEC int SDLCALL SDL_SemPost(SDL_sem * sem);145146/**147* Returns the current count of the semaphore.148*/149extern DECLSPEC Uint32 SDLCALL SDL_SemValue(SDL_sem * sem);150151/*@}*//*Semaphore functions*/152153154/**155* \name Condition variable functions156*/157/*@{*/158159/* The SDL condition variable structure, defined in SDL_cond.c */160struct SDL_cond;161typedef struct SDL_cond SDL_cond;162163/**164* Create a condition variable.165*166* Typical use of condition variables:167*168* Thread A:169* SDL_LockMutex(lock);170* while ( ! condition ) {171* SDL_CondWait(cond, lock);172* }173* SDL_UnlockMutex(lock);174*175* Thread B:176* SDL_LockMutex(lock);177* ...178* condition = true;179* ...180* SDL_CondSignal(cond);181* SDL_UnlockMutex(lock);182*183* There is some discussion whether to signal the condition variable184* with the mutex locked or not. There is some potential performance185* benefit to unlocking first on some platforms, but there are some186* potential race conditions depending on how your code is structured.187*188* In general it's safer to signal the condition variable while the189* mutex is locked.190*/191extern DECLSPEC SDL_cond *SDLCALL SDL_CreateCond(void);192193/**194* Destroy a condition variable.195*/196extern DECLSPEC void SDLCALL SDL_DestroyCond(SDL_cond * cond);197198/**199* Restart one of the threads that are waiting on the condition variable.200*201* \return 0 or -1 on error.202*/203extern DECLSPEC int SDLCALL SDL_CondSignal(SDL_cond * cond);204205/**206* Restart all threads that are waiting on the condition variable.207*208* \return 0 or -1 on error.209*/210extern DECLSPEC int SDLCALL SDL_CondBroadcast(SDL_cond * cond);211212/**213* Wait on the condition variable, unlocking the provided mutex.214*215* \warning The mutex must be locked before entering this function!216*217* The mutex is re-locked once the condition variable is signaled.218*219* \return 0 when it is signaled, or -1 on error.220*/221extern DECLSPEC int SDLCALL SDL_CondWait(SDL_cond * cond, SDL_mutex * mutex);222223/**224* Waits for at most \c ms milliseconds, and returns 0 if the condition225* variable is signaled, ::SDL_MUTEX_TIMEDOUT if the condition is not226* signaled in the allotted time, and -1 on error.227*228* \warning On some platforms this function is implemented by looping with a229* delay of 1 ms, and so should be avoided if possible.230*/231extern DECLSPEC int SDLCALL SDL_CondWaitTimeout(SDL_cond * cond,232SDL_mutex * mutex, Uint32 ms);233234/*@}*//*Condition variable functions*/235236237/* Ends C function definitions when using C++ */238#ifdef __cplusplus239/* *INDENT-OFF* */240}241/* *INDENT-ON* */242#endif243#include "close_code.h"244245#endif /* _SDL_mutex_h */246247/* vi: set ts=4 sw=4 expandtab: */248249250