From e1ddf5d3fed3af028a4c0b599cf9d742da018987 Mon Sep 17 00:00:00 2001 From: ganlan Date: Wed, 27 Jul 2022 17:26:06 +0800 Subject: [PATCH] =?UTF-8?q?stacksize=E5=92=8Cpageguard=E6=A0=B9=E6=8D=AE?= =?UTF-8?q?=E4=BA=A7=E5=93=81=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: ganlan --- .../functionalext/thread/pthread_guard_size.c | 47 +++++++++++++++++++ .../functionalext/thread/pthread_stack_size.c | 47 +++++++++++++++++++ .../thread/test_src_functionalext_thread.gni | 2 + libc-test/test_template.gni | 23 +++++++++ musl_config.gni | 6 +++ musl_template.gni | 14 ++++++ .../linux/user/src/internal/pthread_impl.h | 9 ++++ 7 files changed, 148 insertions(+) create mode 100644 libc-test/src/functionalext/thread/pthread_guard_size.c create mode 100644 libc-test/src/functionalext/thread/pthread_stack_size.c diff --git a/libc-test/src/functionalext/thread/pthread_guard_size.c b/libc-test/src/functionalext/thread/pthread_guard_size.c new file mode 100644 index 00000000..7e2be0d1 --- /dev/null +++ b/libc-test/src/functionalext/thread/pthread_guard_size.c @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2022 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 +#include + +#include "pthread_util.h" + +#define DEFAULT_GUARD_SIZE 8192 + +static void pthread_guard_size_0100(void) +{ + pthread_attr_t attr = {0}; + size_t guard_size = 0; + pthread_getattr_default_np(&attr); + pthread_attr_getguardsize(&attr, &guard_size); +#ifdef TARGET_GUARD_SIZE + TEST(guard_size == TARGET_GUARD_SIZE); +#else + TEST(guard_size == DEFAULT_GUARD_SIZE); +#endif +} + +TEST_FUN G_Fun_Array[] = { + pthread_guard_size_0100, +}; + +int main(void) +{ + int num = sizeof(G_Fun_Array) / sizeof(TEST_FUN); + for (int pos = 0; pos < num; ++pos) { + G_Fun_Array[pos](); + } + return t_status; +} \ No newline at end of file diff --git a/libc-test/src/functionalext/thread/pthread_stack_size.c b/libc-test/src/functionalext/thread/pthread_stack_size.c new file mode 100644 index 00000000..62c4ceb2 --- /dev/null +++ b/libc-test/src/functionalext/thread/pthread_stack_size.c @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2022 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 +#include + +#include "pthread_util.h" + +#define DEFAULT_STACK_SIZE 131072 + +static void pthread_stack_size_0100(void) +{ + pthread_attr_t attr = {0}; + size_t stack_size = 0; + pthread_getattr_default_np(&attr); + pthread_attr_getstacksize(&attr, &stack_size); +#ifdef TARGET_STACK_SIZE + TEST(stack_size == TARGET_STACK_SIZE); +#else + TEST(stack_size == DEFAULT_STACK_SIZE); +#endif +} + +TEST_FUN G_Fun_Array[] = { + pthread_stack_size_0100, +}; + +int main(void) +{ + int num = sizeof(G_Fun_Array) / sizeof(TEST_FUN); + for (int pos = 0; pos < num; ++pos) { + G_Fun_Array[pos](); + } + return t_status; +} \ No newline at end of file diff --git a/libc-test/src/functionalext/thread/test_src_functionalext_thread.gni b/libc-test/src/functionalext/thread/test_src_functionalext_thread.gni index db6e2f87..d4629ace 100644 --- a/libc-test/src/functionalext/thread/test_src_functionalext_thread.gni +++ b/libc-test/src/functionalext/thread/test_src_functionalext_thread.gni @@ -15,4 +15,6 @@ functionalext_thread_list = [ "pthread_mutex", "pthread_rwlock_rdlock", "pthread_rwlock_wrlock", + "pthread_guard_size", + "pthread_stack_size", ] diff --git a/libc-test/test_template.gni b/libc-test/test_template.gni index 9103a005..27ac3f42 100644 --- a/libc-test/test_template.gni +++ b/libc-test/test_template.gni @@ -1,4 +1,5 @@ import("//build/test.gni") +import("//third_party/musl/musl_config.gni") musl_base_dir = "third_party/musl/" test_dir = "${musl_base_dir}/libc-test" @@ -174,6 +175,28 @@ template("test_unittest") { ldflags += [ "-Wl,-rpath=./" ] } } + + if (target_dir == "functionalext/thread") { + if (!defined(defines)) { + defines = [] + } + if (musl_target_os == "linux" && product_path != "" && + product_path != rebase_path("//productdefine/common/products")) { + _product_config = read_file("${product_path}/config.json", "json") + if (target_name == "pthread_stack_size") { + if (defined(_product_config.device_stack_size)) { + defines += + [ "TARGET_STACK_SIZE=${_product_config.device_stack_size}" ] + } + } + if (target_name == "pthread_guard_size") { + if (defined(_product_config.device_guard_size)) { + defines += + [ "TARGET_GUARD_SIZE=${_product_config.device_guard_size}" ] + } + } + } + } } } diff --git a/musl_config.gni b/musl_config.gni index 8ce8b48b..39cabcda 100644 --- a/musl_config.gni +++ b/musl_config.gni @@ -53,3 +53,9 @@ declare_args() { declare_args() { enable_musl_log = false } + +declare_args() { + if (!defined(product_path)) { + product_path = "" + } +} diff --git a/musl_template.gni b/musl_template.gni index 8ba52a57..292a4cca 100644 --- a/musl_template.gni +++ b/musl_template.gni @@ -379,6 +379,20 @@ template("musl_libs") { configs -= musl_inherited_configs configs += [ ":${abi_prefix}_musl_config" ] + if (!defined(defines)) { + defines = [] + } + if (musl_target_os == "linux" && product_path != "" && + product_path != rebase_path("//productdefine/common/products")) { + _product_config = read_file("${product_path}/config.json", "json") + if (defined(_product_config.device_stack_size)) { + defines += [ "TARGET_STACK_SIZE=${_product_config.device_stack_size}" ] + } + if (defined(_product_config.device_guard_size)) { + defines += [ "TARGET_GUARD_SIZE=${_product_config.device_guard_size}" ] + } + } + deps = porting_deps } diff --git a/porting/linux/user/src/internal/pthread_impl.h b/porting/linux/user/src/internal/pthread_impl.h index 84a701fc..dfc557a7 100644 --- a/porting/linux/user/src/internal/pthread_impl.h +++ b/porting/linux/user/src/internal/pthread_impl.h @@ -242,8 +242,17 @@ extern hidden volatile int __thread_list_lock; extern hidden unsigned __default_stacksize; extern hidden unsigned __default_guardsize; +#ifdef TARGET_STACK_SIZE +#define DEFAULT_STACK_SIZE TARGET_STACK_SIZE +#else #define DEFAULT_STACK_SIZE 131072 +#endif + +#ifdef TARGET_GUARD_SIZE +#define DEFAULT_GUARD_SIZE TARGET_GUARD_SIZE +#else #define DEFAULT_GUARD_SIZE 8192 +#endif #define DEFAULT_STACK_MAX (8<<20) #define DEFAULT_GUARD_MAX (1<<20) -- GitLab