2 Copyright 2004-2005 Chris Tallon
4 This file is part of VOMP.
6 VOMP is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 VOMP is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with VOMP; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 #include "threadwin.h"
23 ThreadWin::~ThreadWin()
26 CloseHandle(threadCond);
27 CloseHandle(threadCondMutex);
28 CloseHandle(threadKillable);
32 // Undeclared functions, only for use in this file to start the thread
33 DWORD WINAPI threadInternalStart(void *arg)
35 Thread *t = (Thread *)arg;
36 t->threadInternalStart2();
40 int ThreadWin::threadStart()
42 threadCond = CreateEvent(NULL,/*FALSE*/TRUE,FALSE,NULL);
43 if (threadCond == NULL) return 0;
44 threadKillable = CreateEvent(NULL,/*FALSE*/TRUE,FALSE,NULL);
45 if (threadKillable == NULL) return 0;
46 threadCondMutex = CreateMutex(NULL,FALSE,NULL);
47 if (threadCondMutex == NULL)
49 CloseHandle(threadCond);
55 pthread = CreateThread(NULL, 0, threadInternalStart, (void*)this,0, &threadId);
58 CloseHandle(threadCond);
59 CloseHandle(threadCondMutex);
60 CloseHandle(threadKillable);
66 void ThreadWin::threadStop()
69 // Signal thread here in case it's waiting
71 WaitForSingleObject(pthread, INFINITE);
72 this->threadPostStopCleanup();
75 void ThreadWin::threadCancel()
79 HANDLE objs[]={threadKillable,pthread};
80 if (WaitForMultipleObjects(2,objs,FALSE,INFINITE)==WAIT_OBJECT_0) {
81 TerminateThread(pthread, 0);
83 this->threadPostStopCleanup();
86 void ThreadWin::threadCheckExit()
88 if (!threadActive) ExitThread(NULL);
91 void ThreadWin::threadLock()
93 WaitForSingleObject(threadCondMutex, INFINITE);
96 void ThreadWin::threadUnlock()
98 ReleaseMutex(threadCondMutex);
101 void ThreadWin::threadSignal()
103 WaitForSingleObject(threadCondMutex, INFINITE);
104 // PulseEvent(threadCond);
105 SetEvent(threadCond);
106 ReleaseMutex(threadCondMutex);
109 void ThreadWin::threadSignalNoLock()
111 // PulseEvent(threadCond);
112 SetEvent(threadCond);
115 void ThreadWin::threadWaitForSignal()
118 WaitForSingleObject(threadCond,INFINITE);
119 ResetEvent(threadCond);
123 void ThreadWin::threadWaitForSignalTimed(struct timespec* ts)
126 HANDLE handles[2] ={threadCond, NULL};
127 LARGE_INTEGER duration;
128 duration.QuadPart=(((LONGLONG)ts->tv_sec)*1000LL*1000LL*10LL+((LONGLONG)ts->tv_nsec)/100LL)+WINDOWS_TIME_BASE_OFFSET;
131 GetSystemTime(&debug);
132 SystemTimeToFileTime(&debug,&debugfile);
134 handles[1]=CreateWaitableTimer(NULL,TRUE,NULL);
135 SetWaitableTimer(handles[1], &duration, 0, NULL, NULL, 0);
136 WaitForMultipleObjects(2,handles,FALSE,INFINITE);
137 ResetEvent(threadCond);
138 CloseHandle(handles[1]);
142 void ThreadWin::threadSetKillable()
144 //WIN32:Ignore or use a separate Event Object to simulate this
145 SetEvent(threadKillable);
148 void ThreadWin::threadSuicide()
150 /* if(!pthread_detach(pthread_self()))
153 if(!pthread_detach(pthread_self()))
156 pthread_detach(pthread_self());
164 unsigned int ThreadWin::getThreadID()
169 unsigned int ThreadWin::thisThreadID() // returns the ID of the calling thread
171 return GetCurrentThreadId();