1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
|
#pragma once
#ifndef SPARKLYTHREAD_H
#define SPARKLYTHREAD_H
#include <SparklyCore.h>
// thread for windows
#if defined SPwindows
#define spThreadBegin DWORD WINAPI
#define spThreadHandle HANDLE
#define spThreadEnd return 1
typedef DWORD (WINAPI *spThreadFunction)( LPVOID lpThreadParameter );
class spThread
{
public:
HANDLE id;
bool visrunning;
spThread(spThreadFunction func, void *data)
{
id = CreateThread(0,0, func, data, CREATE_SUSPENDED,0);
visrunning = 0;
}
void start()
{
if(!id) return;
ResumeThread(id);
visrunning = 1;
}
void destroy()
{
if(!id) return;
TerminateThread( id, 0 );
visrunning = 0;
CloseHandle(id);
}
void pause()
{
if(!id) return;
SuspendThread(id);
visrunning = 0;
}
bool exists()
{
if(!id ) return false;
DWORD result = WaitForSingleObject( id, 0);
if (result == WAIT_OBJECT_0) return false;
return true;
}
bool isrunning()
{
if(!id ) return false;
if(!visrunning ) return false;
if( !exists() ) return false;
return true;
}
void waituntildone()
{
if(!isrunning()) return;
WaitForSingleObject(id, INFINITE);
}
void waintuntildonemutli( int count, spThread **in )
{
HANDLE *p = new HANDLE[count];
for( int a = 0; a < count; a++ ) p[a] = in[a]->id;
WaitForMultipleObjects( count, p, true, INFINITE);
}
void waituntildone( unsigned int time )
{
if(!isrunning()) return;
WaitForSingleObject(id, time);
}
void waintuntildonemutli( int count, spThread **in, unsigned int time )
{
HANDLE *p = new HANDLE[count];
for( int a = 0; a < count; a++ ) p[a] = in[a]->id;
WaitForMultipleObjects( count, p, true, time);
}
void SetPriorityLevel( int level )
{
if(!id ) return;
}
int GetPriorityLevel( char level )
{
if(!id ) return 0;
return GetThreadPriority( id );
}
~spThread()
{
if( exists() ) destroy();
if(id) CloseHandle(id);
}
};
#endif
// thread for linux
#if defined SPlinux
#include <pthread.h>
#define spThreadBegin void *
#define spThreadHandle pthread_t
#define spThreadEnd pthread_exit(0)
typedef void *( *spThreadFunction)( void *lpThreadParameter );
class spThread
{
public:
spThreadHandle id;
bool visrunning;
pthread_mutex_t m_SuspendMutex;
pthread_cond_t m_ResumeCond;
spThread(spThreadFunction func, void *data)
{
pthread_create(&id,0, func, data);
}
void start()
{
pthread_mutex_lock(&m_SuspendMutex);
visrunning = true;
pthread_cond_signal(&m_ResumeCond);
pthread_mutex_unlock(&m_SuspendMutex);
}
void destroy()
{
}
void pause()
{
pthread_mutex_lock(&m_SuspendMutex);
pthread_join(id, NULL);
visrunning = false;
do
{
pthread_cond_wait(&m_ResumeCond, &m_SuspendMutex);
}
while (!visrunning);
pthread_mutex_unlock(&m_SuspendMutex);
}
bool exists()
{
}
bool isrunning()
{
}
void waituntildone()
{
}
void waintuntildonemutli( int count, spThread **in )
{
}
void waituntildone( unsigned int time )
{
}
void waintuntildonemutli( int count, spThread **in, unsigned int time )
{
}
void SetPriorityLevel( int level )
{
}
int GetPriorityLevel( char level )
{
}
~spThread()
{
}
};
#endif
#endif
|