| 1 | package SDL2::mutex 0.01 { | ||
| 2 | use SDL2::Utils; | ||
| 3 | # | ||
| 4 | use SDL2::stdinc; | ||
| 5 | use SDL2::error; | ||
| 6 | # | ||
| 7 | define mutex => [ [ SDL_MUTEX_TIMEDOUT => 1 ], [ SDL_MUTEX_MAXWAIT => -1 ] ]; | ||
| 8 | |||
| 9 | package SDL2::Mutex { | ||
| 10 | use SDL2::Utils; | ||
| 11 | our $TYPE = has(); | ||
| 12 | }; | ||
| 13 | attach mutex => { | ||
| 14 | SDL_CreateMutex => [ [], 'SDL_Mutex' ], | ||
| 15 | SDL_LockMutex => [ ['SDL_Mutex'], 'int' ], | ||
| 16 | SDL_TryLockMutex => [ ['SDL_Mutex'], 'int' ], | ||
| 17 | SDL_UnlockMutex => [ ['SDL_Mutex'], 'int' ], | ||
| 18 | SDL_DestroyMutex => [ ['SDL_Mutex'] ] | ||
| 19 | }; | ||
| 20 | |||
| 21 | package SDL2::Semaphore { | ||
| 22 | use SDL2::Utils; | ||
| 23 | our $TYPE = has(); | ||
| 24 | }; | ||
| 25 | attach mutex => { | ||
| 26 | SDL_CreateSemaphore => [ ['uint32'], 'SDL_Semaphore' ], | ||
| 27 | SDL_DestroySemaphore => [ ['SDL_Semaphore'] ], | ||
| 28 | SDL_SemWait => [ ['SDL_Semaphore'], 'int' ], | ||
| 29 | SDL_SemTryWait => [ ['SDL_Semaphore'], 'int' ], | ||
| 30 | SDL_SemWaitTimeout => [ [ 'SDL_Semaphore', 'uint32' ], 'int' ], | ||
| 31 | SDL_SemPost => [ ['SDL_Semaphore'], 'int' ], | ||
| 32 | SDL_SemValue => [ ['SDL_Semaphore'], 'uint32' ] | ||
| 33 | }; | ||
| 34 | |||
| 35 | package SDL2::Cond { | ||
| 36 | use SDL2::Utils; | ||
| 37 | our $TYPE = has(); | ||
| 38 | }; | ||
| 39 | attach mutex => { | ||
| 40 | SDL_CreateCond => [ [], 'SDL_Cond' ], | ||
| 41 | SDL_DestroyCond => [ ['SDL_Cond'] ], | ||
| 42 | SDL_CondSignal => [ ['SDL_Cond'], 'int' ], | ||
| 43 | SDL_CondBroadcast => [ ['SDL_Cond'], 'int' ], | ||
| 44 | SDL_CondWait => [ [ 'SDL_Cond', 'SDL_Mutex' ], 'int' ], | ||
| 45 | SDL_CondWaitTimeout => [ [ 'SDL_Cond', 'SDL_Mutex', 'uint32' ], 'int' ], | ||
| 46 | }; | ||
| 47 | |||
| 48 | =encoding utf-8 | ||
| 49 | |||
| 50 | =head1 NAME | ||
| 51 | |||
| 52 | SDL2::mutex - Functions to Provide Thread Synchronization Primitives | ||
| 53 | |||
| 54 | =head1 SYNOPSIS | ||
| 55 | |||
| 56 | use SDL2 qw[:mutex]; | ||
| 57 | |||
| 58 | =head1 DESCRIPTION | ||
| 59 | |||
| 60 | Functions in this group provide thread synchronization primitives for | ||
| 61 | multi-threaded programing. | ||
| 62 | |||
| 63 | There are three primitives available in SDL: | ||
| 64 | |||
| 65 | =over | ||
| 66 | |||
| 67 | =item Mutex | ||
| 68 | |||
| 69 | =item Semaphore | ||
| 70 | |||
| 71 | =item Condition Variable | ||
| 72 | |||
| 73 | =back | ||
| 74 | |||
| 75 | The SDL mutex is implemented as a recursive mutex so you can nest lock and | ||
| 76 | unlock calls to the same mutex. | ||
| 77 | |||
| 78 | =head1 Functions | ||
| 79 | |||
| 80 | These functions may be imported by name or with the C<:mutex> tag. | ||
| 81 | |||
| 82 | =head2 C<SDL_CreateMutex( )> | ||
| 83 | |||
| 84 | Create a new mutex. | ||
| 85 | |||
| 86 | All newly-created mutexes begin in the _unlocked_ state. | ||
| 87 | |||
| 88 | Calls to L<< C<SDL_LockMutex( ... )>|/C<SDL_LockMutex( ... )> >> will not | ||
| 89 | return while the mutex is locked by another thread. See L<< C<SDL_TryLockMutex( | ||
| 90 | ... )>|/C<SDL_TryLockMutex( ... )> >> to attempt to lock without blocking. | ||
| 91 | |||
| 92 | SDL mutexes are reentrant. | ||
| 93 | |||
| 94 | Returns the initialized and unlocked mutex or C<undef> on failure; call | ||
| 95 | C<SDL_GetError( )> for more information. | ||
| 96 | |||
| 97 | =head2 C<SDL_LockMutex( ... )> | ||
| 98 | |||
| 99 | Lock the mutex. | ||
| 100 | |||
| 101 | This will block until the mutex is available, which is to say it is in the | ||
| 102 | unlocked state and the OS has chosen the caller as the next thread to lock it. | ||
| 103 | Of all threads waiting to lock the mutex, only one may do so at a time. | ||
| 104 | |||
| 105 | It is legal for the owning thread to lock an already-locked mutex. It must | ||
| 106 | unlock it the same number of times before it is actually made available for | ||
| 107 | other threads in the system (this is known as a "recursive mutex"). | ||
| 108 | |||
| 109 | Expected parameters include: | ||
| 110 | |||
| 111 | =over | ||
| 112 | |||
| 113 | =item C<mutex> - the mutex to lock | ||
| 114 | |||
| 115 | =back | ||
| 116 | |||
| 117 | Returns C<0> on success, or C<-1> on error. | ||
| 118 | |||
| 119 | =head2 C<SDL_TryLockMutex( ... )> | ||
| 120 | |||
| 121 | Try to lock a mutex without blocking. | ||
| 122 | |||
| 123 | This works just like L<< C<SDL_LockMutex( ... )>|/C<SDL_LockMutex( ... )> >>, | ||
| 124 | but if the mutex is not available, this function returns C<SDL_MUTEX_TIMEOUT> | ||
| 125 | immediately. | ||
| 126 | |||
| 127 | This technique is useful if you need exclusive access to a resource but don't | ||
| 128 | want to wait for it, and will return to it to try again later. | ||
| 129 | |||
| 130 | Expected parameters include: | ||
| 131 | |||
| 132 | =over | ||
| 133 | |||
| 134 | =item C<mutex> - the mutex to attempt to lock | ||
| 135 | |||
| 136 | =back | ||
| 137 | |||
| 138 | Returns C<0>, C<SDL_MUTEX_TIMEDOUT>, or C<-1> on error; call C<SDL_GetError( )> | ||
| 139 | for more information. | ||
| 140 | |||
| 141 | =head2 C<SDL_UnlockMutex( ... )> | ||
| 142 | |||
| 143 | Unlock the mutex. | ||
| 144 | |||
| 145 | It is legal for the owning thread to lock an already-locked mutex. It must | ||
| 146 | unlock it the same number of times before it is actually made available for | ||
| 147 | other threads in the system (this is known as a "recursive mutex"). | ||
| 148 | |||
| 149 | It is an error to unlock a mutex that has not been locked by the current | ||
| 150 | thread, and doing so results in undefined behavior. | ||
| 151 | |||
| 152 | It is also an error to unlock a mutex that isn't locked at all. | ||
| 153 | |||
| 154 | Expected parameters include: | ||
| 155 | |||
| 156 | =over | ||
| 157 | |||
| 158 | =item C<mutex> - the mutex to unlock | ||
| 159 | |||
| 160 | =back | ||
| 161 | |||
| 162 | Returns C<0> on success, or C<-1> on error. | ||
| 163 | |||
| 164 | =head2 C<SDL_DestroyMutex( ... )> | ||
| 165 | |||
| 166 | Destroy a mutex created with SDL_CreateMutex(). | ||
| 167 | |||
| 168 | This function must be called on any mutex that is no longer needed. Failure to | ||
| 169 | destroy a mutex will result in a system memory or resource leak. While it is | ||
| 170 | safe to destroy a mutex that is _unlocked_, it is not safe to attempt to | ||
| 171 | destroy a locked mutex, and may result in undefined behavior depending on the | ||
| 172 | platform. | ||
| 173 | |||
| 174 | Expected parameters include: | ||
| 175 | |||
| 176 | =over | ||
| 177 | |||
| 178 | =item C<mutex> - the mutex to destroy | ||
| 179 | |||
| 180 | =back | ||
| 181 | |||
| 182 | =head2 C<SDL_CreateSemaphore( ... )> | ||
| 183 | |||
| 184 | Create a semaphore. | ||
| 185 | |||
| 186 | This function creates a new semaphore and initializes it with the value | ||
| 187 | C<initial_value>. Each wait operation on the semaphore will atomically | ||
| 188 | decrement the semaphore value and potentially block if the semaphore value is | ||
| 189 | C<0>. Each post operation will atomically increment the semaphore value and | ||
| 190 | wake waiting threads and allow them to retry the wait operation. | ||
| 191 | |||
| 192 | Expected parameters include: | ||
| 193 | |||
| 194 | =over | ||
| 195 | |||
| 196 | =item C<initial_value> - the starting value of the semaphore | ||
| 197 | |||
| 198 | =back | ||
| 199 | |||
| 200 | Returns a new L<semaphore|SDL2::Semaphore> or C<undef> on failure; call | ||
| 201 | C<SDL_GetError( )> for more information. | ||
| 202 | |||
| 203 | =head2 C<SDL_DestroySemaphore( ... )> | ||
| 204 | |||
| 205 | Destroy a semaphore. | ||
| 206 | |||
| 207 | It is not safe to destroy a semaphore if there are threads currently waiting on | ||
| 208 | it. | ||
| 209 | |||
| 210 | Expected parameters include: | ||
| 211 | |||
| 212 | =over | ||
| 213 | |||
| 214 | =item C<sem> - the semaphore to destroy | ||
| 215 | |||
| 216 | =back | ||
| 217 | |||
| 218 | =head2 C<SDL_SemWait( ... )> | ||
| 219 | |||
| 220 | Wait until a semaphore has a positive value and then decrements it. | ||
| 221 | |||
| 222 | This function suspends the calling thread until either the semaphore pointed to | ||
| 223 | by `sem` has a positive value or the call is interrupted by a signal or error. | ||
| 224 | If the call is successful it will atomically decrement the semaphore value. | ||
| 225 | |||
| 226 | This function is the equivalent of calling L<< C<SDL_SemWaitTimeout( ... | ||
| 227 | )>|/C<SDL_SemWaitTimeout( ... )> >> with a time length of C<SDL_MUTEX_MAXWAIT>. | ||
| 228 | |||
| 229 | Expected parameters include: | ||
| 230 | |||
| 231 | =over | ||
| 232 | |||
| 233 | =item C<sem> - the semaphore to wait on | ||
| 234 | |||
| 235 | =back | ||
| 236 | |||
| 237 | Returns C<0> on success or a negative error code on failure; call | ||
| 238 | C<SDL_GetError( )> for more information. | ||
| 239 | |||
| 240 | =head2 C<SDL_SemTryWait( ... )> | ||
| 241 | |||
| 242 | See if a semaphore has a positive value and decrement it if it does. | ||
| 243 | |||
| 244 | This function checks to see if the semaphore pointed to by C<sem> has a | ||
| 245 | positive value and atomically decrements the semaphore value if it does. If the | ||
| 246 | semaphore doesn't have a positive value, the function immediately returns | ||
| 247 | C<SDL_MUTEX_TIMEDOUT>. | ||
| 248 | |||
| 249 | Expected parameters include: | ||
| 250 | |||
| 251 | =over | ||
| 252 | |||
| 253 | =item C<sem> - the semaphore to wait on | ||
| 254 | |||
| 255 | =back | ||
| 256 | |||
| 257 | Returns C<0> if the wait succeeds, C<SDL_MUTEX_TIMEDOUT> if the wait would | ||
| 258 | block, or a negative error code on failure; call C<SDL_GetError( )> for more | ||
| 259 | information. | ||
| 260 | |||
| 261 | =head2 C<SDL_SemWaitTimeout( ... )> | ||
| 262 | |||
| 263 | Wait until a semaphore has a positive value and then decrements it. | ||
| 264 | |||
| 265 | This function suspends the calling thread until either the semaphore pointed to | ||
| 266 | by `sem` has a positive value, the call is interrupted by a signal or error, or | ||
| 267 | the specified time has elapsed. If the call is successful it will atomically | ||
| 268 | decrement the semaphore value. | ||
| 269 | |||
| 270 | Expected parameters include: | ||
| 271 | |||
| 272 | =over | ||
| 273 | |||
| 274 | =item C<sem> - the semaphore to wait on | ||
| 275 | |||
| 276 | =item C<ms> - the length of the timeout, in milliseconds | ||
| 277 | |||
| 278 | =back | ||
| 279 | |||
| 280 | Returns C<0> if the wait succeeds, C<SDL_MUTEX_TIMEDOUT> if the wait does not | ||
| 281 | succeed in the allotted time, or a negative error code on failure; call | ||
| 282 | C<SDL_GetError( )> for more information. | ||
| 283 | |||
| 284 | =head2 C<SDL_SemPost( ... )> | ||
| 285 | |||
| 286 | Atomically increment a semaphore's value and wake waiting threads. | ||
| 287 | |||
| 288 | Expected parameters include: | ||
| 289 | |||
| 290 | =over | ||
| 291 | |||
| 292 | =item C<sem> - the semaphore to increment | ||
| 293 | |||
| 294 | =back | ||
| 295 | |||
| 296 | Returns C<0> on success or a negative error code on failure; call | ||
| 297 | C<SDL_GetError( )> for more information. | ||
| 298 | |||
| 299 | =head2 C<SDL_SemValue( ... )> | ||
| 300 | |||
| 301 | Get the current value of a semaphore. | ||
| 302 | |||
| 303 | Expected parameters include: | ||
| 304 | |||
| 305 | =over | ||
| 306 | |||
| 307 | =item C<sem> - the semaphore to query | ||
| 308 | |||
| 309 | =back | ||
| 310 | |||
| 311 | Returns the current value of the semaphore. | ||
| 312 | |||
| 313 | =head2 C<SDL_CreateCond( )> | ||
| 314 | |||
| 315 | Create a condition variable. | ||
| 316 | |||
| 317 | Returns a new condition variable or C<undef> on failure; call C<SDL_GetError( | ||
| 318 | )> for more information. | ||
| 319 | |||
| 320 | =head2 C<SDL_DestroyCond( ... )> | ||
| 321 | |||
| 322 | Destroy a condition variable. | ||
| 323 | |||
| 324 | Expected parameters include: | ||
| 325 | |||
| 326 | =over | ||
| 327 | |||
| 328 | =item C<cond> - the condition variable to destroy | ||
| 329 | |||
| 330 | =back | ||
| 331 | |||
| 332 | =head2 C<SDL_CondSignal( ... )> | ||
| 333 | |||
| 334 | Restart one of the threads that are waiting on the condition variable. | ||
| 335 | |||
| 336 | Expected parameters include: | ||
| 337 | |||
| 338 | =over | ||
| 339 | |||
| 340 | =item C<cond> - the condition variable to signal | ||
| 341 | |||
| 342 | =back | ||
| 343 | |||
| 344 | Returns C<0> on success or a negative error code on failure; call | ||
| 345 | C<SDL_GetError( )> for more information. | ||
| 346 | |||
| 347 | =head2 C<SDL_CondBroadcast( ... )> | ||
| 348 | |||
| 349 | Restart all threads that are waiting on the condition variable. | ||
| 350 | |||
| 351 | Expected parameters include: | ||
| 352 | |||
| 353 | =over | ||
| 354 | |||
| 355 | =item C<cond> - the condition variable to signal | ||
| 356 | |||
| 357 | =back | ||
| 358 | |||
| 359 | Returns C<0> on success or a negative error code on failure; call | ||
| 360 | C<SDL_GetError( )> for more information. | ||
| 361 | |||
| 362 | =head2 C<SDL_CondWait( ... )> | ||
| 363 | |||
| 364 | Wait until a condition variable is signaled. | ||
| 365 | |||
| 366 | This function unlocks the specified C<mutex> and waits for another thread to | ||
| 367 | call L<< C<SDL_CondSignal( ... )>|/C<SDL_CondSignal( ... )> >> or L<< | ||
| 368 | C<SDL_CondBroadcast( ... )>|/C<SDL_CondBroadcast( ... )> >> on the condition | ||
| 369 | variable C<cond>. Once the condition variable is signaled, the mutex is | ||
| 370 | re-locked and the function returns. | ||
| 371 | |||
| 372 | The mutex must be locked before calling this function. | ||
| 373 | |||
| 374 | This function is the equivalent of calling L<< C<SDL_CondWaitTimeout( ... | ||
| 375 | )>|/C<SDL_CondWaitTimeout( ... )> >> with a time length of | ||
| 376 | C<SDL_MUTEX_MAXWAIT>. | ||
| 377 | |||
| 378 | Expected parameters include: | ||
| 379 | |||
| 380 | =over | ||
| 381 | |||
| 382 | =item C<cond> - the condition variable to wait on | ||
| 383 | |||
| 384 | =item C<mutex> - the mutex used to coordinate thread access | ||
| 385 | |||
| 386 | =back | ||
| 387 | |||
| 388 | Returns C<0> when it is signaled or a negative error code on failure; call | ||
| 389 | C<SDL_GetError( )> for more information. | ||
| 390 | |||
| 391 | =head2 C<SDL_CondWaitTimeout( ... )> | ||
| 392 | |||
| 393 | Wait until a condition variable is signaled or a certain time has passed. | ||
| 394 | |||
| 395 | This function unlocks the specified C<mutex> and waits for another thread to | ||
| 396 | call L<< C<SDL_CondSignal( ... )>|/C<SDL_CondSignal( ... )> >> or L<< | ||
| 397 | C<SDL_CondBroadcast( ... )>|/C<SDL_CondBroadcast( ... )> >> on the condition | ||
| 398 | variable C<cond>, or for the specified time to elapse. Once the condition | ||
| 399 | variable is signaled or the time elapsed, the mutex is re-locked and the | ||
| 400 | function returns. | ||
| 401 | |||
| 402 | The mutex must be locked before calling this function. | ||
| 403 | |||
| 404 | |||
| 405 | Expected parameters include: | ||
| 406 | |||
| 407 | =over | ||
| 408 | |||
| 409 | =item C<cond> - the condition variable to wait on | ||
| 410 | |||
| 411 | =item C<mutex> - the mutex used to coordinate thread access | ||
| 412 | |||
| 413 | =item C<ms> - the maximum time to wait, in milliseconds, or C<SDL_MUTEX_MAXWAIT> to wait indefinitely | ||
| 414 | |||
| 415 | =back | ||
| 416 | |||
| 417 | Returns C<0> if the condition variable is signaled, C<SDL_MUTEX_TIMEDOUT> if | ||
| 418 | the condition is not signaled in the allotted time, or a negative error code on | ||
| 419 | failure; call C<SDL_GetError( )> for more information. | ||
| 420 | |||
| 421 | =head1 Defined Variables and Enumerations | ||
| 422 | |||
| 423 | These variables may be imported by name or with the C<:mutex> tag. Enumerations | ||
| 424 | may be imported with the given tag. | ||
| 425 | |||
| 426 | =head2 C<SDL_MUTEX_TIMEDOUT> | ||
| 427 | |||
| 428 | Synchronization functions which can time out return this value if they time | ||
| 429 | out. | ||
| 430 | |||
| 431 | =head2 C<SDL_MUTEX_MAXWAIT> | ||
| 432 | |||
| 433 | This is the timeout value which corresponds to never time out. | ||
| 434 | |||
| 435 | =head1 LICENSE | ||
| 436 | |||
| 437 | Copyright (C) Sanko Robinson. | ||
| 438 | |||
| 439 | This library is free software; you can redistribute it and/or modify it under | ||
| 440 | the terms found in the Artistic License 2. Other copyrights, terms, and | ||
| 441 | conditions may apply to data transmitted through this module. | ||
| 442 | |||
| 443 | =head1 AUTHOR | ||
| 444 | |||
| 445 | Sanko Robinson E<lt>sanko@cpan.orgE<gt> | ||
| 446 | |||
| 447 | =begin stopwords | ||
| 448 | |||
| 449 | sem | ||
| 450 | |||
| 451 | =end stopwords | ||
| 452 | |||
| 453 | =cut | ||
| 454 | |||
| 455 | }; | ||
| 456 | 1; |