11 #include <drift/dslcore.h>
12 #include <drift/Mutex.h>
13 #include <drift/Threading.h>
14 #include <drift/GenLib.h>
15 #if defined(DSL_THREADING_USE_C11)
16 #define DSL_LIST_TYPE unordered_set
17 #include <unordered_set>
21 #define DSL_LIST_TYPE set
28 DSL_Mutex * DSL_Thread_Mutex()
30 static DSL_Mutex actualMutex;
33 typedef DSL_LIST_TYPE<DSL_THREAD_INFO *> DSL_List_Type;
34 DSL_List_Type DSL_List;
35 uint32 DSL_NoThreads=0;
38 AutoMutexPtr(DSL_Thread_Mutex());
39 DSL_List_Type::iterator x = DSL_List.find(tt);
40 if (x != DSL_List.end()) {
45 CloseHandle(tt->hThread);
53 typedef struct tagTHREADNAME_INFO
61 DSL_API
void DSL_CC DSL_SetThreadName(DWORD dwThreadID, LPCSTR szThreadName) {
62 if (szThreadName == NULL) {
return; }
65 info.szName = szThreadName;
66 info.dwThreadID = dwThreadID;
71 RaiseException( 0x406D1388, 0,
sizeof(info)/
sizeof(DWORD), (
const ULONG_PTR *)&info );
73 RaiseException( 0x406D1388, 0,
sizeof(info)/
sizeof(DWORD), (DWORD*)&info );
76 __except (EXCEPTION_CONTINUE_EXECUTION) {
80 DSL_API
void DSL_CC DSL_SetThreadName(pthread_t thread_id,
const char * szThreadName) {
81 if (szThreadName == NULL) {
return; }
82 pthread_setname_np(thread_id, szThreadName);
101 DSL_Thread_Mutex()->Lock();
102 DSL_List.insert(ret);
104 DSL_Thread_Mutex()->Release();
106 if (desc) {
sstrcpy(ret->desc, desc); }
109 ret->RemoveMe = DSL_UnregisterThread;
112 unsigned int ThreadID=0;
113 ret->hThread = (HANDLE)_beginthreadex(NULL, 0, Thread, ret, 0, &ThreadID);
114 if (ret->hThread != 0) {
115 DSL_SetThreadName(ThreadID, desc);
117 DSL_UnregisterThread(ret);
121 if (pthread_create(&ret->hThread, NULL, Thread, (
void *)ret) == 0) {
122 DSL_SetThreadName(ret->hThread, desc);
123 pthread_detach(ret->hThread);
125 DSL_UnregisterThread(ret);
135 unsigned int ThreadID=0;
136 HANDLE hThread = (HANDLE)_beginthreadex(NULL, 0, Thread, Parm, 0, &ThreadID);
138 DSL_SetThreadName(ThreadID, Desc);
139 CloseHandle(hThread);
144 if (pthread_create(&a_thread, NULL, Thread, Parm) == 0) {
145 DSL_SetThreadName(a_thread, Desc);
146 pthread_detach(a_thread);
155 TerminateThread(tt->hThread, 0);
156 DSL_UnregisterThread(tt);
159 if (pthread_cancel(tt->hThread) == 0) {
160 DSL_UnregisterThread(tt);
167 void DSL_CC DSL_PrintRunningThreads() {
168 AutoMutexPtr(DSL_Thread_Mutex());
170 printf(
"Running threads:");
171 for (DSL_List_Type::iterator x = DSL_List.begin(); x != DSL_List.end(); x++) {
173 printf(
" [%s]", tScan->desc);
178 void DSL_CC DSL_PrintRunningThreadsWithID(
int id) {
179 AutoMutexPtr(DSL_Thread_Mutex());
181 printf(
"Running threads:");
182 for (DSL_List_Type::iterator x = DSL_List.begin(); x != DSL_List.end(); x++) {
184 if (tScan->
id ==
id) {
185 printf(
" [%s]",tScan->desc);
192 AutoMutexPtr(DSL_Thread_Mutex());
193 return DSL_NoThreads;
198 AutoMutexPtr(DSL_Thread_Mutex());
199 for (DSL_List_Type::iterator x = DSL_List.begin(); x != DSL_List.end(); x++) {
201 if (tScan->
id ==
id) { ret++; }
206 void DSL_CC safe_sleep(
int sleepfor,
bool inmilli) {
207 #if defined(DSL_THREADING_USE_C11)
208 this_thread::sleep_for(inmilli ? chrono::milliseconds(sleepfor) : chrono::seconds(sleepfor));
211 Sleep(1000 * sleepfor);
216 struct timespec delta, rem;
218 delta.tv_sec = sleepfor;
221 delta.tv_sec = sleepfor / 1000;
222 sleepfor -= (delta.tv_sec * 1000);
223 delta.tv_nsec= sleepfor * 1000000;
226 while (nanosleep(&delta,&rem) == -1 && errno == EINTR) {
227 memcpy(&delta, &rem,
sizeof(rem));
DSL_API void DSL_CC dsl_free(void *ptr)
uint32 DSL_CC DSL_NumThreads()
Number of active threads (does not count threads started with DSL_StartThreadNoRecord)
bool DSL_CC DSL_StartThreadNoRecord(ThreadProto Thread, void *Parm, const char *Desc)
DSL_THREAD_INFO *DSL_CC DSL_StartThread(ThreadProto Thread, void *Parm, const char *desc, int32 id)
uint32 DSL_CC DSL_NumThreadsWithID(int id)
Number of active threads with matching id #.
void * parm
User-specified parameter.
int32 id
User-specified ID, if DSL_StartThread() is used with no ID, it defaults to -1.