Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039 #include "lwip/opt.h"
00040
00041 #if (NO_SYS == 0)
00042
00043 #include "lwip/sys.h"
00044 #include "lwip/def.h"
00045 #include "lwip/memp.h"
00046 #include "lwip/tcpip.h"
00047
00048
00049
00050
00051
00052 struct sswt_cb
00053 {
00054 s16_t timeflag;
00055 sys_sem_t *psem;
00056 };
00057
00058
00059
00060
00061
00062
00063
00064
00065 void
00066 sys_mbox_fetch(sys_mbox_t mbox, void **msg)
00067 {
00068 u32_t time_needed;
00069 struct sys_timeouts *timeouts;
00070 struct sys_timeo *tmptimeout;
00071 sys_timeout_handler h;
00072 void *arg;
00073
00074 again:
00075 timeouts = sys_arch_timeouts();
00076
00077 if (!timeouts || !timeouts->next) {
00078 UNLOCK_TCPIP_CORE();
00079 time_needed = sys_arch_mbox_fetch(mbox, msg, 0);
00080 LOCK_TCPIP_CORE();
00081 } else {
00082 if (timeouts->next->time > 0) {
00083 UNLOCK_TCPIP_CORE();
00084 time_needed = sys_arch_mbox_fetch(mbox, msg, timeouts->next->time);
00085 LOCK_TCPIP_CORE();
00086 } else {
00087 time_needed = SYS_ARCH_TIMEOUT;
00088 }
00089
00090 if (time_needed == SYS_ARCH_TIMEOUT) {
00091
00092
00093
00094 tmptimeout = timeouts->next;
00095 timeouts->next = tmptimeout->next;
00096 h = tmptimeout->h;
00097 arg = tmptimeout->arg;
00098 memp_free(MEMP_SYS_TIMEOUT, tmptimeout);
00099 if (h != NULL) {
00100 LWIP_DEBUGF(SYS_DEBUG, ("smf calling h=%p(%p)\n", *(void**)&h, arg));
00101 h(arg);
00102 }
00103
00104
00105 goto again;
00106 } else {
00107
00108
00109
00110 if (time_needed < timeouts->next->time) {
00111 timeouts->next->time -= time_needed;
00112 } else {
00113 timeouts->next->time = 0;
00114 }
00115 }
00116 }
00117 }
00118
00119
00120
00121
00122
00123
00124
00125 void
00126 sys_sem_wait(sys_sem_t sem)
00127 {
00128 u32_t time_needed;
00129 struct sys_timeouts *timeouts;
00130 struct sys_timeo *tmptimeout;
00131 sys_timeout_handler h;
00132 void *arg;
00133
00134 again:
00135
00136 timeouts = sys_arch_timeouts();
00137
00138 if (!timeouts || !timeouts->next) {
00139 sys_arch_sem_wait(sem, 0);
00140 } else {
00141 if (timeouts->next->time > 0) {
00142 time_needed = sys_arch_sem_wait(sem, timeouts->next->time);
00143 } else {
00144 time_needed = SYS_ARCH_TIMEOUT;
00145 }
00146
00147 if (time_needed == SYS_ARCH_TIMEOUT) {
00148
00149
00150
00151 tmptimeout = timeouts->next;
00152 timeouts->next = tmptimeout->next;
00153 h = tmptimeout->h;
00154 arg = tmptimeout->arg;
00155 memp_free(MEMP_SYS_TIMEOUT, tmptimeout);
00156 if (h != NULL) {
00157 LWIP_DEBUGF(SYS_DEBUG, ("ssw h=%p(%p)\n", *(void**)&h, (void *)arg));
00158 h(arg);
00159 }
00160
00161
00162 goto again;
00163 } else {
00164
00165
00166
00167 if (time_needed < timeouts->next->time) {
00168 timeouts->next->time -= time_needed;
00169 } else {
00170 timeouts->next->time = 0;
00171 }
00172 }
00173 }
00174 }
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187 void
00188 sys_timeout(u32_t msecs, sys_timeout_handler h, void *arg)
00189 {
00190 struct sys_timeouts *timeouts;
00191 struct sys_timeo *timeout, *t;
00192
00193 timeout = memp_malloc(MEMP_SYS_TIMEOUT);
00194 if (timeout == NULL) {
00195 LWIP_ASSERT("sys_timeout: timeout != NULL", timeout != NULL);
00196 return;
00197 }
00198 timeout->next = NULL;
00199 timeout->h = h;
00200 timeout->arg = arg;
00201 timeout->time = msecs;
00202
00203 timeouts = sys_arch_timeouts();
00204
00205 LWIP_DEBUGF(SYS_DEBUG, ("sys_timeout: %p msecs=%"U32_F" h=%p arg=%p\n",
00206 (void *)timeout, msecs, *(void**)&h, (void *)arg));
00207
00208 if (timeouts == NULL) {
00209 LWIP_ASSERT("sys_timeout: timeouts != NULL", timeouts != NULL);
00210 return;
00211 }
00212
00213 if (timeouts->next == NULL) {
00214 timeouts->next = timeout;
00215 return;
00216 }
00217
00218 if (timeouts->next->time > msecs) {
00219 timeouts->next->time -= msecs;
00220 timeout->next = timeouts->next;
00221 timeouts->next = timeout;
00222 } else {
00223 for(t = timeouts->next; t != NULL; t = t->next) {
00224 timeout->time -= t->time;
00225 if (t->next == NULL || t->next->time > timeout->time) {
00226 if (t->next != NULL) {
00227 t->next->time -= timeout->time;
00228 }
00229 timeout->next = t->next;
00230 t->next = timeout;
00231 break;
00232 }
00233 }
00234 }
00235 }
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247 void
00248 sys_untimeout(sys_timeout_handler h, void *arg)
00249 {
00250 struct sys_timeouts *timeouts;
00251 struct sys_timeo *prev_t, *t;
00252
00253 timeouts = sys_arch_timeouts();
00254
00255 if (timeouts == NULL) {
00256 LWIP_ASSERT("sys_untimeout: timeouts != NULL", timeouts != NULL);
00257 return;
00258 }
00259 if (timeouts->next == NULL) {
00260 return;
00261 }
00262
00263 for (t = timeouts->next, prev_t = NULL; t != NULL; prev_t = t, t = t->next) {
00264 if ((t->h == h) && (t->arg == arg)) {
00265
00266
00267 if (prev_t == NULL) {
00268 timeouts->next = t->next;
00269 } else {
00270 prev_t->next = t->next;
00271 }
00272
00273 if (t->next != NULL) {
00274 t->next->time += t->time;
00275 }
00276 memp_free(MEMP_SYS_TIMEOUT, t);
00277 return;
00278 }
00279 }
00280 return;
00281 }
00282
00283
00284
00285
00286
00287
00288 static void
00289 sswt_handler(void *arg)
00290 {
00291 struct sswt_cb *sswt_cb = (struct sswt_cb *) arg;
00292
00293
00294 sswt_cb->timeflag = 1;
00295 sys_sem_signal(*(sswt_cb->psem));
00296 }
00297
00298
00299
00300
00301
00302
00303
00304
00305 int
00306 sys_sem_wait_timeout(sys_sem_t sem, u32_t timeout)
00307 {
00308 struct sswt_cb sswt_cb;
00309
00310 sswt_cb.psem = &sem;
00311 sswt_cb.timeflag = 0;
00312
00313
00314 if (timeout > 0) {
00315
00316 sys_timeout(timeout, sswt_handler, &sswt_cb);
00317 }
00318 sys_sem_wait(sem);
00319
00320 if (sswt_cb.timeflag) {
00321
00322 return 0;
00323 } else {
00324
00325 sys_untimeout(sswt_handler, &sswt_cb);
00326 return 1;
00327 }
00328 }
00329
00330
00331
00332
00333
00334
00335 void
00336 sys_msleep(u32_t ms)
00337 {
00338 sys_sem_t delaysem = sys_sem_new(0);
00339
00340 sys_sem_wait_timeout(delaysem, ms);
00341
00342 sys_sem_free(delaysem);
00343 }
00344
00345
00346 #endif