| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
#include <poll.h> |
| 3 |
#include <threads.h> |
| 4 |
#include <unistd.h> |
| 5 |
|
| 6 |
#include "errno-util.h" |
| 7 |
#include "fiber-ops.h" |
| 8 |
|
| 9 |
static thread_local const FiberOps *fiber_ops = NULL; |
| 10 |
|
| 11 |
bool fiber_ops_is_set(void) { |
| 12 |
return fiber_ops != NULL; |
| 13 |
} |
| 14 |
|
| 15 |
void fiber_ops_set(const FiberOps *ops) { |
| 16 |
fiber_ops = ops; |
| 17 |
} |
| 18 |
|
| 19 |
int fiber_ops_ppoll(struct pollfd *fds, size_t n_fds, const struct timespec *timeout, const sigset_t *sigmask) { |
| 20 |
if (fiber_ops) |
| 21 |
return fiber_ops->ppoll(fds, n_fds, timeout, sigmask); |
| 22 |
|
| 23 |
return RET_NERRNO(ppoll(fds, n_fds, timeout, sigmask)); |
| 24 |
} |
| 25 |
|
| 26 |
ssize_t fiber_ops_read(int fd, void *buf, size_t count) { |
| 27 |
if (fiber_ops) |
| 28 |
return fiber_ops->read(fd, buf, count); |
| 29 |
|
| 30 |
ssize_t n = read(fd, buf, count); |
| 31 |
return n < 0 ? -errno : n; |
| 32 |
} |
| 33 |
|
| 34 |
ssize_t fiber_ops_write(int fd, const void *buf, size_t count) { |
| 35 |
if (fiber_ops) |
| 36 |
return fiber_ops->write(fd, buf, count); |
| 37 |
|
| 38 |
return RET_NERRNO(write(fd, buf, count)); |
| 39 |
} |
| 40 |
|
| 41 |
sd_future* fiber_ops_timeout(uint64_t timeout) { |
| 42 |
assert(fiber_ops); |
| 43 |
|
| 44 |
return fiber_ops->timeout(timeout); |
| 45 |
} |
| 46 |
|
| 47 |
sd_future* fiber_ops_cancel_wait_unref(sd_future *f) { |
| 48 |
assert(fiber_ops); |
| 49 |
|
| 50 |
return fiber_ops->cancel_wait_unref(f); |
| 51 |
} |