1 | // |
---|
2 | // Semaphore_POSIX.cpp |
---|
3 | // |
---|
4 | // $Id: //poco/1.3/Foundation/src/Semaphore_POSIX.cpp#1 $ |
---|
5 | // |
---|
6 | // Library: Foundation |
---|
7 | // Package: Threading |
---|
8 | // Module: Semaphore |
---|
9 | // |
---|
10 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
---|
11 | // and Contributors. |
---|
12 | // |
---|
13 | // Permission is hereby granted, free of charge, to any person or organization |
---|
14 | // obtaining a copy of the software and accompanying documentation covered by |
---|
15 | // this license (the "Software") to use, reproduce, display, distribute, |
---|
16 | // execute, and transmit the Software, and to prepare derivative works of the |
---|
17 | // Software, and to permit third-parties to whom the Software is furnished to |
---|
18 | // do so, all subject to the following: |
---|
19 | // |
---|
20 | // The copyright notices in the Software and this entire statement, including |
---|
21 | // the above license grant, this restriction and the following disclaimer, |
---|
22 | // must be included in all copies of the Software, in whole or in part, and |
---|
23 | // all derivative works of the Software, unless such copies or derivative |
---|
24 | // works are solely in the form of machine-executable object code generated by |
---|
25 | // a source language processor. |
---|
26 | // |
---|
27 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
28 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
29 | // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT |
---|
30 | // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE |
---|
31 | // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, |
---|
32 | // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
33 | // DEALINGS IN THE SOFTWARE. |
---|
34 | // |
---|
35 | |
---|
36 | |
---|
37 | #include "Poco/Semaphore_POSIX.h" |
---|
38 | #include <sys/time.h> |
---|
39 | |
---|
40 | |
---|
41 | namespace Poco { |
---|
42 | |
---|
43 | |
---|
44 | SemaphoreImpl::SemaphoreImpl(int n, int max): _n(n), _max(max) |
---|
45 | { |
---|
46 | poco_assert (n >= 0 && max > 0 && n <= max); |
---|
47 | |
---|
48 | if (pthread_mutex_init(&_mutex, NULL)) |
---|
49 | throw SystemException("cannot create semaphore (mutex)"); |
---|
50 | if (pthread_cond_init(&_cond, NULL)) |
---|
51 | throw SystemException("cannot create semaphore (condition)"); |
---|
52 | } |
---|
53 | |
---|
54 | |
---|
55 | SemaphoreImpl::~SemaphoreImpl() |
---|
56 | { |
---|
57 | pthread_cond_destroy(&_cond); |
---|
58 | pthread_mutex_destroy(&_mutex); |
---|
59 | } |
---|
60 | |
---|
61 | |
---|
62 | void SemaphoreImpl::waitImpl() |
---|
63 | { |
---|
64 | if (pthread_mutex_lock(&_mutex)) |
---|
65 | throw SystemException("wait for semaphore failed (lock)"); |
---|
66 | while (_n < 1) |
---|
67 | { |
---|
68 | if (pthread_cond_wait(&_cond, &_mutex)) |
---|
69 | { |
---|
70 | pthread_mutex_unlock(&_mutex); |
---|
71 | throw SystemException("wait for semaphore failed"); |
---|
72 | } |
---|
73 | } |
---|
74 | --_n; |
---|
75 | pthread_mutex_unlock(&_mutex); |
---|
76 | } |
---|
77 | |
---|
78 | |
---|
79 | bool SemaphoreImpl::waitImpl(long milliseconds) |
---|
80 | { |
---|
81 | int rc = 0; |
---|
82 | struct timespec abstime; |
---|
83 | |
---|
84 | #if defined(__VMS) |
---|
85 | struct timespec delta; |
---|
86 | delta.tv_sec = milliseconds / 1000; |
---|
87 | delta.tv_nsec = (milliseconds % 1000)*1000000; |
---|
88 | pthread_get_expiration_np(&delta, &abstime); |
---|
89 | #else |
---|
90 | struct timeval tv; |
---|
91 | gettimeofday(&tv, NULL); |
---|
92 | abstime.tv_sec = tv.tv_sec + milliseconds / 1000; |
---|
93 | abstime.tv_nsec = tv.tv_usec*1000 + (milliseconds % 1000)*1000000; |
---|
94 | if (abstime.tv_nsec >= 1000000000) |
---|
95 | { |
---|
96 | abstime.tv_nsec -= 1000000000; |
---|
97 | abstime.tv_sec++; |
---|
98 | } |
---|
99 | #endif |
---|
100 | |
---|
101 | if (pthread_mutex_lock(&_mutex) != 0) |
---|
102 | throw SystemException("wait for semaphore failed (lock)"); |
---|
103 | while (_n < 1) |
---|
104 | { |
---|
105 | if ((rc = pthread_cond_timedwait(&_cond, &_mutex, &abstime))) |
---|
106 | { |
---|
107 | if (rc == ETIMEDOUT) break; |
---|
108 | pthread_mutex_unlock(&_mutex); |
---|
109 | throw SystemException("cannot wait for semaphore"); |
---|
110 | } |
---|
111 | } |
---|
112 | if (rc == 0) --_n; |
---|
113 | pthread_mutex_unlock(&_mutex); |
---|
114 | return rc == 0; |
---|
115 | } |
---|
116 | |
---|
117 | |
---|
118 | } // namespace Poco |
---|