From be91de317731420d654706d0e602eaf91c214c38 Mon Sep 17 00:00:00 2001 From: lijialiang Date: Wed, 5 Jun 2024 11:16:09 +0800 Subject: [PATCH] =?UTF-8?q?libsync=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 --- bundle.json | 10 +++++++++ libsync/BUILD.gn | 34 ++++++++++++++++++++++++++++++ libsync/include/sync.h | 20 ++++++++++++++++++ libsync/src/sync.c | 48 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 112 insertions(+) create mode 100644 libsync/BUILD.gn create mode 100644 libsync/include/sync.h create mode 100644 libsync/src/sync.c diff --git a/bundle.json b/bundle.json index 9db5bf7..29817f7 100644 --- a/bundle.json +++ b/bundle.json @@ -32,6 +32,7 @@ "//commonlibrary/memory_utils/libdmabufheap:libdmabufheap", "//commonlibrary/memory_utils/libmeminfo:libmeminfo", "//commonlibrary/memory_utils/libpurgeablemem:libpurgeablemem", + "//commonlibrary/memory_utils/libsync:libsync", "//commonlibrary/memory_utils/libpurgeablemem:purgeable_memory_ndk" ], "inner_kits": [ @@ -53,6 +54,15 @@ "header_base": "//commonlibrary/memory_utils/libmeminfo/include" } }, + { + "name": "//commonlibrary/memory_utils/libsync:libsync", + "header": { + "header_files": [ + "sync.h" + ], + "header_base": "//commonlibrary/memory_utils/libsync/include" + } + }, { "name": "//commonlibrary/memory_utils/libpurgeablemem:libpurgeablemem", "header": { diff --git a/libsync/BUILD.gn b/libsync/BUILD.gn new file mode 100644 index 0000000..13ee1ca --- /dev/null +++ b/libsync/BUILD.gn @@ -0,0 +1,34 @@ +# 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 +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/ohos.gni") + +config("libsync_config") { + include_dirs = [ "include" ] +} + +ohos_shared_library("libsync") { + sources = [ "src/sync.c" ] + include_dirs = [ "include" ] + + public_configs = [ ":libsync_config" ] + subsystem_name = "commonlibrary" + part_name = "memory_utils" + + sanitize = { + cfi = true + cfi_cross_dso = true + debug = false + } + branch_protector_ret = "pac_ret" +} diff --git a/libsync/include/sync.h b/libsync/include/sync.h new file mode 100644 index 0000000..cf0eabe --- /dev/null +++ b/libsync/include/sync.h @@ -0,0 +1,20 @@ +/* + * 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef COMM_UTILS_SYNC_H +#define COMM_UTILS_SYNC_H + +int SyncWait(int num, int time); + +#endif \ No newline at end of file diff --git a/libsync/src/sync.c b/libsync/src/sync.c new file mode 100644 index 0000000..5d216be --- /dev/null +++ b/libsync/src/sync.c @@ -0,0 +1,48 @@ +/* + * 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "sync.h" +#include +#include +#include + +int SyncWait(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; +} -- Gitee