2011-03-15 21:26:34 +01:00
|
|
|
/****************************************************************************
|
2014-06-26 22:23:21 +02:00
|
|
|
* net/utils/net_lock.c
|
2011-03-15 21:26:34 +01:00
|
|
|
*
|
2016-02-14 15:38:44 +01:00
|
|
|
* Copyright (C) 2011-2012, 2014-2016 Gregory Nutt. All rights reserved.
|
2012-03-13 21:51:48 +01:00
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
2011-03-15 21:26:34 +01:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
|
|
|
* used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
2012-09-17 20:35:37 +02:00
|
|
|
#include <unistd.h>
|
2011-03-15 21:26:34 +01:00
|
|
|
#include <semaphore.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
2016-02-14 15:38:44 +01:00
|
|
|
#include <nuttx/irq.h>
|
2011-03-15 21:26:34 +01:00
|
|
|
#include <nuttx/arch.h>
|
2014-07-05 00:38:51 +02:00
|
|
|
#include <nuttx/net/net.h>
|
2011-03-15 21:26:34 +01:00
|
|
|
|
2014-06-26 22:23:21 +02:00
|
|
|
#include "utils/utils.h"
|
|
|
|
|
2011-03-15 21:26:34 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#define NO_HOLDER (pid_t)-1
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-07-02 02:21:25 +02:00
|
|
|
static sem_t g_netlock;
|
2011-03-15 21:26:34 +01:00
|
|
|
static pid_t g_holder = NO_HOLDER;
|
|
|
|
static unsigned int g_count = 0;
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: _net_takesem
|
2011-03-15 21:26:34 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Take the semaphore
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-06-29 03:26:16 +02:00
|
|
|
static void _net_takesem(void)
|
2011-03-15 21:26:34 +01:00
|
|
|
{
|
2014-07-02 02:21:25 +02:00
|
|
|
while (sem_wait(&g_netlock) != 0)
|
2011-03-15 21:26:34 +01:00
|
|
|
{
|
|
|
|
/* The only case that an error should occur here is if the wait was
|
|
|
|
* awakened by a signal.
|
|
|
|
*/
|
|
|
|
|
2015-07-05 19:18:56 +02:00
|
|
|
ASSERT(get_errno() == EINTR);
|
2011-03-15 21:26:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: net_lockinitialize
|
2011-03-15 21:26:34 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Initialize the locking facility
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-06-26 22:23:21 +02:00
|
|
|
void net_lockinitialize(void)
|
2011-03-15 21:26:34 +01:00
|
|
|
{
|
2014-07-02 02:21:25 +02:00
|
|
|
sem_init(&g_netlock, 0, 1);
|
2011-03-15 21:26:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: net_lock
|
2011-03-15 21:26:34 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Take the lock
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2016-12-03 23:28:19 +01:00
|
|
|
void net_lock(void)
|
2011-03-15 21:26:34 +01:00
|
|
|
{
|
|
|
|
pid_t me = getpid();
|
|
|
|
|
|
|
|
/* Does this thread already hold the semaphore? */
|
|
|
|
|
|
|
|
if (g_holder == me)
|
|
|
|
{
|
|
|
|
/* Yes.. just increment the reference count */
|
|
|
|
|
|
|
|
g_count++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* No.. take the semaphore (perhaps waiting) */
|
|
|
|
|
2014-06-29 03:26:16 +02:00
|
|
|
_net_takesem();
|
2011-03-15 21:26:34 +01:00
|
|
|
|
|
|
|
/* Now this thread holds the semaphore */
|
|
|
|
|
|
|
|
g_holder = me;
|
|
|
|
g_count = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: net_unlock
|
2011-03-15 21:26:34 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Release the lock.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2016-12-03 23:28:19 +01:00
|
|
|
void net_unlock(void)
|
2011-03-15 21:26:34 +01:00
|
|
|
{
|
|
|
|
DEBUGASSERT(g_holder == getpid() && g_count > 0);
|
|
|
|
|
|
|
|
/* If the count would go to zero, then release the semaphore */
|
|
|
|
|
|
|
|
if (g_count == 1)
|
|
|
|
{
|
2011-03-25 15:51:44 +01:00
|
|
|
/* We no longer hold the semaphore */
|
2011-03-15 21:26:34 +01:00
|
|
|
|
|
|
|
g_holder = NO_HOLDER;
|
|
|
|
g_count = 0;
|
2014-07-02 02:21:25 +02:00
|
|
|
sem_post(&g_netlock);
|
2011-03-15 21:26:34 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-03-25 15:51:44 +01:00
|
|
|
/* We still hold the semaphore. Just decrement the count */
|
2011-03-15 21:26:34 +01:00
|
|
|
|
|
|
|
g_count--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: net_timedwait
|
2011-03-15 21:26:34 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2015-02-09 14:50:10 +01:00
|
|
|
* Atomically wait for sem (or a timeout( while temporarily releasing
|
|
|
|
* the lock on the network.
|
2011-03-15 21:26:34 +01:00
|
|
|
*
|
2015-01-22 13:51:31 +01:00
|
|
|
* Input Parameters:
|
2015-02-09 14:50:10 +01:00
|
|
|
* sem - A reference to the semaphore to be taken.
|
|
|
|
* abstime - The absolute time to wait until a timeout is declared.
|
2015-01-22 13:51:31 +01:00
|
|
|
*
|
|
|
|
* Returned value:
|
2015-02-09 14:50:10 +01:00
|
|
|
* The returned value is the same as sem_wait() or sem_timedwait(): Zero
|
|
|
|
* (OK) is returned on success; -1 (ERROR) is returned on a failure with
|
|
|
|
* the errno value set appropriately.
|
2015-01-22 13:51:31 +01:00
|
|
|
*
|
2011-03-15 21:26:34 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
2015-02-09 14:50:10 +01:00
|
|
|
int net_timedwait(sem_t *sem, FAR const struct timespec *abstime)
|
2011-03-15 21:26:34 +01:00
|
|
|
{
|
|
|
|
pid_t me = getpid();
|
|
|
|
unsigned int count;
|
|
|
|
irqstate_t flags;
|
|
|
|
int ret;
|
|
|
|
|
2016-02-14 15:38:44 +01:00
|
|
|
flags = enter_critical_section(); /* No interrupts */
|
2011-03-15 21:26:34 +01:00
|
|
|
sched_lock(); /* No context switches */
|
|
|
|
if (g_holder == me)
|
|
|
|
{
|
2015-01-22 21:46:36 +01:00
|
|
|
/* Release the network lock, remembering my count */
|
2014-04-13 22:32:20 +02:00
|
|
|
|
2011-03-15 21:26:34 +01:00
|
|
|
count = g_count;
|
|
|
|
g_holder = NO_HOLDER;
|
|
|
|
g_count = 0;
|
2014-07-02 02:21:25 +02:00
|
|
|
sem_post(&g_netlock);
|
2011-03-15 21:26:34 +01:00
|
|
|
|
2015-02-09 14:50:10 +01:00
|
|
|
/* Now take the semaphore, waiting if so requested. */
|
2011-03-15 21:26:34 +01:00
|
|
|
|
2015-02-09 14:50:10 +01:00
|
|
|
if (abstime)
|
|
|
|
{
|
|
|
|
/* Wait until we get the lock or until the timeout expires */
|
|
|
|
|
|
|
|
ret = sem_timedwait(sem, abstime);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-05-29 22:32:56 +02:00
|
|
|
/* Wait as long as necessary to get the lock */
|
2015-02-09 14:50:10 +01:00
|
|
|
|
|
|
|
ret = sem_wait(sem);
|
|
|
|
}
|
2011-03-15 21:26:34 +01:00
|
|
|
|
2015-01-22 21:46:36 +01:00
|
|
|
/* Recover the network lock at the proper count */
|
2011-03-15 21:26:34 +01:00
|
|
|
|
2014-06-29 03:26:16 +02:00
|
|
|
_net_takesem();
|
2011-03-15 21:26:34 +01:00
|
|
|
g_holder = me;
|
|
|
|
g_count = count;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = sem_wait(sem);
|
|
|
|
}
|
|
|
|
|
|
|
|
sched_unlock();
|
2016-02-14 15:38:44 +01:00
|
|
|
leave_critical_section(flags);
|
2011-03-15 21:26:34 +01:00
|
|
|
return ret;
|
2015-02-09 14:50:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: net_lockedwait
|
2015-02-09 14:50:10 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Atomically wait for sem while temporarily releasing g_netlock.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* sem - A reference to the semaphore to be taken.
|
|
|
|
*
|
|
|
|
* Returned value:
|
|
|
|
* The returned value is the same as sem_wait(): Zero (OK) is returned
|
|
|
|
* on success; -1 (ERROR) is returned on a failure with the errno value
|
|
|
|
* set appropriately.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int net_lockedwait(sem_t *sem)
|
|
|
|
{
|
|
|
|
return net_timedwait(sem, NULL);
|
|
|
|
}
|
2011-03-15 21:26:34 +01:00
|
|
|
|