1 | // Copyright (c) 2013 by István Váradi
|
---|
2 |
|
---|
3 | // This file is part of VSCPL, a simple cross-platform utility library
|
---|
4 |
|
---|
5 | // Redistribution and use in source and binary forms, with or without
|
---|
6 | // modification, are permitted provided that the following conditions are met:
|
---|
7 |
|
---|
8 | // 1. Redistributions of source code must retain the above copyright notice, this
|
---|
9 | // list of conditions and the following disclaimer.
|
---|
10 | // 2. Redistributions in binary form must reproduce the above copyright notice,
|
---|
11 | // this list of conditions and the following disclaimer in the documentation
|
---|
12 | // and/or other materials provided with the distribution.
|
---|
13 |
|
---|
14 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
---|
15 | // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
---|
16 | // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
---|
17 | // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
---|
18 | // ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
---|
19 | // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
---|
20 | // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
---|
21 | // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
22 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
---|
23 | // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
24 |
|
---|
25 | // The views and conclusions contained in the software and documentation are those
|
---|
26 | // of the authors and should not be interpreted as representing official policies,
|
---|
27 | // either expressed or implied, of the FreeBSD Project.
|
---|
28 |
|
---|
29 | #ifndef HU_VARADIISTVAN_SCPL_POSIX_CONDVAR_H
|
---|
30 | #define HU_VARADIISTVAN_SCPL_POSIX_CONDVAR_H
|
---|
31 | //------------------------------------------------------------------------------
|
---|
32 |
|
---|
33 | #include "Mutex.h"
|
---|
34 |
|
---|
35 | //------------------------------------------------------------------------------
|
---|
36 |
|
---|
37 | namespace hu { namespace varadiistvan { namespace scpl {
|
---|
38 |
|
---|
39 | //------------------------------------------------------------------------------
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * A condition variable for POSIX threads.
|
---|
43 | */
|
---|
44 | class CondVar
|
---|
45 | {
|
---|
46 | private:
|
---|
47 | /**
|
---|
48 | * Fill the given timespec with the absolute time corresponding to
|
---|
49 | * the given relative timeout.
|
---|
50 | */
|
---|
51 | static void getAbsTime(struct ::timespec& abstime, int timeout);
|
---|
52 |
|
---|
53 | /**
|
---|
54 | * The pthreads structure.
|
---|
55 | */
|
---|
56 | pthread_cond_t cond;
|
---|
57 |
|
---|
58 | public:
|
---|
59 | /**
|
---|
60 | * Construct the condition variable.
|
---|
61 | */
|
---|
62 | CondVar();
|
---|
63 |
|
---|
64 | /**
|
---|
65 | * Destroy the condition variable.
|
---|
66 | */
|
---|
67 | ~CondVar();
|
---|
68 |
|
---|
69 | /**
|
---|
70 | * Wait for the condition variable.
|
---|
71 | *
|
---|
72 | * @param mutex the mutex to unlock and re-lock.
|
---|
73 | * @param timeout the number of milliseconds to wait at most. If
|
---|
74 | * negative, the function will wait indefinitely.
|
---|
75 | *
|
---|
76 | * @return true, if the condition variable was signalled, false
|
---|
77 | * if a timeout occured
|
---|
78 | */
|
---|
79 | bool wait(Mutex& mutex, int timeout = -1);
|
---|
80 |
|
---|
81 | /**
|
---|
82 | * Signal the condition variable to wake up one thread.
|
---|
83 | */
|
---|
84 | void signal();
|
---|
85 |
|
---|
86 | /**
|
---|
87 | * Signal all threads waiting on the condition variable.
|
---|
88 | */
|
---|
89 | void broadcast();
|
---|
90 | };
|
---|
91 |
|
---|
92 | //------------------------------------------------------------------------------
|
---|
93 | // Inline definitions
|
---|
94 | //------------------------------------------------------------------------------
|
---|
95 |
|
---|
96 | inline CondVar::CondVar()
|
---|
97 | {
|
---|
98 | pthread_cond_init(&cond, 0);
|
---|
99 | }
|
---|
100 |
|
---|
101 | //------------------------------------------------------------------------------
|
---|
102 |
|
---|
103 | inline CondVar::~CondVar()
|
---|
104 | {
|
---|
105 | pthread_cond_destroy(&cond);
|
---|
106 | }
|
---|
107 |
|
---|
108 | //------------------------------------------------------------------------------
|
---|
109 |
|
---|
110 | inline bool CondVar::wait(Mutex& mutex, int timeout)
|
---|
111 | {
|
---|
112 | if (timeout<0) {
|
---|
113 | pthread_cond_wait(&cond, &mutex.mutex);
|
---|
114 | return true;
|
---|
115 | } else {
|
---|
116 | struct ::timespec abstime;
|
---|
117 | getAbsTime(abstime, timeout);
|
---|
118 | return pthread_cond_timedwait(&cond, &mutex.mutex, &abstime)==0;
|
---|
119 | }
|
---|
120 | }
|
---|
121 |
|
---|
122 | //------------------------------------------------------------------------------
|
---|
123 |
|
---|
124 | inline void CondVar::signal()
|
---|
125 | {
|
---|
126 | pthread_cond_signal(&cond);
|
---|
127 | }
|
---|
128 |
|
---|
129 | //------------------------------------------------------------------------------
|
---|
130 |
|
---|
131 | inline void CondVar::broadcast()
|
---|
132 | {
|
---|
133 | pthread_cond_broadcast(&cond);
|
---|
134 | }
|
---|
135 |
|
---|
136 | //------------------------------------------------------------------------------
|
---|
137 |
|
---|
138 | } /* namespace hu::varadiistvan::scpl */ } /* namespace hu::varadiistvan */ } /* namespace hu */
|
---|
139 |
|
---|
140 | //------------------------------------------------------------------------------
|
---|
141 | #endif // HU_VARADIISTVAN_SCPL_CONDVAR_H
|
---|
142 |
|
---|
143 | // Local Variables:
|
---|
144 | // mode: C++
|
---|
145 | // c-basic-offset: 4
|
---|
146 | // indent-tabs-mode: nil
|
---|
147 | // End:
|
---|