diff --git a/bundle.json b/bundle.json index 9db5bf723184f9710b59e20d904cdb7a5422f872..29817f761749b88cac792a9ac81176ae8054c717 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 0000000000000000000000000000000000000000..13ee1ca214ec6c2838d8a5415c7d47268457c7b7 --- /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 0000000000000000000000000000000000000000..cf0eabea86110bdda780ebacdf7ddc4114d69c5e --- /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 0000000000000000000000000000000000000000..5d216bea3940bc28b7b8f89a473cb2fdc35d660a --- /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; +}