github.com/256dpi/max-go@v0.7.0/lib/max/ext_systhread.h (about) 1 #ifndef _EXT_SYSTHREAD_H_ 2 #define _EXT_SYSTHREAD_H_ 3 4 #include "ext_obex.h" 5 6 BEGIN_USING_C_LINKAGE 7 8 /** An opaque thread instance pointer. 9 @ingroup threading 10 */ 11 typedef void *t_systhread; 12 13 /** An opaque mutex handle. 14 @ingroup threading 15 */ 16 typedef void *t_systhread_mutex; 17 18 19 /** An opaque cond handle. 20 @ingroup threading 21 */ 22 typedef void *t_systhread_cond; 23 24 typedef void *t_systhread_rwlock; 25 26 typedef void *t_systhread_key; 27 28 /** systhread_mutex_new() flags 29 @ingroup threading 30 */ 31 typedef enum { 32 SYSTHREAD_MUTEX_NORMAL = 0x00000000, ///< Normal 33 SYSTHREAD_MUTEX_ERRORCHECK = 0x00000001, ///< Error-checking 34 SYSTHREAD_MUTEX_RECURSIVE = 0x00000002 ///< Recursive 35 } e_max_systhread_mutex_flags; 36 37 typedef enum { 38 SYSTHREAD_PRIORITY_MIN = -30, 39 SYSTHREAD_PRIORITY_DEFAULT = 0, 40 #ifndef LINUX_VERSION 41 SYSTHREAD_PRIORITY_MAX = 30 42 #else 43 SYSTHREAD_PRIORITY_MAX = 0 // Linux does not have high prio non-rt threads 44 #endif 45 } e_max_systhread_priority; 46 47 typedef enum { 48 SYSTHREAD_RWLOCK_NORMAL = 0x00000000, 49 SYSTHREAD_RWLOCK_LITE = 0x00000001 50 } e_max_systhread_rwlock_flags; 51 52 /** 53 Create a new thread. 54 @ingroup threading 55 56 @param entryproc A method to call in the new thread when the thread is created. 57 @param arg An argument to pass to the method specified for entryproc. 58 Typically this might be a pointer to your object's struct. 59 @param stacksize Not used. Pass 0 for this argument. 60 @param priority Pass 0 for default priority. The priority can range from -32 to 32 where -32 is low, 0 is default and 32 is high. 61 @param flags Not used. Pass 0 for this argument. 62 @param thread The address of a #t_systhread where this thread's instance pointer will be stored. 63 @return A Max error code as defined in #e_max_errorcodes. 64 */ 65 long systhread_create(method entryproc, void *arg, long stacksize, long priority, long flags, t_systhread *thread); 66 67 68 /** 69 Forcefully kill a thread -- not recommended. 70 @ingroup threading 71 72 @param thread The thread to kill. 73 @return A Max error code as defined in #e_max_errorcodes. 74 */ 75 long systhread_terminate(t_systhread thread); 76 77 78 /** 79 Suspend the execution of the calling thread. 80 @ingroup threading 81 82 @param milliseconds The number of milliseconds to suspend the execution of the calling thread. 83 The actual amount of time may be longer depending on various factors. 84 */ 85 void systhread_sleep(long milliseconds); 86 87 88 /** 89 Exit the calling thread. 90 Call this from within a thread made using systhread_create() when the thread is no longer needed. 91 92 @ingroup threading 93 @param status You will typically pass 0 for status. 94 This value will be accessible by systhread_join(), if needed. 95 */ 96 void systhread_exit(long status); 97 98 99 /** 100 Wait for thread to quit and get return value from systhread_exit(). 101 102 @ingroup threading 103 @param thread The thread to join. 104 @param retval The address of a long to hold the return value (status) from systhread_exit(). 105 @return A Max error code as defined in #e_max_errorcodes. 106 107 @remark If your object is freed, and your thread function accesses memory from your object, 108 then you will obviously have a memory violation. 109 A common use of systhread_join() is to prevent this situation by waiting (in your free method) 110 for the thread to exit. 111 */ 112 long systhread_join(t_systhread thread, unsigned int* retval); 113 114 /** 115 Detach a thread. After detaching a thread you cannot call systhread_join() on it. 116 117 @ingroup threading 118 @param thread The thread to join. 119 @return A Max error code as defined in #e_max_errorcodes. 120 121 @remark You should either call systhread_join() on a thread or systhread_detach() 122 to allow the system to reclaim resources. 123 */ 124 long systhread_detach(t_systhread thread); 125 126 /** 127 Return the thread instance pointer for the calling thread. 128 @ingroup threading 129 @return The thread instance pointer for the thread from which this function is called. 130 */ 131 t_systhread systhread_self(void); 132 133 /** 134 Compare two threads to see if they reference the same thread. 135 The t_systhread type is opaque and two should not be compared directly. 136 @ingroup threading 137 @param thread1 the first thread to be compared 138 @param thread2 the second thread to be compared 139 @return nonzero if the two parameters reference the same thread 140 */ 141 long systhread_equal(t_systhread thread1, t_systhread thread2); 142 143 /** 144 Set the thread priority for the given thread. 145 @ingroup threading 146 @param thread The thread for which to set the priority. 147 @param priority A value in the range -32 to 32 where -32 is lowest, 0 is default, and 32 is highest. 148 */ 149 void systhread_setpriority(t_systhread thread, int priority); 150 151 /** 152 Get the thread priority for the given thread. 153 @ingroup threading 154 @param thread The thread for which to find the priority. 155 @return The current priority value for the given thread. 156 */ 157 int systhread_getpriority(t_systhread thread); 158 159 char *systhread_getstackbase(void); 160 161 /** Check to see if the function currently being executed is in the main thread. 162 @ingroup threading 163 @return Returns true if the function is being executed in the main thread, otherwise false. 164 */ 165 short systhread_ismainthread(void); 166 167 168 /** Check to see if the function currently being executed is in a scheduler thread. 169 @ingroup threading 170 @return Returns true if the function is being executed in a scheduler thread, otherwise false. 171 */ 172 short systhread_istimerthread(void); 173 174 175 /** Check to see if the function currently being executed is in an audio thread. 176 @ingroup threading 177 @return Returns true if the function is being executed in an audio thread, otherwise false. 178 */ 179 180 short systhread_isaudiothread(void); 181 182 /** 183 Set the name of the current thread, for debugging purposes. 184 Recommended to call from the top of the entryproc passed to systhread_create 185 186 @ingroup threading 187 @param name The name to be given. 188 */ 189 void systhread_set_name(const char* name); 190 191 /** 192 Create a new mutex, which can be used to place thread locks around critical code. 193 The mutex should be freed with systhread_mutex_free(). 194 @ingroup mutex 195 196 @param pmutex The address of a variable to store the mutex pointer. 197 @param flags Flags to determine the behaviour of the mutex, as defined in #e_max_systhread_mutex_flags. 198 @return A Max error code as defined in #e_max_errorcodes. 199 200 @remark One reason to use systhread_mutex_new() instead of @ref critical is to 201 create non-recursive locks, which are lighter-weight than recursive locks. 202 */ 203 long systhread_mutex_new(t_systhread_mutex *pmutex,long flags); 204 205 206 /** 207 Free a mutex created with systhread_mutex_new(). 208 @ingroup mutex 209 @param pmutex The mutex instance pointer. 210 @return A Max error code as defined in #e_max_errorcodes. 211 */ 212 long systhread_mutex_free(t_systhread_mutex pmutex); 213 214 215 /** 216 Enter block of locked code code until a systhread_mutex_unlock() is reached. 217 It is important to keep the code in this block as small as possible. 218 @ingroup mutex 219 @param pmutex The mutex instance pointer. 220 @return A Max error code as defined in #e_max_errorcodes. 221 @see systhread_mutex_trylock() 222 */ 223 long systhread_mutex_lock(t_systhread_mutex pmutex); 224 225 226 /** 227 Exit a block of code locked with systhread_mutex_lock(). 228 @ingroup mutex 229 @param pmutex The mutex instance pointer. 230 @return A Max error code as defined in #e_max_errorcodes. 231 */ 232 long systhread_mutex_unlock(t_systhread_mutex pmutex); 233 234 235 /** 236 Try to enter block of locked code code until a systhread_mutex_unlock() is reached. 237 If the lock cannot be entered, this function will return non-zero. 238 239 @ingroup mutex 240 @param pmutex The mutex instance pointer. 241 @return Returns non-zero if there was a problem entering. 242 @see systhread_mutex_lock() 243 */ 244 long systhread_mutex_trylock(t_systhread_mutex pmutex); 245 246 247 /** 248 Convenience utility that combines systhread_mutex_new() and systhread_mutex_lock(). 249 @ingroup mutex 250 @param pmutex The address of a variable to store the mutex pointer. 251 @param flags Flags to determine the behaviour of the mutex, as defined in #e_max_systhread_mutex_flags. 252 @return A Max error code as defined in #e_max_errorcodes. 253 */ 254 long systhread_mutex_newlock(t_systhread_mutex *pmutex,long flags); 255 256 t_max_err systhread_rwlock_new(t_systhread_rwlock *rwlock, long flags); 257 t_max_err systhread_rwlock_free(t_systhread_rwlock rwlock); 258 t_max_err systhread_rwlock_rdlock(t_systhread_rwlock rwlock); 259 t_max_err systhread_rwlock_tryrdlock(t_systhread_rwlock rwlock); 260 t_max_err systhread_rwlock_rdunlock(t_systhread_rwlock rwlock); 261 t_max_err systhread_rwlock_wrlock(t_systhread_rwlock rwlock); 262 t_max_err systhread_rwlock_trywrlock(t_systhread_rwlock rwlock); 263 t_max_err systhread_rwlock_wrunlock(t_systhread_rwlock rwlock); 264 t_max_err systhread_rwlock_setspintime(t_systhread_rwlock rwlock, double spintime_ms); 265 t_max_err systhread_rwlock_getspintime(t_systhread_rwlock rwlock, double *spintime_ms); 266 267 long systhread_cond_new(t_systhread_cond *pcond, long flags); 268 long systhread_cond_free(t_systhread_cond pcond); 269 long systhread_cond_wait(t_systhread_cond pcond, t_systhread_mutex pmutex); 270 long systhread_cond_signal(t_systhread_cond pcond); 271 long systhread_cond_broadcast(t_systhread_cond pcond); 272 273 long systhread_key_create(t_systhread_key *key, void (*destructor)(void*)); 274 long systhread_key_delete(t_systhread_key key); 275 void* systhread_getspecific(t_systhread_key key); 276 long systhread_setspecific(t_systhread_key key, const void *value); 277 void systhread_eliminatedenormals(void); 278 279 END_USING_C_LINKAGE 280 281 #endif // _EXT_SYSTHREAD_H_ 282