]> git.vomp.tv Git - vompclient.git/blob - threadwin.cc
Step back a bit when going from ffwd to play
[vompclient.git] / threadwin.cc
1 /*
2     Copyright 2004-2005 Chris Tallon
3
4     This file is part of VOMP.
5
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.
10
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.
15
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
19 */
20
21 #include "threadwin.h"
22
23 // Undeclared functions, only for use in this file to start the thread
24 DWORD WINAPI threadInternalStart(void *arg)
25 {
26   Thread *t = (Thread *)arg;
27   t->threadInternalStart2();
28   return 0;
29 }
30
31 int ThreadWin::threadStart()
32 {
33   threadCond = CreateEvent(NULL,/*FALSE*/TRUE,FALSE,NULL);
34   if (threadCond == NULL) return 0;
35   threadCondMutex = CreateMutex(NULL,FALSE,NULL);
36   if (threadCondMutex == NULL)
37   {
38     CloseHandle(threadCond);
39     return 0;
40   }
41
42   threadActive = 1;
43   DWORD threadId;
44   pthread = CreateThread(NULL, 0, threadInternalStart, (void*)this,0, &threadId);
45   if (pthread == NULL)
46   {
47     CloseHandle(threadCond);
48     CloseHandle(threadCondMutex);
49     return 0;
50   }
51   return 1;
52 }
53
54 void ThreadWin::threadStop()
55 {
56   threadActive = 0;
57   // Signal thread here in case it's waiting
58   threadSignal();
59   WaitForSingleObject(pthread, INFINITE);
60   this->threadPostStopCleanup();
61 }
62
63 void ThreadWin::threadCancel()
64 {
65   threadActive = 0;
66   //TerminateThread(pthread, 0);
67   threadSignalNoLock();
68   WaitForSingleObject(pthread, INFINITE);
69   this->threadPostStopCleanup();
70 }
71
72 void ThreadWin::threadCheckExit()
73 {
74   if (!threadActive) ExitThread(NULL);
75 }
76
77 void ThreadWin::threadLock()
78 {
79   WaitForSingleObject(threadCondMutex, INFINITE);
80 }
81
82 void ThreadWin::threadUnlock()
83 {
84   ReleaseMutex(threadCondMutex);
85 }
86
87 void ThreadWin::threadSignal()
88 {
89   WaitForSingleObject(threadCondMutex, INFINITE);
90  // PulseEvent(threadCond);
91   SetEvent(threadCond);
92   ReleaseMutex(threadCondMutex);
93 }
94
95 void ThreadWin::threadSignalNoLock()
96 {
97 //    PulseEvent(threadCond);
98         SetEvent(threadCond);
99 }
100
101 void ThreadWin::threadWaitForSignal()
102 {
103   threadUnlock();
104   WaitForSingleObject(threadCond,INFINITE);
105   ResetEvent(threadCond);
106   threadLock();
107 }
108
109 void ThreadWin::threadWaitForSignalTimed(struct timespec* ts)
110 {
111         threadUnlock();
112         HANDLE handles[2] ={threadCond, NULL};
113         LARGE_INTEGER duration;
114         duration.QuadPart=(((LONGLONG)ts->tv_sec)*1000LL*1000LL*10LL+((LONGLONG)ts->tv_nsec)/100LL)+WINDOWS_TIME_BASE_OFFSET;
115         SYSTEMTIME debug;
116         FILETIME debugfile;
117         GetSystemTime(&debug);
118         SystemTimeToFileTime(&debug,&debugfile);
119         
120         handles[1]=CreateWaitableTimer(NULL,TRUE,NULL);
121         SetWaitableTimer(handles[1], &duration, 0, NULL, NULL, 0);
122         WaitForMultipleObjects(2,handles,FALSE,INFINITE);
123         ResetEvent(threadCond);
124         CloseHandle(handles[1]);
125         threadLock();
126 }
127
128 void ThreadWin::threadSetKillable()
129 {
130   //WIN32:Ignore or use a separate Event Object to simulate this
131 }
132