00001 /* 00002 SDL - Simple DirectMedia Layer 00003 Copyright (C) 1997-2009 Sam Lantinga 00004 00005 This library is free software; you can redistribute it and/or 00006 modify it under the terms of the GNU Lesser General Public 00007 License as published by the Free Software Foundation; either 00008 version 2.1 of the License, or (at your option) any later version. 00009 00010 This library is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 Lesser General Public License for more details. 00014 00015 You should have received a copy of the GNU Lesser General Public 00016 License along with this library; if not, write to the Free Software 00017 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 00018 00019 Sam Lantinga 00020 slouken@libsdl.org 00021 */ 00022 00023 #ifndef _SDL_mutex_h 00024 #define _SDL_mutex_h 00025 00032 #include "SDL_stdinc.h" 00033 #include "SDL_error.h" 00034 00035 #include "begin_code.h" 00036 /* Set up for C function definitions, even when using C++ */ 00037 #ifdef __cplusplus 00038 /* *INDENT-OFF* */ 00039 extern "C" { 00040 /* *INDENT-ON* */ 00041 #endif 00042 00043 /* Synchronization functions which can time out return this value 00044 if they time out. 00045 */ 00046 #define SDL_MUTEX_TIMEDOUT 1 00047 00048 /* This is the timeout value which corresponds to never time out */ 00049 #define SDL_MUTEX_MAXWAIT (~(Uint32)0) 00050 00051 00052 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ 00053 /* Mutex functions */ 00054 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ 00055 00056 /* The SDL mutex structure, defined in SDL_mutex.c */ 00057 struct SDL_mutex; 00058 typedef struct SDL_mutex SDL_mutex; 00059 00060 /* Create a mutex, initialized unlocked */ 00061 extern DECLSPEC SDL_mutex *SDLCALL SDL_CreateMutex(void); 00062 00063 /* Lock the mutex (Returns 0, or -1 on error) */ 00064 #define SDL_LockMutex(m) SDL_mutexP(m) 00065 extern DECLSPEC int SDLCALL SDL_mutexP(SDL_mutex * mutex); 00066 00067 /* Unlock the mutex (Returns 0, or -1 on error) 00068 It is an error to unlock a mutex that has not been locked by 00069 the current thread, and doing so results in undefined behavior. 00070 */ 00071 #define SDL_UnlockMutex(m) SDL_mutexV(m) 00072 extern DECLSPEC int SDLCALL SDL_mutexV(SDL_mutex * mutex); 00073 00074 /* Destroy a mutex */ 00075 extern DECLSPEC void SDLCALL SDL_DestroyMutex(SDL_mutex * mutex); 00076 00077 00078 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ 00079 /* Semaphore functions */ 00080 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ 00081 00082 /* The SDL semaphore structure, defined in SDL_sem.c */ 00083 struct SDL_semaphore; 00084 typedef struct SDL_semaphore SDL_sem; 00085 00086 /* Create a semaphore, initialized with value, returns NULL on failure. */ 00087 extern DECLSPEC SDL_sem *SDLCALL SDL_CreateSemaphore(Uint32 initial_value); 00088 00089 /* Destroy a semaphore */ 00090 extern DECLSPEC void SDLCALL SDL_DestroySemaphore(SDL_sem * sem); 00091 00092 /* This function suspends the calling thread until the semaphore pointed 00093 * to by sem has a positive count. It then atomically decreases the semaphore 00094 * count. 00095 */ 00096 extern DECLSPEC int SDLCALL SDL_SemWait(SDL_sem * sem); 00097 00098 /* Non-blocking variant of SDL_SemWait(), returns 0 if the wait succeeds, 00099 SDL_MUTEX_TIMEDOUT if the wait would block, and -1 on error. 00100 */ 00101 extern DECLSPEC int SDLCALL SDL_SemTryWait(SDL_sem * sem); 00102 00103 /* Variant of SDL_SemWait() with a timeout in milliseconds, returns 0 if 00104 the wait succeeds, SDL_MUTEX_TIMEDOUT if the wait does not succeed in 00105 the allotted time, and -1 on error. 00106 On some platforms this function is implemented by looping with a delay 00107 of 1 ms, and so should be avoided if possible. 00108 */ 00109 extern DECLSPEC int SDLCALL SDL_SemWaitTimeout(SDL_sem * sem, Uint32 ms); 00110 00111 /* Atomically increases the semaphore's count (not blocking), returns 0, 00112 or -1 on error. 00113 */ 00114 extern DECLSPEC int SDLCALL SDL_SemPost(SDL_sem * sem); 00115 00116 /* Returns the current count of the semaphore */ 00117 extern DECLSPEC Uint32 SDLCALL SDL_SemValue(SDL_sem * sem); 00118 00119 00120 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ 00121 /* Condition variable functions */ 00122 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ 00123 00124 /* The SDL condition variable structure, defined in SDL_cond.c */ 00125 struct SDL_cond; 00126 typedef struct SDL_cond SDL_cond; 00127 00128 /* Create a condition variable */ 00129 extern DECLSPEC SDL_cond *SDLCALL SDL_CreateCond(void); 00130 00131 /* Destroy a condition variable */ 00132 extern DECLSPEC void SDLCALL SDL_DestroyCond(SDL_cond * cond); 00133 00134 /* Restart one of the threads that are waiting on the condition variable, 00135 returns 0 or -1 on error. 00136 */ 00137 extern DECLSPEC int SDLCALL SDL_CondSignal(SDL_cond * cond); 00138 00139 /* Restart all threads that are waiting on the condition variable, 00140 returns 0 or -1 on error. 00141 */ 00142 extern DECLSPEC int SDLCALL SDL_CondBroadcast(SDL_cond * cond); 00143 00144 /* Wait on the condition variable, unlocking the provided mutex. 00145 The mutex must be locked before entering this function! 00146 The mutex is re-locked once the condition variable is signaled. 00147 Returns 0 when it is signaled, or -1 on error. 00148 */ 00149 extern DECLSPEC int SDLCALL SDL_CondWait(SDL_cond * cond, SDL_mutex * mut); 00150 00151 /* Waits for at most 'ms' milliseconds, and returns 0 if the condition 00152 variable is signaled, SDL_MUTEX_TIMEDOUT if the condition is not 00153 signaled in the allotted time, and -1 on error. 00154 On some platforms this function is implemented by looping with a delay 00155 of 1 ms, and so should be avoided if possible. 00156 */ 00157 extern DECLSPEC int SDLCALL SDL_CondWaitTimeout(SDL_cond * cond, 00158 SDL_mutex * mutex, Uint32 ms); 00159 00160 /* Ends C function definitions when using C++ */ 00161 #ifdef __cplusplus 00162 /* *INDENT-OFF* */ 00163 } 00164 /* *INDENT-ON* */ 00165 #endif 00166 #include "close_code.h" 00167 00168 #endif /* _SDL_mutex_h */ 00169 00170 /* vi: set ts=4 sw=4 expandtab: */