MPD 0.20.6

- Main Page
- Namespaces
- Data Structures
- Files

- File List
- Globals

PosixCond.hxx
Go to the documentation of this file.
1 /*
2 * Copyright (C) 2009-2015 Max Kellermann <max.kellermann@gmail.com>
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * - Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the
14 * distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
19 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
20 * FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
27 * OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 #ifndef THREAD_POSIX_COND_HXX
31 #define THREAD_POSIX_COND_HXX
32
33 #include "PosixMutex.hxx"
34
35 #include <chrono>
36
37 #include <sys/time.h>
38
42 class PosixCond {
43 pthread_cond_t cond;
44
45 public:
46 #ifdef __GLIBC__
47 /* optimized constexpr constructor for pthread implementations
48 that support it */
49 constexpr PosixCond():cond(PTHREAD_COND_INITIALIZER) {}
50 #else
51 /* slow fallback for pthread implementations that are not
52 compatible with "constexpr" */
53 PosixCond() {
54 pthread_cond_init(&cond, nullptr);
55 }
56
57 ~PosixCond() {
58 pthread_cond_destroy(&cond);
59 }
60 #endif
61
62 PosixCond(const PosixCond &other) = delete;
63 PosixCond &operator=(const PosixCond &other) = delete;
64
65 void signal() {
66 pthread_cond_signal(&cond);
67 }
68
69 void broadcast() {
70 pthread_cond_broadcast(&cond);
71 }
72
73 void wait(PosixMutex &mutex) {
74 pthread_cond_wait(&cond, &mutex.mutex);
75 }
76
77 private:
78 bool timed_wait(PosixMutex &mutex, unsigned timeout_ms) {
79 struct timeval now;
80 gettimeofday(&now, nullptr);
81
82 struct timespec ts;
83 ts.tv_sec = now.tv_sec + timeout_ms / 1000;
84 ts.tv_nsec = (now.tv_usec + (timeout_ms % 1000) * 1000) * 1000;
85 // Keep tv_nsec < 1E9 to prevent return of EINVAL
86 if (ts.tv_nsec >= 1000000000) {
87 ts.tv_nsec -= 1000000000;
88 ts.tv_sec++;
89 }
90
91 return pthread_cond_timedwait(&cond, &mutex.mutex, &ts) == 0;
92 }
93
94 public:
95 bool timed_wait(PosixMutex &mutex,
96 std::chrono::steady_clock::duration timeout) {
97 auto timeout_ms = std::chrono::duration_cast<std::chrono::milliseconds>(timeout).count();
98 if (timeout_ms < 0)
99 timeout_ms = 0;
100 else if (timeout_ms > std::numeric_limits<unsigned>::max())
101 timeout_ms = std::numeric_limits<unsigned>::max();
102
103 return timed_wait(mutex, timeout_ms);
104 }
105 };
106
107 #endif
PosixCond::timed_wait
bool timed_wait(PosixMutex &mutex, std::chrono::steady_clock::duration timeout)
Definition: PosixCond.hxx:95
PosixCond
Low-level wrapper for a pthread_cond_t.
Definition: PosixCond.hxx:42
PosixCond::wait
void wait(PosixMutex &mutex)
Definition: PosixCond.hxx:73
PosixMutex
Low-level wrapper for a pthread_mutex_t.
Definition: PosixMutex.hxx:38
PosixMutex.hxx
PosixCond::operator=
PosixCond & operator=(const PosixCond &other)=delete
PosixCond::~PosixCond
~PosixCond()
Definition: PosixCond.hxx:57
PosixCond::signal
void signal()
Definition: PosixCond.hxx:65
PosixCond::PosixCond
PosixCond()
Definition: PosixCond.hxx:53
PosixCond::broadcast
void broadcast()
Definition: PosixCond.hxx:69