| 1 | /* Copyright (C) 2010-2015 The RetroArch team |
| 2 | * |
| 3 | * --------------------------------------------------------------------------------------- |
| 4 | * The following license statement only applies to this file (rthreads.h). |
| 5 | * --------------------------------------------------------------------------------------- |
| 6 | * |
| 7 | * Permission is hereby granted, free of charge, |
| 8 | * to any person obtaining a copy of this software and associated documentation files (the "Software"), |
| 9 | * to deal in the Software without restriction, including without limitation the rights to |
| 10 | * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, |
| 11 | * and to permit persons to whom the Software is furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. |
| 14 | * |
| 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
| 16 | * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 18 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 19 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 21 | */ |
| 22 | |
| 23 | #ifndef __LIBRETRO_SDK_RTHREADS_H__ |
| 24 | #define __LIBRETRO_SDK_RTHREADS_H__ |
| 25 | |
| 26 | #include <boolean.h> |
| 27 | #include <stdint.h> |
| 28 | #include <retro_inline.h> |
| 29 | #include <retro_miscellaneous.h> |
| 30 | |
| 31 | #if defined(__cplusplus) && !defined(_MSC_VER) |
| 32 | extern "C" { |
| 33 | #endif |
| 34 | |
| 35 | typedef struct sthread sthread_t; |
| 36 | typedef struct slock slock_t; |
| 37 | typedef struct scond scond_t; |
| 38 | |
| 39 | /** |
| 40 | * sthread_create: |
| 41 | * @start_routine : thread entry callback function |
| 42 | * @userdata : pointer to userdata that will be made |
| 43 | * available in thread entry callback function |
| 44 | * |
| 45 | * Create a new thread. |
| 46 | * |
| 47 | * Returns: pointer to new thread if successful, otherwise NULL. |
| 48 | */ |
| 49 | sthread_t *sthread_create(void (*thread_func)(void*), void *userdata); |
| 50 | |
| 51 | /** |
| 52 | * sthread_detach: |
| 53 | * @thread : pointer to thread object |
| 54 | * |
| 55 | * Detach a thread. When a detached thread terminates, its |
| 56 | * resource sare automatically released back to the system |
| 57 | * without the need for another thread to join with the |
| 58 | * terminated thread. |
| 59 | * |
| 60 | * Returns: 0 on success, otherwise it returns a non-zero error number. |
| 61 | */ |
| 62 | int sthread_detach(sthread_t *thread); |
| 63 | |
| 64 | /** |
| 65 | * sthread_join: |
| 66 | * @thread : pointer to thread object |
| 67 | * |
| 68 | * Join with a terminated thread. Waits for the thread specified by |
| 69 | * @thread to terminate. If that thread has already terminated, then |
| 70 | * it will return immediately. The thread specified by @thread must |
| 71 | * be joinable. |
| 72 | * |
| 73 | * Returns: 0 on success, otherwise it returns a non-zero error number. |
| 74 | */ |
| 75 | void sthread_join(sthread_t *thread); |
| 76 | |
| 77 | /** |
| 78 | * slock_new: |
| 79 | * |
| 80 | * Create and initialize a new mutex. Must be manually |
| 81 | * freed. |
| 82 | * |
| 83 | * Returns: pointer to a new mutex if successful, otherwise NULL. |
| 84 | **/ |
| 85 | slock_t *slock_new(void); |
| 86 | |
| 87 | /** |
| 88 | * slock_free: |
| 89 | * @lock : pointer to mutex object |
| 90 | * |
| 91 | * Frees a mutex. |
| 92 | **/ |
| 93 | void slock_free(slock_t *lock); |
| 94 | |
| 95 | /** |
| 96 | * slock_lock: |
| 97 | * @lock : pointer to mutex object |
| 98 | * |
| 99 | * Locks a mutex. If a mutex is already locked by |
| 100 | * another thread, the calling thread shall block until |
| 101 | * the mutex becomes available. |
| 102 | **/ |
| 103 | void slock_lock(slock_t *lock); |
| 104 | |
| 105 | /** |
| 106 | * slock_unlock: |
| 107 | * @lock : pointer to mutex object |
| 108 | * |
| 109 | * Unlocks a mutex. |
| 110 | **/ |
| 111 | void slock_unlock(slock_t *lock); |
| 112 | |
| 113 | /** |
| 114 | * scond_new: |
| 115 | * |
| 116 | * Creates and initializes a condition variable. Must |
| 117 | * be manually freed. |
| 118 | * |
| 119 | * Returns: pointer to new condition variable on success, |
| 120 | * otherwise NULL. |
| 121 | **/ |
| 122 | scond_t *scond_new(void); |
| 123 | |
| 124 | /** |
| 125 | * scond_free: |
| 126 | * @cond : pointer to condition variable object |
| 127 | * |
| 128 | * Frees a condition variable. |
| 129 | **/ |
| 130 | void scond_free(scond_t *cond); |
| 131 | |
| 132 | /** |
| 133 | * scond_wait: |
| 134 | * @cond : pointer to condition variable object |
| 135 | * @lock : pointer to mutex object |
| 136 | * |
| 137 | * Block on a condition variable (i.e. wait on a condition). |
| 138 | **/ |
| 139 | void scond_wait(scond_t *cond, slock_t *lock); |
| 140 | |
| 141 | /** |
| 142 | * scond_wait_timeout: |
| 143 | * @cond : pointer to condition variable object |
| 144 | * @lock : pointer to mutex object |
| 145 | * @timeout_us : timeout (in microseconds) |
| 146 | * |
| 147 | * Try to block on a condition variable (i.e. wait on a condition) until |
| 148 | * @timeout_us elapses. |
| 149 | * |
| 150 | * Returns: false (0) if timeout elapses before condition variable is |
| 151 | * signaled or broadcast, otherwise true (1). |
| 152 | **/ |
| 153 | bool scond_wait_timeout(scond_t *cond, slock_t *lock, int64_t timeout_us); |
| 154 | |
| 155 | /** |
| 156 | * scond_broadcast: |
| 157 | * @cond : pointer to condition variable object |
| 158 | * |
| 159 | * Broadcast a condition. Unblocks all threads currently blocked |
| 160 | * on the specified condition variable @cond. |
| 161 | **/ |
| 162 | int scond_broadcast(scond_t *cond); |
| 163 | |
| 164 | /** |
| 165 | * scond_signal: |
| 166 | * @cond : pointer to condition variable object |
| 167 | * |
| 168 | * Signal a condition. Unblocks at least one of the threads currently blocked |
| 169 | * on the specified condition variable @cond. |
| 170 | **/ |
| 171 | void scond_signal(scond_t *cond); |
| 172 | |
| 173 | #if defined(__cplusplus) && !defined(_MSC_VER) |
| 174 | } |
| 175 | #endif |
| 176 | |
| 177 | #endif |