include/SDL_mutex.h
author Sam Lantinga <slouken@libsdl.org>
Sun, 06 Jun 2010 21:09:45 -0700
changeset 4469 f67139f6d87f
parent 3697 f7b03b6838cb
child 5110 0846f18eb625
permissions -rw-r--r--
Fixed wording for consistency
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2010 Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Lesser General Public
     7     License as published by the Free Software Foundation; either
     8     version 2.1 of the License, or (at your option) any later version.
     9 
    10     This library is distributed in the hope that it will be useful,
    11     but WITHOUT ANY WARRANTY; without even the implied warranty of
    12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13     Lesser General Public License for more details.
    14 
    15     You should have received a copy of the GNU Lesser General Public
    16     License along with this library; if not, write to the Free Software
    17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 
    23 #ifndef _SDL_mutex_h
    24 #define _SDL_mutex_h
    25 
    26 /**
    27  *  \file SDL_mutex.h
    28  *  
    29  *  Functions to provide thread synchronization primitives.
    30  */
    31 
    32 #include "SDL_stdinc.h"
    33 #include "SDL_error.h"
    34 
    35 #include "begin_code.h"
    36 /* Set up for C function definitions, even when using C++ */
    37 #ifdef __cplusplus
    38 /* *INDENT-OFF* */
    39 extern "C" {
    40 /* *INDENT-ON* */
    41 #endif
    42 
    43 /**
    44  *  Synchronization functions which can time out return this value
    45  *  if they time out.
    46  */
    47 #define SDL_MUTEX_TIMEDOUT	1
    48 
    49 /**
    50  *  This is the timeout value which corresponds to never time out.
    51  */
    52 #define SDL_MUTEX_MAXWAIT	(~(Uint32)0)
    53 
    54 
    55 /**
    56  *  \name Mutex functions
    57  */
    58 /*@{*/
    59 
    60 /* The SDL mutex structure, defined in SDL_mutex.c */
    61 struct SDL_mutex;
    62 typedef struct SDL_mutex SDL_mutex;
    63 
    64 /**
    65  *  Create a mutex, initialized unlocked.
    66  */
    67 extern DECLSPEC SDL_mutex *SDLCALL SDL_CreateMutex(void);
    68 
    69 /**
    70  *  Lock the mutex.
    71  *  
    72  *  \return 0, or -1 on error.
    73  */
    74 #define SDL_LockMutex(m)	SDL_mutexP(m)
    75 extern DECLSPEC int SDLCALL SDL_mutexP(SDL_mutex * mutex);
    76 
    77 /**
    78  *  Unlock the mutex.
    79  *  
    80  *  \return 0, or -1 on error.
    81  *  
    82  *  \warning It is an error to unlock a mutex that has not been locked by
    83  *           the current thread, and doing so results in undefined behavior.
    84  */
    85 #define SDL_UnlockMutex(m)	SDL_mutexV(m)
    86 extern DECLSPEC int SDLCALL SDL_mutexV(SDL_mutex * mutex);
    87 
    88 /** 
    89  *  Destroy a mutex.
    90  */
    91 extern DECLSPEC void SDLCALL SDL_DestroyMutex(SDL_mutex * mutex);
    92 
    93 /*@}*//*Mutex functions*/
    94 
    95 
    96 /**
    97  *  \name Semaphore functions
    98  */
    99 /*@{*/
   100 
   101 /* The SDL semaphore structure, defined in SDL_sem.c */
   102 struct SDL_semaphore;
   103 typedef struct SDL_semaphore SDL_sem;
   104 
   105 /**
   106  *  Create a semaphore, initialized with value, returns NULL on failure.
   107  */
   108 extern DECLSPEC SDL_sem *SDLCALL SDL_CreateSemaphore(Uint32 initial_value);
   109 
   110 /**
   111  *  Destroy a semaphore.
   112  */
   113 extern DECLSPEC void SDLCALL SDL_DestroySemaphore(SDL_sem * sem);
   114 
   115 /**
   116  *  This function suspends the calling thread until the semaphore pointed 
   117  *  to by \c sem has a positive count. It then atomically decreases the 
   118  *  semaphore count.
   119  */
   120 extern DECLSPEC int SDLCALL SDL_SemWait(SDL_sem * sem);
   121 
   122 /**
   123  *  Non-blocking variant of SDL_SemWait().
   124  *  
   125  *  \return 0 if the wait succeeds, ::SDL_MUTEX_TIMEDOUT if the wait would 
   126  *          block, and -1 on error.
   127  */
   128 extern DECLSPEC int SDLCALL SDL_SemTryWait(SDL_sem * sem);
   129 
   130 /**
   131  *  Variant of SDL_SemWait() with a timeout in milliseconds.
   132  *  
   133  *  \return 0 if the wait succeeds, ::SDL_MUTEX_TIMEDOUT if the wait does not 
   134  *          succeed in the allotted time, and -1 on error.
   135  *  
   136  *  \warning On some platforms this function is implemented by looping with a 
   137  *           delay of 1 ms, and so should be avoided if possible.
   138  */
   139 extern DECLSPEC int SDLCALL SDL_SemWaitTimeout(SDL_sem * sem, Uint32 ms);
   140 
   141 /**
   142  *  Atomically increases the semaphore's count (not blocking).
   143  *  
   144  *  \return 0, or -1 on error.
   145  */
   146 extern DECLSPEC int SDLCALL SDL_SemPost(SDL_sem * sem);
   147 
   148 /**
   149  *  Returns the current count of the semaphore.
   150  */
   151 extern DECLSPEC Uint32 SDLCALL SDL_SemValue(SDL_sem * sem);
   152 
   153 /*@}*//*Semaphore functions*/
   154 
   155 
   156 /**
   157  *  \name Condition variable functions
   158  */
   159 /*@{*/
   160 
   161 /* The SDL condition variable structure, defined in SDL_cond.c */
   162 struct SDL_cond;
   163 typedef struct SDL_cond SDL_cond;
   164 
   165 /**
   166  *  Create a condition variable.
   167  */
   168 extern DECLSPEC SDL_cond *SDLCALL SDL_CreateCond(void);
   169 
   170 /**
   171  *  Destroy a condition variable.
   172  */
   173 extern DECLSPEC void SDLCALL SDL_DestroyCond(SDL_cond * cond);
   174 
   175 /**
   176  *  Restart one of the threads that are waiting on the condition variable.
   177  *  
   178  *  \return 0 or -1 on error.
   179  */
   180 extern DECLSPEC int SDLCALL SDL_CondSignal(SDL_cond * cond);
   181 
   182 /**
   183  *  Restart all threads that are waiting on the condition variable.
   184  *  \return 0 or -1 on error.
   185  */
   186 extern DECLSPEC int SDLCALL SDL_CondBroadcast(SDL_cond * cond);
   187 
   188 /**
   189  *  Wait on the condition variable, unlocking the provided mutex.
   190  *  
   191  *  \warning The mutex must be locked before entering this function!
   192  *  
   193  *  The mutex is re-locked once the condition variable is signaled.
   194  *  
   195  *  \return 0 when it is signaled, or -1 on error.
   196  */
   197 extern DECLSPEC int SDLCALL SDL_CondWait(SDL_cond * cond, SDL_mutex * mutex);
   198 
   199 /**
   200  *  Waits for at most \c ms milliseconds, and returns 0 if the condition
   201  *  variable is signaled, ::SDL_MUTEX_TIMEDOUT if the condition is not
   202  *  signaled in the allotted time, and -1 on error.
   203  *
   204  *  \warning On some platforms this function is implemented by looping with a 
   205  *           delay of 1 ms, and so should be avoided if possible.
   206  */
   207 extern DECLSPEC int SDLCALL SDL_CondWaitTimeout(SDL_cond * cond,
   208                                                 SDL_mutex * mutex, Uint32 ms);
   209 
   210 /*@}*//*Condition variable functions*/
   211 
   212 
   213 /* Ends C function definitions when using C++ */
   214 #ifdef __cplusplus
   215 /* *INDENT-OFF* */
   216 }
   217 /* *INDENT-ON* */
   218 #endif
   219 #include "close_code.h"
   220 
   221 #endif /* _SDL_mutex_h */
   222 
   223 /* vi: set ts=4 sw=4 expandtab: */