1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
|
/** $Id: sys_arch.c $ */
/** @file
* System dependent parts of lwIP, implemented with IPRT.
*/
/*
* Copyright (C) 2007-2012 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* you can redistribute it and/or modify it under the terms of the GNU
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*/
#include <lwip/sys.h>
#include <iprt/assert.h>
#include <iprt/err.h>
#include <iprt/mem.h>
#include <iprt/string.h>
#include <iprt/critsect.h>
#include <iprt/thread.h>
#include <iprt/time.h>
/** @todo during my tests on Debian Lenny 64 bit I ran into trouble using
* mutex semaphores (crash deep down in the pthreads lib). Using the write
* case of rw semaphores also gives mutual exclusion, and didn't show those
* crashes. Should be investigated, because this "fix" might be just covering
* the symptoms of a bug elsewhere. */
#if HC_ARCH_BITS == 64 && defined RT_ARCH_LINUX
#define LWIPMUTEXTYPE RTSEMRW
#define LWIPMutexCreate RTSemRWCreate
#define LWIPMutexDestroy RTSemRWDestroy
#define LWIPMutexRequest RTSemRWRequestWrite
#define LWIPMutexRelease RTSemRWReleaseWrite
#else
#define LWIPMUTEXTYPE RTSEMMUTEX
#define LWIPMutexCreate RTSemMutexCreate
#define LWIPMutexDestroy RTSemMutexDestroy
#define LWIPMutexRequest RTSemMutexRequest
#define LWIPMutexRelease RTSemMutexRelease
#endif
/** Maximum number of threads lwIP is allowed to create. */
#define THREADS_MAX 5
/** Maximum number of mbox entries needed for reasonable performance. */
#define MBOX_ENTRIES_MAX 128
/** Data type for slots in TLS. */
typedef struct
{
RTTHREAD tid;
void (* thread)(void *arg);
void *arg;
struct sys_timeouts timeouts;
} THREADLOCALSTORAGE;
/** Actual declaration of the mbox type. */
struct sys_mbox
{
LWIPMUTEXTYPE mutex;
RTSEMEVENTMULTI nonempty, nonfull;
void *apvEntries[MBOX_ENTRIES_MAX];
u32_t head, tail;
};
#if SYS_LIGHTWEIGHT_PROT
/** Critical section variable for short term synchronization. */
static RTCRITSECT g_ProtCritSect;
#else
/** Synchronization for thread creation handling. */
static RTSEMEVENT g_ThreadSem;
#endif
/** Number of threads currently created by lwIP. */
static u32_t g_cThreads = 0;
/** The simulated thread local storage for lwIP things. */
static THREADLOCALSTORAGE g_aTLS[THREADS_MAX];
/**
* Initialize the port to IPRT.
*/
void sys_init(void)
{
int rc;
unsigned i;
#if SYS_LIGHTWEIGHT_PROT
rc = RTCritSectInit(&g_ProtCritSect);
AssertRC(rc);
#else
rc = RTSemEventCreate(&g_ThreadSem);
AssertRC(rc);
rc = RTSemEventSignal(g_ThreadSem);
AssertRC(rc);
#endif
for (i = 0; i < THREADS_MAX; i++)
g_aTLS[i].tid = NIL_RTTHREAD;
}
/**
* Create a new (binary) semaphore.
*/
sys_sem_t sys_sem_new(u8_t count)
{
int rc;
RTSEMEVENT sem;
Assert(count <= 1);
rc = RTSemEventCreate(&sem);
AssertRC(rc);
if (count == 1)
{
rc = RTSemEventSignal(sem);
AssertRC(rc);
}
return sem;
}
/**
* Destroy a (binary) semaphore.
*/
void sys_sem_free(sys_sem_t sem)
{
int rc;
rc = RTSemEventDestroy(sem);
AssertRC(rc);
}
/**
* Signal a (binary) semaphore.
*/
void sys_sem_signal(sys_sem_t sem)
{
int rc;
rc = RTSemEventSignal(sem);
AssertRC(rc);
}
/**
* Wait for a (binary) semaphore.
*/
u32_t sys_arch_sem_wait(sys_sem_t sem, u32_t timeout)
{
int rc;
RTMSINTERVAL cMillies;
uint64_t tsStart, tsEnd;
tsStart = RTTimeMilliTS();
if (timeout == 0)
cMillies = RT_INDEFINITE_WAIT;
else
cMillies = timeout;
rc = RTSemEventWait(sem, cMillies);
if (rc == VERR_TIMEOUT)
return SYS_ARCH_TIMEOUT;
AssertRC(rc);
tsEnd = RTTimeMilliTS();
return tsEnd - tsStart;
}
/**
* Create new mbox.
*/
sys_mbox_t sys_mbox_new(void)
{
int rc;
struct sys_mbox *mbox;
mbox = RTMemAllocZ(sizeof(*mbox));
Assert(mbox != NULL);
if (!mbox)
return mbox;
rc = LWIPMutexCreate(&mbox->mutex);
AssertRC(rc);
if (RT_FAILURE(rc))
{
RTMemFree(mbox);
return NULL;
}
rc = RTSemEventMultiCreate(&mbox->nonempty);
AssertRC(rc);
if (RT_FAILURE(rc))
{
rc = LWIPMutexDestroy(mbox->mutex);
AssertRC(rc);
RTMemFree(mbox);
return NULL;
}
rc = RTSemEventMultiCreate(&mbox->nonfull);
AssertRC(rc);
if (RT_FAILURE(rc))
{
rc = RTSemEventMultiDestroy(mbox->nonempty);
AssertRC(rc);
rc = LWIPMutexDestroy(mbox->mutex);
AssertRC(rc);
RTMemFree(mbox);
return NULL;
}
return mbox;
}
/**
* Free an mbox.
*/
void sys_mbox_free(sys_mbox_t mbox)
{
Assert(mbox != NULL);
LWIPMutexDestroy(mbox->mutex);
RTSemEventMultiDestroy(mbox->nonempty);
RTSemEventMultiDestroy(mbox->nonfull);
RTMemFree(mbox);
}
/**
* Place an entry in an mbox.
*/
void sys_mbox_post(sys_mbox_t mbox, void *msg)
{
int rc;
Assert(mbox != NULL);
rc = LWIPMutexRequest(mbox->mutex, RT_INDEFINITE_WAIT);
AssertRC(rc);
while ((mbox->head + 1) % MBOX_ENTRIES_MAX == mbox->tail)
{
/* mbox is full, have to wait until a slot becomes available. */
rc = LWIPMutexRelease(mbox->mutex);
AssertRC(rc);
rc = RTSemEventMultiWait(mbox->nonfull, RT_INDEFINITE_WAIT);
AssertRC(rc);
rc = LWIPMutexRequest(mbox->mutex, RT_INDEFINITE_WAIT);
AssertRC(rc);
}
if (mbox->head == mbox->tail)
{
rc = RTSemEventMultiSignal(mbox->nonempty);
AssertRC(rc);
}
mbox->apvEntries[mbox->head] = msg;
mbox->head++;
mbox->head %= MBOX_ENTRIES_MAX;
if ((mbox->head + 1) % MBOX_ENTRIES_MAX == mbox->tail)
{
rc = RTSemEventMultiReset(mbox->nonfull);
AssertRC(rc);
}
rc = LWIPMutexRelease(mbox->mutex);
AssertRC(rc);
}
/**
* Get an entry from an mbox.
*/
u32_t sys_arch_mbox_fetch(sys_mbox_t mbox, void **msg, u32_t timeout)
{
int rc;
RTMSINTERVAL cMillies;
uint64_t tsStart, tsEnd;
Assert(mbox != NULL);
tsStart = RTTimeMilliTS();
if (timeout == 0)
cMillies = RT_INDEFINITE_WAIT;
else
cMillies = timeout;
rc = LWIPMutexRequest(mbox->mutex, cMillies);
if (rc == VERR_TIMEOUT)
return SYS_ARCH_TIMEOUT;
AssertRC(rc);
while (mbox->head == mbox->tail)
{
/* mbox is empty, have to wait until a slot is filled. */
rc = LWIPMutexRelease(mbox->mutex);
AssertRC(rc);
if (timeout != 0)
{
tsEnd = RTTimeMilliTS();
if (tsEnd - tsStart >= cMillies)
return SYS_ARCH_TIMEOUT;
cMillies -= tsEnd - tsStart;
}
rc = RTSemEventMultiWait(mbox->nonempty, cMillies);
if (rc == VERR_TIMEOUT)
return SYS_ARCH_TIMEOUT;
AssertRC(rc);
if (timeout != 0)
{
tsEnd = RTTimeMilliTS();
if (tsEnd - tsStart >= cMillies)
return SYS_ARCH_TIMEOUT;
cMillies -= tsEnd - tsStart;
}
rc = LWIPMutexRequest(mbox->mutex, cMillies);
if (rc == VERR_TIMEOUT)
return SYS_ARCH_TIMEOUT;
AssertRC(rc);
}
if ((mbox->head + 1) % MBOX_ENTRIES_MAX == mbox->tail)
{
rc = RTSemEventMultiSignal(mbox->nonfull);
AssertRC(rc);
}
if (msg != NULL)
*msg = mbox->apvEntries[mbox->tail];
mbox->tail++;
mbox->tail %= MBOX_ENTRIES_MAX;
rc = RTSemEventMultiSignal(mbox->nonfull);
if (mbox->head == mbox->tail)
{
rc = RTSemEventMultiReset(mbox->nonempty);
AssertRC(rc);
}
rc = LWIPMutexRelease(mbox->mutex);
AssertRC(rc);
tsEnd = RTTimeMilliTS();
return tsEnd - tsStart;
}
/**
* Grab the pointer to this thread's timeouts from TLS.
*/
struct sys_timeouts *sys_arch_timeouts(void)
{
unsigned i;
#if SYS_LIGHTWEIGHT_PROT
SYS_ARCH_DECL_PROTECT(old_level);
#endif
RTTHREAD myself;
struct sys_timeouts *to = NULL;
myself = RTThreadSelf();
#if SYS_LIGHTWEIGHT_PROT
SYS_ARCH_PROTECT(old_level);
#else
RTSemEventWait(g_ThreadSem, RT_INDEFINITE_WAIT);
#endif
for (i = 0; i < g_cThreads; i++)
{
if (g_aTLS[i].tid == myself)
{
to = &g_aTLS[i].timeouts;
break;
}
}
/* Auto-adopt new threads which use lwIP as they pop up. */
if (!to)
{
unsigned id;
id = g_cThreads;
g_cThreads++;
Assert(g_cThreads <= THREADS_MAX);
g_aTLS[id].tid = myself;
to = &g_aTLS[id].timeouts;
}
#if SYS_LIGHTWEIGHT_PROT
SYS_ARCH_UNPROTECT(old_level);
#else
RTSemEventSignal(g_ThreadSem);
#endif
return to;
}
/**
* Internal: thread main function adapter, dropping the first parameter. Needed
* to make lwip thread main function compatible with IPRT thread main function.
*/
static int sys_thread_adapter(RTTHREAD ThreadSelf, void *pvUser)
{
THREADLOCALSTORAGE *tls = (THREADLOCALSTORAGE *)pvUser;
tls->thread(tls->arg);
return 0;
}
/**
* Create new thread.
*/
sys_thread_t sys_thread_new(void (* thread)(void *arg), void *arg, int prio)
{
int rc;
#if SYS_LIGHTWEIGHT_PROT
SYS_ARCH_DECL_PROTECT(old_level);
#endif
unsigned id;
RTTHREAD tid;
#if SYS_LIGHTWEIGHT_PROT
SYS_ARCH_PROTECT(old_level);
#else
RTSemEventWait(g_ThreadSem, RT_INDEFINITE_WAIT);
#endif
id = g_cThreads;
g_cThreads++;
Assert(g_cThreads <= THREADS_MAX);
g_aTLS[id].thread = thread;
g_aTLS[id].arg = arg;
rc = RTThreadCreateF(&tid, sys_thread_adapter, &g_aTLS[id], 0,
RTTHREADTYPE_IO, 0, "lwIP%u", id);
if (RT_FAILURE(rc))
{
g_cThreads--;
tid = NIL_RTTHREAD;
}
else
g_aTLS[id].tid = tid;
#if SYS_LIGHTWEIGHT_PROT
SYS_ARCH_UNPROTECT(old_level);
#else
RTSemEventSignal(g_ThreadSem);
#endif
AssertRC(rc);
return tid;
}
#if SYS_LIGHTWEIGHT_PROT
/**
* Start a short critical section.
*/
sys_prot_t sys_arch_protect(void)
{
int rc;
rc = RTCritSectEnter(&g_ProtCritSect);
AssertRC(rc);
return NULL;
}
#endif
#if SYS_LIGHTWEIGHT_PROT
/**
* End a short critical section.
*/
void sys_arch_unprotect(sys_prot_t pval)
{
int rc;
(void)pval;
rc = RTCritSectLeave(&g_ProtCritSect);
AssertRC(rc);
}
#endif
|