ReactOS 0.4.15-dev-7907-g95bf896
sys_arch.c File Reference
#include <debug.h>
#include <lwip/sys.h>
#include "lwip_glue.h"
Include dependency graph for sys_arch.c:

Go to the source code of this file.

Classes

struct  _thread_t
 

Typedefs

typedef struct _thread_tthread_t
 

Functions

u32_t sys_now (void)
 
void sys_arch_protect (sys_prot_t *lev)
 
void sys_arch_unprotect (sys_prot_t lev)
 
err_t sys_sem_new (sys_sem_t *sem, u8_t count)
 
int sys_sem_valid (sys_sem_t *sem)
 
void sys_sem_set_invalid (sys_sem_t *sem)
 
void sys_sem_free (sys_sem_t *sem)
 
void sys_sem_signal (sys_sem_t *sem)
 
u32_t sys_arch_sem_wait (sys_sem_t *sem, u32_t timeout)
 
err_t sys_mbox_new (sys_mbox_t *mbox, int size)
 
int sys_mbox_valid (sys_mbox_t *mbox)
 
void sys_mbox_set_invalid (sys_mbox_t *mbox)
 
void sys_mbox_free (sys_mbox_t *mbox)
 
void sys_mbox_post (sys_mbox_t *mbox, void *msg)
 
u32_t sys_arch_mbox_fetch (sys_mbox_t *mbox, void **msg, u32_t timeout)
 
u32_t sys_arch_mbox_tryfetch (sys_mbox_t *mbox, void **msg)
 
err_t sys_mbox_trypost (sys_mbox_t *mbox, void *msg)
 
VOID NTAPI LwipThreadMain (PVOID Context)
 
sys_thread_t sys_thread_new (const char *name, lwip_thread_fn thread, void *arg, int stacksize, int prio)
 
void sys_init (void)
 
void sys_shutdown (void)
 

Variables

static LIST_ENTRY ThreadListHead
 
static KSPIN_LOCK ThreadListLock
 
KEVENT TerminationEvent
 
NPAGED_LOOKASIDE_LIST MessageLookasideList
 
NPAGED_LOOKASIDE_LIST QueueEntryLookasideList
 
static LARGE_INTEGER StartTime
 

Typedef Documentation

◆ thread_t

Function Documentation

◆ LwipThreadMain()

VOID NTAPI LwipThreadMain ( PVOID  Context)

Definition at line 256 of file sys_arch.c.

257{
258 thread_t Container = (thread_t)Context;
260
262
263 Container->ThreadFunction(Container->ThreadContext);
264
266 RemoveEntryList(&Container->ListEntry);
268
269 ExFreePool(Container);
270
272}
#define RemoveEntryList(Entry)
Definition: env_spec_w32.h:986
UCHAR KIRQL
Definition: env_spec_w32.h:591
#define KeReleaseSpinLock(sl, irql)
Definition: env_spec_w32.h:627
#define KeAcquireSpinLock(sl, irql)
Definition: env_spec_w32.h:609
#define ExFreePool(addr)
Definition: env_spec_w32.h:352
PLIST_ENTRY NTAPI ExInterlockedInsertHeadList(IN OUT PLIST_ENTRY ListHead, IN OUT PLIST_ENTRY ListEntry, IN OUT PKSPIN_LOCK Lock)
Definition: interlocked.c:114
NTSTATUS NTAPI PsTerminateSystemThread(IN NTSTATUS ExitStatus)
Definition: kill.c:1145
#define STATUS_SUCCESS
Definition: shellext.h:65
void(* ThreadFunction)(void *arg)
Definition: sys_arch.c:18
void * ThreadContext
Definition: sys_arch.c:19
LIST_ENTRY ListEntry
Definition: sys_arch.c:20
static LIST_ENTRY ThreadListHead
Definition: sys_arch.c:6
static KSPIN_LOCK ThreadListLock
Definition: sys_arch.c:7
struct _thread_t * thread_t
_Requires_lock_held_ Interrupt _Releases_lock_ Interrupt _In_ _IRQL_restores_ KIRQL OldIrql
Definition: kefuncs.h:778

Referenced by sys_thread_new().

◆ sys_arch_mbox_fetch()

u32_t sys_arch_mbox_fetch ( sys_mbox_t mbox,
void **  msg,
u32_t  timeout 
)

Wait for a new message to arrive in the mbox

Parameters
mboxmbox to get a message from
msgpointer where the message is stored
timeoutmaximum time (in milliseconds) to wait for a message
Returns
time (in milliseconds) waited for a message, may be 0 if not waited or SYS_ARCH_TIMEOUT on timeout The returned time has to be accurate to prevent timer jitter!

