2003-01-18 18:18:02 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2001-2003 Swedish Institute of Computer Science.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* 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. The name of the author may not be used to endorse or promote products
|
|
|
|
* derived from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
|
|
|
|
*
|
|
|
|
* This file is part of the lwIP TCP/IP stack.
|
|
|
|
*
|
|
|
|
* Author: Adam Dunkels <adam@sics.se>
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Wed Apr 17 16:05:29 EDT 2002 (James Roth)
|
|
|
|
*
|
|
|
|
* - Fixed an unlikely sys_thread_new() race condition.
|
|
|
|
*
|
|
|
|
* - Made current_thread() work with threads which where
|
|
|
|
* not created with sys_thread_new(). This includes
|
|
|
|
* the main thread and threads made with pthread_create().
|
|
|
|
*
|
|
|
|
* - Catch overflows where more than SYS_MBOX_SIZE messages
|
|
|
|
* are waiting to be read. The sys_mbox_post() routine
|
|
|
|
* will block until there is more room instead of just
|
|
|
|
* leaking messages.
|
|
|
|
*/
|
|
|
|
#include "lwip/debug.h"
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
|
|
|
|
#include "lwip/sys.h"
|
|
|
|
#include "lwip/opt.h"
|
|
|
|
#include "lwip/stats.h"
|
|
|
|
|
|
|
|
#define UMAX(a, b) ((a) > (b) ? (a) : (b))
|
|
|
|
|
2010-01-07 10:21:13 +00:00
|
|
|
static struct timeval starttime;
|
|
|
|
|
|
|
|
#if !NO_SYS
|
|
|
|
|
2003-01-18 18:18:02 +00:00
|
|
|
static struct sys_thread *threads = NULL;
|
|
|
|
static pthread_mutex_t threads_mutex = PTHREAD_MUTEX_INITIALIZER;
|
|
|
|
|
|
|
|
struct sys_mbox_msg {
|
|
|
|
struct sys_mbox_msg *next;
|
|
|
|
void *msg;
|
|
|
|
};
|
|
|
|
|
|
|
|
#define SYS_MBOX_SIZE 128
|
|
|
|
|
|
|
|
struct sys_mbox {
|
|
|
|
int first, last;
|
|
|
|
void *msgs[SYS_MBOX_SIZE];
|
2008-05-30 11:28:28 +00:00
|
|
|
struct sys_sem *not_empty;
|
|
|
|
struct sys_sem *not_full;
|
2003-01-18 18:18:02 +00:00
|
|
|
struct sys_sem *mutex;
|
|
|
|
int wait_send;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct sys_sem {
|
|
|
|
unsigned int c;
|
|
|
|
pthread_cond_t cond;
|
|
|
|
pthread_mutex_t mutex;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct sys_thread {
|
|
|
|
struct sys_thread *next;
|
|
|
|
pthread_t pthread;
|
|
|
|
};
|
|
|
|
|
2010-02-21 11:26:15 +00:00
|
|
|
#if SYS_LIGHTWEIGHT_PROT
|
2003-02-04 22:52:01 +00:00
|
|
|
static pthread_mutex_t lwprot_mutex = PTHREAD_MUTEX_INITIALIZER;
|
2010-02-12 13:57:58 +00:00
|
|
|
static pthread_t lwprot_thread = (pthread_t)0xDEAD;
|
2003-02-04 22:52:01 +00:00
|
|
|
static int lwprot_count = 0;
|
2010-02-21 11:26:15 +00:00
|
|
|
#endif /* SYS_LIGHTWEIGHT_PROT */
|
2003-02-04 22:52:01 +00:00
|
|
|
|
2008-05-30 11:28:28 +00:00
|
|
|
static struct sys_sem *sys_sem_new_internal(u8_t count);
|
|
|
|
static void sys_sem_free_internal(struct sys_sem *sem);
|
2003-01-18 18:18:02 +00:00
|
|
|
|
2003-02-04 14:48:54 +00:00
|
|
|
static u32_t cond_wait(pthread_cond_t * cond, pthread_mutex_t * mutex,
|
2003-02-18 21:14:26 +00:00
|
|
|
u32_t timeout);
|
2003-01-18 18:18:02 +00:00
|
|
|
|
|
|
|
/*-----------------------------------------------------------------------------------*/
|
|
|
|
static struct sys_thread *
|
|
|
|
introduce_thread(pthread_t id)
|
|
|
|
{
|
|
|
|
struct sys_thread *thread;
|
2010-03-26 14:20:39 +00:00
|
|
|
|
|
|
|
thread = (struct sys_thread *)malloc(sizeof(struct sys_thread));
|
|
|
|
|
2006-07-03 07:23:05 +00:00
|
|
|
if (thread != NULL) {
|
2003-01-18 18:18:02 +00:00
|
|
|
pthread_mutex_lock(&threads_mutex);
|
|
|
|
thread->next = threads;
|
|
|
|
thread->pthread = id;
|
|
|
|
threads = thread;
|
|
|
|
pthread_mutex_unlock(&threads_mutex);
|
|
|
|
}
|
2010-03-26 14:20:39 +00:00
|
|
|
|
2003-01-18 18:18:02 +00:00
|
|
|
return thread;
|
|
|
|
}
|
|
|
|
/*-----------------------------------------------------------------------------------*/
|
2003-02-11 20:59:49 +00:00
|
|
|
sys_thread_t
|
2010-03-26 14:20:39 +00:00
|
|
|
sys_thread_new(const char *name, lwip_thread_fn function, void *arg, int stacksize, int prio)
|
2003-01-18 18:18:02 +00:00
|
|
|
{
|
|
|
|
int code;
|
|
|
|
pthread_t tmp;
|
|
|
|
struct sys_thread *st = NULL;
|
2008-07-16 20:37:56 +00:00
|
|
|
LWIP_UNUSED_ARG(name);
|
|
|
|
LWIP_UNUSED_ARG(stacksize);
|
|
|
|
LWIP_UNUSED_ARG(prio);
|
|
|
|
|
2003-01-18 18:18:02 +00:00
|
|
|
code = pthread_create(&tmp,
|
2003-02-18 21:14:26 +00:00
|
|
|
NULL,
|
|
|
|
(void *(*)(void *))
|
|
|
|
function,
|
|
|
|
arg);
|
2003-01-18 18:18:02 +00:00
|
|
|
|
2003-05-01 13:27:52 +00:00
|
|
|
if (0 == code) {
|
2003-01-18 18:18:02 +00:00
|
|
|
st = introduce_thread(tmp);
|
|
|
|
}
|
|
|
|
|
2003-05-01 13:27:52 +00:00
|
|
|
if (NULL == st) {
|
2008-09-30 13:48:07 +00:00
|
|
|
LWIP_DEBUGF(SYS_DEBUG, ("sys_thread_new: pthread_create %d, st = 0x%lx",
|
|
|
|
code, (unsigned long)st));
|
2003-01-18 18:18:02 +00:00
|
|
|
abort();
|
|
|
|
}
|
2003-02-11 20:59:49 +00:00
|
|
|
return st;
|
2003-01-18 18:18:02 +00:00
|
|
|
}
|
|
|
|
/*-----------------------------------------------------------------------------------*/
|
2010-02-12 13:57:58 +00:00
|
|
|
err_t
|
|
|
|
sys_mbox_new(struct sys_mbox **mb, int size)
|
2003-01-18 18:18:02 +00:00
|
|
|
{
|
|
|
|
struct sys_mbox *mbox;
|
2008-07-16 20:37:56 +00:00
|
|
|
LWIP_UNUSED_ARG(size);
|
2010-03-26 14:20:39 +00:00
|
|
|
|
|
|
|
mbox = (struct sys_mbox *)malloc(sizeof(struct sys_mbox));
|
2010-02-12 13:57:58 +00:00
|
|
|
if (mbox == NULL) {
|
|
|
|
return ERR_MEM;
|
|
|
|
}
|
|
|
|
mbox->first = mbox->last = 0;
|
|
|
|
mbox->not_empty = sys_sem_new_internal(0);
|
|
|
|
mbox->not_full = sys_sem_new_internal(0);
|
|
|
|
mbox->mutex = sys_sem_new_internal(1);
|
|
|
|
mbox->wait_send = 0;
|
|
|
|
|
2010-02-13 17:34:20 +00:00
|
|
|
SYS_STATS_INC_USED(mbox);
|
2010-02-12 13:57:58 +00:00
|
|
|
*mb = mbox;
|
|
|
|
return ERR_OK;
|
2003-01-18 18:18:02 +00:00
|
|
|
}
|
|
|
|
/*-----------------------------------------------------------------------------------*/
|
|
|
|
void
|
2010-02-12 13:57:58 +00:00
|
|
|
sys_mbox_free(struct sys_mbox **mb)
|
2003-01-18 18:18:02 +00:00
|
|
|
{
|
2010-02-12 13:57:58 +00:00
|
|
|
if ((mb != NULL) && (*mb != SYS_MBOX_NULL)) {
|
|
|
|
struct sys_mbox *mbox = *mb;
|
2010-02-13 17:34:20 +00:00
|
|
|
SYS_STATS_DEC(mbox.used);
|
2010-02-12 13:57:58 +00:00
|
|
|
sys_arch_sem_wait(&mbox->mutex, 0);
|
2003-01-18 18:18:02 +00:00
|
|
|
|
2008-05-30 11:28:28 +00:00
|
|
|
sys_sem_free_internal(mbox->not_empty);
|
|
|
|
sys_sem_free_internal(mbox->not_full);
|
|
|
|
sys_sem_free_internal(mbox->mutex);
|
|
|
|
mbox->not_empty = mbox->not_full = mbox->mutex = NULL;
|
2003-06-10 10:45:29 +00:00
|
|
|
/* LWIP_DEBUGF("sys_mbox_free: mbox 0x%lx\n", mbox); */
|
2003-01-18 18:18:02 +00:00
|
|
|
free(mbox);
|
|
|
|
}
|
|
|
|
}
|
2008-01-17 11:51:09 +00:00
|
|
|
/*-----------------------------------------------------------------------------------*/
|
|
|
|
err_t
|
2010-02-12 13:57:58 +00:00
|
|
|
sys_mbox_trypost(struct sys_mbox **mb, void *msg)
|
2008-01-17 11:51:09 +00:00
|
|
|
{
|
|
|
|
u8_t first;
|
2010-02-12 13:57:58 +00:00
|
|
|
struct sys_mbox *mbox;
|
|
|
|
LWIP_ASSERT("invalid mbox", (mb != NULL) && (*mb != NULL));
|
|
|
|
mbox = *mb;
|
2010-03-26 14:20:39 +00:00
|
|
|
|
2010-02-12 13:57:58 +00:00
|
|
|
sys_arch_sem_wait(&mbox->mutex, 0);
|
2010-03-26 14:20:39 +00:00
|
|
|
|
2008-01-17 11:51:09 +00:00
|
|
|
LWIP_DEBUGF(SYS_DEBUG, ("sys_mbox_trypost: mbox %p msg %p\n",
|
|
|
|
(void *)mbox, (void *)msg));
|
2010-03-26 14:20:39 +00:00
|
|
|
|
2008-05-30 11:41:51 +00:00
|
|
|
if ((mbox->last + 1) >= (mbox->first + SYS_MBOX_SIZE)) {
|
2010-02-12 13:57:58 +00:00
|
|
|
sys_sem_signal(&mbox->mutex);
|
2008-01-17 11:51:09 +00:00
|
|
|
return ERR_MEM;
|
2008-05-30 11:41:51 +00:00
|
|
|
}
|
2003-01-18 18:18:02 +00:00
|
|
|
|
2008-01-17 11:51:09 +00:00
|
|
|
mbox->msgs[mbox->last % SYS_MBOX_SIZE] = msg;
|
2010-03-26 14:20:39 +00:00
|
|
|
|
2008-01-17 11:51:09 +00:00
|
|
|
if (mbox->last == mbox->first) {
|
|
|
|
first = 1;
|
|
|
|
} else {
|
|
|
|
first = 0;
|
|
|
|
}
|
2010-03-26 14:20:39 +00:00
|
|
|
|
2008-01-17 11:51:09 +00:00
|
|
|
mbox->last++;
|
2010-03-26 14:20:39 +00:00
|
|
|
|
2008-01-17 11:51:09 +00:00
|
|
|
if (first) {
|
2010-02-12 13:57:58 +00:00
|
|
|
sys_sem_signal(&mbox->not_empty);
|
2008-01-17 11:51:09 +00:00
|
|
|
}
|
|
|
|
|
2010-02-12 13:57:58 +00:00
|
|
|
sys_sem_signal(&mbox->mutex);
|
2008-01-17 11:51:09 +00:00
|
|
|
|
|
|
|
return ERR_OK;
|
|
|
|
}
|
2003-01-18 18:18:02 +00:00
|
|
|
/*-----------------------------------------------------------------------------------*/
|
|
|
|
void
|
2010-02-12 13:57:58 +00:00
|
|
|
sys_mbox_post(struct sys_mbox **mb, void *msg)
|
2003-01-18 18:18:02 +00:00
|
|
|
{
|
|
|
|
u8_t first;
|
2010-02-12 13:57:58 +00:00
|
|
|
struct sys_mbox *mbox;
|
|
|
|
LWIP_ASSERT("invalid mbox", (mb != NULL) && (*mb != NULL));
|
|
|
|
mbox = *mb;
|
|
|
|
|
|
|
|
sys_arch_sem_wait(&mbox->mutex, 0);
|
2010-03-26 14:20:39 +00:00
|
|
|
|
2003-06-10 10:45:29 +00:00
|
|
|
LWIP_DEBUGF(SYS_DEBUG, ("sys_mbox_post: mbox %p msg %p\n", (void *)mbox, (void *)msg));
|
2010-03-26 14:20:39 +00:00
|
|
|
|
2003-05-01 13:27:52 +00:00
|
|
|
while ((mbox->last + 1) >= (mbox->first + SYS_MBOX_SIZE)) {
|
2003-01-18 18:18:02 +00:00
|
|
|
mbox->wait_send++;
|
2010-02-12 13:57:58 +00:00
|
|
|
sys_sem_signal(&mbox->mutex);
|
|
|
|
sys_arch_sem_wait(&mbox->not_full, 0);
|
|
|
|
sys_arch_sem_wait(&mbox->mutex, 0);
|
2003-01-18 18:18:02 +00:00
|
|
|
mbox->wait_send--;
|
|
|
|
}
|
2010-03-26 14:20:39 +00:00
|
|
|
|
2003-01-18 18:18:02 +00:00
|
|
|
mbox->msgs[mbox->last % SYS_MBOX_SIZE] = msg;
|
2010-03-26 14:20:39 +00:00
|
|
|
|
2003-05-01 13:27:52 +00:00
|
|
|
if (mbox->last == mbox->first) {
|
2003-01-18 18:18:02 +00:00
|
|
|
first = 1;
|
|
|
|
} else {
|
|
|
|
first = 0;
|
|
|
|
}
|
2010-03-26 14:20:39 +00:00
|
|
|
|
2003-01-18 18:18:02 +00:00
|
|
|
mbox->last++;
|
2010-03-26 14:20:39 +00:00
|
|
|
|
2003-05-01 13:27:52 +00:00
|
|
|
if (first) {
|
2010-02-12 13:57:58 +00:00
|
|
|
sys_sem_signal(&mbox->not_empty);
|
2003-01-18 18:18:02 +00:00
|
|
|
}
|
|
|
|
|
2010-02-12 13:57:58 +00:00
|
|
|
sys_sem_signal(&mbox->mutex);
|
2003-01-18 18:18:02 +00:00
|
|
|
}
|
|
|
|
/*-----------------------------------------------------------------------------------*/
|
2003-02-04 14:48:54 +00:00
|
|
|
u32_t
|
2010-02-12 13:57:58 +00:00
|
|
|
sys_arch_mbox_tryfetch(struct sys_mbox **mb, void **msg)
|
2008-01-15 13:10:51 +00:00
|
|
|
{
|
2010-02-12 13:57:58 +00:00
|
|
|
struct sys_mbox *mbox;
|
|
|
|
LWIP_ASSERT("invalid mbox", (mb != NULL) && (*mb != NULL));
|
|
|
|
mbox = *mb;
|
|
|
|
|
|
|
|
sys_arch_sem_wait(&mbox->mutex, 0);
|
2008-01-15 13:10:51 +00:00
|
|
|
|
|
|
|
if (mbox->first == mbox->last) {
|
2010-02-12 13:57:58 +00:00
|
|
|
sys_sem_signal(&mbox->mutex);
|
2008-01-15 13:10:51 +00:00
|
|
|
return SYS_MBOX_EMPTY;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (msg != NULL) {
|
|
|
|
LWIP_DEBUGF(SYS_DEBUG, ("sys_mbox_tryfetch: mbox %p msg %p\n", (void *)mbox, *msg));
|
|
|
|
*msg = mbox->msgs[mbox->first % SYS_MBOX_SIZE];
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
LWIP_DEBUGF(SYS_DEBUG, ("sys_mbox_tryfetch: mbox %p, null msg\n", (void *)mbox));
|
|
|
|
}
|
|
|
|
|
|
|
|
mbox->first++;
|
2010-03-26 14:20:39 +00:00
|
|
|
|
2008-01-15 13:10:51 +00:00
|
|
|
if (mbox->wait_send) {
|
2010-02-12 13:57:58 +00:00
|
|
|
sys_sem_signal(&mbox->not_full);
|
2008-01-15 13:10:51 +00:00
|
|
|
}
|
|
|
|
|
2010-02-12 13:57:58 +00:00
|
|
|
sys_sem_signal(&mbox->mutex);
|
2008-01-15 13:10:51 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/*-----------------------------------------------------------------------------------*/
|
|
|
|
u32_t
|
2010-02-12 13:57:58 +00:00
|
|
|
sys_arch_mbox_fetch(struct sys_mbox **mb, void **msg, u32_t timeout)
|
2003-01-18 18:18:02 +00:00
|
|
|
{
|
2008-07-16 20:37:56 +00:00
|
|
|
u32_t time_needed = 0;
|
2010-02-12 13:57:58 +00:00
|
|
|
struct sys_mbox *mbox;
|
|
|
|
LWIP_ASSERT("invalid mbox", (mb != NULL) && (*mb != NULL));
|
|
|
|
mbox = *mb;
|
|
|
|
|
2003-01-18 18:18:02 +00:00
|
|
|
/* The mutex lock is quick so we don't bother with the timeout
|
|
|
|
stuff here. */
|
2010-02-12 13:57:58 +00:00
|
|
|
sys_arch_sem_wait(&mbox->mutex, 0);
|
2003-01-18 18:18:02 +00:00
|
|
|
|
2003-05-01 13:27:52 +00:00
|
|
|
while (mbox->first == mbox->last) {
|
2010-02-12 13:57:58 +00:00
|
|
|
sys_sem_signal(&mbox->mutex);
|
2010-03-26 14:20:39 +00:00
|
|
|
|
2003-01-18 18:18:02 +00:00
|
|
|
/* We block while waiting for a mail to arrive in the mailbox. We
|
|
|
|
must be prepared to timeout. */
|
2003-05-01 13:27:52 +00:00
|
|
|
if (timeout != 0) {
|
2010-02-12 13:57:58 +00:00
|
|
|
time_needed = sys_arch_sem_wait(&mbox->not_empty, timeout);
|
2010-03-26 14:20:39 +00:00
|
|
|
|
2008-07-16 20:37:56 +00:00
|
|
|
if (time_needed == SYS_ARCH_TIMEOUT) {
|
2003-05-19 14:45:02 +00:00
|
|
|
return SYS_ARCH_TIMEOUT;
|
2003-01-18 18:18:02 +00:00
|
|
|
}
|
|
|
|
} else {
|
2010-02-12 13:57:58 +00:00
|
|
|
sys_arch_sem_wait(&mbox->not_empty, 0);
|
2003-01-18 18:18:02 +00:00
|
|
|
}
|
2010-03-26 14:20:39 +00:00
|
|
|
|
2010-02-12 13:57:58 +00:00
|
|
|
sys_arch_sem_wait(&mbox->mutex, 0);
|
2003-01-18 18:18:02 +00:00
|
|
|
}
|
|
|
|
|
2003-05-01 13:27:52 +00:00
|
|
|
if (msg != NULL) {
|
2004-07-29 09:37:46 +00:00
|
|
|
LWIP_DEBUGF(SYS_DEBUG, ("sys_mbox_fetch: mbox %p msg %p\n", (void *)mbox, *msg));
|
2003-01-18 18:18:02 +00:00
|
|
|
*msg = mbox->msgs[mbox->first % SYS_MBOX_SIZE];
|
|
|
|
}
|
2004-07-29 09:37:46 +00:00
|
|
|
else{
|
|
|
|
LWIP_DEBUGF(SYS_DEBUG, ("sys_mbox_fetch: mbox %p, null msg\n", (void *)mbox));
|
|
|
|
}
|
2003-01-18 18:18:02 +00:00
|
|
|
|
|
|
|
mbox->first++;
|
2010-03-26 14:20:39 +00:00
|
|
|
|
2003-05-01 13:27:52 +00:00
|
|
|
if (mbox->wait_send) {
|
2010-02-12 13:57:58 +00:00
|
|
|
sys_sem_signal(&mbox->not_full);
|
2003-01-18 18:18:02 +00:00
|
|
|
}
|
|
|
|
|
2010-02-12 13:57:58 +00:00
|
|
|
sys_sem_signal(&mbox->mutex);
|
2003-01-18 18:18:02 +00:00
|
|
|
|
2008-07-16 20:37:56 +00:00
|
|
|
return time_needed;
|
2003-01-18 18:18:02 +00:00
|
|
|
}
|
|
|
|
/*-----------------------------------------------------------------------------------*/
|
|
|
|
static struct sys_sem *
|
2008-05-30 11:28:28 +00:00
|
|
|
sys_sem_new_internal(u8_t count)
|
2003-01-18 18:18:02 +00:00
|
|
|
{
|
|
|
|
struct sys_sem *sem;
|
2010-03-26 14:20:39 +00:00
|
|
|
|
|
|
|
sem = (struct sys_sem *)malloc(sizeof(struct sys_sem));
|
2006-07-03 07:23:05 +00:00
|
|
|
if (sem != NULL) {
|
|
|
|
sem->c = count;
|
|
|
|
pthread_cond_init(&(sem->cond), NULL);
|
|
|
|
pthread_mutex_init(&(sem->mutex), NULL);
|
|
|
|
}
|
2003-01-18 18:18:02 +00:00
|
|
|
return sem;
|
|
|
|
}
|
2008-05-30 11:28:28 +00:00
|
|
|
/*-----------------------------------------------------------------------------------*/
|
2010-02-12 13:57:58 +00:00
|
|
|
err_t
|
|
|
|
sys_sem_new(struct sys_sem **sem, u8_t count)
|
2008-05-30 11:28:28 +00:00
|
|
|
{
|
2010-02-13 17:34:20 +00:00
|
|
|
SYS_STATS_INC_USED(sem);
|
2010-02-12 13:57:58 +00:00
|
|
|
*sem = sys_sem_new_internal(count);
|
|
|
|
if (*sem == NULL) {
|
|
|
|
return ERR_MEM;
|
|
|
|
}
|
|
|
|
return ERR_OK;
|
2008-05-30 11:28:28 +00:00
|
|
|
}
|
2003-01-18 18:18:02 +00:00
|
|
|
/*-----------------------------------------------------------------------------------*/
|
2003-02-04 14:48:54 +00:00
|
|
|
static u32_t
|
|
|
|
cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex, u32_t timeout)
|
2003-01-18 18:18:02 +00:00
|
|
|
{
|
|
|
|
int tdiff;
|
|
|
|
unsigned long sec, usec;
|
|
|
|
struct timeval rtime1, rtime2;
|
|
|
|
struct timespec ts;
|
|
|
|
int retval;
|
2010-03-26 14:20:39 +00:00
|
|
|
|
2003-05-01 13:27:52 +00:00
|
|
|
if (timeout > 0) {
|
2003-01-18 18:18:02 +00:00
|
|
|
/* Get a timestamp and add the timeout value. */
|
2010-03-26 14:20:39 +00:00
|
|
|
gettimeofday(&rtime1, NULL);
|
2003-01-18 18:18:02 +00:00
|
|
|
sec = rtime1.tv_sec;
|
|
|
|
usec = rtime1.tv_usec;
|
|
|
|
usec += timeout % 1000 * 1000;
|
|
|
|
sec += (int)(timeout / 1000) + (int)(usec / 1000000);
|
|
|
|
usec = usec % 1000000;
|
|
|
|
ts.tv_nsec = usec * 1000;
|
|
|
|
ts.tv_sec = sec;
|
2010-03-26 14:20:39 +00:00
|
|
|
|
2003-01-18 18:18:02 +00:00
|
|
|
retval = pthread_cond_timedwait(cond, mutex, &ts);
|
2010-03-26 14:20:39 +00:00
|
|
|
|
2003-05-01 13:27:52 +00:00
|
|
|
if (retval == ETIMEDOUT) {
|
2003-05-19 14:45:02 +00:00
|
|
|
return SYS_ARCH_TIMEOUT;
|
2003-01-18 18:18:02 +00:00
|
|
|
} else {
|
|
|
|
/* Calculate for how long we waited for the cond. */
|
2010-03-26 14:20:39 +00:00
|
|
|
gettimeofday(&rtime2, NULL);
|
2003-01-18 18:18:02 +00:00
|
|
|
tdiff = (rtime2.tv_sec - rtime1.tv_sec) * 1000 +
|
2003-02-18 21:14:26 +00:00
|
|
|
(rtime2.tv_usec - rtime1.tv_usec) / 1000;
|
2010-03-26 14:20:39 +00:00
|
|
|
|
2003-05-01 13:27:52 +00:00
|
|
|
if (tdiff <= 0) {
|
2003-03-28 20:47:10 +00:00
|
|
|
return 0;
|
2003-01-18 18:18:02 +00:00
|
|
|
}
|
2010-03-26 14:20:39 +00:00
|
|
|
|
2003-01-18 18:18:02 +00:00
|
|
|
return tdiff;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
pthread_cond_wait(cond, mutex);
|
2003-05-19 14:45:02 +00:00
|
|
|
return SYS_ARCH_TIMEOUT;
|
2003-01-18 18:18:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/*-----------------------------------------------------------------------------------*/
|
2003-02-04 14:48:54 +00:00
|
|
|
u32_t
|
2010-02-12 13:57:58 +00:00
|
|
|
sys_arch_sem_wait(struct sys_sem **s, u32_t timeout)
|
2003-01-18 18:18:02 +00:00
|
|
|
{
|
2008-07-16 20:37:56 +00:00
|
|
|
u32_t time_needed = 0;
|
2010-02-12 13:57:58 +00:00
|
|
|
struct sys_sem *sem;
|
|
|
|
LWIP_ASSERT("invalid sem", (s != NULL) && (*s != NULL));
|
|
|
|
sem = *s;
|
|
|
|
|
2003-01-18 18:18:02 +00:00
|
|
|
pthread_mutex_lock(&(sem->mutex));
|
2003-05-01 13:27:52 +00:00
|
|
|
while (sem->c <= 0) {
|
|
|
|
if (timeout > 0) {
|
2008-07-16 20:37:56 +00:00
|
|
|
time_needed = cond_wait(&(sem->cond), &(sem->mutex), timeout);
|
2010-03-26 14:20:39 +00:00
|
|
|
|
2008-07-16 20:37:56 +00:00
|
|
|
if (time_needed == SYS_ARCH_TIMEOUT) {
|
2003-02-18 21:14:26 +00:00
|
|
|
pthread_mutex_unlock(&(sem->mutex));
|
2003-05-19 14:45:02 +00:00
|
|
|
return SYS_ARCH_TIMEOUT;
|
2003-01-18 18:18:02 +00:00
|
|
|
}
|
|
|
|
/* pthread_mutex_unlock(&(sem->mutex));
|
2008-07-16 20:37:56 +00:00
|
|
|
return time_needed; */
|
2003-01-18 18:18:02 +00:00
|
|
|
} else {
|
|
|
|
cond_wait(&(sem->cond), &(sem->mutex), 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sem->c--;
|
|
|
|
pthread_mutex_unlock(&(sem->mutex));
|
2008-07-16 20:37:56 +00:00
|
|
|
return time_needed;
|
2003-01-18 18:18:02 +00:00
|
|
|
}
|
|
|
|
/*-----------------------------------------------------------------------------------*/
|
|
|
|
void
|
2010-02-12 13:57:58 +00:00
|
|
|
sys_sem_signal(struct sys_sem **s)
|
2003-01-18 18:18:02 +00:00
|
|
|
{
|
2010-02-12 13:57:58 +00:00
|
|
|
struct sys_sem *sem;
|
|
|
|
LWIP_ASSERT("invalid sem", (s != NULL) && (*s != NULL));
|
|
|
|
sem = *s;
|
|
|
|
|
2003-01-18 18:18:02 +00:00
|
|
|
pthread_mutex_lock(&(sem->mutex));
|
|
|
|
sem->c++;
|
|
|
|
|
2003-05-01 13:27:52 +00:00
|
|
|
if (sem->c > 1) {
|
2003-01-18 18:18:02 +00:00
|
|
|
sem->c = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
pthread_cond_broadcast(&(sem->cond));
|
|
|
|
pthread_mutex_unlock(&(sem->mutex));
|
|
|
|
}
|
|
|
|
/*-----------------------------------------------------------------------------------*/
|
2008-05-30 11:28:28 +00:00
|
|
|
static void
|
|
|
|
sys_sem_free_internal(struct sys_sem *sem)
|
|
|
|
{
|
|
|
|
pthread_cond_destroy(&(sem->cond));
|
|
|
|
pthread_mutex_destroy(&(sem->mutex));
|
|
|
|
free(sem);
|
|
|
|
}
|
|
|
|
/*-----------------------------------------------------------------------------------*/
|
2003-01-18 18:18:02 +00:00
|
|
|
void
|
2010-02-12 13:57:58 +00:00
|
|
|
sys_sem_free(struct sys_sem **sem)
|
2003-01-18 18:18:02 +00:00
|
|
|
{
|
2010-02-12 13:57:58 +00:00
|
|
|
if ((sem != NULL) && (*sem != SYS_SEM_NULL)) {
|
2010-02-13 17:34:20 +00:00
|
|
|
SYS_STATS_DEC(sem.used);
|
2010-02-12 13:57:58 +00:00
|
|
|
sys_sem_free_internal(*sem);
|
2003-01-18 18:18:02 +00:00
|
|
|
}
|
|
|
|
}
|
2010-01-07 10:21:13 +00:00
|
|
|
#endif /* !NO_SYS */
|
2003-01-18 18:18:02 +00:00
|
|
|
/*-----------------------------------------------------------------------------------*/
|
2010-03-26 14:20:39 +00:00
|
|
|
u32_t
|
2008-11-19 13:38:10 +00:00
|
|
|
sys_now(void)
|
2003-01-18 18:18:02 +00:00
|
|
|
{
|
|
|
|
struct timeval tv;
|
2009-03-25 11:01:00 +00:00
|
|
|
u32_t sec, usec, msec;
|
2010-03-26 14:20:39 +00:00
|
|
|
gettimeofday(&tv, NULL);
|
|
|
|
|
2009-03-25 11:01:00 +00:00
|
|
|
sec = (u32_t)(tv.tv_sec - starttime.tv_sec);
|
|
|
|
usec = (u32_t)(tv.tv_usec - starttime.tv_usec);
|
2003-01-18 18:18:02 +00:00
|
|
|
msec = sec * 1000 + usec / 1000;
|
2010-03-26 14:20:39 +00:00
|
|
|
|
2003-01-18 18:18:02 +00:00
|
|
|
return msec;
|
|
|
|
}
|
|
|
|
/*-----------------------------------------------------------------------------------*/
|
|
|
|
void
|
2010-03-26 14:20:39 +00:00
|
|
|
sys_init(void)
|
2003-01-18 18:18:02 +00:00
|
|
|
{
|
2010-03-26 14:20:39 +00:00
|
|
|
gettimeofday(&starttime, NULL);
|
2003-01-18 18:18:02 +00:00
|
|
|
}
|
|
|
|
/*-----------------------------------------------------------------------------------*/
|
2010-01-07 10:21:13 +00:00
|
|
|
#if SYS_LIGHTWEIGHT_PROT
|
2003-02-18 21:14:26 +00:00
|
|
|
/** sys_prot_t sys_arch_protect(void)
|
|
|
|
|
|
|
|
This optional function does a "fast" critical region protection and returns
|
|
|
|
the previous protection level. This function is only called during very short
|
|
|
|
critical regions. An embedded system which supports ISR-based drivers might
|
|
|
|
want to implement this function by disabling interrupts. Task-based systems
|
|
|
|
might want to implement this by using a mutex or disabling tasking. This
|
|
|
|
function should support recursive calls from the same task or interrupt. In
|
|
|
|
other words, sys_arch_protect() could be called while already protected. In
|
|
|
|
that case the return value indicates that it is already protected.
|
|
|
|
|
|
|
|
sys_arch_protect() is only required if your port is supporting an operating
|
|
|
|
system.
|
|
|
|
*/
|
2003-02-12 16:38:57 +00:00
|
|
|
sys_prot_t
|
2003-02-04 22:52:01 +00:00
|
|
|
sys_arch_protect(void)
|
|
|
|
{
|
|
|
|
/* Note that for the UNIX port, we are using a lightweight mutex, and our
|
|
|
|
* own counter (which is locked by the mutex). The return code is not actually
|
|
|
|
* used. */
|
|
|
|
if (lwprot_thread != pthread_self())
|
|
|
|
{
|
|
|
|
/* We are locking the mutex where it has not been locked before *
|
|
|
|
* or is being locked by another thread */
|
|
|
|
pthread_mutex_lock(&lwprot_mutex);
|
|
|
|
lwprot_thread = pthread_self();
|
|
|
|
lwprot_count = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
/* It is already locked by THIS thread */
|
|
|
|
lwprot_count++;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/*-----------------------------------------------------------------------------------*/
|
2003-02-18 21:14:26 +00:00
|
|
|
/** void sys_arch_unprotect(sys_prot_t pval)
|
|
|
|
|
|
|
|
This optional function does a "fast" set of critical region protection to the
|
|
|
|
value specified by pval. See the documentation for sys_arch_protect() for
|
|
|
|
more information. This function is only required if your port is supporting
|
|
|
|
an operating system.
|
|
|
|
*/
|
2003-02-04 22:52:01 +00:00
|
|
|
void
|
2003-02-12 16:38:57 +00:00
|
|
|
sys_arch_unprotect(sys_prot_t pval)
|
2003-02-04 22:52:01 +00:00
|
|
|
{
|
2008-07-16 20:37:56 +00:00
|
|
|
LWIP_UNUSED_ARG(pval);
|
2003-02-04 22:52:01 +00:00
|
|
|
if (lwprot_thread == pthread_self())
|
|
|
|
{
|
|
|
|
if (--lwprot_count == 0)
|
|
|
|
{
|
|
|
|
lwprot_thread = (pthread_t) 0xDEAD;
|
|
|
|
pthread_mutex_unlock(&lwprot_mutex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-01-07 10:21:13 +00:00
|
|
|
#endif /* SYS_LIGHTWEIGHT_PROT */
|
2003-02-04 22:52:01 +00:00
|
|
|
|
|
|
|
/*-----------------------------------------------------------------------------------*/
|
2003-06-19 11:42:56 +00:00
|
|
|
|
|
|
|
#ifndef MAX_JIFFY_OFFSET
|
2009-06-25 12:51:23 +00:00
|
|
|
#define MAX_JIFFY_OFFSET ((~0U >> 1)-1)
|
2003-06-19 11:42:56 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef HZ
|
|
|
|
#define HZ 100
|
|
|
|
#endif
|
|
|
|
|
2008-09-30 13:48:07 +00:00
|
|
|
u32_t
|
2003-06-19 11:42:56 +00:00
|
|
|
sys_jiffies(void)
|
|
|
|
{
|
|
|
|
struct timeval tv;
|
2008-06-19 15:31:12 +00:00
|
|
|
unsigned long sec;
|
|
|
|
long usec;
|
2003-06-19 11:42:56 +00:00
|
|
|
|
|
|
|
gettimeofday(&tv,NULL);
|
2008-06-19 15:31:12 +00:00
|
|
|
sec = tv.tv_sec - starttime.tv_sec;
|
|
|
|
usec = tv.tv_usec;
|
2003-06-19 11:42:56 +00:00
|
|
|
|
|
|
|
if (sec >= (MAX_JIFFY_OFFSET / HZ))
|
2009-06-25 12:51:23 +00:00
|
|
|
return MAX_JIFFY_OFFSET;
|
2003-06-19 11:42:56 +00:00
|
|
|
usec += 1000000L / HZ - 1;
|
|
|
|
usec /= 1000000L / HZ;
|
|
|
|
return HZ * sec + usec;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if PPP_DEBUG
|
|
|
|
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
|
|
|
void ppp_trace(int level, const char *format, ...)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
(void)level;
|
|
|
|
va_start(args, format);
|
|
|
|
vprintf(format, args);
|
|
|
|
va_end(args);
|
|
|
|
}
|
|
|
|
#endif
|