blob: adf3b79e8a1ab56f0870d13a8c58e96261db1329 [file] [log] [blame]
Emeric Vigier2f625822012-08-06 11:09:52 -04001// 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++/exception.h>
42#include <cc++/thread.h>
43#include "private.h"
44#include <cstdio>
45#include <cstdlib>
46
47#ifdef CCXX_NAMESPACES
48namespace ost {
49#endif
50
51#ifndef WIN32
52
53extern "C" {
54#include <sys/types.h>
55}
56
57#include <cerrno>
58
59Semaphore::Semaphore(unsigned resource)
60{
61 pthread_mutexattr_t attr;
62 pthread_mutexattr_init(&attr);
63 pthread_mutex_init(&_mutex, &attr);
64 pthread_mutexattr_destroy(&attr);
65 if(pthread_cond_init(&_cond, NULL) && Thread::getException() == Thread::throwObject)
66 THROW(this);
67
68 _count = resource;
69 _waiters = 0;
70}
71
72Semaphore::~Semaphore()
73{
74 // Unlock is needed to unlock the mutex in the case of a cancel during Semaphore::wait()
75 // see PTHREAD_COND_TIMEDWAIT(3P)
76 pthread_mutex_unlock(&_mutex);
77 pthread_cond_destroy(&_cond);
78 pthread_mutex_destroy(&_mutex);
79}
80
81void Semaphore::force_unlock_after_cancellation()
82{
83 pthread_mutex_unlock(&_mutex);
84}
85
86bool Semaphore::wait(timeout_t timeout)
87{
88 struct timespec ts;
89 bool flag = true;
90 int rc;
91 pthread_mutex_lock(&_mutex);
92 ++_waiters;
93
94 if(_count)
95 goto exiting;
96
97 if(!timeout) {
98 while(_count == 0)
99 pthread_cond_wait(&_cond, &_mutex);
100 goto exiting;
101 }
102
103 getTimeout(&ts, timeout);
104 rc = pthread_cond_timedwait(&_cond, &_mutex, &ts);
105 if(rc == ETIMEDOUT || _count == 0)
106 flag = false;
107
108exiting:
109 --_waiters;
110 if(_count)
111 --_count;
112 pthread_mutex_unlock(&_mutex);
113 return flag;
114}
115
116void Semaphore::post(void)
117{
118 pthread_mutex_lock(&_mutex);
119 if(_waiters > 0)
120 pthread_cond_signal(&_cond);
121 ++_count;
122 pthread_mutex_unlock(&_mutex);
123}
124
125#if 0 // stripped since only in posix
126int Semaphore::getValue(void)
127{
128 int value;
129
130 pthread_mutex_lock(&_mutex);
131 value = _count;
132 pthread_mutex_unlock(&_mutex);
133 return value;
134}
135#endif
136
137#else
138
139Semaphore::Semaphore(unsigned resource)
140{
141 semObject = ::CreateSemaphore((LPSECURITY_ATTRIBUTES)NULL, (LONG)resource, MAX_SEM_VALUE, (LPCTSTR)NULL);
142}
143
144Semaphore::~Semaphore()
145{
146 ::CloseHandle(semObject);
147}
148
149bool Semaphore::wait(timeout_t timeout)
150{
151 if(!timeout)
152 timeout = INFINITE;
153
154 return Thread::waitThread(semObject, timeout) == WAIT_OBJECT_0;
155}
156
157void Semaphore::post(void)
158{
159 ::ReleaseSemaphore(semObject, 1, (LPLONG)NULL);
160}
161
162
163#endif //WIN32
164
165#ifdef CCXX_NAMESPACES
166}
167#endif
168
169/** EMACS **
170 * Local variables:
171 * mode: c++
172 * c-basic-offset: 4
173 * End:
174 */