2015-10-03 01:43:18 +02:00
|
|
|
/****************************************************************************
|
2018-05-29 21:21:26 +02:00
|
|
|
* libs/libc/misc/lib_filesem.c
|
2012-11-10 17:06:01 +01:00
|
|
|
*
|
2021-04-02 09:22:52 +02:00
|
|
|
* 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
|
2012-11-10 17:06:01 +01:00
|
|
|
*
|
2021-04-02 09:22:52 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2012-11-10 17:06:01 +01:00
|
|
|
*
|
2021-04-02 09:22:52 +02:00
|
|
|
* 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.
|
2012-11-10 17:06:01 +01:00
|
|
|
*
|
2015-10-03 01:43:18 +02:00
|
|
|
****************************************************************************/
|
2012-11-10 17:06:01 +01:00
|
|
|
|
2015-10-03 01:43:18 +02:00
|
|
|
/****************************************************************************
|
2012-11-10 17:06:01 +01:00
|
|
|
* Included Files
|
2015-10-03 01:43:18 +02:00
|
|
|
****************************************************************************/
|
2012-11-10 17:06:01 +01:00
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
2017-10-08 19:52:32 +02:00
|
|
|
#include <nuttx/semaphore.h>
|
2017-02-09 22:16:36 +01:00
|
|
|
#include <nuttx/fs/fs.h>
|
|
|
|
|
2015-12-30 00:31:17 +01:00
|
|
|
#include "libc.h"
|
2012-11-10 17:06:01 +01:00
|
|
|
|
2017-02-09 16:24:44 +01:00
|
|
|
#ifndef CONFIG_STDIO_DISABLE_BUFFERING
|
2012-11-10 17:06:01 +01:00
|
|
|
|
2015-10-03 01:43:18 +02:00
|
|
|
/****************************************************************************
|
2015-10-03 00:30:35 +02:00
|
|
|
* Public Functions
|
2015-10-03 01:43:18 +02:00
|
|
|
****************************************************************************/
|
2012-11-10 17:06:01 +01:00
|
|
|
|
2015-10-03 01:43:18 +02:00
|
|
|
/****************************************************************************
|
2012-11-10 17:06:01 +01:00
|
|
|
* lib_sem_initialize
|
2015-10-03 01:43:18 +02:00
|
|
|
****************************************************************************/
|
2012-11-10 17:06:01 +01:00
|
|
|
|
|
|
|
void lib_sem_initialize(FAR struct file_struct *stream)
|
|
|
|
{
|
2013-01-11 20:08:51 +01:00
|
|
|
/* Initialize the LIB semaphore to one (to support one-at-a-time access
|
|
|
|
* to private data sets.
|
2012-11-10 17:06:01 +01:00
|
|
|
*/
|
|
|
|
|
2020-08-13 20:59:16 +02:00
|
|
|
_SEM_INIT(&stream->fs_sem, 0, 1);
|
2012-11-10 17:06:01 +01:00
|
|
|
|
|
|
|
stream->fs_holder = -1;
|
|
|
|
stream->fs_counts = 0;
|
|
|
|
}
|
|
|
|
|
2015-10-03 01:43:18 +02:00
|
|
|
/****************************************************************************
|
2012-11-10 17:06:01 +01:00
|
|
|
* lib_take_semaphore
|
2015-10-03 01:43:18 +02:00
|
|
|
****************************************************************************/
|
2012-11-10 17:06:01 +01:00
|
|
|
|
|
|
|
void lib_take_semaphore(FAR struct file_struct *stream)
|
|
|
|
{
|
|
|
|
pid_t my_pid = getpid();
|
2017-10-08 20:25:58 +02:00
|
|
|
int ret;
|
2012-11-10 17:06:01 +01:00
|
|
|
|
|
|
|
/* Do I already have the semaphore? */
|
|
|
|
|
|
|
|
if (stream->fs_holder == my_pid)
|
|
|
|
{
|
|
|
|
/* Yes, just increment the number of references that I have */
|
|
|
|
|
|
|
|
stream->fs_counts++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Take the semaphore (perhaps waiting) */
|
|
|
|
|
2017-10-08 20:25:58 +02:00
|
|
|
while ((ret = _SEM_WAIT(&stream->fs_sem)) < 0)
|
2013-01-11 20:08:51 +01:00
|
|
|
{
|
|
|
|
/* The only case that an error should occr here is if the wait
|
|
|
|
* was awakened by a signal.
|
|
|
|
*/
|
2012-11-10 17:06:01 +01:00
|
|
|
|
2020-08-13 20:59:16 +02:00
|
|
|
DEBUGASSERT(_SEM_ERRNO(ret) == EINTR ||
|
|
|
|
_SEM_ERRNO(ret) == ECANCELED);
|
2017-10-08 20:25:58 +02:00
|
|
|
UNUSED(ret);
|
2013-01-11 20:08:51 +01:00
|
|
|
}
|
2012-11-10 17:06:01 +01:00
|
|
|
|
|
|
|
/* We have it. Claim the stak and return */
|
|
|
|
|
|
|
|
stream->fs_holder = my_pid;
|
|
|
|
stream->fs_counts = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-03 01:43:18 +02:00
|
|
|
/****************************************************************************
|
2012-11-10 17:06:01 +01:00
|
|
|
* lib_give_semaphore
|
2015-10-03 01:43:18 +02:00
|
|
|
****************************************************************************/
|
2012-11-10 17:06:01 +01:00
|
|
|
|
|
|
|
void lib_give_semaphore(FAR struct file_struct *stream)
|
|
|
|
{
|
|
|
|
/* I better be holding at least one reference to the semaphore */
|
|
|
|
|
2017-10-08 19:52:32 +02:00
|
|
|
DEBUGASSERT(stream->fs_holder == getpid());
|
2012-11-10 17:06:01 +01:00
|
|
|
|
|
|
|
/* 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;
|
|
|
|
stream->fs_counts = 0;
|
2017-10-08 19:52:32 +02:00
|
|
|
DEBUGVERIFY(_SEM_POST(&stream->fs_sem));
|
2012-11-10 17:06:01 +01:00
|
|
|
}
|
|
|
|
}
|
2017-02-09 16:24:44 +01:00
|
|
|
|
|
|
|
#endif /* CONFIG_STDIO_DISABLE_BUFFERING */
|