forked from jan-hoelscher-mss/safmq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Signal.cpp
executable file
·141 lines (119 loc) · 3.45 KB
/
Signal.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
Copyright 2004-2008 Matthew J. Battey
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
This software implements a platform independent Store and Forward Message Queue.
*/
#ifdef _WIN32
#define NOMINMAX 1
#include <windows.h>
#endif
#include "safmq.h" // include the SAFMQ_INT32 definition
#include "safmq-Signal.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
#ifdef _WIN32
//#include <windows.h>
struct signalData {
signalData(bool broadcast) {
event = ::CreateEvent(NULL,broadcast?TRUE:FALSE,FALSE,NULL);
}
~signalData() {
::CloseHandle(event);
}
Signal::Result wait(SAFMQ_INT32 timeoutmilliseconds) {
DWORD result;
if (timeoutmilliseconds < 0)
result = ::WaitForSingleObject(event,INFINITE);
else
result = ::WaitForSingleObject(event,timeoutmilliseconds);
if (result == WAIT_OBJECT_0)
return Signal::SIGNAL_SET;
else if (result == WAIT_TIMEOUT)
return Signal::SIGNAL_TIMEOUT;
return Signal::SIGNAL_ERROR;
}
void set() {
::PulseEvent(event);
}
HANDLE event;
};
#else
#include <pthread.h>
#include <sys/time.h>
#include <errno.h>
struct signalData {
signalData(bool broadcast) {
::pthread_cond_init(&cond,NULL);
::pthread_mutex_init(&mtx, NULL);
this->broadcast = broadcast;
}
~signalData() {
::pthread_cond_destroy(&cond);
::pthread_mutex_destroy(&mtx);
}
Signal::Result wait(SAFMQ_INT32 timeoutmilliseconds) {
int retcode;
if (timeoutmilliseconds < 0) {
pthread_mutex_lock(&mtx);
retcode = ::pthread_cond_wait(&cond, &mtx);
pthread_mutex_unlock(&mtx);
} else {
struct timeval now;
struct timespec timeout;
gettimeofday(&now,NULL);
timeout.tv_sec = now.tv_sec + (timeoutmilliseconds / 1000);
timeout.tv_nsec = now.tv_usec * 1000 + (timeoutmilliseconds % 1000) * 1000 * 1000;
if (timeout.tv_nsec / (1000*1000*1000)) {
timeout.tv_sec += timeout.tv_nsec / (1000*1000*1000);
timeout.tv_nsec %= (1000*1000*1000);
}
pthread_mutex_lock(&mtx);
retcode = ::pthread_cond_timedwait(&cond, &mtx, &timeout);
pthread_mutex_unlock(&mtx);
}
if (retcode == 0)
return Signal::SIGNAL_SET;
else if (retcode == ETIMEDOUT)
return Signal::SIGNAL_TIMEOUT;
return Signal::SIGNAL_ERROR;
}
void set() {
if (broadcast) {
pthread_cond_broadcast(&cond);
pthread_cond_signal(&cond);
} else {
::pthread_cond_signal(&cond);
}
}
pthread_cond_t cond;
pthread_mutex_t mtx;
bool broadcast;
};
#endif
///////////////////////////////////////////////////////////////////////////////
//
//
Signal::Signal(bool broadcast)
{
data = new signalData(broadcast);
}
Signal::~Signal()
{
delete data;
}
Signal::Result Signal::WaitFor(SAFMQ_INT32 timeoutmilliseconds)
{
return data->wait(timeoutmilliseconds);
}
void Signal::Set()
{
data->set();
}