From 7fc8dae52d5ccd803d838e6b8c67706b9650efd6 Mon Sep 17 00:00:00 2001 From: chenziang_sjtu <1094744965@qq.com> Date: Fri, 25 Oct 2024 14:20:29 +0800 Subject: [PATCH] =?UTF-8?q?Libsync=E4=BB=A3=E7=A0=81=E9=87=8D=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: chenziang_sjtu <1094744965@qq.com> --- OAT.xml | 3 +-- libsync/include/sync.h | 4 ++-- libsync/src/sync.c | 31 +++++++++++++++---------------- 3 files changed, 18 insertions(+), 20 deletions(-) diff --git a/OAT.xml b/OAT.xml index 20cc6bc..95921d6 100644 --- a/OAT.xml +++ b/OAT.xml @@ -22,8 +22,7 @@ - - \ No newline at end of file + diff --git a/libsync/include/sync.h b/libsync/include/sync.h index cf0eabe..7f6adfa 100644 --- a/libsync/include/sync.h +++ b/libsync/include/sync.h @@ -15,6 +15,6 @@ #ifndef COMM_UTILS_SYNC_H #define COMM_UTILS_SYNC_H -int SyncWait(int num, int time); +int SyncWait(int fileDescriptor, int timeout); -#endif \ No newline at end of file +#endif diff --git a/libsync/src/sync.c b/libsync/src/sync.c index 755df16..8291466 100644 --- a/libsync/src/sync.c +++ b/libsync/src/sync.c @@ -1,5 +1,5 @@ /* - * Copyright 2012 Google, Inc + * Copyright (c) 2024 Huawei Device Co., Ltd. * 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 @@ -17,32 +17,31 @@ #include #include -int SyncWait(int num, int time) +int SyncWait(int fileDescriptor, int timeout) { - struct pollfd work; - int result; - - if (num < 0) { + if (fileDescriptor < 0) { errno = EINVAL; return -1; } - work.fd = num; - work.events = POLLIN; + struct pollfd pfd = { .fd = fileDescriptor, .events = POLLIN }; + int pollResult; + + while (1) { + pollResult = poll(&pfd, 1, timeout); - do { - result = poll(&work, 1, time); - if (result > 0) { - if (work.revents & (POLLERR | POLLNVAL)) { + if (pollResult > 0) { + if (pfd.revents & (POLLERR | POLLNVAL)) { errno = EINVAL; return -1; } return 0; - } else if (result == 0) { + } else if (pollResult == 0) { errno = ETIME; return -1; + } else if (pollResult != -1 || (errno != EINTR && errno != EAGAIN)) { + break; } - } while (result == -1 && (errno == EINTR || errno == EAGAIN)); - - return result; + } + return pollResult; } -- Gitee