]> git.vomp.tv Git - vompclient.git/blob - threadwin.cc
Live TV updates
[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 ThreadWin::~ThreadWin()
24 {
25     CloseHandle(pthread);
26     CloseHandle(threadCond);
27     CloseHandle(threadCondMutex);
28 }
29
30
31 // Undeclared functions, only for use in this file to start the thread
32 DWORD WINAPI threadInternalStart(void *arg)
33 {
34   Thread *t = (Thread *)arg;
35   t->threadInternalStart2();
36   return 0;
37 }
38
39 int ThreadWin::threadStart()
40 {
41   threadCond = CreateEvent(NULL,/*FALSE*/TRUE,FALSE,NULL);
42   if (threadCond == NULL) return 0;
43   threadCondMutex = CreateMutex(NULL,FALSE,NULL);
44   if (threadCondMutex == NULL)
45   {
46     CloseHandle(threadCond);
47     return 0;
48   }
49
50   threadActive = 1;
51   
52   pthread = CreateThread(NULL, 0, threadInternalStart, (void*)this,0, &threadId);
53   if (pthread == NULL)
54   {
55     CloseHandle(threadCond);
56     CloseHandle(threadCondMutex);
57     return 0;
58   }
59   return 1;
60 }
61
62 void ThreadWin::threadStop()
63 {
64   threadActive = 0;
65   // Signal thread here in case it's waiting
66   threadSignal();
67   WaitForSingleObject(pthread, INFINITE);
68   this->threadPostStopCleanup();
69 }
70
71 void ThreadWin::threadCancel()
72 {
73   threadActive = 0;
74   //TerminateThread(pthread, 0);
75   threadSignalNoLock();
76   WaitForSingleObject(pthread, INFINITE);
77   this->threadPostStopCleanup();
78 }
79
80 void ThreadWin::threadCheckExit()
81 {
82   if (!threadActive) ExitThread(NULL);
83 }
84
85 void ThreadWin::threadLock()
86 {
87   WaitForSingleObject(threadCondMutex, INFINITE);
88 }
89
90 void ThreadWin::threadUnlock()
91 {
92   ReleaseMutex(threadCondMutex);
93 }
94
95 void ThreadWin::threadSignal()
96 {
97   WaitForSingleObject(threadCondMutex, INFINITE);
98  // PulseEvent(threadCond);
99   SetEvent(threadCond);
100   ReleaseMutex(threadCondMutex);
101 }
102
103 void ThreadWin::threadSignalNoLock()
104 {
105 //    PulseEvent(threadCond);
106         SetEvent(threadCond);
107 }
108
109 void ThreadWin::threadWaitForSignal()
110 {
111   threadUnlock();
112   WaitForSingleObject(threadCond,INFINITE);
113   ResetEvent(threadCond);
114   threadLock();
115 }
116
117 void ThreadWin::threadWaitForSignalTimed(struct timespec* ts)
118 {
119         threadUnlock();
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;
123         SYSTEMTIME debug;
124         FILETIME debugfile;
125         GetSystemTime(&debug);
126         SystemTimeToFileTime(&debug,&debugfile);
127         
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]);
133         threadLock();
134 }
135
136 void ThreadWin::threadSetKillable()
137 {
138   //WIN32:Ignore or use a separate Event Object to simulate this
139 }
140
141 void ThreadWin::threadSuicide()
142 {
143 /*  if(!pthread_detach(pthread_self()))
144   {
145     MILLISLEEP(1000);
146     if(!pthread_detach(pthread_self()))
147     {
148       MILLISLEEP(1000);
149       pthread_detach(pthread_self());
150     }
151   }
152 */
153     
154     ExitThread(0);
155 }
156
157 unsigned int ThreadWin::getThreadID()
158 {
159     return threadId;
160 }
161
162 unsigned int ThreadWin::thisThreadID() // returns the ID of the calling thread
163 {
164   return GetCurrentThreadId();
165 }