regify utility  2.0.0-0
thread.h
1 /*
2  * Copyright regify
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20  * SOFTWARE.
21  */
22 
23 #ifndef REGIFY_UTIL_THREAD_H
24 #define REGIFY_UTIL_THREAD_H
25 
26 #ifdef RUMS
27 #define RU_THREAD_LOCAL __declspec(thread)
28 #else
29 #include <pthread.h>
30 #ifdef DO_IOS
31 #define RU_THREAD_LOCAL thread_local
32 #else
33 #define RU_THREAD_LOCAL __thread
34 #endif
35 #endif
36 
46 typedef void* ruTrace;
47 
52 RUAPI void ruBacktraceInit(perm_chars exePath);
53 
60 RUAPI ruList ruBacktrace(int32_t* code);
61 
68 RUAPI void ruTraceLog(trans_chars tag, int32_t skip);
69 
76 
83 
90 
97 
103 RUAPI uint32_t ruTraceLine(ruTrace rt);
104 
111 
118 
122 typedef unsigned long ru_tid;
123 
127 #ifdef _WIN32
128 typedef HANDLE ruThreadId;
129 #else
130 typedef pthread_t ruThreadId;
131 #endif
132 
136 typedef void* ruThread;
137 
141 typedef void* ruCount;
142 
146 typedef void* (*ruStartFunc) (void* context);
147 
148 
153 RUAPI ru_tid ruThreadGetId(void);
154 
159 RUAPI void ruThreadSetName(trans_chars name);
160 
166 
174 RUAPI ruThread ruThreadCreate(ruStartFunc start, alloc_chars name, void* usrCtx);
175 
183 RUAPI ruThread ruThreadCreateBg(ruStartFunc start, alloc_chars name, void* usrCtx);
184 
191 RUAPI bool ruThreadFinished(ruThread tid, int32_t* code);
192 
199 RUAPI ruThreadId ruThreadNativeId(ruThread tid, int32_t* code);
200 
211 RUAPI bool ruThreadWait(ruThread tid, sec_t tosecs, void** exitVal);
212 
219 RUAPI int ruThreadJoin(ruThread tid, void** exitVal );
220 
228 RUAPI int32_t ruThreadKill(ruThread tid);
229 
233 typedef void* ruCond;
234 
238 typedef void* ruMutex;
239 
245 RUAPI ruCond ruCondInit(void);
246 
253 
258 RUAPI void ruCondSignal(ruCond c);
259 
269 RUAPI void ruCondWaitTil(ruCond c, ruMutex m, int32_t msTimeout);
270 
279 RUAPI void ruCondWait(ruCond c, ruMutex m);
280 
285 RUAPI ruMutex ruMutexInit(void);
286 
295 RUAPI bool ruMutexTryLockLoc(ruMutex m, trans_chars filePath, trans_chars func,
296  int32_t line);
297 
298 #define ruMutexTryLock(m) ruMutexTryLockLoc(m, __FILE__, __func__, __LINE__)
299 
310 RUAPI void ruMutexLockLoc(ruMutex m, trans_chars filePath, trans_chars func,
311  int32_t line);
312 
313 #define ruMutexLock(m) ruMutexLockLoc(m, __FILE__, __func__, __LINE__)
314 
322 RUAPI void ruMutexUnlockLoc(ruMutex m, trans_chars filePath, trans_chars func,
323  int32_t line);
324 
325 #define ruMutexUnlock(m) ruMutexUnlockLoc(m, __FILE__, __func__, __LINE__)
326 
333 
339 RUAPI ruCount ruCounterNew(int64_t initialCount);
340 
348 RUAPI int64_t ruCounterIncValue(ruCount counter, int64_t value, int32_t* code);
349 
353 #define ruCounterInc(counter, value) ruCounterIncValue(counter, value, NULL)
354 
358 #define ruCounterRead(counter) ruCounterIncValue(counter, 0, NULL)
359 
367 RUAPI int64_t ruCountSetValue(ruCount counter, int64_t value, int32_t* code);
368 
372 #define ruCountSet(counter, value) ruCountSetValue(counter, value, NULL)
373 
379 RUAPI ruCount ruCountFree(ruCount counter);
380 
385 #endif //REGIFY_UTIL_THREAD_H
void * ruList
Opaque pointer to list object. See List Collection.
Definition: list.h:103
const void * perm_ptr
A permanent pointer.
Definition: regify-util.h:277
const char * trans_chars
A transient NULL terminated string pointer.
Definition: regify-util.h:186
const char * perm_chars
A permanent NULL terminated string pointer.
Definition: regify-util.h:176
long sec_t
A signed type for expressing seconds.
Definition: regify-util.h:312
char * alloc_chars
An allocated NULL terminated string pointer.
Definition: regify-util.h:199
RUAPI void ruCondSignal(ruCond c)
Unblock a thread waiting for the given condition variable.
void * ruTrace
Definition: thread.h:46
RUAPI ruCond ruCondFree(ruCond c)
Free up given ruCond object.
RUAPI void ruMutexUnlockLoc(ruMutex m, trans_chars filePath, trans_chars func, int32_t line)
Return the aquired mutex lock so other threads can use it.
RUAPI bool ruThreadFinished(ruThread tid, int32_t *code)
Whether thraed has finished and is joinable.
RUAPI ruCount ruCountFree(ruCount counter)
Frees given counter.
RUAPI ruThread ruThreadCreate(ruStartFunc start, alloc_chars name, void *usrCtx)
RUAPI void ruBacktraceInit(perm_chars exePath)
Sets the path to the current executable.
RUAPI bool ruMutexTryLockLoc(ruMutex m, trans_chars filePath, trans_chars func, int32_t line)
Tries to aquire a lock for the given ruMutex without blocking.
RUAPI ruCond ruCondInit(void)
Initialize a new ruCond condition variable.
RUAPI int64_t ruCountSetValue(ruCount counter, int64_t value, int32_t *code)
Sets the counter to given value and returns the previous value.
RUAPI perm_ptr ruTraceAddr(ruTrace rt)
Returns the instruction pointer of the given ruTrace object.
RUAPI perm_ptr ruTraceOffset(ruTrace rt)
Returns the offset from the given function of the given ruTrace object if set.
RUAPI void ruMutexLockLoc(ruMutex m, trans_chars filePath, trans_chars func, int32_t line)
Aquire a lock for the given ruMutex blocking until it is given.
RUAPI ruThreadId ruThreadNativeId(ruThread tid, int32_t *code)
Returns the thread id of the given ruThread.
RUAPI ru_tid ruThreadGetId(void)
void * ruCond
Opaque object abstracting condition signaling.
Definition: thread.h:233
RUAPI uint32_t ruTraceLine(ruTrace rt)
Returns the line number of the given ruTrace object if set.
RUAPI int32_t ruThreadKill(ruThread tid)
RUAPI ruList ruBacktrace(int32_t *code)
Returns a list of ruTrace elements ordered so that the caller is last.
void * ruCount
Definition: thread.h:141
RUAPI void ruCondWaitTil(ruCond c, ruMutex m, int32_t msTimeout)
Wait on the given condition variable or the given timeout.
RUAPI void ruThreadSetName(trans_chars name)
void *(* ruStartFunc)(void *context)
Definition: thread.h:146
void * ruMutex
Opaque object abstracting object locking.
Definition: thread.h:238
RUAPI void ruCondWait(ruCond c, ruMutex m)
Wait on the given condition variable.
RUAPI perm_chars ruTraceFileName(ruTrace rt)
Returns the basename of the filename of the given ruTrace object if set.
RUAPI perm_chars ruThreadGetName(void)
RUAPI bool ruThreadWait(ruThread tid, sec_t tosecs, void **exitVal)
RUAPI void ruTraceLog(trans_chars tag, int32_t skip)
Logs a backtrace.
RUAPI perm_chars ruTraceStr(ruTrace rt)
Returns an optimized string representation of the given ruTrace object.
void * ruThread
Definition: thread.h:136
RUAPI ruMutex ruMutexInit(void)
Initialize a new ruMutex.
pthread_t ruThreadId
Definition: thread.h:130
RUAPI int ruThreadJoin(ruThread tid, void **exitVal)
unsigned long ru_tid
Definition: thread.h:122
RUAPI int64_t ruCounterIncValue(ruCount counter, int64_t value, int32_t *code)
Increments the counter by given value and returns the new value.
RUAPI perm_chars ruTraceFunc(ruTrace rt)
Returns the function name of the given ruTrace object if set.
RUAPI ruThread ruThreadCreateBg(ruStartFunc start, alloc_chars name, void *usrCtx)
Creates a new thread with background priority.
RUAPI perm_chars ruTraceFilePath(ruTrace rt)
Returns the full path to the filename of the given ruTrace object if set.
RUAPI ruMutex ruMutexFree(ruMutex m)
Free up given ruMutex object.
RUAPI ruCount ruCounterNew(int64_t initialCount)
Returns a new thread safe counter initialized to given value.