⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sys_rabbit2000.c.save

📁 lwIP-Softools-11Jul2002-alpha
💻 SAVE
字号:
/* * Functions to start the ethernet system. * * (C) 2002 Compendium Technologies, Inc. * (C) 2002 Softools, Inc. * All rights reserved. * * This software is the proprietary information of Compendium Technologies, Inc. * and Softools, Inc. Use is subject to license terms. */#include <time.h>#include "lwip/sys.h"#include "lwip/def.h"#include "lwip/mem.h"#include "semaphore.h"#include "lwipopts.h"#define DEBUG/** * There are no independent threads. */struct sys_timeouts timeouts;#define MAX_SEMAPHORES (MEMP_NUM_NETCONN * 2 + SEMAPHORE_SLOTS)/** * Allocate one semaphore for possible connection in this system. */semaphore lwip_semaphores[MAX_SEMAPHORES];/** * Create a new mailbox. */sys_mbox_tsys_mbox_new(void){    sys_mbox_t mbox = mem_malloc(sizeof (sys_mbox));    mbox->first = mbox->last = 0;    mbox->numMessages = 0;    // Need a semaphore to signal.    mbox->mail = sys_sem_new(0);    return mbox;}/** * Free a mailbox. */voidsys_mbox_free(sys_mbox_t mbox){    if (mbox != SYS_MBOX_NULL)    {        mem_free(mbox);    }}/** * Post a message to a mailbox. */voidsys_mbox_post(sys_mbox_t mbox, void *data){    int fromEmpty = 0;    register char last;    ipset3();#ifdef DEBUG    //puts("\nsys_mbox_post: <");#endif    if (mbox->numMessages < MESSAGE_BOX_SIZE)    {        last = mbox->last;        mbox->msgs[last] = data;        // Set flag for mailbox going from empty to something in it.        fromEmpty = (mbox->numMessages == 0);        last++;        mbox->numMessages++;        mbox->last = (last == MESSAGE_BOX_SIZE ?  0 : last);        ipres();#ifdef DEBUG        //puts(">");#endif        if (fromEmpty)        {            sys_sem_signal(mbox->mail);#ifdef DEBUG            puts("signal\n");#endif        }    }    else    {        // Handle overflows by ignoring messages        ipres();#ifdef DEBUG        //puts(">");#endif    }}/** * Fetch a message from the mailbox. Wait only the desired timeout for * one to show up. A return value of 1 indicates that the message * was fetched. A data parameter value of NULL indicates that the caller * doesn't care about the message being returned; it is just discarded in * this case. */u16_t sys_arch_mbox_fetch(sys_mbox_t mbox, void **data, u16_t timeout){    ipset3();#ifdef DEBUG    //puts("\nsys_arch_mbox_fetch: <");#endif    if (mbox->numMessages == 0)    {        ipres();#ifdef DEBUG        //puts(">");#endif        if (timeout == 0 ||            sys_arch_sem_wait(mbox->mail, timeout) == 0)        {            // Nothing in the mailbox after the timeout            return 0;        }        ipset3();#ifdef DEBUG        printf("%d messages in mbox %p\n", mbox->numMessages, mbox);        //puts("<");#endif    }    // Only return the message if there's somewhere to put, otherwise    // it just gets removed.    if (data != NULL)    {        *data = mbox->msgs[mbox->first];    }    if (mbox->first == MESSAGE_BOX_SIZE - 1)    {        mbox->first = 0;    }    else    {        mbox->first++;    }    mbox->numMessages--;    ipres();#ifdef DEBUG    //puts(">");#endif    // Got a message, indicate so.    return 1;}/** * Allocate a new semaphore. */sys_sem_tsys_sem_new(u8_t count){    int i;    for (i = 0; i < MAX_SEMAPHORES; i++)    {        ipset3();#ifdef DEBUG        //puts("\nsys_sem_new: <");#endif        if (lwip_semaphores[i] == SEMAPHORE_UNALLOCATED)        {            lwip_semaphores[i] = count;            ipres();            return &lwip_semaphores[i];        }        ipres();#ifdef DEBUG        //puts(">");#endif    }    return 0;}/** * Wait for a semaphore, or the specified number of milliseconds. This * function as implemented just waits the number of milliseconds desired. * This returns zero if it times out, one if the semaphore is signaled * while waiting. */u16_tsys_arch_sem_wait(sys_sem_t sem, u16_t timeout){    int tries = timeout / SEMAPHORE_TEST_TIMEOUT_MILLIS;    int remainder = timeout % SEMAPHORE_TEST_TIMEOUT_MILLIS;    ipset3();#ifdef DEBUG    //puts("\nsys_arch_sem_wait: <");#endif    if (*sem != 0)    {       // Ready right now.       (*sem)--;       ipres();#ifdef DEBUG        //puts(">");#endif       return 1;    }    else    {        int i;        // Wait -- this is a blocking wait.        for (i = 0; i <= tries; i++)        {            ipres();#ifdef DEBUG            //puts("block\n");            //puts(">");#endif            delay(i == tries ? remainder : SEMAPHORE_TEST_TIMEOUT_MILLIS);            ipset3();#ifdef DEBUG            //puts("<");#endif            if (*sem != 0)            {                (*sem)--;                ipres();#ifdef DEBUG                printf("over %p\n", sem);                //puts(">");#endif                return 1;            }        }        ipres();#ifdef DEBUG        //puts(">");#endif        // Timed out        return 0;    }}/** * Signal a semaphore. This will wake up a thread that's waiting if there is * one. Be careful to disable interrupts so this operation is atomic. */voidsys_sem_signal(sys_sem_t sem){    ipset3();    (*sem)++;    ipres();}/** * Free up a semaphore that is not longer needed. */voidsys_sem_free(sys_sem_t sem){    ipset3();    *sem = SEMAPHORE_UNALLOCATED;    ipres();}/*-----------------------------------------------------------------------------------*/voidsys_init(void){    int i;    // Need to allocate system semaphores here, if they haven't already been    // allocated.    for (i = 0; i < MAX_SEMAPHORES; i++)    {        lwip_semaphores[i] = SEMAPHORE_UNALLOCATED;    }    // This is unchanged -- this is a no thread case so there is only one    // set of timeouts.    // TODO: Account for interrupt as a different thread.    timeouts.next = NULL;}/** * Only one set of timeouts to return for the single thread. */struct sys_timeouts *sys_arch_timeouts(void){    // TODO: Account for interrupt as a different thread.    return &timeouts;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -