| 1 |
/*=========================================================================== |
| 2 |
FILE: |
| 3 |
Event.cpp |
| 4 |
|
| 5 |
DESCRIPTION: |
| 6 |
Implementation of cEvent class |
| 7 |
|
| 8 |
PUBLIC CLASSES AND METHODS: |
| 9 |
WaitOnMultipleEvents |
| 10 |
cEvent |
| 11 |
Functionality to mimic Windows events using UNIX pipes (enhanced |
| 12 |
somewhat to allow one to specify a DWORD value to pass through |
| 13 |
when signalling the event) |
| 14 |
|
| 15 |
WARNING: |
| 16 |
This class is not designed to be thread safe |
| 17 |
|
| 18 |
Copyright (c) 2012, Code Aurora Forum. All rights reserved. |
| 19 |
|
| 20 |
Redistribution and use in source and binary forms, with or without |
| 21 |
modification, are permitted provided that the following conditions are met: |
| 22 |
* Redistributions of source code must retain the above copyright |
| 23 |
notice, this list of conditions and the following disclaimer. |
| 24 |
* Redistributions in binary form must reproduce the above copyright |
| 25 |
notice, this list of conditions and the following disclaimer in the |
| 26 |
documentation and/or other materials provided with the distribution. |
| 27 |
* Neither the name of Code Aurora Forum nor |
| 28 |
the names of its contributors may be used to endorse or promote |
| 29 |
products derived from this software without specific prior written |
| 30 |
permission. |
| 31 |
|
| 32 |
|
| 33 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 34 |
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 35 |
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 36 |
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 37 |
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 38 |
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 39 |
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 40 |
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 41 |
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 42 |
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 43 |
POSSIBILITY OF SUCH DAMAGE. |
| 44 |
===========================================================================*/ |
| 45 |
|
| 46 |
//--------------------------------------------------------------------------- |
| 47 |
// Include Files |
| 48 |
//--------------------------------------------------------------------------- |
| 49 |
#include "StdAfx.h" |
| 50 |
#include "Event.h" |
| 51 |
|
| 52 |
/*=========================================================================== |
| 53 |
METHOD: |
| 54 |
WaitOnMultipleEvents (Free Method) |
| 55 |
|
| 56 |
DESCRIPTION: |
| 57 |
Wait for any of the events to be set and return the value |
| 58 |
|
| 59 |
Note: If multiple events are set, only the event specified by |
| 60 |
eventIndex will be read from. Run this function again |
| 61 |
to get the next event. |
| 62 |
|
| 63 |
PARAMETERS: |
| 64 |
events [ I ] - Vector of events which may be signaled |
| 65 |
timeoutMS [ I ] - Relative timeout length (in milliseconds) |
| 66 |
val [ O ] - Associated value upon success |
| 67 |
eventIndex [ O ] - Index of event which was signaled |
| 68 |
|
| 69 |
RETURN VALUE: |
| 70 |
Return code |
| 71 |
positive for number of events set |
| 72 |
-ETIME on timeout |
| 73 |
negative errno value on failure |
| 74 |
===========================================================================*/ |
| 75 |
int WaitOnMultipleEvents( |
| 76 |
std::vector <cEvent *> events, |
| 77 |
DWORD timeoutMS, |
| 78 |
DWORD & val, |
| 79 |
DWORD & eventIndex ) |
| 80 |
{ |
| 81 |
// Check internal pipes' status |
| 82 |
for (ULONG index = 0; index < events.size(); index++) |
| 83 |
{ |
| 84 |
int error = events[index]->mError; |
| 85 |
if (error != 0) |
| 86 |
{ |
| 87 |
TRACE( "cEvent %lu has error %d\n", index, error ); |
| 88 |
return -error; |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
// Initialize the FD set |
| 93 |
fd_set fds; |
| 94 |
FD_ZERO( &fds ); |
| 95 |
|
| 96 |
// Add each item to the FD set, keeping track of the largest, |
| 97 |
// which is used for select() |
| 98 |
int largestFD = 0; |
| 99 |
for (ULONG index = 0; index < events.size(); index++) |
| 100 |
{ |
| 101 |
int pipe = events[index]->mPipes[READING]; |
| 102 |
FD_SET( pipe, &fds ); |
| 103 |
|
| 104 |
largestFD = std::max( pipe, largestFD ); |
| 105 |
} |
| 106 |
|
| 107 |
struct timeval timeOut; |
| 108 |
|
| 109 |
// Add avoiding an overflow on (long)usec |
| 110 |
timeOut.tv_sec = timeoutMS / 1000l; |
| 111 |
timeOut.tv_usec = ( timeoutMS % 1000l ) * 1000l; |
| 112 |
|
| 113 |
// Wait for activity on the pipes for the specified amount of time |
| 114 |
int rc = select( largestFD + 1, &fds, 0, 0, &timeOut ); |
| 115 |
if (rc == -1) |
| 116 |
{ |
| 117 |
TRACE( "WaitOnMultipleEvents error %d\n", errno ); |
| 118 |
return -errno; |
| 119 |
} |
| 120 |
else if (rc == 0) |
| 121 |
{ |
| 122 |
// No activity on the pipes |
| 123 |
return -ETIME; |
| 124 |
} |
| 125 |
|
| 126 |
int numSignaled = rc; |
| 127 |
|
| 128 |
// Only read from first pipe which was signaled |
| 129 |
int signaled = -1; |
| 130 |
for (ULONG index = 0; index < events.size(); index++) |
| 131 |
{ |
| 132 |
int pipe = events[index]->mPipes[READING]; |
| 133 |
if (FD_ISSET( pipe, &fds ) != 0) |
| 134 |
{ |
| 135 |
signaled = index; |
| 136 |
break; |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
if (signaled == -1) |
| 141 |
{ |
| 142 |
// Odd, no one was signaled |
| 143 |
return -ENODATA; |
| 144 |
} |
| 145 |
|
| 146 |
DWORD tempVal = 0; |
| 147 |
rc = events[signaled]->Read( tempVal ); |
| 148 |
if (rc == 0) |
| 149 |
{ |
| 150 |
// Success |
| 151 |
val = tempVal; |
| 152 |
eventIndex = signaled; |
| 153 |
return numSignaled; |
| 154 |
} |
| 155 |
else |
| 156 |
{ |
| 157 |
// failure |
| 158 |
return rc; |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
/*=========================================================================*/ |
| 163 |
// cEvent Methods |
| 164 |
/*=========================================================================*/ |
| 165 |
|
| 166 |
/*=========================================================================== |
| 167 |
METHOD: |
| 168 |
cEvent (Public Method) |
| 169 |
|
| 170 |
DESCRIPTION: |
| 171 |
Constructor |
| 172 |
|
| 173 |
RETURN VALUE: |
| 174 |
None |
| 175 |
===========================================================================*/ |
| 176 |
cEvent::cEvent() |
| 177 |
: mError( 0 ) |
| 178 |
{ |
| 179 |
int rc = pipe( mPipes ); |
| 180 |
if (rc != 0) |
| 181 |
{ |
| 182 |
mError = errno; |
| 183 |
TRACE( "cEvent - Error %d creating pipe, %s\n", |
| 184 |
mError, |
| 185 |
strerror( mError ) ); |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
/*=========================================================================== |
| 190 |
METHOD: |
| 191 |
~cEvent (Public Method) |
| 192 |
|
| 193 |
DESCRIPTION: |
| 194 |
Destructor |
| 195 |
|
| 196 |
RETURN VALUE: |
| 197 |
None |
| 198 |
===========================================================================*/ |
| 199 |
cEvent::~cEvent() |
| 200 |
{ |
| 201 |
// Check internal pipe status |
| 202 |
if (mError == 0) |
| 203 |
{ |
| 204 |
Close(); |
| 205 |
mError = EBADF; |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
/*=========================================================================== |
| 210 |
METHOD: |
| 211 |
Close (Internal Method) |
| 212 |
|
| 213 |
DESCRIPTION: |
| 214 |
Close pipe |
| 215 |
|
| 216 |
RETURN VALUE: |
| 217 |
Return code |
| 218 |
0 on success |
| 219 |
errno value on failure |
| 220 |
===========================================================================*/ |
| 221 |
int cEvent::Close() |
| 222 |
{ |
| 223 |
int retCode = 0; |
| 224 |
|
| 225 |
int rc = close( mPipes[READING] ); |
| 226 |
mPipes[READING] = -1; |
| 227 |
|
| 228 |
if (rc != 0) |
| 229 |
{ |
| 230 |
retCode = errno; |
| 231 |
TRACE( "cEvent - Error %d deleting pipe[READING], %s\n", |
| 232 |
retCode, |
| 233 |
strerror( retCode ) ); |
| 234 |
} |
| 235 |
|
| 236 |
rc = close( mPipes[WRITING] ); |
| 237 |
mPipes[WRITING] = -1; |
| 238 |
|
| 239 |
if (rc != 0) |
| 240 |
{ |
| 241 |
retCode = errno; |
| 242 |
TRACE( "cEvent - Error %d deleting pipe[WRITING], %s\n", |
| 243 |
retCode, |
| 244 |
strerror( retCode ) ); |
| 245 |
} |
| 246 |
|
| 247 |
return retCode; |
| 248 |
} |
| 249 |
|
| 250 |
/*=========================================================================== |
| 251 |
METHOD: |
| 252 |
Set (Public Method) |
| 253 |
|
| 254 |
DESCRIPTION: |
| 255 |
Set/signal the event with the specified value |
| 256 |
|
| 257 |
PARAMETERS: |
| 258 |
val [ I ] - Value to pass through with signal |
| 259 |
|
| 260 |
RETURN VALUE: |
| 261 |
Return code |
| 262 |
0 on success |
| 263 |
errno value on failure |
| 264 |
===========================================================================*/ |
| 265 |
int cEvent::Set( DWORD val ) |
| 266 |
{ |
| 267 |
// Check internal pipe status |
| 268 |
if (mError != 0) |
| 269 |
{ |
| 270 |
return mError; |
| 271 |
} |
| 272 |
|
| 273 |
PBYTE pWrite = (PBYTE)&val; |
| 274 |
|
| 275 |
int writeSize = sizeof( DWORD ); |
| 276 |
while (writeSize > 0) |
| 277 |
{ |
| 278 |
int bytesWritten = write( mPipes[WRITING], pWrite, writeSize ); |
| 279 |
if (bytesWritten == -1) |
| 280 |
{ |
| 281 |
// Store error from write |
| 282 |
int writeErr = errno; |
| 283 |
|
| 284 |
// First error? |
| 285 |
if (mError == 0) |
| 286 |
{ |
| 287 |
// Yes, save the error |
| 288 |
mError = writeErr; |
| 289 |
} |
| 290 |
|
| 291 |
// We cannot recover from this error |
| 292 |
Close(); |
| 293 |
return writeErr; |
| 294 |
} |
| 295 |
|
| 296 |
pWrite += bytesWritten; |
| 297 |
writeSize -= bytesWritten; |
| 298 |
} |
| 299 |
|
| 300 |
// Success |
| 301 |
return 0; |
| 302 |
} |
| 303 |
|
| 304 |
/*=========================================================================== |
| 305 |
METHOD: |
| 306 |
Wait (Free Method) |
| 307 |
|
| 308 |
DESCRIPTION: |
| 309 |
Wait for the event to be signalled and return the read in value |
| 310 |
|
| 311 |
PARAMETERS: |
| 312 |
timeoutMS [ I ] - Relative timeout length (in milliseconds) |
| 313 |
val [ O ] - Associated value upon success |
| 314 |
|
| 315 |
RETURN VALUE: |
| 316 |
Return code |
| 317 |
0 on success |
| 318 |
ETIME on timeout |
| 319 |
errno value on failure |
| 320 |
===========================================================================*/ |
| 321 |
int cEvent::Wait( |
| 322 |
DWORD timeoutMS, |
| 323 |
DWORD & val ) |
| 324 |
{ |
| 325 |
// Check internal pipe status |
| 326 |
if (mError != 0) |
| 327 |
{ |
| 328 |
return mError; |
| 329 |
} |
| 330 |
|
| 331 |
fd_set fds; |
| 332 |
FD_ZERO( &fds ); |
| 333 |
FD_SET( mPipes[READING], &fds ); |
| 334 |
|
| 335 |
struct timeval timeOut; |
| 336 |
|
| 337 |
// Add avoiding an overflow on (long)usec |
| 338 |
timeOut.tv_sec = timeoutMS / 1000l; |
| 339 |
timeOut.tv_usec = ( timeoutMS % 1000l ) * 1000l; |
| 340 |
|
| 341 |
// Wait for activity on the pipe for the specified amount of time |
| 342 |
int rc = select( mPipes[READING] + 1, &fds, 0, 0, &timeOut ); |
| 343 |
if (rc == -1) |
| 344 |
{ |
| 345 |
// Store error from select |
| 346 |
int selectErr = errno; |
| 347 |
|
| 348 |
// First error? |
| 349 |
if (mError == 0) |
| 350 |
{ |
| 351 |
// Yes, save the error |
| 352 |
mError = selectErr; |
| 353 |
} |
| 354 |
|
| 355 |
// We cannot recover from this error |
| 356 |
Close(); |
| 357 |
return selectErr; |
| 358 |
} |
| 359 |
else if (rc == 0) |
| 360 |
{ |
| 361 |
// No activity on the pipe |
| 362 |
return ETIME; |
| 363 |
} |
| 364 |
|
| 365 |
return Read( val ); |
| 366 |
} |
| 367 |
|
| 368 |
/*=========================================================================== |
| 369 |
METHOD: |
| 370 |
Clear (Free Method) |
| 371 |
|
| 372 |
DESCRIPTION: |
| 373 |
Read and discard all values currently in the pipe |
| 374 |
===========================================================================*/ |
| 375 |
void cEvent::Clear() |
| 376 |
{ |
| 377 |
DWORD unusedVal; |
| 378 |
int rc = 0; |
| 379 |
while (rc == 0) |
| 380 |
{ |
| 381 |
rc = Wait( (DWORD)0, unusedVal ); |
| 382 |
} |
| 383 |
} |
| 384 |
|
| 385 |
|
| 386 |
/*=========================================================================== |
| 387 |
METHOD: |
| 388 |
Read (Internal Method) |
| 389 |
|
| 390 |
DESCRIPTION: |
| 391 |
Read a DWORD from the pipe |
| 392 |
|
| 393 |
RETURN VALUE: |
| 394 |
Return code |
| 395 |
0 on success |
| 396 |
errno value on failure |
| 397 |
===========================================================================*/ |
| 398 |
int cEvent::Read( DWORD & val ) |
| 399 |
{ |
| 400 |
DWORD tempVal; |
| 401 |
PBYTE pRead = (PBYTE)&tempVal; |
| 402 |
|
| 403 |
int readSize = sizeof( DWORD ); |
| 404 |
while (readSize > 0) |
| 405 |
{ |
| 406 |
int bytesRead = read( mPipes[READING], pRead, readSize ); |
| 407 |
if (bytesRead <= 0) |
| 408 |
{ |
| 409 |
// Store error from read |
| 410 |
int readErr = errno; |
| 411 |
if (readErr == 0) |
| 412 |
{ |
| 413 |
// Hard error! This should NEVER happen for a pipe |
| 414 |
ASSERT( 0 ); |
| 415 |
readErr = EBADF; |
| 416 |
} |
| 417 |
|
| 418 |
// First error? |
| 419 |
if (mError == 0) |
| 420 |
{ |
| 421 |
// Yes, store the error |
| 422 |
mError = readErr; |
| 423 |
} |
| 424 |
|
| 425 |
// We cannot recover from this error |
| 426 |
Close(); |
| 427 |
return readErr; |
| 428 |
} |
| 429 |
|
| 430 |
pRead += bytesRead; |
| 431 |
readSize -= bytesRead; |
| 432 |
} |
| 433 |
|
| 434 |
val = tempVal; |
| 435 |
|
| 436 |
return 0; |
| 437 |
} |