<commit_before>// Copyright (c) 2013-2014 Flowgrammable.org
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
// 
// http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an "AS IS"
// BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
// or implied. See the License for the specific language governing
// permissions and limitations under the License.

namespace freeflow {

inline
Selector::Selector()
{
  FD_ZERO(&readset);
  FD_ZERO(&writeset);
  read_fds.insert(-1);
  write_fds.insert(-1);
}

inline int
max(const Selector& s)
{
  int max_read_fd = *s.read_fds.rbegin();
  int max_write_fd = *s.write_fds.rbegin();
  return std::max(max_read_fd, max_write_fd) + 1;
}

inline void
add_reader(Selector& s, int fd)
{
  FD_SET(fd, &s.readset);
  s.read_fds.insert(fd);
}

inline void
add_writer(Selector& s, int fd)
{
  FD_SET(fd, &s.writeset);
  s.write_fds.insert(fd);
}

inline void
del_reader(Selector& s, int fd)
{
  FD_CLR(fd, &s.readset);
  s.read_fds.erase(fd);
}

inline void
del_writer(Selector& s, int fd)
{
  FD_CLR(fd, &s.writeset);
  s.write_fds.erase(fd);
}

inline bool
is_readable(const Selector& s, int fd)
{
  return FD_ISSET(fd, &s.readset);
}

inline bool
is_writable(const Selector& s, int fd)
{
  return FD_ISSET(fd, &s.writeset);
}

inline int
select(Selector& s, const MicroTime& mt)
{
  timespec ts, *ts_ptr;
  ts_ptr = nullptr;

  std::chrono::microseconds usec = std::chrono::duration_cast<std::chrono::microseconds>(mt);
  if(usec != std::chrono::microseconds(0)) {
    ts.tv_sec = usec.count() / 1000000;
    ts.tv_nsec = 1000*(usec.count() % 1000000);
    ts_ptr = &ts;
  }

  int result = ::pselect(max(s), &s.readset, &s.writeset, nullptr, ts_ptr, nullptr);
  if(result == -1 and errno != EINTR)
    throw system_error();
  return result;
}

} // namespace freeflow<commit_msg>Fixed: read/write sets must be init(ed) on every call to select<commit_after>// Copyright (c) 2013-2014 Flowgrammable.org
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
// 
// http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an "AS IS"
// BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
// or implied. See the License for the specific language governing
// permissions and limitations under the License.

namespace freeflow {

inline
Selector::Selector()
{
  FD_ZERO(&readset);
  FD_ZERO(&writeset);
  read_fds.insert(-1);
  write_fds.insert(-1);
}

inline int
max(const Selector& s)
{
  int max_read_fd = *s.read_fds.rbegin();
  int max_write_fd = *s.write_fds.rbegin();
  return std::max(max_read_fd, max_write_fd) + 1;
}

inline void
add_reader(Selector& s, int fd)
{
  s.read_fds.insert(fd);
}

inline void
add_writer(Selector& s, int fd)
{
  s.write_fds.insert(fd);
}

inline void
del_reader(Selector& s, int fd)
{
  s.read_fds.erase(fd);
}

inline void
del_writer(Selector& s, int fd)
{
  s.write_fds.erase(fd);
}

inline bool
is_readable(const Selector& s, int fd)
{
  return FD_ISSET(fd, &s.readset);
}

inline bool
is_writable(const Selector& s, int fd)
{
  return FD_ISSET(fd, &s.writeset);
}

void inline
init_sets(Selector& s)
{
  for(auto fd : s.read_fds) {
    FD_SET(fd, &s.readset);
  }
  for(auto fd : s.write_fds) {
    FD_SET(fd, &s.writeset);
  }
}

inline int
select(Selector& s, const MicroTime& mt)
{
  timespec ts, *ts_ptr;
  ts_ptr = nullptr;

  std::chrono::microseconds usec = std::chrono::duration_cast<std::chrono::microseconds>(mt);
  if(usec != std::chrono::microseconds(0)) {
    ts.tv_sec = usec.count() / 1000000;
    ts.tv_nsec = 1000*(usec.count() % 1000000);
    ts_ptr = &ts;
  }

  init_sets(s);
  int result = ::pselect(max(s), &s.readset, &s.writeset, nullptr, ts_ptr, nullptr);
  if(result == -1 and errno != EINTR)
    throw system_error();
  return result;
}

} // namespace freeflow
{'fraction_non_alphanumeric': 0.14018691588785046, 'fraction_numerical': 0.016355140186915886, 'mean_word_length': 4.01288056206089, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7556393', 'n_tokens_mistral': 1665, 'n_tokens_neox': 1586, 'n_words': 428}