pkarashchenko commented on code in PR #6152: URL: https://github.com/apache/incubator-nuttx/pull/6152#discussion_r861493334
########## include/nuttx/lib/lib.h: ########## @@ -113,12 +113,11 @@ void lib_stream_release(FAR struct task_group_s *group); #ifdef CONFIG_STDIO_DISABLE_BUFFERING # define lib_sem_initialize(s) -# define lib_take_semaphore(s) -# define lib_give_semaphore(s) +# define lib_take_semaphore(s, f) #else void lib_sem_initialize(FAR struct file_struct *stream); -void lib_take_semaphore(FAR struct file_struct *stream); -void lib_give_semaphore(FAR struct file_struct *stream); +int lib_take_semaphore(FAR struct file_struct *stream, Review Comment: ```suggestion int lib_take_semaphore(FAR struct file_struct *stream, ``` ########## libs/libc/stdio/lib_ftrylockfile.c: ########## @@ -0,0 +1,100 @@ +/**************************************************************************** + * libs/libc/stdio/lib_ftrylockfile.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 <stdio.h> + +#include <sys/types.h> +#include <unistd.h> +#include <errno.h> +#include <assert.h> + +#include <nuttx/semaphore.h> +#include <nuttx/fs/fs.h> + +#include "libc.h" + +#ifndef CONFIG_STDIO_DISABLE_BUFFERING + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: _take_semaphore_nonblocking + * + * Description: + * Take a semaphore if its available, returning inmediately if not. + * + * Input Parameters: + * sem - The semaphore. + * + * Returned Value: + * OK - The semaphore is taken successfully. + * -EAGAIN - The semaphore is not available. + * -EINVAL - Invalid attempt to get the semaphore. + * + ****************************************************************************/ + +static int _take_semaphore_nonblocking(sem_t *sem) +{ + int ret; + + /* Try to take the semaphore (without waiting) */ + + if ((ret = _SEM_TRYWAIT(sem)) < 0) + { + DEBUGASSERT(_SEM_ERRNO(ret) == EAGAIN || + _SEM_ERRNO(ret) == EINVAL); Review Comment: why `EINVAL` is considered as normal operation here, but abnormal in `_take_semaphore_blocking`? ########## include/stdio.h: ########## @@ -173,6 +173,12 @@ int setvbuf(FAR FILE *stream, FAR char *buffer, int mode, size_t size); int ungetc(int c, FAR FILE *stream); +#ifndef CONFIG_STDIO_DISABLE_BUFFERING +void flockfile(FAR FILE *stream); +int ftrylockfile(FAR FILE *stream); +void funlockfile(FAR FILE *stream); Review Comment: ```suggestion void flockfile(FAR FILE *stream); int ftrylockfile(FAR FILE *stream); void funlockfile(FAR FILE *stream); ``` ########## libs/libc/stdio/lib_funlockfile.c: ########## @@ -0,0 +1,81 @@ +/**************************************************************************** + * libs/libc/stdio/lib_funlockfile.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 <stdio.h> + +#include <sys/types.h> +#include <unistd.h> +#include <errno.h> +#include <assert.h> + +#include <nuttx/semaphore.h> +#include <nuttx/fs/fs.h> + +#include "libc.h" + +#ifndef CONFIG_STDIO_DISABLE_BUFFERING + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: funlockfile + * + * Description: + * funlockfile() relinquish the ownership of a stream granted to the + * thread. + * + * The behavior is undefined if called by a thread other than the current + * owner. + * + ****************************************************************************/ + +void funlockfile(FAR FILE *stream) +{ + /* I better be holding at least one reference to the semaphore */ + + DEBUGASSERT(stream->fs_holder == getpid()); + + /* Do I hold multiple references to the semphore */ + + if (stream->fs_counts > 1) + { + /* Yes, just release one count and return */ + + stream->fs_counts--; + } + else + { + /* Nope, this is the last reference I have */ + + stream->fs_holder = -1; Review Comment: ```suggestion stream->fs_holder = INVALID_PROCESS_ID; ``` ########## libs/libc/stdio/lib_libfilesem.c: ########## @@ -60,7 +60,8 @@ void lib_sem_initialize(FAR struct file_struct *stream) * lib_take_semaphore ****************************************************************************/ -void lib_take_semaphore(FAR struct file_struct *stream) +int lib_take_semaphore(FAR struct file_struct *stream, Review Comment: Maybe we can add `flag` param instead of `take_func` and pass `O_NONBLOCK` is case if we need to use `_SEM_TRYWAIT` instead of `_SEM_WAIT`? @yamt what do you think? -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
