2018-08-13 19:15:09 +02:00
|
|
|
//
|
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2018
|
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
2018-09-10 01:37:05 +02:00
|
|
|
#include "td/utils/port/PollFlags.h"
|
2018-08-13 19:15:09 +02:00
|
|
|
|
|
|
|
namespace td {
|
2018-09-10 01:37:05 +02:00
|
|
|
|
2018-08-13 19:15:09 +02:00
|
|
|
bool PollFlagsSet::write_flags(PollFlags flags) {
|
|
|
|
if (flags.empty()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
auto old_flags = to_write_.fetch_or(flags.raw(), std::memory_order_relaxed);
|
|
|
|
return (flags.raw() & ~old_flags) != 0;
|
|
|
|
}
|
2018-09-10 01:37:05 +02:00
|
|
|
|
2018-08-13 19:15:09 +02:00
|
|
|
bool PollFlagsSet::write_flags_local(PollFlags flags) {
|
|
|
|
return flags_.add_flags(flags);
|
|
|
|
}
|
2018-09-10 01:37:05 +02:00
|
|
|
|
2018-08-13 19:15:09 +02:00
|
|
|
bool PollFlagsSet::flush() const {
|
|
|
|
if (to_write_.load(std::memory_order_relaxed) == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
auto to_write = to_write_.exchange(0, std::memory_order_relaxed);
|
|
|
|
auto old_flags = flags_;
|
|
|
|
flags_.add_flags(PollFlags::from_raw(to_write));
|
|
|
|
if (flags_.can_close()) {
|
|
|
|
flags_.remove_flags(PollFlags::Write());
|
|
|
|
}
|
|
|
|
return flags_ != old_flags;
|
|
|
|
}
|
2018-09-10 01:37:05 +02:00
|
|
|
|
2018-08-13 19:15:09 +02:00
|
|
|
PollFlags PollFlagsSet::read_flags() const {
|
|
|
|
flush();
|
|
|
|
return flags_;
|
|
|
|
}
|
2018-09-10 01:37:05 +02:00
|
|
|
|
2018-08-13 19:15:09 +02:00
|
|
|
PollFlags PollFlagsSet::read_flags_local() const {
|
|
|
|
return flags_;
|
|
|
|
}
|
2018-09-10 01:37:05 +02:00
|
|
|
|
2018-08-13 19:15:09 +02:00
|
|
|
void PollFlagsSet::clear_flags(PollFlags flags) {
|
|
|
|
flags_.remove_flags(flags);
|
|
|
|
}
|
2018-09-10 01:37:05 +02:00
|
|
|
|
2018-08-13 19:15:09 +02:00
|
|
|
void PollFlagsSet::clear() {
|
|
|
|
to_write_ = 0;
|
|
|
|
flags_ = {};
|
|
|
|
}
|
|
|
|
|
2018-09-10 01:37:05 +02:00
|
|
|
StringBuilder &operator<<(StringBuilder &sb, PollFlags flags) {
|
2018-09-13 05:08:49 +02:00
|
|
|
sb << '[';
|
2018-09-10 01:37:05 +02:00
|
|
|
if (flags.can_read()) {
|
2018-09-13 05:08:49 +02:00
|
|
|
sb << 'R';
|
2018-09-10 01:37:05 +02:00
|
|
|
}
|
|
|
|
if (flags.can_write()) {
|
2018-09-13 05:08:49 +02:00
|
|
|
sb << 'W';
|
2018-09-10 01:37:05 +02:00
|
|
|
}
|
|
|
|
if (flags.can_close()) {
|
2018-09-13 05:08:49 +02:00
|
|
|
sb << 'C';
|
2018-09-10 01:37:05 +02:00
|
|
|
}
|
|
|
|
if (flags.has_pending_error()) {
|
2018-09-13 05:08:49 +02:00
|
|
|
sb << 'E';
|
2018-09-10 01:37:05 +02:00
|
|
|
}
|
2018-09-13 05:08:49 +02:00
|
|
|
return sb << ']';
|
2018-09-10 01:37:05 +02:00
|
|
|
}
|
|
|
|
|
2018-08-13 19:15:09 +02:00
|
|
|
} // namespace td
|