Back to home page

Quest Cross Reference

 
 

    


Warning, cross-references for /kernel/lwip/core/sys.c need to be fixed.

0001 /**
0002  * @file
0003  * lwIP Operating System abstraction
0004  *
0005  */
0006 
0007 /*
0008  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
0009  * All rights reserved.
0010  *
0011  * Redistribution and use in source and binary forms, with or without modification,
0012  * are permitted provided that the following conditions are met:
0013  *
0014  * 1. Redistributions of source code must retain the above copyright notice,
0015  *    this list of conditions and the following disclaimer.
0016  * 2. Redistributions in binary form must reproduce the above copyright notice,
0017  *    this list of conditions and the following disclaimer in the documentation
0018  *    and/or other materials provided with the distribution.
0019  * 3. The name of the author may not be used to endorse or promote products
0020  *    derived from this software without specific prior written permission.
0021  *
0022  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
0023  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
0024  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
0025  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
0026  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
0027  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0028  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0029  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
0030  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
0031  * OF SUCH DAMAGE.
0032  *
0033  * This file is part of the lwIP TCP/IP stack.
0034  *
0035  * Author: Adam Dunkels <adam@sics.se>
0036  *
0037  */
0038 
0039 #include "lwip/opt.h"
0040 
0041 #if (NO_SYS == 0) /* don't build if not configured for use in lwipopts.h */
0042 
0043 #include "lwip/sys.h"
0044 #include "lwip/def.h"
0045 #include "lwip/memp.h"
0046 #include "lwip/tcpip.h"
0047 
0048 /**
0049  * Struct used for sys_sem_wait_timeout() to tell wether the time
0050  * has run out or the semaphore has really become available.
0051  */
0052 struct sswt_cb
0053 {
0054   s16_t timeflag;
0055   sys_sem_t *psem;
0056 };
0057 
0058 /**
0059  * Wait (forever) for a message to arrive in an mbox.
0060  * While waiting, timeouts (for this thread) are processed.
0061  *
0062  * @param mbox the mbox to fetch the message from
0063  * @param msg the place to store the message
0064  */
0065 void
0066 sys_mbox_fetch(sys_mbox_t mbox, void **msg)
0067 {
0068   u32_t time_needed;
0069   struct sys_timeouts *timeouts;
0070   struct sys_timeo *tmptimeout;
0071   sys_timeout_handler h;
0072   void *arg;
0073 
0074  again:
0075   timeouts = sys_arch_timeouts();
0076 
0077   if (!timeouts || !timeouts->next) {
0078     UNLOCK_TCPIP_CORE();
0079     time_needed = sys_arch_mbox_fetch(mbox, msg, 0);
0080     LOCK_TCPIP_CORE();
0081   } else {
0082     if (timeouts->next->time > 0) {
0083       UNLOCK_TCPIP_CORE();
0084       time_needed = sys_arch_mbox_fetch(mbox, msg, timeouts->next->time);
0085       LOCK_TCPIP_CORE();
0086     } else {
0087       time_needed = SYS_ARCH_TIMEOUT;
0088     }
0089 
0090     if (time_needed == SYS_ARCH_TIMEOUT) {
0091       /* If time == SYS_ARCH_TIMEOUT, a timeout occured before a message
0092          could be fetched. We should now call the timeout handler and
0093          deallocate the memory allocated for the timeout. */
0094       tmptimeout = timeouts->next;
0095       timeouts->next = tmptimeout->next;
0096       h   = tmptimeout->h;
0097       arg = tmptimeout->arg;
0098       memp_free(MEMP_SYS_TIMEOUT, tmptimeout);
0099       if (h != NULL) {
0100         LWIP_DEBUGF(SYS_DEBUG, ("smf calling h=%p(%p)\n", *(void**)&h, arg));
0101         h(arg);
0102       }
0103 
0104       /* We try again to fetch a message from the mbox. */
0105       goto again;
0106     } else {
0107       /* If time != SYS_ARCH_TIMEOUT, a message was received before the timeout
0108          occured. The time variable is set to the number of
0109          milliseconds we waited for the message. */
0110       if (time_needed < timeouts->next->time) {
0111         timeouts->next->time -= time_needed;
0112       } else {
0113         timeouts->next->time = 0;
0114       }
0115     }
0116   }
0117 }
0118 
0119 /**
0120  * Wait (forever) for a semaphore to become available.
0121  * While waiting, timeouts (for this thread) are processed.
0122  *
0123  * @param sem semaphore to wait for
0124  */
0125 void
0126 sys_sem_wait(sys_sem_t sem)
0127 {
0128   u32_t time_needed;
0129   struct sys_timeouts *timeouts;
0130   struct sys_timeo *tmptimeout;
0131   sys_timeout_handler h;
0132   void *arg;
0133 
0134  again:
0135 
0136   timeouts = sys_arch_timeouts();
0137 
0138   if (!timeouts || !timeouts->next) {
0139     sys_arch_sem_wait(sem, 0);
0140   } else {
0141     if (timeouts->next->time > 0) {
0142       time_needed = sys_arch_sem_wait(sem, timeouts->next->time);
0143     } else {
0144       time_needed = SYS_ARCH_TIMEOUT;
0145     }
0146 
0147     if (time_needed == SYS_ARCH_TIMEOUT) {
0148       /* If time == SYS_ARCH_TIMEOUT, a timeout occured before a message
0149         could be fetched. We should now call the timeout handler and
0150         deallocate the memory allocated for the timeout. */
0151       tmptimeout = timeouts->next;
0152       timeouts->next = tmptimeout->next;
0153       h = tmptimeout->h;
0154       arg = tmptimeout->arg;
0155       memp_free(MEMP_SYS_TIMEOUT, tmptimeout);
0156       if (h != NULL) {
0157         LWIP_DEBUGF(SYS_DEBUG, ("ssw h=%p(%p)\n", *(void**)&h, (void *)arg));
0158         h(arg);
0159       }
0160 
0161       /* We try again to fetch a message from the mbox. */
0162       goto again;
0163     } else {
0164       /* If time != SYS_ARCH_TIMEOUT, a message was received before the timeout
0165          occured. The time variable is set to the number of
0166          milliseconds we waited for the message. */
0167       if (time_needed < timeouts->next->time) {
0168         timeouts->next->time -= time_needed;
0169       } else {
0170         timeouts->next->time = 0;
0171       }
0172     }
0173   }
0174 }
0175 
0176 /**
0177  * Create a one-shot timer (aka timeout). Timeouts are processed in the
0178  * following cases:
0179  * - while waiting for a message using sys_mbox_fetch()
0180  * - while waiting for a semaphore using sys_sem_wait() or sys_sem_wait_timeout()
0181  * - while sleeping using the inbuilt sys_msleep()
0182  *
0183  * @param msecs time in milliseconds after that the timer should expire
0184  * @param h callback function to call when msecs have elapsed
0185  * @param arg argument to pass to the callback function
0186  */
0187 void
0188 sys_timeout(u32_t msecs, sys_timeout_handler h, void *arg)
0189 {
0190   struct sys_timeouts *timeouts;
0191   struct sys_timeo *timeout, *t;
0192 
0193   timeout = memp_malloc(MEMP_SYS_TIMEOUT);
0194   if (timeout == NULL) {
0195     LWIP_ASSERT("sys_timeout: timeout != NULL", timeout != NULL);
0196     return;
0197   }
0198   timeout->next = NULL;
0199   timeout->h = h;
0200   timeout->arg = arg;
0201   timeout->time = msecs;
0202 
0203   timeouts = sys_arch_timeouts();
0204 
0205   LWIP_DEBUGF(SYS_DEBUG, ("sys_timeout: %p msecs=%"U32_F" h=%p arg=%p\n",
0206     (void *)timeout, msecs, *(void**)&h, (void *)arg));
0207 
0208   if (timeouts == NULL) {
0209     LWIP_ASSERT("sys_timeout: timeouts != NULL", timeouts != NULL);
0210     return;
0211   }
0212 
0213   if (timeouts->next == NULL) {
0214     timeouts->next = timeout;
0215     return;
0216   }
0217 
0218   if (timeouts->next->time > msecs) {
0219     timeouts->next->time -= msecs;
0220     timeout->next = timeouts->next;
0221     timeouts->next = timeout;
0222   } else {
0223     for(t = timeouts->next; t != NULL; t = t->next) {
0224       timeout->time -= t->time;
0225       if (t->next == NULL || t->next->time > timeout->time) {
0226         if (t->next != NULL) {
0227           t->next->time -= timeout->time;
0228         }
0229         timeout->next = t->next;
0230         t->next = timeout;
0231         break;
0232       }
0233     }
0234   }
0235 }
0236 
0237 /**
0238  * Go through timeout list (for this task only) and remove the first matching
0239  * entry, even though the timeout has not triggered yet.
0240  *
0241  * @note This function only works as expected if there is only one timeout
0242  * calling 'h' in the list of timeouts.
0243  *
0244  * @param h callback function that would be called by the timeout
0245  * @param arg callback argument that would be passed to h
0246 */
0247 void
0248 sys_untimeout(sys_timeout_handler h, void *arg)
0249 {
0250   struct sys_timeouts *timeouts;
0251   struct sys_timeo *prev_t, *t;
0252 
0253   timeouts = sys_arch_timeouts();
0254 
0255   if (timeouts == NULL) {
0256     LWIP_ASSERT("sys_untimeout: timeouts != NULL", timeouts != NULL);
0257     return;
0258   }
0259   if (timeouts->next == NULL) {
0260     return;
0261   }
0262 
0263   for (t = timeouts->next, prev_t = NULL; t != NULL; prev_t = t, t = t->next) {
0264     if ((t->h == h) && (t->arg == arg)) {
0265       /* We have a match */
0266       /* Unlink from previous in list */
0267       if (prev_t == NULL) {
0268         timeouts->next = t->next;
0269       } else {
0270         prev_t->next = t->next;
0271       }
0272       /* If not the last one, add time of this one back to next */
0273       if (t->next != NULL) {
0274         t->next->time += t->time;
0275       }
0276       memp_free(MEMP_SYS_TIMEOUT, t);
0277       return;
0278     }
0279   }
0280   return;
0281 }
0282 
0283 /**
0284  * Timeout handler function for sys_sem_wait_timeout()
0285  *
0286  * @param arg struct sswt_cb* used to signal a semaphore and end waiting.
0287  */
0288 static void
0289 sswt_handler(void *arg)
0290 {
0291   struct sswt_cb *sswt_cb = (struct sswt_cb *) arg;
0292 
0293   /* Timeout. Set flag to TRUE and signal semaphore */
0294   sswt_cb->timeflag = 1;
0295   sys_sem_signal(*(sswt_cb->psem));
0296 }
0297 
0298 /**
0299  * Wait for a semaphore with timeout (specified in ms)
0300  *
0301  * @param sem semaphore to wait
0302  * @param timeout timeout in ms (0: wait forever)
0303  * @return 0 on timeout, 1 otherwise
0304  */
0305 int
0306 sys_sem_wait_timeout(sys_sem_t sem, u32_t timeout)
0307 {
0308   struct sswt_cb sswt_cb;
0309 
0310   sswt_cb.psem = &sem;
0311   sswt_cb.timeflag = 0;
0312 
0313   /* If timeout is zero, then just wait forever */
0314   if (timeout > 0) {
0315     /* Create a timer and pass it the address of our flag */
0316     sys_timeout(timeout, sswt_handler, &sswt_cb);
0317   }
0318   sys_sem_wait(sem);
0319   /* Was it a timeout? */
0320   if (sswt_cb.timeflag) {
0321     /* timeout */
0322     return 0;
0323   } else {
0324     /* Not a timeout. Remove timeout entry */
0325     sys_untimeout(sswt_handler, &sswt_cb);
0326     return 1;
0327   }
0328 }
0329 
0330 /**
0331  * Sleep for some ms. Timeouts are processed while sleeping.
0332  *
0333  * @param ms number of milliseconds to sleep
0334  */
0335 void
0336 sys_msleep(u32_t ms)
0337 {
0338   sys_sem_t delaysem = sys_sem_new(0);
0339 
0340   sys_sem_wait_timeout(delaysem, ms);
0341 
0342   sys_sem_free(delaysem);
0343 }
0344 
0345 
0346 #endif /* NO_SYS */