From 7bf42331fd6e4d06e9ce35f50f25b69c7c669b9c Mon Sep 17 00:00:00 2001 From: lijialiang Date: Tue, 4 Jun 2024 15:42:13 +0800 Subject: [PATCH] =?UTF-8?q?sync=5Fwait=E4=B8=8A=E5=BA=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: lijialiang --- sync.h | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 sync.h diff --git a/sync.h b/sync.h new file mode 100644 index 0000000..f3d5d5b --- /dev/null +++ b/sync.h @@ -0,0 +1,38 @@ +#ifndef SYNC_H +#define SYNC_H + +#include +#include +#include + +static inline int sync_wait(int num, int time) +{ + struct pollfd work; + int result; + + if (num < 0) { + errno = EINVAL; + return -1; + } + + work.fd = num; + work.events = POLLIN; + + do { + result = poll(&work, 1, time); + if (result > 0) { + if (work.revents & (POLLERR | POLLNVAL)) { + errno = EINVAL; + return -1; + } + return 0; + } else if (result == 0) { + errno = ETIME; + return -1; + } + } while (result == -1 && (errno == EINTR || errno == EAGAIN)); + + return result; +} + +#endif \ No newline at end of file -- Gitee