YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
Event.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-FileCopyrightText: 2006-2010 RobotCub Consortium
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#include <yarp/os/Event.h>
8#include <yarp/os/Semaphore.h>
9
10#include <mutex>
11
13{
14public:
15 Private(bool autoReset) :
16 autoReset(autoReset),
17 action(0)
18 {
19 signalled = false;
20 waiters = 0;
21 }
22
23 void wait()
24 {
25 stateMutex.lock();
26 if (signalled) {
27 stateMutex.unlock();
28 return;
29 }
30 waiters++;
31 stateMutex.unlock();
32 action.wait();
33 if (autoReset) {
34 reset();
35 }
36 }
37
38 void signal(bool after = true)
39 {
40 stateMutex.lock();
41 int w = waiters;
42 if (w > 0) {
43 if (autoReset) {
44 w = 1;
45 }
46 for (int i = 0; i < w; i++) {
47 action.post();
48 waiters--;
49 }
50 }
51 signalled = after;
52 stateMutex.unlock();
53 }
54
55 void reset()
56 {
57 stateMutex.lock();
58 signalled = false;
59 stateMutex.unlock();
60 }
61
62private:
63 bool autoReset;
64 bool signalled;
65 int waiters;
66 std::mutex stateMutex;
67 Semaphore action;
68};
69
70
75
77{
78 delete mPriv;
79}
80
82{
83 mPriv->wait();
84}
85
87{
88 mPriv->signal();
89}
90
92{
93 mPriv->reset();
94}
A mini-server for performing network communication in the background.
Private(bool autoReset)
Definition Event.cpp:15
void signal(bool after=true)
Definition Event.cpp:38
void signal()
Put the event in a signaled state.
Definition Event.cpp:86
void wait()
Wait for the event to be signaled.
Definition Event.cpp:81
void reset()
Put the event in a reset state.
Definition Event.cpp:91
Event(bool autoResetAfterWait=true)
Constructor.
Definition Event.cpp:71
virtual ~Event()
Destructor.
Definition Event.cpp:76
A class for thread synchronization and mutual exclusion.
Definition Semaphore.h:25
void wait()
Decrement the counter, even if we must wait to do that.
Definition Semaphore.cpp:96
void post()
Increment the counter.