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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "threadwin.h"
23 ThreadWin::~ThreadWin()
26 CloseHandle(threadCond);
27 CloseHandle(threadCondMutex);
31 // Undeclared functions, only for use in this file to start the thread
32 DWORD WINAPI threadInternalStart(void *arg)
34 Thread *t = (Thread *)arg;
35 t->threadInternalStart2();
39 int ThreadWin::threadStart()
41 threadCond = CreateEvent(NULL,/*FALSE*/TRUE,FALSE,NULL);
42 if (threadCond == NULL) return 0;
43 threadCondMutex = CreateMutex(NULL,FALSE,NULL);
44 if (threadCondMutex == NULL)
46 CloseHandle(threadCond);
52 pthread = CreateThread(NULL, 0, threadInternalStart, (void*)this,0, &threadId);
55 CloseHandle(threadCond);
56 CloseHandle(threadCondMutex);
62 void ThreadWin::threadStop()
65 // Signal thread here in case it's waiting
67 WaitForSingleObject(pthread, INFINITE);
68 this->threadPostStopCleanup();
71 void ThreadWin::threadCancel()
74 //TerminateThread(pthread, 0);
76 WaitForSingleObject(pthread, INFINITE);
77 this->threadPostStopCleanup();
80 void ThreadWin::threadCheckExit()
82 if (!threadActive) ExitThread(NULL);
85 void ThreadWin::threadLock()
87 WaitForSingleObject(threadCondMutex, INFINITE);
90 void ThreadWin::threadUnlock()
92 ReleaseMutex(threadCondMutex);
95 void ThreadWin::threadSignal()
97 WaitForSingleObject(threadCondMutex, INFINITE);
98 // PulseEvent(threadCond);
100 ReleaseMutex(threadCondMutex);
103 void ThreadWin::threadSignalNoLock()
105 // PulseEvent(threadCond);
106 SetEvent(threadCond);
109 void ThreadWin::threadWaitForSignal()
112 WaitForSingleObject(threadCond,INFINITE);
113 ResetEvent(threadCond);
117 void ThreadWin::threadWaitForSignalTimed(struct timespec* ts)
120 HANDLE handles[2] ={threadCond, NULL};
121 LARGE_INTEGER duration;
122 duration.QuadPart=(((LONGLONG)ts->tv_sec)*1000LL*1000LL*10LL+((LONGLONG)ts->tv_nsec)/100LL)+WINDOWS_TIME_BASE_OFFSET;
125 GetSystemTime(&debug);
126 SystemTimeToFileTime(&debug,&debugfile);
128 handles[1]=CreateWaitableTimer(NULL,TRUE,NULL);
129 SetWaitableTimer(handles[1], &duration, 0, NULL, NULL, 0);
130 WaitForMultipleObjects(2,handles,FALSE,INFINITE);
131 ResetEvent(threadCond);
132 CloseHandle(handles[1]);
136 void ThreadWin::threadSetKillable()
138 //WIN32:Ignore or use a separate Event Object to simulate this
141 void ThreadWin::threadSuicide()
143 /* if(!pthread_detach(pthread_self()))
146 if(!pthread_detach(pthread_self()))
149 pthread_detach(pthread_self());
157 unsigned int ThreadWin::getThreadID()
162 unsigned int ThreadWin::thisThreadID() // returns the ID of the calling thread
164 return GetCurrentThreadId();