sys.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /**
  2. * @file
  3. * lwIP Operating System abstraction
  4. *
  5. */
  6. /*
  7. * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without modification,
  11. * are permitted provided that the following conditions are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * 3. The name of the author may not be used to endorse or promote products
  19. * derived from this software without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  22. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  23. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  24. * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  25. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  26. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  29. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  30. * OF SUCH DAMAGE.
  31. *
  32. * This file is part of the lwIP TCP/IP stack.
  33. *
  34. * Author: Adam Dunkels <adam@sics.se>
  35. *
  36. */
  37. #include "lwip/opt.h"
  38. #if (NO_SYS == 0) /* don't build if not configured for use in lwipopts.h */
  39. #include "lwip/sys.h"
  40. #include "lwip/def.h"
  41. #include "lwip/memp.h"
  42. #include "lwip/tcpip.h"
  43. /**
  44. * Struct used for sys_sem_wait_timeout() to tell wether the time
  45. * has run out or the semaphore has really become available.
  46. */
  47. struct sswt_cb
  48. {
  49. s16_t timeflag;
  50. sys_sem_t *psem;
  51. };
  52. /**
  53. * Wait (forever) for a message to arrive in an mbox.
  54. * While waiting, timeouts (for this thread) are processed.
  55. *
  56. * @param mbox the mbox to fetch the message from
  57. * @param msg the place to store the message
  58. */
  59. void
  60. sys_mbox_fetch(sys_mbox_t mbox, void **msg)
  61. {
  62. u32_t time_needed;
  63. struct sys_timeouts *timeouts;
  64. struct sys_timeo *tmptimeout;
  65. sys_timeout_handler h;
  66. void *arg;
  67. again:
  68. timeouts = sys_arch_timeouts();
  69. if (!timeouts || !timeouts->next) {
  70. UNLOCK_TCPIP_CORE();
  71. time_needed = sys_arch_mbox_fetch(mbox, msg, 0);
  72. LOCK_TCPIP_CORE();
  73. } else {
  74. if (timeouts->next->time > 0) {
  75. UNLOCK_TCPIP_CORE();
  76. time_needed = sys_arch_mbox_fetch(mbox, msg, timeouts->next->time);
  77. LOCK_TCPIP_CORE();
  78. } else {
  79. time_needed = SYS_ARCH_TIMEOUT;
  80. }
  81. if (time_needed == SYS_ARCH_TIMEOUT) {
  82. /* If time == SYS_ARCH_TIMEOUT, a timeout occured before a message
  83. could be fetched. We should now call the timeout handler and
  84. deallocate the memory allocated for the timeout. */
  85. tmptimeout = timeouts->next;
  86. timeouts->next = tmptimeout->next;
  87. h = tmptimeout->h;
  88. arg = tmptimeout->arg;
  89. memp_free(MEMP_SYS_TIMEOUT, tmptimeout);
  90. if (h != NULL) {
  91. LWIP_DEBUGF(SYS_DEBUG, ("smf calling h=%p(%p)\n", *(void**)&h, arg));
  92. h(arg);
  93. }
  94. /* We try again to fetch a message from the mbox. */
  95. goto again;
  96. } else {
  97. /* If time != SYS_ARCH_TIMEOUT, a message was received before the timeout
  98. occured. The time variable is set to the number of
  99. milliseconds we waited for the message. */
  100. if (time_needed < timeouts->next->time) {
  101. timeouts->next->time -= time_needed;
  102. } else {
  103. timeouts->next->time = 0;
  104. }
  105. }
  106. }
  107. }
  108. /**
  109. * Wait (forever) for a semaphore to become available.
  110. * While waiting, timeouts (for this thread) are processed.
  111. *
  112. * @param sem semaphore to wait for
  113. */
  114. void
  115. sys_sem_wait(sys_sem_t sem)
  116. {
  117. u32_t time_needed;
  118. struct sys_timeouts *timeouts;
  119. struct sys_timeo *tmptimeout;
  120. sys_timeout_handler h;
  121. void *arg;
  122. again:
  123. timeouts = sys_arch_timeouts();
  124. if (!timeouts || !timeouts->next) {
  125. sys_arch_sem_wait(sem, 0);
  126. } else {
  127. if (timeouts->next->time > 0) {
  128. time_needed = sys_arch_sem_wait(sem, timeouts->next->time);
  129. } else {
  130. time_needed = SYS_ARCH_TIMEOUT;
  131. }
  132. if (time_needed == SYS_ARCH_TIMEOUT) {
  133. /* If time == SYS_ARCH_TIMEOUT, a timeout occured before a message
  134. could be fetched. We should now call the timeout handler and
  135. deallocate the memory allocated for the timeout. */
  136. tmptimeout = timeouts->next;
  137. timeouts->next = tmptimeout->next;
  138. h = tmptimeout->h;
  139. arg = tmptimeout->arg;
  140. memp_free(MEMP_SYS_TIMEOUT, tmptimeout);
  141. if (h != NULL) {
  142. LWIP_DEBUGF(SYS_DEBUG, ("ssw h=%p(%p)\n", *(void**)&h, (void *)arg));
  143. h(arg);
  144. }
  145. /* We try again to fetch a message from the mbox. */
  146. goto again;
  147. } else {
  148. /* If time != SYS_ARCH_TIMEOUT, a message was received before the timeout
  149. occured. The time variable is set to the number of
  150. milliseconds we waited for the message. */
  151. if (time_needed < timeouts->next->time) {
  152. timeouts->next->time -= time_needed;
  153. } else {
  154. timeouts->next->time = 0;
  155. }
  156. }
  157. }
  158. }
  159. /**
  160. * Create a one-shot timer (aka timeout). Timeouts are processed in the
  161. * following cases:
  162. * - while waiting for a message using sys_mbox_fetch()
  163. * - while waiting for a semaphore using sys_sem_wait() or sys_sem_wait_timeout()
  164. * - while sleeping using the inbuilt sys_msleep()
  165. *
  166. * @param msecs time in milliseconds after that the timer should expire
  167. * @param h callback function to call when msecs have elapsed
  168. * @param arg argument to pass to the callback function
  169. */
  170. void
  171. sys_timeout(u32_t msecs, sys_timeout_handler h, void *arg)
  172. {
  173. struct sys_timeouts *timeouts;
  174. struct sys_timeo *timeout, *t;
  175. timeout = memp_malloc(MEMP_SYS_TIMEOUT);
  176. if (timeout == NULL) {
  177. LWIP_ASSERT("sys_timeout: timeout != NULL", timeout != NULL);
  178. return;
  179. }
  180. timeout->next = NULL;
  181. timeout->h = h;
  182. timeout->arg = arg;
  183. timeout->time = msecs;
  184. timeouts = sys_arch_timeouts();
  185. LWIP_DEBUGF(SYS_DEBUG, ("sys_timeout: %p msecs=%"U32_F" h=%p arg=%p\n",
  186. (void *)timeout, msecs, *(void**)&h, (void *)arg));
  187. if (timeouts == NULL) {
  188. LWIP_ASSERT("sys_timeout: timeouts != NULL", timeouts != NULL);
  189. return;
  190. }
  191. if (timeouts->next == NULL) {
  192. timeouts->next = timeout;
  193. return;
  194. }
  195. if (timeouts->next->time > msecs) {
  196. timeouts->next->time -= msecs;
  197. timeout->next = timeouts->next;
  198. timeouts->next = timeout;
  199. } else {
  200. for(t = timeouts->next; t != NULL; t = t->next) {
  201. timeout->time -= t->time;
  202. if (t->next == NULL || t->next->time > timeout->time) {
  203. if (t->next != NULL) {
  204. t->next->time -= timeout->time;
  205. }
  206. timeout->next = t->next;
  207. t->next = timeout;
  208. break;
  209. }
  210. }
  211. }
  212. }
  213. /**
  214. * Go through timeout list (for this task only) and remove the first matching
  215. * entry, even though the timeout has not triggered yet.
  216. *
  217. * @note This function only works as expected if there is only one timeout
  218. * calling 'h' in the list of timeouts.
  219. *
  220. * @param h callback function that would be called by the timeout
  221. * @param arg callback argument that would be passed to h
  222. */
  223. void
  224. sys_untimeout(sys_timeout_handler h, void *arg)
  225. {
  226. struct sys_timeouts *timeouts;
  227. struct sys_timeo *prev_t, *t;
  228. timeouts = sys_arch_timeouts();
  229. if (timeouts == NULL) {
  230. LWIP_ASSERT("sys_untimeout: timeouts != NULL", timeouts != NULL);
  231. return;
  232. }
  233. if (timeouts->next == NULL) {
  234. return;
  235. }
  236. for (t = timeouts->next, prev_t = NULL; t != NULL; prev_t = t, t = t->next) {
  237. if ((t->h == h) && (t->arg == arg)) {
  238. /* We have a match */
  239. /* Unlink from previous in list */
  240. if (prev_t == NULL) {
  241. timeouts->next = t->next;
  242. } else {
  243. prev_t->next = t->next;
  244. }
  245. /* If not the last one, add time of this one back to next */
  246. if (t->next != NULL) {
  247. t->next->time += t->time;
  248. }
  249. memp_free(MEMP_SYS_TIMEOUT, t);
  250. return;
  251. }
  252. }
  253. return;
  254. }
  255. /**
  256. * Timeout handler function for sys_sem_wait_timeout()
  257. *
  258. * @param arg struct sswt_cb* used to signal a semaphore and end waiting.
  259. */
  260. static void
  261. sswt_handler(void *arg)
  262. {
  263. struct sswt_cb *sswt_cb = (struct sswt_cb *) arg;
  264. /* Timeout. Set flag to TRUE and signal semaphore */
  265. sswt_cb->timeflag = 1;
  266. sys_sem_signal(*(sswt_cb->psem));
  267. }
  268. /**
  269. * Wait for a semaphore with timeout (specified in ms)
  270. *
  271. * @param sem semaphore to wait
  272. * @param timeout timeout in ms (0: wait forever)
  273. * @return 0 on timeout, 1 otherwise
  274. */
  275. int
  276. sys_sem_wait_timeout(sys_sem_t sem, u32_t timeout)
  277. {
  278. struct sswt_cb sswt_cb;
  279. sswt_cb.psem = &sem;
  280. sswt_cb.timeflag = 0;
  281. /* If timeout is zero, then just wait forever */
  282. if (timeout > 0) {
  283. /* Create a timer and pass it the address of our flag */
  284. sys_timeout(timeout, sswt_handler, &sswt_cb);
  285. }
  286. sys_sem_wait(sem);
  287. /* Was it a timeout? */
  288. if (sswt_cb.timeflag) {
  289. /* timeout */
  290. return 0;
  291. } else {
  292. /* Not a timeout. Remove timeout entry */
  293. sys_untimeout(sswt_handler, &sswt_cb);
  294. return 1;
  295. }
  296. }
  297. /**
  298. * Sleep for some ms. Timeouts are processed while sleeping.
  299. *
  300. * @param ms number of milliseconds to sleep
  301. */
  302. void
  303. sys_msleep(u32_t ms)
  304. {
  305. sys_sem_t delaysem = sys_sem_new(0);
  306. sys_sem_wait_timeout(delaysem, ms);
  307. sys_sem_free(delaysem);
  308. }
  309. #endif /* NO_SYS */