diff --git a/src/sys/include/net/arch/sys_arch.h b/src/sys/include/net/arch/sys_arch.h index b5161d6..10d50a0 100644 --- a/src/sys/include/net/arch/sys_arch.h +++ b/src/sys/include/net/arch/sys_arch.h @@ -7,18 +7,25 @@ #define SYS_SEM_NULL NULL #define SYS_MBOX_SIZE 100 +/* Structs */ struct sys_timeouts { struct sys_timeout *next; }; struct sys_sem { - unsigned int c; + int signaled; ubthread_cond_t cond; ubthread_mutex_t mutex; }; typedef struct sys_sem sys_sem_t; +struct sys_mutex { + ubthread_mutex_t mutex; +}; + +typedef struct sys_mutex sys_mutex_t; + struct sys_mbox { uint32_t head; uint32_t tail; @@ -44,6 +51,4 @@ typedef struct sys_thread * sys_thread_t; -//void sys_thread_new(void (*)(void), void *); - #endif diff --git a/src/sys/include/net/lwipopts.h b/src/sys/include/net/lwipopts.h index 6ecebb7..0abac09 100644 --- a/src/sys/include/net/lwipopts.h +++ b/src/sys/include/net/lwipopts.h @@ -33,79 +33,58 @@ #ifndef LWIP_HDR_LWIPOPTS_H #define LWIP_HDR_LWIPOPTS_H +/* NEVER Set To 1, 1 Will Remove OS Abstraction */ #define NO_SYS 0 -#define LWIP_TIMERS 1 + +/* Timer Settings */ +#define LWIP_TIMERS 1 #define LWIP_TIMERS_CUSTOM 0 + #define MEMCPY(dst, src, len) memcpy(dst,src,len) #define SMEMCPY(dst, src, len) memcpy(dst,src,len) -#define LWIP_MPU_COMPATIBLE 0 -#define LWIP_TCPIP_CORE_LOCKING 1 -#define LWIP_TCPIP_CORE_LOCKING_INPUT 0 -#define SYS_LIGHTWEIGHT_PROT 0 -#define MEM_LIBC_MALLOC 0 +/* Core locking and MPU */ +#define LWIP_MPU_COMPATIBLE 0 +#define LWIP_TCPIP_CORE_LOCKING 1 +#define LWIP_TCPIP_CORE_LOCKING_INPUT 0 +#define SYS_LIGHTWEIGHT_PROT 1 + + +#define LWIP_ALLOW_MEM_FREE_FROM_OTHER_CONTEXT 0 +#define MEM_ALIGNMENT 1 +#define MEM_LIBC_MALLOC 0 +#define MEM_SIZE 1600 +#define MEM_USE_POOLS 0 +#define MEM_USE_POOLS_TRY_BIGGER_POOL 0 #define MEMP_MEM_MALLOC 0 - -#define MEM_ALIGNMENT 1 - -#define MEM_SIZE 1600 - +#define MEMP_SANITY_CHECK 0 +#define MEMP_USE_CUSTOM_POOLS 0 #define MEMP_OVERFLOW_CHECK 0 -#define MEMP_SANITY_CHECK 0 - -#define MEM_USE_POOLS 0 - -#define MEM_USE_POOLS_TRY_BIGGER_POOL 0 - -#define MEMP_USE_CUSTOM_POOLS 0 - -#define LWIP_ALLOW_MEM_FREE_FROM_OTHER_CONTEXT 0 - -#define MEMP_NUM_PBUF 16 - -#define MEMP_NUM_RAW_PCB 4 - -#define MEMP_NUM_UDP_PCB 4 - -#define MEMP_NUM_TCP_PCB 5 - -#define MEMP_NUM_TCP_PCB_LISTEN 8 - -#define MEMP_NUM_TCP_SEG 16 - -#define MEMP_NUM_REASSDATA 5 - -#define MEMP_NUM_FRAG_PBUF 15 - +#define MEMP_NUM_API_MSG MEMP_NUM_TCPIP_MSG_API #define MEMP_NUM_ARP_QUEUE 30 - +#define MEMP_NUM_DNS_API_MSG MEMP_NUM_TCPIP_MSG_API +#define MEMP_NUM_FRAG_PBUF 15 #define MEMP_NUM_IGMP_GROUP 8 - -#define MEMP_NUM_SYS_TIMEOUT (LWIP_TCP + IP_REASSEMBLY + LWIP_ARP + (2*LWIP_DHCP) + LWIP_AUTOIP + LWIP_IGMP + LWIP_DNS + (PPP_SUPPORT*6*MEMP_NUM_PPP_PCB) + (LWIP_IPV6 ? (1 + LWIP_IPV6_REASS + LWIP_IPV6_MLD) : 0)) - -#define MEMP_NUM_NETBUF 2 - -#define MEMP_NUM_NETCONN 4 - -#define MEMP_NUM_TCPIP_MSG_API 8 - -#define MEMP_NUM_TCPIP_MSG_INPKT 8 - -#define MEMP_NUM_NETDB 1 - #define MEMP_NUM_LOCALHOSTLIST 1 - +#define MEMP_NUM_NETBUF 2 +#define MEMP_NUM_NETCONN 4 +#define MEMP_NUM_NETDB 1 +#define MEMP_NUM_NETIFAPI_MSG MEMP_NUM_TCPIP_MSG_API +#define MEMP_NUM_PBUF 16 +#define MEMP_NUM_RAW_PCB 4 +#define MEMP_NUM_REASSDATA 5 +#define MEMP_NUM_SOCKET_SETGETSOCKOPT_DATA MEMP_NUM_TCPIP_MSG_API +#define MEMP_NUM_TCP_PCB 5 +#define MEMP_NUM_TCP_PCB_LISTEN 8 +#define MEMP_NUM_TCP_SEG 16 +#define MEMP_NUM_TCPIP_MSG_API 8 +#define MEMP_NUM_TCPIP_MSG_INPKT 8 +#define MEMP_NUM_UDP_PCB 4 #define PBUF_POOL_SIZE 16 -#define MEMP_NUM_API_MSG MEMP_NUM_TCPIP_MSG_API - -#define MEMP_NUM_DNS_API_MSG MEMP_NUM_TCPIP_MSG_API - -#define MEMP_NUM_SOCKET_SETGETSOCKOPT_DATA MEMP_NUM_TCPIP_MSG_API - -#define MEMP_NUM_NETIFAPI_MSG MEMP_NUM_TCPIP_MSG_API +#define MEMP_NUM_SYS_TIMEOUT (LWIP_TCP + IP_REASSEMBLY + LWIP_ARP + (2*LWIP_DHCP) + LWIP_AUTOIP + LWIP_IGMP + LWIP_DNS + (PPP_SUPPORT*6*MEMP_NUM_PPP_PCB) + (LWIP_IPV6 ? (1 + LWIP_IPV6_REASS + LWIP_IPV6_MLD) : 0)) #define LWIP_ARP 1 diff --git a/src/sys/include/net/opt.h b/src/sys/include/net/opt.h index 042d369..3156c2c 100644 --- a/src/sys/include/net/opt.h +++ b/src/sys/include/net/opt.h @@ -1,4 +1,3 @@ - /* * Copyright (c) 2001-2004 Swedish Institute of Computer Science. * All rights reserved. @@ -63,11 +62,6 @@ * @ingroup lwip_opts */ - /* - ------------------------------------ - -------------- NO SYS -------------- - ------------------------------------ -*/ /** * NO_SYS==1: Use lwIP without OS-awareness (no thread, semaphores, mutexes or * mboxes). This means threaded APIs cannot be used (socket, netconn, @@ -79,7 +73,6 @@ #define NO_SYS 0 #endif - /** * LWIP_TIMERS==0: Drop support for sys_timeout and lwip-internal cyclic timers. * (the array of lwip-internal cyclic timers is still provided) @@ -121,10 +114,10 @@ #endif /* - ------------------------------------ - ----------- Core locking ----------- - ------------------------------------ -*/ + ------------------------------------ + ----------- Core locking ----------- + ------------------------------------ + */ /** * LWIP_MPU_COMPATIBLE: enables special memory management mechanism * which makes lwip able to work on MPU (Memory Protection Unit) system @@ -175,10 +168,10 @@ */ /* - ------------------------------------ - ---------- Memory options ---------- - ------------------------------------ -*/ + ------------------------------------ + ---------- Memory options ---------- + ------------------------------------ + */ /** * @defgroup lwip_opts_mem Heap and memory pools * @ingroup lwip_opts_infrastructure @@ -298,10 +291,10 @@ */ /* - ------------------------------------------------ - ---------- Internal Memory Pool Sizes ---------- - ------------------------------------------------ -*/ + ------------------------------------------------ + ---------- Internal Memory Pool Sizes ---------- + ------------------------------------------------ + */ /** * @defgroup lwip_opts_memp Internal memory pools * @ingroup lwip_opts_infrastructure @@ -493,10 +486,10 @@ */ /* - --------------------------------- - ---------- ARP options ---------- - --------------------------------- -*/ + --------------------------------- + ---------- ARP options ---------- + --------------------------------- + */ /** * @defgroup lwip_opts_arp ARP * @ingroup lwip_opts_ipv4 @@ -591,10 +584,10 @@ */ /* - -------------------------------- - ---------- IP options ---------- - -------------------------------- -*/ + -------------------------------- + ---------- IP options ---------- + -------------------------------- + */ /** * @defgroup lwip_opts_ipv4 IPv4 * @ingroup lwip_opts @@ -720,10 +713,10 @@ */ /* - ---------------------------------- - ---------- ICMP options ---------- - ---------------------------------- -*/ + ---------------------------------- + ---------- ICMP options ---------- + ---------------------------------- + */ /** * @defgroup lwip_opts_icmp ICMP * @ingroup lwip_opts_ipv4 @@ -762,10 +755,10 @@ */ /* - --------------------------------- - ---------- RAW options ---------- - --------------------------------- -*/ + --------------------------------- + ---------- RAW options ---------- + --------------------------------- + */ /** * @defgroup lwip_opts_raw RAW * @ingroup lwip_opts_callback @@ -789,10 +782,10 @@ */ /* - ---------------------------------- - ---------- DHCP options ---------- - ---------------------------------- -*/ + ---------------------------------- + ---------- DHCP options ---------- + ---------------------------------- + */ /** * @defgroup lwip_opts_dhcp DHCP * @ingroup lwip_opts_ipv4 @@ -838,7 +831,7 @@ * LWIP_DHCP_GETS_NTP==1: Request NTP servers with discover/select. For each * response packet, an callback is called, which has to be provided by the port: * void dhcp_set_ntp_servers(u8_t num_ntp_servers, ip_addr_t* ntp_server_addrs); -*/ + */ #if !defined LWIP_DHCP_GET_NTP_SRV || defined __DOXYGEN__ #define LWIP_DHCP_GET_NTP_SRV 0 #endif @@ -863,10 +856,10 @@ */ /* - ------------------------------------ - ---------- AUTOIP options ---------- - ------------------------------------ -*/ + ------------------------------------ + ---------- AUTOIP options ---------- + ------------------------------------ + */ /** * @defgroup lwip_opts_autoip AUTOIP * @ingroup lwip_opts_ipv4 @@ -907,10 +900,10 @@ */ /* - ---------------------------------- - ----- SNMP MIB2 support ----- - ---------------------------------- -*/ + ---------------------------------- + ----- SNMP MIB2 support ----- + ---------------------------------- + */ /** * @defgroup lwip_opts_mib2 SNMP MIB2 callbacks * @ingroup lwip_opts_infrastructure @@ -929,10 +922,10 @@ */ /* - ---------------------------------- - ----- Multicast/IGMP options ----- - ---------------------------------- -*/ + ---------------------------------- + ----- Multicast/IGMP options ----- + ---------------------------------- + */ /** * @defgroup lwip_opts_igmp IGMP * @ingroup lwip_opts_ipv4 @@ -961,10 +954,10 @@ */ /* - ---------------------------------- - ---------- DNS options ----------- - ---------------------------------- -*/ + ---------------------------------- + ---------- DNS options ----------- + ---------------------------------- + */ /** * @defgroup lwip_opts_dns DNS * @ingroup lwip_opts_callback @@ -1042,10 +1035,10 @@ */ /* - --------------------------------- - ---------- UDP options ---------- - --------------------------------- -*/ + --------------------------------- + ---------- UDP options ---------- + --------------------------------- + */ /** * @defgroup lwip_opts_udp UDP * @ingroup lwip_opts_callback @@ -1083,10 +1076,10 @@ */ /* - --------------------------------- - ---------- TCP options ---------- - --------------------------------- -*/ + --------------------------------- + ---------- TCP options ---------- + --------------------------------- + */ /** * @defgroup lwip_opts_tcp TCP * @ingroup lwip_opts_callback @@ -1162,7 +1155,6 @@ #define TCP_CALCULATE_EFF_SEND_MSS 1 #endif - /** * TCP_SND_BUF: TCP sender buffer space (bytes). * To achieve good performance, this should be at least 2 * TCP_MSS. @@ -1236,7 +1228,7 @@ * suggested values are: * * 0: Disable oversized allocation. Each tcp_write() allocates a new - pbuf (old behaviour). + pbuf (old behaviour). * 1: Allocate size-aligned pbufs with minimal excess. Use this if your * scatter-gather DMA requires aligned fragments. * 128: Limit the pbuf/memory overhead to 20%. @@ -1301,10 +1293,10 @@ */ /* - ---------------------------------- - ---------- Pbuf options ---------- - ---------------------------------- -*/ + ---------------------------------- + ---------- Pbuf options ---------- + ---------------------------------- + */ /** * @defgroup lwip_opts_pbuf PBUF * @ingroup lwip_opts @@ -1344,10 +1336,10 @@ */ /* - ------------------------------------------------ - ---------- Network Interfaces options ---------- - ------------------------------------------------ -*/ + ------------------------------------------------ + ---------- Network Interfaces options ---------- + ------------------------------------------------ + */ /** * @defgroup lwip_opts_netif NETIF * @ingroup lwip_opts @@ -1428,10 +1420,10 @@ */ /* - ------------------------------------ - ---------- LOOPIF options ---------- - ------------------------------------ -*/ + ------------------------------------ + ---------- LOOPIF options ---------- + ------------------------------------ + */ /** * @defgroup lwip_opts_loop Loopback interface * @ingroup lwip_opts_netif @@ -1490,10 +1482,10 @@ */ /* - ------------------------------------ - ---------- Thread options ---------- - ------------------------------------ -*/ + ------------------------------------ + ---------- Thread options ---------- + ------------------------------------ + */ /** * @defgroup lwip_opts_thread Threading * @ingroup lwip_opts_infrastructure @@ -1631,10 +1623,10 @@ */ /* - ---------------------------------------------- - ---------- Sequential layer options ---------- - ---------------------------------------------- -*/ + ---------------------------------------------- + ---------- Sequential layer options ---------- + ---------------------------------------------- + */ /** * @defgroup lwip_opts_netconn Netconn * @ingroup lwip_opts_threadsafe_apis @@ -1684,10 +1676,10 @@ */ /* - ------------------------------------ - ---------- Socket options ---------- - ------------------------------------ -*/ + ------------------------------------ + ---------- Socket options ---------- + ------------------------------------ + */ /** * @defgroup lwip_opts_socket Sockets * @ingroup lwip_opts_threadsafe_apis @@ -1830,10 +1822,10 @@ */ /* - ---------------------------------------- - ---------- Statistics options ---------- - ---------------------------------------- -*/ + ---------------------------------------- + ---------- Statistics options ---------- + ---------------------------------------- + */ /** * @defgroup lwip_opts_stats Statistics * @ingroup lwip_opts_debug @@ -2004,10 +1996,10 @@ */ /* - -------------------------------------- - ---------- Checksum options ---------- - -------------------------------------- -*/ + -------------------------------------- + ---------- Checksum options ---------- + -------------------------------------- + */ /** * @defgroup lwip_opts_checksum Checksum * @ingroup lwip_opts_infrastructure @@ -2104,10 +2096,10 @@ */ /* - --------------------------------------- - ---------- IPv6 options --------------- - --------------------------------------- -*/ + --------------------------------------- + ---------- IPv6 options --------------- + --------------------------------------- + */ /** * @defgroup lwip_opts_ipv6 IPv6 * @ingroup lwip_opts @@ -2368,10 +2360,10 @@ #endif /* - --------------------------------------- - ---------- Hook options --------------- - --------------------------------------- -*/ + --------------------------------------- + ---------- Hook options --------------- + --------------------------------------- + */ /** * @defgroup lwip_opts_hooks Hooks @@ -2457,7 +2449,7 @@ * This function is meant to implement advanced IPv4 routing together with * LWIP_HOOK_IP4_ROUTE(). The actual routing/gateway table implementation is * not part of lwIP but can e.g. be hidden in the netif's state argument. -*/ + */ #ifdef __DOXYGEN__ #define LWIP_HOOK_ETHARP_GET_GW(netif, dest) #endif @@ -2500,7 +2492,7 @@ * This function is meant to implement advanced IPv6 routing together with * LWIP_HOOK_IP6_ROUTE(). The actual routing/gateway table implementation is * not part of lwIP but can e.g. be hidden in the netif's state argument. -*/ + */ #ifdef __DOXYGEN__ #define LWIP_HOOK_ND6_GET_GW(netif, dest) #endif @@ -2563,10 +2555,10 @@ */ /* - --------------------------------------- - ---------- Debugging options ---------- - --------------------------------------- -*/ + --------------------------------------- + ---------- Debugging options ---------- + --------------------------------------- + */ /** * @defgroup lwip_opts_debugmsg Debug messages * @ingroup lwip_opts_debug @@ -2820,10 +2812,10 @@ */ /* - -------------------------------------------------- - ---------- Performance tracking options ---------- - -------------------------------------------------- -*/ + -------------------------------------------------- + ---------- Performance tracking options ---------- + -------------------------------------------------- + */ /** * @defgroup lwip_opts_perf Performance * @ingroup lwip_opts_debug diff --git a/src/sys/include/net/sys.h b/src/sys/include/net/sys.h index 1339f27..622e6af 100644 --- a/src/sys/include/net/sys.h +++ b/src/sys/include/net/sys.h @@ -1,8 +1,3 @@ -/** - * @file - * OS abstraction layer - */ - /* * Copyright (c) 2001-2004 Swedish Institute of Computer Science. * All rights reserved. @@ -34,8 +29,8 @@ * Author: Adam Dunkels */ -#ifndef LWIP_HDR_SYS_H -#define LWIP_HDR_SYS_H +#ifndef NET_HDR_SYS_H +#define NET_HDR_SYS_H #include #include @@ -46,42 +41,7 @@ #if NO_SYS -/* For a totally minimal and standalone system, we provide null definitions of the sys_ functions. */ -typedef u8_t sys_sem_t; -typedef u8_t sys_mutex_t; -typedef u8_t sys_mbox_t; - -#define sys_sem_new(s, c) ERR_OK -#define sys_sem_signal(s) -#define sys_sem_wait(s) -#define sys_arch_sem_wait(s,t) -#define sys_sem_free(s) -#define sys_sem_valid(s) 0 -#define sys_sem_valid_val(s) 0 -#define sys_sem_set_invalid(s) -#define sys_sem_set_invalid_val(s) -#define sys_mutex_new(mu) ERR_OK -#define sys_mutex_lock(mu) -#define sys_mutex_unlock(mu) -#define sys_mutex_free(mu) -#define sys_mutex_valid(mu) 0 -#define sys_mutex_set_invalid(mu) -#define sys_mbox_new(m, s) ERR_OK -#define sys_mbox_fetch(m,d) -#define sys_mbox_tryfetch(m,d) -#define sys_mbox_post(m,d) -#define sys_mbox_trypost(m,d) -#define sys_mbox_free(m) -#define sys_mbox_valid(m) -#define sys_mbox_valid_val(m) -#define sys_mbox_set_invalid(m) -#define sys_mbox_set_invalid_val(m) - -#define sys_thread_new(n,t,a,s,p) - -#define sys_msleep(t) - -#else /* NO_SYS */ +#else /* NO_SYS */ - Mr O /** Return code for timeouts from sys_arch_mbox_fetch and sys_arch_sem_wait */ #define SYS_ARCH_TIMEOUT 0xffffffffUL @@ -452,4 +412,4 @@ } #endif -#endif /* LWIP_HDR_SYS_H */ +#endif /* NET_HDR_SYS_H */ diff --git a/src/sys/net/net/sys_arch.c b/src/sys/net/net/sys_arch.c index 0525206..2caa334 100644 --- a/src/sys/net/net/sys_arch.c +++ b/src/sys/net/net/sys_arch.c @@ -1,36 +1,3 @@ -/* - * Copyright (c) 2001, 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. Neither the name of the Institute 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 INSTITUTE 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 INSTITUTE 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. - * - * This file is part of the lwIP TCP/IP stack. - * - * Author: Adam Dunkels - */ - #include #include #include @@ -46,6 +13,7 @@ #include +/* Get Definitions For These */ #define ERR_NOT_READY 0 #define ERR_TIMED_OUT 1 #define INFINITE_TIME 0 @@ -54,19 +22,91 @@ static spinLock_t netThreadSpinlock = SPIN_LOCK_INITIALIZER; static struct sys_thread *threads = 0x0; -#ifdef _BALLS -struct sys_mbox *sys_mbox_new() { - struct sys_mbox *mbox; - - mbox = kmalloc(sizeof(struct sys_mbox)); - memset(mbox, 0x0, sizeof(struct sys_mbox)); - mbox->first = mbox->last = 0; - mbox->mail = sys_sem_new_(0); - mbox->mutex = sys_sem_new_(1); - - return (mbox); +/* sys_arch layer initializer */ +void sys_init() { + struct timezone tz; + gettimeofday(&starttime, &tz); } -#endif + +/* Create a new semaphore */ +err_t sys_sem_new(sys_sem_t *sem, uint8_t count) { + sem->signaled = count; + + ubthread_cond_init(&(sem->cond), NULL); + ubthread_mutex_init(&(sem->mutex), NULL); + + return (ERR_OK); +} + +/* Deallocate semaphore */ +void sys_sem_free(sys_sem_t *sem) { + ubthread_cond_destroy(&(sem->cond)); + ubthread_mutex_destroy(&(sem->mutex)); + //MrOlsen maybe not here + kfree(sem); +} + +/* Signal semaphore */ +void sys_sem_signal(sys_sem_t *sem) { + ubthread_mutex_lock(&(sem->mutex)); + + sem->signaled++; + if (sem->signaled > 1) + sem->signaled = 1; + + ubthread_cond_signal(&(sem->cond)); + ubthread_mutex_unlock(&(sem->mutex)); +} + +uint32_t sys_arch_sem_wait(struct sys_sem *sem, uint32_t timeout) { + uint32_t time = sys_now(); + + ubthread_mutex_lock(&(sem->mutex)); + + while (sem->signaled <= 0) { + if (timeout > 0) { + time = cond_wait(&(sem->cond), &(sem->mutex), timeout); + if (time == 0) { + ubthread_mutex_unlock(&(sem->mutex)); + return(0); + } + } + else { + cond_wait(&(sem->cond), &(sem->mutex), 0); + } + } + sem->signaled--; + ubthread_mutex_lock(&(sem->mutex)); + return (sys_now() - time); +} + +int sys_sem_valid(sys_sem_t *sem) { + if (sem == 0) + return 1; + else + return 0; +} + +void sys_sem_set_invalid(sys_sem_t *sem) { + kprintf("NEED TO DO THIS"); +} + +err_t sys_mutex_new(sys_mutex_t *mutex) { + ubthread_mutex_init(&(mutex->mutex), NULL); + return ERR_OK; +} + +void sys_mutex_free(sys_mutex_t *mutex) { + ubthread_mutex_destroy(&(mutex->mutex)); +} + +void sys_mutex_lock(sys_mutex_t *mutex) { + ubthread_mutex_lock(&(mutex->mutex)) ; +} + +void sys_mutex_unlock(sys_mutex_t *mutex) { + ubthread_mutex_unlock(&(mutex->mutex)) ; +} err_t sys_mbox_new(sys_mbox_t *mbox, int size) { LWIP_ASSERT("mbox null", mbox); @@ -87,6 +127,98 @@ return (ERR_OK); } +void sys_mbox_free(sys_mbox_t *mbox) { + kfree(mbox->queue); + mbox->queue = NULL; +} + +void sys_mbox_post(sys_mbox_t * mbox, void *msg) { + sem_wait(&mbox->empty); + mutex_acquire(&mbox->lock); + + mbox->queue[mbox->head] = msg; + mbox->head = (mbox->head + 1) % mbox->size; + + mutex_release(&mbox->lock); + sem_post(&mbox->full); +} + +err_t sys_mbox_trypost(sys_mbox_t * mbox, void *msg) { +uint32_t res; + +res = sem_trywait(&mbox->empty); +if (res == ERR_NOT_READY) +return ERR_TIMEOUT; + +mutex_acquire(&mbox->lock); + +mbox->queue[mbox->head] = msg; +mbox->head = (mbox->head + 1) % mbox->size; + +mutex_release(&mbox->lock); +sem_post(&mbox->full); + +return ERR_OK; +} + +uint32_t sys_arch_mbox_fetch(sys_mbox_t *mbox, void **msg, uint32_t timeout) { + + //status_t res; + uint32_t res; + + //lk_time_t start = current_time(); + + uint32_t start = sys_now(); + + res = sem_timedwait(&mbox->full, timeout ? timeout : INFINITE_TIME); + if (res == ERR_TIMED_OUT) { + //LTRACE_EXIT; + return SYS_ARCH_TIMEOUT; //timeout ? SYS_ARCH_TIMEOUT : 0; + } + + mutex_acquire(&mbox->lock); + + *msg = mbox->queue[mbox->tail]; + mbox->tail = (mbox->tail + 1) % mbox->size; + + mutex_release(&mbox->lock); + sem_post(&mbox->empty); + + return sys_now() - start; +} + +uint32_t sys_arch_mbox_tryfetch(sys_mbox_t * mbox, void **msg) { +//LTRACE_ENTRY; + +//status_t res; + uint32_t res; + +res = sem_trywait(&mbox->full); +if (res == ERR_NOT_READY) { +//LTRACE_EXIT; +return SYS_MBOX_EMPTY; +} + +mutex_acquire(&mbox->lock); + +*msg = mbox->queue[mbox->tail]; +mbox->tail = (mbox->tail + 1) % mbox->size; + +mutex_release(&mbox->lock); +sem_post(&mbox->empty); + +//LTRACE_EXIT; +return 0; +} + + +int sys_mbox_valid(sys_mbox_t *mbox) { + return mbox->queue != NULL; +} + +void sys_mbox_set_invalid(sys_mbox_t *mbox) { +} + sys_thread_t sys_thread_new(const char *name, void (*thread)(void *arg), void *arg, int stacksize, int prio) { //void sys_thread_new(void (*function)(void), void *arg) { struct sys_thread *new_thread = 0x0; @@ -124,55 +256,125 @@ return (new_thread); } -err_t sys_sem_new(sys_sem_t *sem, uint8_t count) { - //struct sys_sem *sem; +/* OLD */ +static uint16_t cond_wait(ubthread_cond_t *cond, ubthread_mutex_t *mutex, uint16_t timeout); - sem = kmalloc(sizeof(struct sys_sem)); - memset(sem, 0x0, sizeof(struct sys_sem)); - sem->c = count; +struct thread_start_param { + struct sys_thread *thread; + void (*function)(void *); + void *arg; +}; - ubthread_cond_init(&(sem->cond), NULL); - ubthread_mutex_init(&(sem->mutex), NULL); - return (ERR_OK); -} +static uint16_t cond_wait(ubthread_cond_t *cond, ubthread_mutex_t *mutex, uint16_t timeout) { + unsigned int tdiff; + unsigned long sec, usec; + struct timeval rtime1, rtime2; + struct timespec ts; + struct timezone tz; + int retval; -void sys_sem_free(struct sys_sem *sem) { - if (sem != SYS_SEM_NULL) { - ubthread_cond_destroy(&(sem->cond)); - ubthread_mutex_destroy(&(sem->mutex)); - kfree(sem); + if (timeout > 0) { + /* Get a timestamp and add the timeout value. */ + gettimeofday(&rtime1, &tz); + 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; + + retval = ubthread_cond_timedwait(cond, mutex, &ts); + if (retval == ETIMEDOUT) { + return 0; + } + else { + /* Calculate for how long we waited for the cond. */ + gettimeofday(&rtime2, &tz); + tdiff = (rtime2.tv_sec - rtime1.tv_sec) * 1000 + (rtime2.tv_usec - rtime1.tv_usec) / 1000; + if (tdiff == 0) { + return 1; + } + return tdiff; + } + } + else { + ubthread_cond_wait(cond, mutex); + return 0; } } -uint32_t sys_arch_mbox_fetch(sys_mbox_t *mbox, void **msg, uint32_t timeout) { - //status_t res; - uint32_t res; - - //lk_time_t start = current_time(); - - uint32_t start = sys_now(); - - res = sem_timedwait(&mbox->full, timeout ? timeout : INFINITE_TIME); - if (res == ERR_TIMED_OUT) { - //LTRACE_EXIT; - return SYS_ARCH_TIMEOUT; //timeout ? SYS_ARCH_TIMEOUT : 0; +static struct sys_thread *current_thread(void) { + struct sys_thread *st; + kTask_t *pt; + pt = ubthread_self(); + //kprintf("SL: %i-0x%X]", _current->id, &netThreadSpinlock); + spinLock(&netThreadSpinlock); + for (st = threads; st != NULL; st = st->next) { + if (st->ubthread == pt) { + //kprintf("SUL: %i-0x%X]", _current->id, &netThreadSpinlock); + spinUnlock(&netThreadSpinlock); + return st; + } } + //kprintf("SUL: %i-0x%X]", _current->id, &netThreadSpinlock); + spinUnlock(&netThreadSpinlock); + kprintf("sys: current_thread: could not find current thread!\n"); + kprintf("This is due to a race condition in the LinuxThreads\n"); + kprintf("ubthreads implementation. Start the program again.\n"); - mutex_acquire(&mbox->lock); + kpanic("ABORT"); + return (0x0); +} - *msg = mbox->queue[mbox->tail]; - mbox->tail = (mbox->tail + 1) % mbox->size; +struct sys_timeouts *sys_arch_timeouts(void) { + struct sys_thread *thread; + thread = current_thread(); + return (&thread->timeouts); +} - mutex_release(&mbox->lock); - sem_post(&mbox->empty); -; - return sys_now() - start; +unsigned long sys_unix_now() { + struct timeval tv; + struct timezone tz; + long sec, usec; + unsigned long msec; + + gettimeofday(&tv, &tz); + + sec = tv.tv_sec - starttime.tv_sec; + usec = tv.tv_usec - starttime.tv_usec; + msec = sec * 1000 + usec / 1000; + return msec; +} + +uint32_t sys_now() { + return (sys_unix_now()); } #ifdef _BALLS -uint32_t sys_arch_mbox_fetch_dead(struct sys_mbox *mbox, void **msg, uint32_t timeout) { + +#define UMAX(a, b) ((a) > (b) ? (a) : (b)) + +struct sys_mbox_msg { + struct sys_mbox_msg *next; + void *msg; +}; + +struct sys_mbox *sys_mbox_new_BALLS() { + struct sys_mbox *mbox; + + mbox = kmalloc(sizeof(struct sys_mbox)); + memset(mbox, 0x0, sizeof(struct sys_mbox)); + mbox->first = mbox->last = 0; + mbox->mail = sys_sem_new_(0); + mbox->mutex = sys_sem_new_(1); + + return (mbox); +} + +uint32_t sys_arch_mbox_fetch_BALLS(struct sys_mbox *mbox, void **msg, uint32_t timeout) { uint16_t time = 1; /* The mutex lock is quick so we don't bother with the timeout @@ -224,59 +426,8 @@ return (time); } -#endif -//#define UMAX(a, b) ((a) > (b) ? (a) : (b)) - -struct sys_mbox_msg { - struct sys_mbox_msg *next; - void *msg; -}; - -#define SYS_MBOX_SIZE 100 - -/* - struct sys_mbox { - uint16_t first, last; - void *msgs[SYS_MBOX_SIZE]; - struct sys_sem *mail; - struct sys_sem *mutex; - }; - */ - -/* - struct sys_sem { - unsigned int c; - ubthread_cond_t cond; - ubthread_mutex_t mutex; - }; - */ - -static uint16_t cond_wait(ubthread_cond_t *cond, ubthread_mutex_t *mutex, uint16_t timeout); - -struct thread_start_param { - struct sys_thread *thread; - void (*function)(void *); - void *arg; -}; - -/* - static void *thread_start(void *arg) { - struct thread_start_param *tp = arg; - tp->thread->ubthread = ubthread_self(); - tp->function(tp->arg); - kfree(tp); - return(NULL); - } - */ - -void sys_mbox_free(sys_mbox_t *mbox) { - kfree(mbox->queue); - mbox->queue = NULL; -} - -#ifdef _BALLS -void sys_mbox_free(struct sys_mbox *mbox) { +void sys_mbox_free_BALLS(struct sys_mbox *mbox) { if (mbox != SYS_MBOX_NULL) { sys_sem_wait(mbox->mutex); sys_sem_free_(mbox->mail); @@ -285,21 +436,8 @@ kfree(mbox); } } -#endif -void sys_mbox_post(sys_mbox_t * mbox, void *msg) { - sem_wait(&mbox->empty); - mutex_acquire(&mbox->lock); - - mbox->queue[mbox->head] = msg; - mbox->head = (mbox->head + 1) % mbox->size; - - mutex_release(&mbox->lock); - sem_post(&mbox->full); -} - -#ifdef _BALLS -void sys_mbox_post(struct sys_mbox *mbox, void *msg) { +void sys_mbox_post_BALLS(struct sys_mbox *mbox, void *msg) { uInt8 first; sys_sem_wait(mbox->mutex); @@ -324,189 +462,3 @@ sys_sem_signal(mbox->mutex); } #endif - -static uint16_t cond_wait(ubthread_cond_t *cond, ubthread_mutex_t *mutex, uint16_t timeout) { - unsigned int tdiff; - unsigned long sec, usec; - struct timeval rtime1, rtime2; - struct timespec ts; - struct timezone tz; - int retval; - - if (timeout > 0) { - /* Get a timestamp and add the timeout value. */ - gettimeofday(&rtime1, &tz); - 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; - - retval = ubthread_cond_timedwait(cond, mutex, &ts); - if (retval == ETIMEDOUT) { - return 0; - } - else { - /* Calculate for how long we waited for the cond. */ - gettimeofday(&rtime2, &tz); - tdiff = (rtime2.tv_sec - rtime1.tv_sec) * 1000 + (rtime2.tv_usec - rtime1.tv_usec) / 1000; - if (tdiff == 0) { - return 1; - } - return tdiff; - } - } - else { - ubthread_cond_wait(cond, mutex); - return 0; - } -} - -uint32_t sys_arch_sem_wait(struct sys_sem *sem, uint32_t timeout) { - uint16_t time = 1; - ubthread_mutex_lock(&(sem->mutex)); - while (sem->c <= 0) { - if (timeout > 0) { - time = cond_wait(&(sem->cond), &(sem->mutex), timeout); - if (time == 0) { - ubthread_mutex_unlock(&(sem->mutex)); - return 0; - } - } - else { - cond_wait(&(sem->cond), &(sem->mutex), 0); - } - } - sem->c--; - ubthread_mutex_unlock(&(sem->mutex)); - return (time); -} - -void sys_sem_signal(struct sys_sem *sem) { - //kprintf("HERE: %i:0x%X", _current->id,&(sem->mutex)); - - ubthread_mutex_lock(&(sem->mutex)); - - sem->c++; - if (sem->c > 1) - sem->c = 1; - - ubthread_cond_signal(&(sem->cond)); - ubthread_mutex_unlock(&(sem->mutex)); -} - -void sys_init() { - struct timezone tz; - gettimeofday(&starttime, &tz); -} - -static struct sys_thread *current_thread(void) { - struct sys_thread *st; - kTask_t *pt; - pt = ubthread_self(); - //kprintf("SL: %i-0x%X]", _current->id, &netThreadSpinlock); - spinLock(&netThreadSpinlock); - for (st = threads; st != NULL; st = st->next) { - if (st->ubthread == pt) { - //kprintf("SUL: %i-0x%X]", _current->id, &netThreadSpinlock); - spinUnlock(&netThreadSpinlock); - return st; - } - } - //kprintf("SUL: %i-0x%X]", _current->id, &netThreadSpinlock); - spinUnlock(&netThreadSpinlock); - kprintf("sys: current_thread: could not find current thread!\n"); - kprintf("This is due to a race condition in the LinuxThreads\n"); - kprintf("ubthreads implementation. Start the program again.\n"); - - kpanic("ABORT"); - return (0x0); -} - -struct sys_timeouts *sys_arch_timeouts(void) { - struct sys_thread *thread; - thread = current_thread(); - return (&thread->timeouts); -} - -unsigned long sys_unix_now() { - struct timeval tv; - struct timezone tz; - long sec, usec; - unsigned long msec; - - gettimeofday(&tv, &tz); - - sec = tv.tv_sec - starttime.tv_sec; - usec = tv.tv_usec - starttime.tv_usec; - msec = sec * 1000 + usec / 1000; - return msec; -} - -uint32_t sys_now() { - return (sys_unix_now()); -} - -int sys_mbox_valid(sys_mbox_t *mbox) { - return mbox->queue != NULL; -} - -err_t sys_mbox_trypost(sys_mbox_t * mbox, void *msg) { - //status_t res; - uint32_t res; - - res = sem_trywait(&mbox->empty); - if (res == ERR_NOT_READY) - return ERR_TIMEOUT; - - mutex_acquire(&mbox->lock); - - mbox->queue[mbox->head] = msg; - mbox->head = (mbox->head + 1) % mbox->size; - - mutex_release(&mbox->lock); - sem_post(&mbox->full); - - return ERR_OK; -} - - -int sys_sem_valid(sys_sem_t *sem) -{ - return 1; -} - -void sys_sem_set_invalid(sys_sem_t *sem) -{ -} - -void sys_mbox_set_invalid(sys_mbox_t *mbox) -{ -} - -uint32_t sys_arch_mbox_tryfetch(sys_mbox_t * mbox, void **msg) -{ - //LTRACE_ENTRY; - - //status_t res; - uint32_t res; - - res = sem_trywait(&mbox->full); - if (res == ERR_NOT_READY) { - //LTRACE_EXIT; - return SYS_MBOX_EMPTY; - } - - mutex_acquire(&mbox->lock); - - *msg = mbox->queue[mbox->tail]; - mbox->tail = (mbox->tail + 1) % mbox->size; - - mutex_release(&mbox->lock); - sem_post(&mbox->empty); - - //LTRACE_EXIT; - return 0; -}