Definition at line 177 of file sys_arch.c.

178{
179 LARGE_INTEGER LargeTimeout, PreWaitTime, PostWaitTime;
180 UINT64 TimeDiff;
183 PLWIP_MESSAGE_CONTAINER Container;
186 PVOID WaitObjects[] = {&mbox->Event, &TerminationEvent};
187
188 LargeTimeout.QuadPart = Int32x32To64(timeout, -10000);
189
190 KeQuerySystemTime(&PreWaitTime);
191
193 WaitObjects,
194 WaitAny,
195 Executive,
197 FALSE,
198 timeout != 0 ? &LargeTimeout : NULL,
199 NULL);
200
201 if (Status == STATUS_WAIT_0)
202 {
205 ASSERT(Entry);
209
210 Container = CONTAINING_RECORD(Entry, LWIP_MESSAGE_CONTAINER, ListEntry);
211 Message = Container->Message;
212 ExFreePool(Container);
213
214 if (msg)
215 *msg = Message;
216
217 KeQuerySystemTime(&PostWaitTime);
218 TimeDiff = PostWaitTime.QuadPart - PreWaitTime.QuadPart;
219 TimeDiff /= 10000;
220
221 return TimeDiff;
222 }
223 else if (Status == STATUS_WAIT_1)
224 {
225 /* DON'T remove ourselves from the thread list! */
227
228 /* We should never get here! */
229 ASSERT(FALSE);
230
231 return 0;
232 }
233
234 return SYS_ARCH_TIMEOUT;
235}
unsigned long long UINT64
#define msg(x)
Definition: auth_time.c:54
LONG NTSTATUS
Definition: precomp.h:26
#define NULL
Definition: types.h:112
#define FALSE
Definition: types.h:117
static const WCHAR Message[]
Definition: register.c:74
#define IsListEmpty(ListHead)
Definition: env_spec_w32.h:954
#define KeQuerySystemTime(t)
Definition: env_spec_w32.h:570
#define RemoveHeadList(ListHead)
Definition: env_spec_w32.h:964
VOID NTAPI KeClearEvent(IN PKEVENT Event)
Definition: eventobj.c:22
Status
Definition: gdiplustypes.h:25
#define ASSERT(a)
Definition: mode.c:44
#define KernelMode
Definition: asm.h:34
#define Int32x32To64(a, b)
@ WaitAny
NTSTATUS NTAPI KeWaitForMultipleObjects(IN ULONG Count, IN PVOID Object[], IN WAIT_TYPE WaitType, IN KWAIT_REASON WaitReason, IN KPROCESSOR_MODE WaitMode, IN BOOLEAN Alertable, IN PLARGE_INTEGER Timeout OPTIONAL, OUT PKWAIT_BLOCK WaitBlockArray OPTIONAL)
Definition: wait.c:586
#define STATUS_WAIT_0
Definition: ntstatus.h:237
#define STATUS_WAIT_1
Definition: ntstatus.h:71
base of all file and directory entries
Definition: entries.h:83
Definition: typedefs.h:120
KSPIN_LOCK Lock
Definition: sys_arch.h:12
KEVENT Event
Definition: sys_arch.h:14
LIST_ENTRY ListHead
Definition: sys_arch.h:13
Definition: dhcpd.h:245
#define SYS_ARCH_TIMEOUT
Definition: sys.h:78
KEVENT TerminationEvent
Definition: sys_arch.c:9
static sys_mbox_t mbox
Definition: tcpip.c:58
#define CONTAINING_RECORD(address, type, field)
Definition: typedefs.h:260
LONGLONG QuadPart
Definition: typedefs.h:114
@ Executive
Definition: ketypes.h:415

Referenced by sys_arch_mbox_tryfetch().

◆ sys_arch_mbox_tryfetch()

u32_t sys_arch_mbox_tryfetch ( sys_mbox_t mbox,
void **  msg 
)

Wait for a new message to arrive in the mbox

Parameters
mboxmbox to get a message from
msgpointer where the message is stored
timeoutmaximum time (in milliseconds) to wait for a message
Returns
0 (milliseconds) if a message has been received or SYS_MBOX_EMPTY if the mailbox is empty

Definition at line 238 of file sys_arch.c.

