48 using Waiters = std::list<std::pair<double, Semaphore*>>;
74 auto waiter_it = waiters->begin();
75 while (waiter_it != waiters->end()) {
76 Semaphore* waiterSemaphore = waiter_it->second;
77 waiter_it = waiters->erase(waiter_it);
78 if (waiterSemaphore !=
nullptr) {
79 waiterSemaphore->
post();
87 NetworkBase::disconnect(clockName, port.getName(), style);
93 bool ok = bot.
read(reader);
100 if (!ok && !closing) {
101 yCError(NETWORKCLOCK,
"Error reading clock port");
106 double oldTime = _time;
109 _time = sec + (nsec * 1e-9);
114 auto waiter_it = waiters->begin();
115 if (oldTime > _time) {
118 waiter_it->first = _time + (waiter_it->first - oldTime);
121 while (waiter_it != waiters->end())
123 if (waiter_it->first - _time < 1E-12) {
124 Semaphore* waiterSemaphore = waiter_it->second;
125 waiter_it = waiters->erase(waiter_it);
126 if (waiterSemaphore !=
nullptr) {
127 waiterSemaphore->
post();
138 NetworkClock::NetworkClock() :
145 yCWarning(NETWORKCLOCK,
"Destroying network clock");
151 mPriv->port.setReadOnly();
152 mPriv->port.setReader(*mPriv);
153 if (!clockSourcePortName.empty()) {
154 mPriv->clockName = clockSourcePortName;
161 if (localPortName.empty()) {
162 const int MAX_STRING_SIZE = 255;
163 char hostName[MAX_STRING_SIZE];
170 localPortName += std::string(hostName) +
"/" + processInfo.
name +
"/" + std::string(std::to_string(processInfo.
pid)) +
"/clock:i";
174 bool ret = mPriv->port.open(localPortName);
185 yCError(NETWORKCLOCK,
"Cannot find time port \"%s\" or a time topic \"%s@\"\n", mPriv->clockName.c_str(), mPriv->clockName.c_str());
194 mPriv->timeMutex.lock();
195 double result = mPriv->_time;
196 mPriv->timeMutex.unlock();
202 if (seconds <= 1E-12) {
206 mPriv->listMutex.lock();
207 if (mPriv->closing) {
210 mPriv->listMutex.unlock();
215 std::pair<double, Semaphore*> waiter(
now() + seconds,
new Semaphore(0));
216 mPriv->waiters->push_back(waiter);
217 mPriv->listMutex.unlock();
219 waiter.second->wait();
220 delete waiter.second;
221 waiter.second =
nullptr;
226 return mPriv->initted;
std::list< std::pair< double, Semaphore * > > Waiters
bool read(ConnectionReader &reader) override
Read this object from a network connection.
A simple collection of objects that can be described and transmitted in a portable way.
bool read(ConnectionReader &reader) override
Set the bottle's value based on input from a network connection.
Value & get(size_type index) const
Reads a Value v from a certain part of the list.
An interface for reading from a network connection.
static bool connect(const std::string &src, const std::string &dest, const std::string &carrier="", bool quiet=true)
Request that an output port connect to an input port.
static Contact queryName(const std::string &name)
Find out information about a registered name.
void delay(double seconds) override
Wait for a certain number of seconds.
bool isValid() const override
Check if time is valid (non-zero).
bool open(const std::string &clockSourcePortName, std::string localPortName="")
double now() override
Return the current time in seconds, relative to an arbitrary starting point.
Interface implemented by all objects that can read themselves from the network, such as Bottle object...
A mini-server for network communication.
A class for thread synchronization and mutual exclusion.
void post()
Increment the counter.
static void delaySystem(double seconds)
static ProcessInfo getProcessInfo(int pid=0)
gets the operating system process information given by its PID.
virtual std::int32_t asInt32() const
Get 32-bit integer value.
#define yCError(component,...)
#define yCWarning(component,...)
#define YARP_OS_LOG_COMPONENT(name, name_string)
The components from which ports and connections are built.
An interface to the operating system, including Port based communication.
void gethostname(char *hostname, size_t size)
Portable wrapper for the gethostname() function.
bool read(ImageOf< PixelRgb > &dest, const std::string &src, image_fileformat format=FORMAT_ANY)
The ProcessInfo struct provides the operating system process information.