Max 5 API Reference
00001 #ifndef _EXT_SYSTHREAD_H_ 00002 #define _EXT_SYSTHREAD_H_ 00003 00004 #ifdef __cplusplus 00005 extern "C" { 00006 #endif 00007 00008 /** An opaque thread instance pointer. 00009 @ingroup threading 00010 */ 00011 typedef void *t_systhread; 00012 00013 00014 /** An opaque mutex handle. 00015 @ingroup threading 00016 */ 00017 typedef void *t_systhread_mutex; 00018 00019 00020 /** An opaque cond handle. 00021 @ingroup threading 00022 */ 00023 typedef void *t_systhread_cond; 00024 00025 00026 /** systhread_mutex_new() flags 00027 @ingroup threading 00028 */ 00029 typedef enum { 00030 SYSTHREAD_MUTEX_NORMAL = 0x00000000, ///< Normal 00031 SYSTHREAD_MUTEX_ERRORCHECK = 0x00000001, ///< Error-checking 00032 SYSTHREAD_MUTEX_RECURSIVE = 0x00000002 ///< Recursive 00033 } e_max_systhread_mutex_flags; 00034 00035 typedef enum { 00036 SYSTHREAD_PRIORITY_MIN = -30, 00037 SYSTHREAD_PRIORITY_DEFAULT = 0, 00038 SYSTHREAD_PRIORITY_MAX = 30 00039 } e_max_systhread_priority; 00040 00041 /** 00042 Create a new thread. 00043 @ingroup threading 00044 00045 @param entryproc A method to call in the new thread when the thread is created. 00046 @param arg An argument to pass to the method specified for entryproc. 00047 Typically this might be a pointer to your object's struct. 00048 @param stacksize Not used. Pass 0 for this argument. 00049 @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. 00050 @param flags Not used. Pass 0 for this argument. 00051 @param thread The address of a #t_systhread where this thread's instance pointer will be stored. 00052 @return A Max error code as defined in #e_max_errorcodes. 00053 */ 00054 long systhread_create(method entryproc, void *arg, long stacksize, long priority, long flags, t_systhread *thread); 00055 00056 00057 /** 00058 Forcefully kill a thread -- not recommended. 00059 @ingroup threading 00060 00061 @param thread The thread to kill. 00062 @return A Max error code as defined in #e_max_errorcodes. 00063 */ 00064 long systhread_terminate(t_systhread thread); 00065 00066 00067 /** 00068 Suspend the execution of the calling thread. 00069 @ingroup threading 00070 00071 @param milliseconds The number of milliseconds to suspend the execution of the calling thread. 00072 The actual amount of time may be longer depending on various factors. 00073 */ 00074 void systhread_sleep(long milliseconds); 00075 00076 00077 /** 00078 Exit the calling thread. 00079 Call this from within a thread made using systhread_create() when the thread is no longer needed. 00080 00081 @ingroup threading 00082 @param status You will typically pass 0 for status. 00083 This value will be accessible by systhread_join(), if needed. 00084 */ 00085 void systhread_exit(long status); 00086 00087 00088 /** 00089 Wait for thread to quit and get return value from systhread_exit(). 00090 00091 @ingroup threading 00092 @param thread The thread to join. 00093 @param retval The address of a long to hold the return value (status) from systhread_exit(). 00094 @return A Max error code as defined in #e_max_errorcodes. 00095 00096 @remark If your object is freed, and your thread function accesses memory from your object, 00097 then you will obviously have a memory violation. 00098 A common use of systhread_join() is to prevent this situation by waiting (in your free method) 00099 for the thread to exit. 00100 */ 00101 long systhread_join(t_systhread thread, unsigned int* retval); // 00102 00103 00104 /** 00105 Return the thread instance pointer for the calling thread. 00106 @ingroup threading 00107 @return The thread instance pointer for the thread from which this function is called. 00108 */ 00109 t_systhread systhread_self(void); 00110 00111 /** 00112 Set the thread priority for the given thread. 00113 @ingroup threading 00114 @param thread The thread for which to set the priority. 00115 @param priority A value in the range -32 to 32 where -32 is lowest, 0 is default, and 32 is highest. 00116 */ 00117 void systhread_setpriority(t_systhread thread, int priority); 00118 00119 /** 00120 Get the thread priority for the given thread. 00121 @ingroup threading 00122 @param thread The thread for which to find the priority. 00123 @return The current priority value for the given thread. 00124 */ 00125 int systhread_getpriority(t_systhread thread); 00126 00127 char *systhread_getstackbase(void); 00128 00129 00130 // private 00131 void systhread_init(void); 00132 void systhread_mainstacksetup(void); 00133 void systhread_timerstacksetup(void); 00134 short systhread_stackcheck(void); 00135 00136 00137 /** Check to see if the function currently being executed is in the main thread. 00138 @ingroup threading 00139 @return Returns true if the function is being executed in the main thread, otherwise false. 00140 */ 00141 short systhread_ismainthread(void); 00142 00143 00144 /** Check to see if the function currently being executed is in the scheduler thread. 00145 @ingroup threading 00146 @return Returns true if the function is being executed in the main thread, otherwise false. 00147 */ 00148 short systhread_istimerthread(void); 00149 00150 00151 00152 00153 /** 00154 Create a new mutex, which can be used to place thread locks around critical code. 00155 The mutex should be freed with systhread_mutex_free(). 00156 @ingroup mutex 00157 00158 @param pmutex The address of a variable to store the mutex pointer. 00159 @param flags Flags to determine the behaviour of the mutex, as defined in #e_max_systhread_mutex_flags. 00160 @return A Max error code as defined in #e_max_errorcodes. 00161 00162 @remark One reason to use systhread_mutex_new() instead of @ref critical is to 00163 create non-recursive locks, which are lighter-weight than recursive locks. 00164 */ 00165 long systhread_mutex_new(t_systhread_mutex *pmutex,long flags); 00166 00167 00168 /** 00169 Free a mutex created with systhread_mutex_new(). 00170 @ingroup mutex 00171 @param pmutex The mutex instance pointer. 00172 @return A Max error code as defined in #e_max_errorcodes. 00173 */ 00174 long systhread_mutex_free(t_systhread_mutex pmutex); 00175 00176 00177 /** 00178 Enter block of locked code code until a systhread_mutex_unlock() is reached. 00179 It is important to keep the code in this block as small as possible. 00180 @ingroup mutex 00181 @param pmutex The mutex instance pointer. 00182 @return A Max error code as defined in #e_max_errorcodes. 00183 @see systhread_mutex_trylock() 00184 */ 00185 long systhread_mutex_lock(t_systhread_mutex pmutex); 00186 00187 00188 /** 00189 Exit a block of code locked with systhread_mutex_lock(). 00190 @ingroup mutex 00191 @param pmutex The mutex instance pointer. 00192 @return A Max error code as defined in #e_max_errorcodes. 00193 */ 00194 long systhread_mutex_unlock(t_systhread_mutex pmutex); 00195 00196 00197 /** 00198 Try to enter block of locked code code until a systhread_mutex_unlock() is reached. 00199 If the lock cannot be entered, this function will return non-zero. 00200 00201 @ingroup mutex 00202 @param pmutex The mutex instance pointer. 00203 @return Returns non-zero if there was a problem entering. 00204 @see systhread_mutex_lock() 00205 */ 00206 long systhread_mutex_trylock(t_systhread_mutex pmutex); 00207 00208 00209 /** 00210 Convenience utility that combines systhread_mutex_new() and systhread_mutex_lock(). 00211 @ingroup mutex 00212 @param pmutex The address of a variable to store the mutex pointer. 00213 @param flags Flags to determine the behaviour of the mutex, as defined in #e_max_systhread_mutex_flags. 00214 @return A Max error code as defined in #e_max_errorcodes. 00215 */ 00216 long systhread_mutex_newlock(t_systhread_mutex *pmutex,long flags); 00217 00218 00219 00220 long systhread_cond_new(t_systhread_cond *pcond, long flags); 00221 long systhread_cond_free(t_systhread_cond pcond); 00222 long systhread_cond_wait(t_systhread_cond pcond, t_systhread_mutex pmutex); 00223 long systhread_cond_signal(t_systhread_cond pcond); 00224 long systhread_cond_broadcast(t_systhread_cond pcond); 00225 00226 00227 #ifdef __cplusplus 00228 } 00229 #endif 00230 00231 #endif // _EXT_SYSTHREAD_H_ 00232
Copyright © 2008, Cycling '74