]> git.vomp.tv Git - vompclient.git/blob - threadwin.cc
Completion of move recording code. Fixes for dir counts, other bugs.
[vompclient.git] / threadwin.cc
1 /*\r
2     Copyright 2004-2005 Chris Tallon\r
3 \r
4     This file is part of VOMP.\r
5 \r
6     VOMP is free software; you can redistribute it and/or modify\r
7     it under the terms of the GNU General Public License as published by\r
8     the Free Software Foundation; either version 2 of the License, or\r
9     (at your option) any later version.\r
10 \r
11     VOMP is distributed in the hope that it will be useful,\r
12     but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14     GNU General Public License for more details.\r
15 \r
16     You should have received a copy of the GNU General Public License\r
17     along with VOMP; if not, write to the Free Software\r
18     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
19 */\r
20 \r
21 #include "threadwin.h"\r
22 \r
23 // Undeclared functions, only for use in this file to start the thread\r
24 DWORD WINAPI threadInternalStart(void *arg)\r
25 {\r
26   Thread *t = (Thread *)arg;\r
27   t->threadInternalStart2();\r
28   return 0;\r
29 }\r
30 \r
31 int ThreadWin::threadStart()\r
32 {\r
33   threadCond = CreateEvent(NULL,/*FALSE*/TRUE,FALSE,NULL);\r
34   if (threadCond == NULL) return 0;\r
35   threadCondMutex = CreateMutex(NULL,FALSE,NULL);\r
36   if (threadCondMutex == NULL)\r
37   {\r
38     CloseHandle(threadCond);\r
39     return 0;\r
40   }\r
41 \r
42   threadActive = 1;\r
43   DWORD threadId;\r
44   pthread = CreateThread(NULL, 0, threadInternalStart, (void*)this,0, &threadId);\r
45   if (pthread == NULL)\r
46   {\r
47     CloseHandle(threadCond);\r
48     CloseHandle(threadCondMutex);\r
49     return 0;\r
50   }\r
51   return 1;\r
52 }\r
53 \r
54 void ThreadWin::threadStop()\r
55 {\r
56   threadActive = 0;\r
57   // Signal thread here in case it's waiting\r
58   threadSignal();\r
59   WaitForSingleObject(pthread, INFINITE);\r
60   this->threadPostStopCleanup();\r
61 }\r
62 \r
63 void ThreadWin::threadCancel()\r
64 {\r
65   threadActive = 0;\r
66   //TerminateThread(pthread, 0);\r
67   threadSignalNoLock();\r
68   WaitForSingleObject(pthread, INFINITE);\r
69   this->threadPostStopCleanup();\r
70 }\r
71 \r
72 void ThreadWin::threadCheckExit()\r
73 {\r
74   if (!threadActive) ExitThread(NULL);\r
75 }\r
76 \r
77 void ThreadWin::threadLock()\r
78 {\r
79   WaitForSingleObject(threadCondMutex, INFINITE);\r
80 }\r
81 \r
82 void ThreadWin::threadUnlock()\r
83 {\r
84   ReleaseMutex(threadCondMutex);\r
85 }\r
86 \r
87 void ThreadWin::threadSignal()\r
88 {\r
89   WaitForSingleObject(threadCondMutex, INFINITE);\r
90  // PulseEvent(threadCond);\r
91   SetEvent(threadCond);\r
92   ReleaseMutex(threadCondMutex);\r
93 }\r
94 \r
95 void ThreadWin::threadSignalNoLock()\r
96 {\r
97 //    PulseEvent(threadCond);\r
98         SetEvent(threadCond);\r
99 }\r
100 \r
101 void ThreadWin::threadWaitForSignal()\r
102 {\r
103   threadUnlock();\r
104   WaitForSingleObject(threadCond,INFINITE);\r
105   ResetEvent(threadCond);\r
106   threadLock();\r
107 }\r
108 \r
109 void ThreadWin::threadWaitForSignalTimed(struct timespec* ts)\r
110 {\r
111         threadUnlock();\r
112         HANDLE handles[2] ={threadCond, NULL};\r
113         LARGE_INTEGER duration;\r
114         duration.QuadPart=(((LONGLONG)ts->tv_sec)*1000LL*1000LL*10LL+((LONGLONG)ts->tv_nsec)/100LL)+WINDOWS_TIME_BASE_OFFSET;\r
115         SYSTEMTIME debug;\r
116         FILETIME debugfile;\r
117         GetSystemTime(&debug);\r
118         SystemTimeToFileTime(&debug,&debugfile);\r
119         \r
120         handles[1]=CreateWaitableTimer(NULL,TRUE,NULL);\r
121         SetWaitableTimer(handles[1], &duration, 0, NULL, NULL, 0);\r
122         WaitForMultipleObjects(2,handles,FALSE,INFINITE);\r
123         ResetEvent(threadCond);\r
124         CloseHandle(handles[1]);\r
125         threadLock();\r
126 }\r
127 \r
128 void ThreadWin::threadSetKillable()\r
129 {\r
130   //WIN32:Ignore or use a separate Event Object to simulate this\r
131 }\r
132 \r