Emeric Vigier | 2f62582 | 2012-08-06 11:09:52 -0400 | [diff] [blame] | 1 | // Copyright (C) 1999-2005 Open Source Telecom Corporation. |
| 2 | // Copyright (C) 2006-2010 David Sugar, Tycho Softworks. |
| 3 | // |
| 4 | // This program is free software; you can redistribute it and/or modify |
| 5 | // it under the terms of the GNU General Public License as published by |
| 6 | // the Free Software Foundation; either version 2 of the License, or |
| 7 | // (at your option) any later version. |
| 8 | // |
| 9 | // This program is distributed in the hope that it will be useful, |
| 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | // GNU General Public License for more details. |
| 13 | // |
| 14 | // You should have received a copy of the GNU General Public License |
| 15 | // along with this program; if not, write to the Free Software |
| 16 | // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 17 | // |
| 18 | // As a special exception, you may use this file as part of a free software |
| 19 | // library without restriction. Specifically, if other files instantiate |
| 20 | // templates or use macros or inline functions from this file, or you compile |
| 21 | // this file and link it with other files to produce an executable, this |
| 22 | // file does not by itself cause the resulting executable to be covered by |
| 23 | // the GNU General Public License. This exception does not however |
| 24 | // invalidate any other reasons why the executable file might be covered by |
| 25 | // the GNU General Public License. |
| 26 | // |
| 27 | // This exception applies only to the code released under the name GNU |
| 28 | // Common C++. If you copy code from other releases into a copy of GNU |
| 29 | // Common C++, as the General Public License permits, the exception does |
| 30 | // not apply to the code that you add in this way. To avoid misleading |
| 31 | // anyone as to the status of such modified files, you must delete |
| 32 | // this exception notice from them. |
| 33 | // |
| 34 | // If you write modifications of your own for GNU Common C++, it is your choice |
| 35 | // whether to permit this exception to apply to your modifications. |
| 36 | // If you do not wish that, delete this exception notice. |
| 37 | // |
| 38 | |
| 39 | #include <cc++/config.h> |
| 40 | #include <cc++/export.h> |
| 41 | #include <cc++/thread.h> |
| 42 | #include "private.h" |
| 43 | |
| 44 | #include <cerrno> |
| 45 | |
| 46 | #ifdef CCXX_NAMESPACES |
| 47 | namespace ost { |
| 48 | #endif |
| 49 | |
| 50 | Event::Event() |
| 51 | { |
| 52 | #ifdef WIN32 |
| 53 | cond = ::CreateEvent(NULL, true, false, NULL); |
| 54 | if(cond == NULL) |
| 55 | THROW(this); |
| 56 | #else |
| 57 | pthread_mutexattr_t attr; |
| 58 | pthread_mutexattr_init(&attr); |
| 59 | pthread_mutex_init(&_mutex, &attr); |
| 60 | pthread_mutexattr_destroy(&attr); |
| 61 | pthread_cond_init(&_cond, NULL); |
| 62 | _signaled = false; |
| 63 | _count = 0; |
| 64 | #endif |
| 65 | } |
| 66 | |
| 67 | Event::~Event() |
| 68 | { |
| 69 | #ifdef WIN32 |
| 70 | ::CloseHandle(cond); |
| 71 | #else |
| 72 | pthread_cond_destroy(&_cond); |
| 73 | pthread_mutex_destroy(&_mutex); |
| 74 | #endif |
| 75 | } |
| 76 | |
| 77 | void Event::signal(void) |
| 78 | { |
| 79 | #ifdef WIN32 |
| 80 | ::SetEvent(cond); |
| 81 | #else |
| 82 | pthread_mutex_lock(&_mutex); |
| 83 | _signaled = true; |
| 84 | ++_count; |
| 85 | pthread_cond_broadcast(&_cond); |
| 86 | pthread_mutex_unlock(&_mutex); |
| 87 | #endif |
| 88 | } |
| 89 | |
| 90 | void Event::reset(void) |
| 91 | { |
| 92 | #ifdef WIN32 |
| 93 | ::ResetEvent(cond); |
| 94 | #else |
| 95 | _signaled = false; |
| 96 | #endif |
| 97 | } |
| 98 | |
| 99 | bool Event::wait(void) |
| 100 | { |
| 101 | #ifdef WIN32 |
| 102 | return (Thread::waitThread(cond, INFINITE) == WAIT_OBJECT_0); |
| 103 | #else |
| 104 | return wait(TIMEOUT_INF); |
| 105 | #endif |
| 106 | } |
| 107 | |
| 108 | bool Event::wait(timeout_t timer) |
| 109 | { |
| 110 | #ifdef WIN32 |
| 111 | return (Thread::waitThread(cond, timer) == WAIT_OBJECT_0); |
| 112 | #else |
| 113 | int rc = 0; |
| 114 | struct timespec spec; |
| 115 | |
| 116 | pthread_mutex_lock(&_mutex); |
| 117 | long count = _count; |
| 118 | while(!_signaled && _count == count) { |
| 119 | if(timer != TIMEOUT_INF) |
| 120 | rc = pthread_cond_timedwait( |
| 121 | &_cond, &_mutex, getTimeout(&spec, timer)); |
| 122 | else |
| 123 | pthread_cond_wait(&_cond, &_mutex); |
| 124 | if(rc == ETIMEDOUT) |
| 125 | break; |
| 126 | } |
| 127 | pthread_mutex_unlock(&_mutex); |
| 128 | if(rc == ETIMEDOUT) |
| 129 | return false; |
| 130 | return true; |
| 131 | #endif |
| 132 | } |
| 133 | |
| 134 | #ifdef CCXX_NAMESPACES |
| 135 | } |
| 136 | #endif |
| 137 | |
| 138 | /** EMACS ** |
| 139 | * Local variables: |
| 140 | * mode: c++ |
| 141 | * c-basic-offset: 4 |
| 142 | * End: |
| 143 | */ |