metalsvm/lwip/src/core/sys.c
U-MobileHooK\Benedikt 1077a5a3ca initial commit
2011-06-04 09:27:30 +02:00

304 lines
6.9 KiB
C

/**
* @file
* lwIP Operating System abstraction
*
*/
/*
* Copyright (c) 2001-2004 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>
*
*/
#include "lwip/opt.h"
#include "lwip/sys.h"
/* Most of the functions defined in sys.h must be implemented in the
* architecture-dependent file sys_arch.c */
#ifdef WIN32
#include <windows.h>
#include <system/threads/bthread.h>
#else
#include <metalsvm/time.h>
#include <asm/tasks.h>
#define TRUE 1
#define FALSE 0
#endif
/* sys_now(): retrives tick count to mesure system time
*
*/
u32_t sys_now(void)
{
#ifdef WIN32
return GetTickCount();
#else
return get_clock_tick();
#endif
}
#if !NO_SYS
/**
* Sleep for some ms. Timeouts are NOT processed while sleeping.
*
* @param ms number of milliseconds to sleep
*/
void
sys_msleep(u32_t ms)
{
if (ms > 0) {
sys_sem_t delaysem;
err_t err = sys_sem_new(&delaysem, 0);
if (err == ERR_OK) {
sys_arch_sem_wait(&delaysem, ms);
sys_sem_free(&delaysem);
}
}
}
/* sys_thread_new(): Spawns a new thread with given attributes as supportet
* Note: In MetalSVM this is realized as kernel tasks
*/
sys_thread_t sys_thread_new(const char *name, lwip_thread_fn thread, void *arg, int stacksize, int prio)
{
#ifdef WIN32
bthread_t tmp;
bthread_attr_t tattr;
tattr.creation_flags = NULL;
tattr.thread_id = NULL;
tattr.stacksize = stacksize;
bthread_create(&tmp,NULL,thread,arg);
return tmp;
#else
tid_t tmp;
create_kernel_task(&tmp,thread,arg);
return tmp;
#endif
}
/* sys_sem_free(): destroy's given semaphore
* and releases system resources.
* This semaphore also gets invalid.
*/
void sys_sem_free(sys_sem_t* sem)
{
sem->valid = FALSE;
sem_destroy(&sem->sem);
}
/* sys_sem_valid(): returns if semaphore is valid
* at the moment
*/
int sys_sem_valid(sys_sem_t* sem)
{
return sem->valid;
}
/* sys_sem_new(): creates a new semaphre with given count.
* This semaphore becomes valid
*/
err_t sys_sem_new(sys_sem_t* sem,u8_t count)
{
sem->valid = TRUE;
return sem_init(&sem->sem,count);
}
/* sys_sem_set_invalid(): this semapohore becomes invalid
* Note: this does not mean it is destroyed
*/
void sys_sem_set_invalid(sys_sem_t * sem)
{
sem->valid = FALSE;
}
/* sys_sem_signal(): this semaphore is signaled
*
*/
void sys_sem_signal(sys_sem_t* sem)
{
sem_post(&sem->sem);
}
/* sys_arch_sem_wait): wait for the given semaphore for
* a given timeout
* Note: timeout = 0 means wait forever
*/
u32_t sys_arch_sem_wait(sys_sem_t *sem, u32_t timeout)
{
int err;
if (!timeout)
return sem_wait(&sem->sem);
while (timeout)
{
err = sem_trywait(&sem->sem);
#ifdef WIN32
if (err != 0x00000102L)
return err;
#else
if (err != -1)
return err;
#endif
timeout--;
}
return SYS_ARCH_TIMEOUT;
}
/* sys_mbox_valid() : returns if the given mailbox
* is valid
*/
int sys_mbox_valid(sys_mbox_t * mbox)
{
return mbox->valid;
}
/* sys_arch_mbox_fetch(): wait for the given mailbox for a specified
* amount of time.
* Note: timeout = 0 means wait forever
*/
u32_t sys_arch_mbox_fetch(sys_mbox_t * mbox, void **msg, u32_t timeout)
{
int err = 0;
if (!timeout)
return mailbox_ptr_fetch(&mbox->mailbox,msg);
while(timeout)
{
if (!mailbox_ptr_tryfetch(&mbox->mailbox,msg))
return 0;
timeout--;
}
return SYS_ARCH_TIMEOUT;
}
/* sys_mbox_free() : free the given mailbox, release the system resources
* and set mbox to invalid
*/
void sys_mbox_free(sys_mbox_t* mbox)
{
mbox->valid = FALSE;
mailbox_ptr_destroy(&mbox->mailbox);
}
/* sys_arch_mbox_tryfetch(): poll for new data in mailbox
*
*/
u32_t sys_arch_mbox_tryfetch(sys_mbox_t* mbox, void** msg)
{
return mailbox_ptr_tryfetch(&mbox->mailbox,msg);
}
/* sys_mbox_new(): create a new mailbox with a minimum size of "size"
*
*/
err_t sys_mbox_new(sys_mbox_t* mbox,int size)
{
mbox->valid = TRUE;
return mailbox_ptr_init(&mbox->mailbox);
}
/* sys_mbox_set_invalid(): set the given mailbox to invalid
* Note: system resources are NOT freed
*/
void sys_mbox_set_invalid(sys_mbox_t* mbox)
{
mbox->valid =FALSE;
}
/* sys_mbox_trypost(): try to post data to the mailbox
* Note: There is at the moment no try post implemented
* so we use the normal post instead
*/
int sys_mbox_trypost(sys_mbox_t* mbox,void* msg)
{
return mailbox_ptr_post(&mbox->mailbox,msg);
}
/* sys_mbox_post(): post new data to the mailbox
*
*/
void sys_mbox_post(sys_mbox_t* mbox,void* msg)
{
mailbox_ptr_post(&mbox->mailbox,msg);
}
/* sys_mutex_lock(): lock the given mutex
* Note: There is no specific mutex in MetalSVM
* so we use a semaphore with 1 element
*/
void sys_mutex_lock(sys_mutex_t* mutex)
{
#ifdef WIN32
bthread_mutex_lock(mutex);
#else
sem_wait(mutex);
#endif
}
/* sys_mutex_unlock(): unlock the given mutex
*
*/
void sys_mutex_unlock(sys_mutex_t* mutex)
{
#ifdef WIN32
bthread_mutex_unlock(mutex);
#else
sem_post(mutex);
#endif
}
/* sys_mutex_new(): create a new mutex
*
*/
err_t sys_mutex_new(sys_mutex_t * mutex)
{
#ifdef WIN32
bthread_mutex_init(mutex);
return 0;
#else
sem_init(mutex,1);
return 0;
#endif
}
/* sys_init(): init needed system resources
* Note: At the moment there are none
*/
void sys_init()
{
return NULL;
}
#endif /* !NO_SYS */