239{
241 return 0;
242 else
243 return SYS_MBOX_EMPTY;
244}
#define SYS_MBOX_EMPTY
Definition: sys.h:83
u32_t sys_arch_mbox_fetch(sys_mbox_t *mbox, void **msg, u32_t timeout)
Definition: sys_arch.c:177

◆ sys_arch_protect()

void sys_arch_protect ( sys_prot_t lev)

Definition at line 33 of file sys_arch.c.

34{
35 /* Preempt the dispatcher */
37}
#define KeRaiseIrql(irql, oldIrql)
Definition: env_spec_w32.h:597
#define DISPATCH_LEVEL
Definition: env_spec_w32.h:696

◆ sys_arch_sem_wait()

u32_t sys_arch_sem_wait ( sys_sem_t sem,
u32_t  timeout 
)

Wait for a semaphore for the specified timeout

Parameters
semthe semaphore to wait for
timeouttimeout in milliseconds to wait (0 = wait forever)
Returns
time (in milliseconds) waited for the semaphore or SYS_ARCH_TIMEOUT on timeout

Definition at line 86 of file sys_arch.c.

87{
88 LARGE_INTEGER LargeTimeout, PreWaitTime, PostWaitTime;
89 UINT64 TimeDiff;
91 PVOID WaitObjects[] = {&sem->Event, &TerminationEvent};
92
93 LargeTimeout.QuadPart = Int32x32To64(timeout, -10000);
94
95 KeQuerySystemTime(&PreWaitTime);
96
98 WaitObjects,
99 WaitAny,
100 Executive,
102 FALSE,
103 timeout != 0 ? &LargeTimeout : NULL,
104 NULL);
105 if (Status == STATUS_WAIT_0)
106 {
107 KeQuerySystemTime(&PostWaitTime);
108 TimeDiff = PostWaitTime.QuadPart - PreWaitTime.QuadPart;
109 TimeDiff /= 10000;
110
111 return TimeDiff;
112 }
113 else if (Status == STATUS_WAIT_1)
114 {
115 /* DON'T remove ourselves from the thread list! */
117
118 /* We should never get here! */
119 ASSERT(FALSE);
120
121 return 0;
122 }
123
124 return SYS_ARCH_TIMEOUT;
125}
static HANDLE sem
Definition: sync.c:674

Referenced by sys_msleep().

◆ sys_arch_unprotect()

void sys_arch_unprotect ( sys_prot_t  lev)

Definition at line 40 of file sys_arch.c.

41{
42 KeLowerIrql(lev);
43}
#define KeLowerIrql(oldIrql)
Definition: env_spec_w32.h:602

◆ sys_init()

void sys_init ( void  )

Definition at line 305 of file sys_arch.c.

306{
309
311
313
315 NULL,
316 NULL,
317 0,
318 sizeof(struct lwip_callback_msg),
320 0);
321
323 NULL,
324 NULL,
325 0,
326 sizeof(QUEUE_ENTRY),
328 0);
329}
#define KeInitializeEvent(pEvt, foo, foo2)
Definition: env_spec_w32.h:477
#define InitializeListHead(ListHead)
Definition: env_spec_w32.h:944
#define KeInitializeSpinLock(sl)
Definition: env_spec_w32.h:604
VOID NTAPI ExInitializeNPagedLookasideList(IN PNPAGED_LOOKASIDE_LIST Lookaside, IN PALLOCATE_FUNCTION Allocate OPTIONAL, IN PFREE_FUNCTION Free OPTIONAL, IN ULONG Flags, IN SIZE_T Size, IN ULONG Tag, IN USHORT Depth)
Definition: lookas.c:218
#define LWIP_MESSAGE_TAG
Definition: lwip_glue.h:11
#define LWIP_QUEUE_TAG
Definition: lwip_glue.h:12
@ NotificationEvent
Definition: lwip_glue.h:18
static LARGE_INTEGER StartTime
Definition: sys_arch.c:13
NPAGED_LOOKASIDE_LIST MessageLookasideList
Definition: sys_arch.c:10
NPAGED_LOOKASIDE_LIST QueueEntryLookasideList
Definition: sys_arch.c:11

Referenced by lwip_init().

◆ sys_mbox_free()

void sys_mbox_free ( sys_mbox_t mbox)

Delete an mbox

Parameters
mboxmbox to delete

Definition at line 152 of file sys_arch.c.

153{
155
157}
void sys_mbox_set_invalid(sys_mbox_t *mbox)
Definition: sys_arch.c:146

◆ sys_mbox_new()

err_t sys_mbox_new ( sys_mbox_t mbox,
int  size 
)

Create a new mbox of specified size

