pkarashchenko commented on code in PR #6197: URL: https://github.com/apache/incubator-nuttx/pull/6197#discussion_r880307793
########## libs/libc/stdlib/lib_atexit.c: ########## @@ -0,0 +1,217 @@ +/**************************************************************************** + * libs/libc/stdlib/lib_atexit.c + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you 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. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#include <stdlib.h> + +#include <nuttx/atexit.h> +#include <nuttx/semaphore.h> +#include <nuttx/tls.h> + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: get_exitfuncs + * + * Description: + * Obtain the list of exit functions. + * + * Input Parameters: + * None + * + * Returned Value: + * Pointer to the list of exit functions. + * + ****************************************************************************/ + +FAR struct atexit_list_s * get_exitfuncs(void) +{ + FAR struct task_info_s *info; + + info = task_get_info(); + return &info->ta_exit; +} + +/**************************************************************************** + * Name: exitfunc_lock + * + * Description: + * Obtain the exit function lock. + * + * Returned Value: + * OK on success, or negated errno on failure + * + ****************************************************************************/ + +static int exitfunc_lock(void) +{ + FAR struct task_info_s *info = task_get_info(); + int ret = _SEM_WAIT(&info->ta_sem); + + if (ret < 0) + { + ret = _SEM_ERRVAL(ret); + } + + return ret; +} + +/**************************************************************************** + * Name: exitfunc_unlock + * + * Description: + * Release exit function lock . + * + ****************************************************************************/ + +static void exitfunc_unlock(void) +{ + FAR struct task_info_s *info = task_get_info(); + + _SEM_POST(&info->ta_sem); +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: atexit + * + * Description: + * Registers a function to be called at program exit. + * The atexit() function registers the given function to be called + * at normal process termination, whether via exit or via return from + * the program's main(). + * + * Limitations in the current implementation: + * + * 1. Only a single atexit function can be registered unless + * CONFIG_LIBC_MAX_EXITFUNS defines a larger number. + * 2. atexit functions are not inherited when a new task is + * created. + * + * Input Parameters: + * func - A pointer to the function to be called when the task exits. + * + * Returned Value: + * Zero on success. Non-zero on failure. + * + ****************************************************************************/ + +int atexit(CODE void (*func)(void)) +{ + return atexit_register(ATTYPE_ATEXIT, func, NULL, NULL); +} + +int atexit_register(int type, CODE void (*func)(void), FAR void *arg, + FAR void *dso) +{ + FAR struct atexit_list_s *aehead; + int idx; + int ret = ERROR; + + /* REVISIT: Missing logic */ + + UNUSED(dso); + + /* The following must be atomic */ + + aehead = get_exitfuncs(); + + if (func) + { + ret = exitfunc_lock(); + if (ret < 0) + { + return ret; + } + + if ((idx = aehead->next) < ATEXIT_MAX) + { + aehead->funcs[idx].type = type; + aehead->funcs[idx].func = func; + aehead->funcs[idx].arg = arg; + aehead->next++; + ret = OK; Review Comment: do we really need this line? ########## include/nuttx/atexit.h: ########## @@ -0,0 +1,86 @@ +/**************************************************************************** + * include/nuttx/atexit.h + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you 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 __INCLUDE_NUTTX_ATEXIT_H +#define __INCLUDE_NUTTX_ATEXIT_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> +#include <stdlib.h> + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* Amount of exit functions */ + +#define ATEXIT_MAX (CONFIG_LIBC_MAX_EXITFUNS) + +/**************************************************************************** + * Public Types + ****************************************************************************/ + +enum atexit_type_e +{ + ATTYPE_NONE, + ATTYPE_ATEXIT, + ATTYPE_ONEXIT, + ATTYPE_CXA +}; + +struct atexit_s +{ + int type; + CODE void (*func)(void); + FAR void *arg; +}; + +struct atexit_list_s +{ + int next; Review Comment: maybe we can rename to `nfuncs` or `count`. The `next` is not obvious. ########## libs/libc/stdlib/lib_atexit.c: ########## @@ -0,0 +1,217 @@ +/**************************************************************************** + * libs/libc/stdlib/lib_atexit.c + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you 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. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#include <stdlib.h> + +#include <nuttx/atexit.h> +#include <nuttx/semaphore.h> +#include <nuttx/tls.h> + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: get_exitfuncs + * + * Description: + * Obtain the list of exit functions. + * + * Input Parameters: + * None + * + * Returned Value: + * Pointer to the list of exit functions. + * + ****************************************************************************/ + +FAR struct atexit_list_s * get_exitfuncs(void) +{ + FAR struct task_info_s *info; + + info = task_get_info(); + return &info->ta_exit; +} + +/**************************************************************************** + * Name: exitfunc_lock + * + * Description: + * Obtain the exit function lock. + * + * Returned Value: + * OK on success, or negated errno on failure + * + ****************************************************************************/ + +static int exitfunc_lock(void) +{ + FAR struct task_info_s *info = task_get_info(); + int ret = _SEM_WAIT(&info->ta_sem); + + if (ret < 0) + { + ret = _SEM_ERRVAL(ret); + } + + return ret; +} + +/**************************************************************************** + * Name: exitfunc_unlock + * + * Description: + * Release exit function lock . + * + ****************************************************************************/ + +static void exitfunc_unlock(void) +{ + FAR struct task_info_s *info = task_get_info(); + + _SEM_POST(&info->ta_sem); +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: atexit + * + * Description: + * Registers a function to be called at program exit. + * The atexit() function registers the given function to be called + * at normal process termination, whether via exit or via return from + * the program's main(). + * + * Limitations in the current implementation: + * + * 1. Only a single atexit function can be registered unless + * CONFIG_LIBC_MAX_EXITFUNS defines a larger number. + * 2. atexit functions are not inherited when a new task is + * created. + * + * Input Parameters: + * func - A pointer to the function to be called when the task exits. + * + * Returned Value: + * Zero on success. Non-zero on failure. + * + ****************************************************************************/ + +int atexit(CODE void (*func)(void)) +{ + return atexit_register(ATTYPE_ATEXIT, func, NULL, NULL); +} + +int atexit_register(int type, CODE void (*func)(void), FAR void *arg, + FAR void *dso) Review Comment: ```suggestion int atexit_register(int type, CODE void (*func)(void), FAR void *arg, FAR void *dso) ``` Also need to add description for `atexit_register` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: commits-unsubscr...@nuttx.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org