Fawkes API Fawkes Development Version
syncpoint.h
1/***************************************************************************
2 * syncpoint.h - Fawkes SyncPoint
3 *
4 * Created: Thu Jan 09 12:22:03 2014
5 * Copyright 2014-2018 Till Hofmann
6 *
7 ****************************************************************************/
8
9/* This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Library General Public License for more details.
18 *
19 * Read the full text in the LICENSE.GPL file in the doc directory.
20 */
21
22#ifndef _SYNCPOINT_SYNCPOINT_H_
23#define _SYNCPOINT_SYNCPOINT_H_
24
25#include <core/threading/mutex.h>
26#include <core/threading/wait_condition.h>
27#include <core/utils/circular_buffer.h>
28#include <core/utils/refptr.h>
29#include <interface/interface.h>
30#include <logging/multi.h>
31#include <syncpoint/syncpoint_call.h>
32#include <utils/time/time.h>
33
34#include <map>
35#include <set>
36#include <string>
37
38namespace fawkes {
39
40class SyncPointManager;
41class SyncPoint;
42
44{
45public:
46 bool operator()(const RefPtr<SyncPoint> sp1, const RefPtr<SyncPoint> sp2) const;
47};
48
50{
51public:
52 /** Type to define when a thread wakes up after waiting for a SyncPoint.
53 * A thread can be either wake up if ANY other thread emits the SyncPoint,
54 * or if ALL registered threads emit the SyncPoint.
55 */
56 typedef enum { WAIT_FOR_ONE, WAIT_FOR_ALL, NONE } WakeupType;
57
58 SyncPoint(std::string identifier,
59 MultiLogger *logger,
60 uint max_waittime_sec = 0,
61 uint max_waittime_nsec = 0);
62 virtual ~SyncPoint();
63
64 /** send a signal to all waiting threads */
65 virtual void emit(const std::string &component);
66
67 /** wait for the sync point to be emitted by any other component */
68 virtual void wait(const std::string &component,
69 WakeupType = WAIT_FOR_ONE,
70 uint wait_sec = 0,
71 uint wait_nsec = 0);
72 /** abort waiting */
73 virtual void unwait(const std::string &component);
74 virtual void wait_for_one(const std::string &component);
75 virtual void wait_for_all(const std::string &component);
76 /** wait for the sync point, but abort after given time */
77 virtual void reltime_wait_for_one(const std::string &component, uint wait_sec, uint wait_nsec);
78 virtual void reltime_wait_for_all(const std::string &component, uint wait_sec, uint wait_nsec);
79
80 /** register as emitter */
81 virtual void register_emitter(const std::string &component);
82
83 /** unregister as emitter */
84 virtual void unregister_emitter(const std::string &component, bool emit_if_pending = true);
85 bool is_emitter(const std::string &component) const;
86 bool is_watcher(const std::string &component) const;
87
88 void lock_until_next_wait(const std::string &component);
89
90 std::string get_identifier() const;
91 bool operator==(const SyncPoint &other) const;
92 bool operator==(const std::string &other) const;
93 bool operator<(const SyncPoint &other) const;
94
95 std::set<std::string> get_watchers() const;
96 std::multiset<std::string> get_emitters() const;
99 bool watcher_is_waiting(std::string watcher, WakeupType type) const;
100
101 /**
102 * allow Syncpoint Manager to edit
103 */
104 friend class SyncPointManager;
105
106protected:
107 std::pair<std::set<std::string>::iterator, bool> add_watcher(std::string watcher);
108 /** send a signal to all waiting threads */
109 virtual void emit(const std::string &component, bool remove_from_pending);
110
111protected:
112 /** The unique identifier of the SyncPoint */
113 const std::string identifier_;
114 /** Set of all components which use this SyncPoint */
115 std::set<std::string> watchers_;
116 /** Set of all components which are currently waiting for a single emitter */
117 std::set<std::string> watchers_wait_for_one_;
118 /** Set of all components which are currently waiting on the barrier */
119 std::set<std::string> watchers_wait_for_all_;
120
121 /** A buffer of the most recent emit calls. */
123 /** A buffer of the most recent wait calls of type WAIT_FOR_ONE. */
125 /** A buffer of the most recent wait calls of type WAIT_FOR_ALL. */
127 /** Time when this SyncPoint was created */
129
130 /** Mutex used to protect all member variables */
132 /** Mutex used to allow lock_until_next_wait */
134 /** WaitCondition used for lock_until_next_wait */
136 /** Mutex used for cond_wait_for_one_ */
138 /** WaitCondition which is used for wait_for_one() */
140 /** Mutex used for cond_wait_for_all_ */
142 /** WaitCondition which is used for wait_for_all() */
144 /** true if the wait for all timer is running */
146 /** the component that started the wait-for-all timer */
148 /** maximum waiting time in secs */
150 /** maximum waiting time in nsecs */
152
153 /** Logger */
155
156private:
157 void reset_emitters();
158 bool is_pending(std::string component);
159 void handle_default(std::string component, WakeupType type);
160 void cleanup();
161
162private:
163 /** The predecessor SyncPoint, which is the SyncPoint one level up
164 * e.g. "/test/sp" -> "/test"
165 */
166 RefPtr<SyncPoint> predecessor_;
167
168 /** all successors */
169 std::set<RefPtr<SyncPoint>, SyncPointSetLessThan> successors_;
170
171 std::multiset<std::string> emitters_;
172 std::multiset<std::string> pending_emitters_;
173
174 std::set<std::string> bad_components_;
175
176 std::string emit_locker_;
177
178 Time last_emitter_reset_;
179};
180
181} // end namespace fawkes
182
183#endif
Circular buffer with a fixed size.
Log through multiple loggers.
Definition: multi.h:35
Mutex mutual exclusion lock.
Definition: mutex.h:33
This class gives access to SyncPoints.
Compare sets of syncpoints.
Definition: syncpoint.h:44
bool operator()(const RefPtr< SyncPoint > sp1, const RefPtr< SyncPoint > sp2) const
LessThan Operator to use for the manager's SyncPoint set Since we store RefPtrs to SyncPoints we have...
The SyncPoint class.
Definition: syncpoint.h:50
bool operator<(const SyncPoint &other) const
LessThan Operator.
Definition: syncpoint.cpp:141
virtual void reltime_wait_for_one(const std::string &component, uint wait_sec, uint wait_nsec)
wait for the sync point, but abort after given time
Definition: syncpoint.cpp:376
CircularBuffer< SyncPointCall > get_wait_calls(WakeupType type=WAIT_FOR_ONE) const
Definition: syncpoint.cpp:529
virtual void wait_for_one(const std::string &component)
Wait for a single emitter.
Definition: syncpoint.cpp:356
virtual void wait(const std::string &component, WakeupType=WAIT_FOR_ONE, uint wait_sec=0, uint wait_nsec=0)
wait for the sync point to be emitted by any other component
Definition: syncpoint.cpp:241
std::multiset< std::string > get_emitters() const
Definition: syncpoint.cpp:545
virtual void reltime_wait_for_all(const std::string &component, uint wait_sec, uint wait_nsec)
Wait for all registered emitters for the given time.
Definition: syncpoint.cpp:387
std::set< std::string > watchers_wait_for_one_
Set of all components which are currently waiting for a single emitter.
Definition: syncpoint.h:117
bool wait_for_all_timer_running_
true if the wait for all timer is running
Definition: syncpoint.h:145
virtual void unwait(const std::string &component)
abort waiting
Definition: syncpoint.cpp:398
CircularBuffer< SyncPointCall > wait_for_all_calls_
A buffer of the most recent wait calls of type WAIT_FOR_ALL.
Definition: syncpoint.h:126
Mutex * mutex_wait_for_one_
Mutex used for cond_wait_for_one_.
Definition: syncpoint.h:137
WaitCondition * cond_wait_for_one_
WaitCondition which is used for wait_for_one()
Definition: syncpoint.h:139
virtual void unregister_emitter(const std::string &component, bool emit_if_pending=true)
unregister as emitter
Definition: syncpoint.cpp:456
uint max_waittime_sec_
maximum waiting time in secs
Definition: syncpoint.h:149
std::string wait_for_all_timer_owner_
the component that started the wait-for-all timer
Definition: syncpoint.h:147
Mutex * mutex_next_wait_
Mutex used to allow lock_until_next_wait.
Definition: syncpoint.h:133
uint max_waittime_nsec_
maximum waiting time in nsecs
Definition: syncpoint.h:151
const Time creation_time_
Time when this SyncPoint was created.
Definition: syncpoint.h:128
Mutex * mutex_wait_for_all_
Mutex used for cond_wait_for_all_.
Definition: syncpoint.h:141
std::set< std::string > get_watchers() const
Definition: syncpoint.cpp:518
std::set< std::string > watchers_wait_for_all_
Set of all components which are currently waiting on the barrier.
Definition: syncpoint.h:119
bool is_emitter(const std::string &component) const
Check if the given component is an emitter.
Definition: syncpoint.cpp:484
std::pair< std::set< std::string >::iterator, bool > add_watcher(std::string watcher)
Add a watcher to the watch list.
Definition: syncpoint.cpp:508
virtual void register_emitter(const std::string &component)
register as emitter
Definition: syncpoint.cpp:439
bool watcher_is_waiting(std::string watcher, WakeupType type) const
Check if the given waiter is currently waiting with the given type.
Definition: syncpoint.cpp:567
virtual void emit(const std::string &component)
send a signal to all waiting threads
Definition: syncpoint.cpp:150
WaitCondition * cond_next_wait_
WaitCondition used for lock_until_next_wait.
Definition: syncpoint.h:135
MultiLogger * logger_
Logger.
Definition: syncpoint.h:154
CircularBuffer< SyncPointCall > get_emit_calls() const
Definition: syncpoint.cpp:554
SyncPoint(std::string identifier, MultiLogger *logger, uint max_waittime_sec=0, uint max_waittime_nsec=0)
Constructor.
Definition: syncpoint.cpp:59
void lock_until_next_wait(const std::string &component)
Lock the SyncPoint for emitters until the specified component does the next wait() call.
Definition: syncpoint.cpp:418
std::set< std::string > watchers_
Set of all components which use this SyncPoint.
Definition: syncpoint.h:115
CircularBuffer< SyncPointCall > emit_calls_
A buffer of the most recent emit calls.
Definition: syncpoint.h:122
CircularBuffer< SyncPointCall > wait_for_one_calls_
A buffer of the most recent wait calls of type WAIT_FOR_ONE.
Definition: syncpoint.h:124
std::string get_identifier() const
Definition: syncpoint.cpp:107
const std::string identifier_
The unique identifier of the SyncPoint.
Definition: syncpoint.h:113
bool is_watcher(const std::string &component) const
Check if the given component is a watch.
Definition: syncpoint.cpp:495
virtual void wait_for_all(const std::string &component)
Wait for all registered emitters.
Definition: syncpoint.cpp:365
WakeupType
Type to define when a thread wakes up after waiting for a SyncPoint.
Definition: syncpoint.h:56
WaitCondition * cond_wait_for_all_
WaitCondition which is used for wait_for_all()
Definition: syncpoint.h:143
bool operator==(const SyncPoint &other) const
EqualOperator.
Definition: syncpoint.cpp:118
Mutex * mutex_
Mutex used to protect all member variables.
Definition: syncpoint.h:131
A class for handling time.
Definition: time.h:93
Wait until a given condition holds.
Fawkes library namespace.