30 bool IsFifoDescriptor(
int fd) {
32 int ret = ::fstat(fd, &info);
33 return (ret == 0) && ((info.st_mode & S_IFMT) == S_IFIFO);
39 virtual ~PosixJobserverClient() {
47 if (has_implicit_slot_) {
48 has_implicit_slot_ =
false;
51 uint8_t slot_char =
'\0';
54 ret = ::read(read_fd_, &slot_char, 1);
55 }
while (ret < 0 && errno == EINTR);
67 assert(!has_implicit_slot_ &&
"Implicit slot cannot be released twice!");
68 has_implicit_slot_ =
true;
75 ret = ::write(write_fd_, &slot_char, 1);
76 }
while (ret < 0 && errno == EINTR);
81 bool InitWithFifo(
const std::string& fifo_path, std::string* error) {
82 if (fifo_path.empty()) {
83 *error =
"Empty fifo path";
86 read_fd_ = ::open(fifo_path.c_str(), O_RDONLY | O_NONBLOCK | O_CLOEXEC);
89 std::string(
"Error opening fifo for reading: ") + strerror(errno);
92 if (!IsFifoDescriptor(read_fd_)) {
93 *error =
"Not a fifo path: " + fifo_path;
97 write_fd_ = ::open(fifo_path.c_str(), O_WRONLY | O_NONBLOCK | O_CLOEXEC);
100 std::string(
"Error opening fifo for writing: ") + strerror(errno);
109 bool has_implicit_slot_ =
true;
121 bool success =
false;
122 auto client = std::unique_ptr<PosixJobserverClient>(
new PosixJobserverClient);
124 success = client->InitWithFifo(config.
path, error);
126 *error =
"Unsupported jobserver mode";
A Jobserver::Client instance models a client of an external GNU jobserver pool, which can be implemen...
virtual Slot TryAcquire()
Try to acquire a slot from the pool.
static std::unique_ptr< Client > Create(const Config &, std::string *error)
Create a new Client instance from a given configuration.
virtual void Release(Slot slot)
Release a slot to the pool.
A Jobserver::Config models how to access or implement a GNU jobserver implementation.
std::string path
For kModeFifo, this is the path to the Unix FIFO to use.
Mode mode
Implementation mode for the pool.
A Jobserver::Slot models a single job slot that can be acquired from.
uint8_t GetExplicitValue() const
Return value of an explicit slot.
static Slot CreateImplicit()
Create instance for the implicit value.
static Slot CreateExplicit(uint8_t value)
Create instance for explicit byte value.
bool IsImplicit() const
Return true if this instance represents an implicit job slot.
bool IsValid() const
Return true if this instance is valid, i.e.