summaryrefslogtreecommitdiffstats
path: root/src/crimson/thread/Condition.h
blob: 2a5c643d4a112e2298afe5188f2944f2d70f3677 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab

#pragma once

#include <seastar/core/reactor.hh>
#include <sys/eventfd.h>

namespace ceph::thread {

/// a synchronization primitive can be used to block a seastar thread, until
/// another thread notifies it.
class Condition {
  seastar::file_desc file_desc;
  int fd;
  seastar::pollable_fd_state fd_state;
  eventfd_t event = 0;
public:
  Condition()
    : file_desc{seastar::file_desc::eventfd(0, 0)},
      fd(file_desc.get()),
      fd_state{std::move(file_desc)}
  {}
  seastar::future<> wait() {
    return seastar::engine().read_some(fd_state, &event, sizeof(event))
      .then([](size_t) {
	  return seastar::now();
	});
  }
  void notify() {
    eventfd_t result = 1;
    ::eventfd_write(fd, result);
  }
};

} // namespace ceph::thread