Parameters
mboxpointer to the mbox to create
size(miminum) number of messages in this mbox
Returns
ERR_OK if successful, another err_t otherwise

Definition at line 128 of file sys_arch.c.

129{
131
133
135
136 mbox->Valid = 1;
137
138 return ERR_OK;
139}
#define ERR_OK
Definition: err.h:52
int Valid
Definition: sys_arch.h:15

Referenced by tcpip_init().

◆ sys_mbox_post()

void sys_mbox_post ( sys_mbox_t mbox,
void msg 
)

Post a message to an mbox - may not fail -> blocks if full, only used from tasks not from ISR

Parameters
mboxmbox to posts the message
msgmessage to post (ATTENTION: can be NULL)

Definition at line 160 of file sys_arch.c.

161{
162 PLWIP_MESSAGE_CONTAINER Container;
163
164 Container = ExAllocatePool(NonPagedPool, sizeof(*Container));
165 ASSERT(Container);
166
167 Container->Message = msg;
168
170 &Container->ListEntry,
171 &mbox->Lock);
172
174}
#define KeSetEvent(pEvt, foo, foo2)
Definition: env_spec_w32.h:476
#define NonPagedPool
Definition: env_spec_w32.h:307
#define ExAllocatePool(type, size)
Definition: fbtusb.h:44
PLIST_ENTRY NTAPI ExInterlockedInsertTailList(IN OUT PLIST_ENTRY ListHead, IN OUT PLIST_ENTRY ListEntry, IN OUT PKSPIN_LOCK Lock)
Definition: interlocked.c:140
LIST_ENTRY ListEntry
Definition: sys_arch.h:25
#define IO_NO_INCREMENT
Definition: iotypes.h:598

Referenced by sys_mbox_trypost(), and tcpip_callback_with_block().

◆ sys_mbox_set_invalid()

void sys_mbox_set_invalid ( sys_mbox_t mbox)

Set an mbox invalid so that sys_mbox_valid returns 0

Definition at line 146 of file sys_arch.c.

147{
148 mbox->Valid = 0;
149}

Referenced by sys_mbox_free().

◆ sys_mbox_trypost()

err_t sys_mbox_trypost ( sys_mbox_t mbox,
void msg 
)

Try to post a message to an mbox - may fail if full or ISR

Parameters
mboxmbox to posts the message
msgmessage to post (ATTENTION: can be NULL)

Definition at line 247 of file sys_arch.c.

248{
250
251 return ERR_OK;
252}
void sys_mbox_post(sys_mbox_t *mbox, void *msg)
Definition: sys_arch.c:160

Referenced by tcpip_callback_with_block(), tcpip_input(), and tcpip_trycallback().

◆ sys_mbox_valid()

int sys_mbox_valid ( sys_mbox_t mbox)

Check if an mbox is valid/allocated: return 1 for valid, 0 for invalid

Definition at line 141 of file sys_arch.c.

142{
143 return mbox->Valid;
144}

Referenced by tcpip_callback_with_block(), tcpip_input(), and tcpip_trycallback().

◆ sys_now()

u32_t sys_now ( void  )

Returns the current time in milliseconds, may be the same as sys_jiffies or at least based on it.

Definition at line 23 of file sys_arch.c.

24{
25 LARGE_INTEGER CurrentTime;
26
27 KeQuerySystemTime(&CurrentTime);
28
29 return (CurrentTime.QuadPart - StartTime.QuadPart) / 10000;
30}

◆ sys_sem_free()

void sys_sem_free ( sys_sem_t sem)

Delete a semaphore

Parameters
semsemaphore to delete

Definition at line 72 of file sys_arch.c.

73{
74 /* No op (allocated in stack) */
75
77}
void sys_sem_set_invalid(sys_sem_t *sem)
Definition: sys_arch.c:66

Referenced by sys_msleep().

◆ sys_sem_new()

err_t sys_sem_new ( sys_sem_t sem,
u8_t  count 
)

Create a new semaphore

Parameters
sempointer to the semaphore to create
countinitial count of the semaphore
Returns
ERR_OK if successful, another err_t otherwise

Definition at line 46 of file sys_arch.c.

47{
48 ASSERT(count == 0 || count == 1);
49
50 /* It seems lwIP uses the semaphore implementation as either a completion event or a lock
51 * so I optimize for this case by using a synchronization event and setting its initial state
52 * to signalled for a lock and non-signalled for a completion event */
53
55
56 sem->Valid = 1;
57
58 return ERR_OK;
59}
GLuint GLuint GLsizei count
Definition: gl.h:1545
@ SynchronizationEvent

