]> git.vomp.tv Git - vompclient.git/blob - signal.cc
Rewrite timers class using std::thread/mutex/cond/chrono
[vompclient.git] / signal.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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19 */
20 #include "signal.h"
21
22 // FIXME
23 // well, I say fix... Delete me.
24
25
26 Signal::Signal()
27 {
28         pthread_cond_init(&cond, NULL);
29         pthread_mutex_init(&condMutex, NULL);
30 }
31
32 Signal::~Signal()
33 {
34         pthread_cond_destroy(&cond);
35         pthread_mutex_destroy(&condMutex);
36 }
37
38
39
40 void Signal::signal()
41 {
42 #ifndef WIN32
43   pthread_mutex_lock(&condMutex);
44   pthread_cond_signal(&cond);
45   pthread_mutex_unlock(&condMutex);
46 #else
47 #error "Not ported yet"
48 #endif
49 }
50
51 void Signal::waitForSignal()
52 {
53 #ifndef WIN32
54   pthread_mutex_lock(&condMutex);
55   pthread_cond_wait(&cond, &condMutex);
56   pthread_mutex_unlock(&condMutex);
57 #else
58 #error "Not ported yet"
59 #endif
60 }
61
62 void Signal::waitForSignalTimed(struct timespec* ts)
63 {
64 #ifndef WIN32
65   pthread_mutex_lock(&condMutex);
66   pthread_cond_timedwait(&cond, &condMutex, ts);
67   pthread_mutex_unlock(&condMutex);
68 #else
69 #error "Not ported yet"
70 #endif
71 }
72
73 void Signal::waitForSignalTimed(unsigned int ts)
74 {
75         struct timespec target_time;
76         clock_gettime(CLOCK_REALTIME,&target_time);
77         target_time.tv_nsec+=1000000LL*ts;
78         if (target_time.tv_nsec>999999999) {
79                 target_time.tv_nsec-=1000000000L;
80                 target_time.tv_sec+=1;
81         }
82         waitForSignalTimed(&target_time);
83 }