!176 Libsync代码重构

Merge pull request !176 from chenziang_sjtu/master
This commit is contained in:
openharmony_ci 2024-10-25 09:48:21 +00:00 committed by Gitee
commit abb204a705
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
3 changed files with 18 additions and 20 deletions

View File

@ -22,8 +22,7 @@
<filefilterlist>
<filefilter name="defaultFilter" desc="Files not to check">
<filteritem type="filename" name="*.png" desc="desc files"/>
<filteritem type="filename" name="sync.c" desc="Waiting for refactoring"/>
</filefilter>
</filefilterlist>
</oatconfig>
</configuration>
</configuration>

View File

@ -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
#endif

View File

@ -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 <poll.h>
#include <stdio.h>
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;
}