Referenced by sys_msleep().

◆ sys_sem_set_invalid()

void sys_sem_set_invalid ( sys_sem_t sem)

Set a semaphore invalid so that sys_sem_valid returns 0

Definition at line 66 of file sys_arch.c.

67{
68 sem->Valid = 0;
69}

Referenced by sys_sem_free().

◆ sys_sem_signal()

void sys_sem_signal ( sys_sem_t sem)

Signals a semaphore

Parameters
semthe semaphore to signal

Definition at line 80 of file sys_arch.c.

81{
83}

◆ sys_sem_valid()

int sys_sem_valid ( sys_sem_t sem)

Check if a sempahore is valid/allocated: return 1 for valid, 0 for invalid

Definition at line 61 of file sys_arch.c.

62{
63 return sem->Valid;
64}

◆ sys_shutdown()

void sys_shutdown ( void  )

Definition at line 332 of file sys_arch.c.

333{
334 PLIST_ENTRY CurrentEntry;
335 thread_t Container;
336
337 /* Set the termination event */
339
340 /* Loop through the thread list and wait for each to die */
342 {
343 Container = CONTAINING_RECORD(CurrentEntry, struct _thread_t, ListEntry);
344
345 if (Container->ThreadFunction)
346 {
347 KeWaitForSingleObject(Container->Handle,
348 Executive,
350 FALSE,
351 NULL);
352
353 ZwClose(Container->Handle);
354 }
355 }
356
359}
#define KeWaitForSingleObject(pEvt, foo, a, b, c)
Definition: env_spec_w32.h:478
PLIST_ENTRY NTAPI ExInterlockedRemoveHeadList(IN OUT PLIST_ENTRY ListHead, IN OUT PKSPIN_LOCK Lock)
Definition: interlocked.c:166
VOID NTAPI ExDeleteNPagedLookasideList(IN PNPAGED_LOOKASIDE_LIST Lookaside)
Definition: lookas.c:170
NTSYSAPI NTSTATUS NTAPI ZwClose(_In_ HANDLE Handle)
HANDLE Handle
Definition: sys_arch.c:17

Referenced by LibIPShutdown().

◆ sys_thread_new()

sys_thread_t sys_thread_new ( const char name,
lwip_thread_fn  thread,
void arg,
int  stacksize,
int  prio 
)

The only thread function: Creates a new thread

Parameters
namehuman-readable name for the thread (used for debugging purposes)
threadthread-function
argparameter passed to 'thread'
stacksizestack size in bytes for the new thread (may be ignored by ports)
priopriority of the new thread (may be ignored by ports)

Definition at line 275 of file sys_arch.c.

276{
277 thread_t Container;
279
280 Container = ExAllocatePool(NonPagedPool, sizeof(*Container));
281 if (!Container)
282 return 0;
283
284 Container->ThreadFunction = thread;
285 Container->ThreadContext = arg;
286
287 Status = PsCreateSystemThread(&Container->Handle,
289 NULL,
290 NULL,
291 NULL,
293 Container);
294
295 if (!NT_SUCCESS(Status))
296 {
297 ExFreePool(Container);
298 return 0;
299 }
300
301 return 0;
302}
static HANDLE thread
Definition: service.c:33
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define THREAD_ALL_ACCESS
Definition: nt_native.h:1339
NTSTATUS NTAPI PsCreateSystemThread(OUT PHANDLE ThreadHandle, IN ACCESS_MASK DesiredAccess, IN POBJECT_ATTRIBUTES ObjectAttributes, IN HANDLE ProcessHandle, IN PCLIENT_ID ClientId, IN PKSTART_ROUTINE StartRoutine, IN PVOID StartContext)
Definition: thread.c:602
VOID NTAPI LwipThreadMain(PVOID Context)
Definition: sys_arch.c:256
void * arg
Definition: msvc.h:10

Referenced by tcpip_init().

Variable Documentation

◆ MessageLookasideList

◆ QueueEntryLookasideList

◆ StartTime

◆ TerminationEvent

KEVENT TerminationEvent

◆ ThreadListHead

LIST_ENTRY ThreadListHead
static

Definition at line 6 of file sys_arch.c.

Referenced by LwipThreadMain(), NtQueryInformationThread(), sys_init(), and sys_shutdown().

◆ ThreadListLock

KSPIN_LOCK ThreadListLock
static

Definition at line 7 of file sys_arch.c.

Referenced by LwipThreadMain(), sys_init(), and sys_shutdown().