ReactOS  0.4.14-dev-583-g2a1ba2c
sys_arch.c
Go to the documentation of this file.
1 #include "lwip/sys.h"
2 
3 #include "lwip/tcp.h"
4 #include "lwip/pbuf.h"
5 #include "lwip/err.h"
6 
7 #include "rosip.h"
8 
9 #include <debug.h>
10 
13 
17 
19 
20 typedef struct _thread_t
21 {
23  void (* ThreadFunction)(void *arg);
26 } *thread_t;
27 
29 {
30  LARGE_INTEGER CurrentTime;
31 
32  KeQuerySystemTime(&CurrentTime);
33 
34  return (CurrentTime.QuadPart - StartTime.QuadPart) / 10000;
35 }
36 
37 void
39 {
40  /* Preempt the dispatcher */
42 }
43 
44 void
46 {
47  KeLowerIrql(lev);
48 }
49 
50 err_t
52 {
53  ASSERT(count == 0 || count == 1);
54 
55  /* It seems lwIP uses the semaphore implementation as either a completion event or a lock
56  * so I optimize for this case by using a synchronization event and setting its initial state
57  * to signalled for a lock and non-signalled for a completion event */
58 
60 
61  sem->Valid = 1;
62 
63  return ERR_OK;
64 }
65 
67 {
68  return sem->Valid;
69 }
70 
72 {
73  sem->Valid = 0;
74 }
75 
76 void
78 {
79  /* No op (allocated in stack) */
80 
82 }
83 
84 void
86 {
88 }
89 
90 u32_t
92 {
93  LARGE_INTEGER LargeTimeout, PreWaitTime, PostWaitTime;
94  UINT64 TimeDiff;
96  PVOID WaitObjects[] = {&sem->Event, &TerminationEvent};
97 
98  LargeTimeout.QuadPart = Int32x32To64(timeout, -10000);
99 
100  KeQuerySystemTime(&PreWaitTime);
101 
103  WaitObjects,
104  WaitAny,
105  Executive,
106  KernelMode,
107  FALSE,
108  timeout != 0 ? &LargeTimeout : NULL,
109  NULL);
110  if (Status == STATUS_WAIT_0)
111  {
112  KeQuerySystemTime(&PostWaitTime);
113  TimeDiff = PostWaitTime.QuadPart - PreWaitTime.QuadPart;
114  TimeDiff /= 10000;
115 
116  return TimeDiff;
117  }
118  else if (Status == STATUS_WAIT_1)
119  {
120  /* DON'T remove ourselves from the thread list! */
122 
123  /* We should never get here! */
124  ASSERT(FALSE);
125 
126  return 0;
127  }
128 
129  return SYS_ARCH_TIMEOUT;
130 }
131 
132 err_t
134 {
136 
138 
140 
141  mbox->Valid = 1;
142 
143  return ERR_OK;
144 }
145 
147 {
148  return mbox->Valid;
149 }
150 
152 {
153  mbox->Valid = 0;
154 }
155 
156 void
158 {
160 
162 }
163 
164 void
166 {
167  PLWIP_MESSAGE_CONTAINER Container;
168 
169  Container = ExAllocatePool(NonPagedPool, sizeof(*Container));
170  ASSERT(Container);
171 
172  Container->Message = msg;
173 
175  &Container->ListEntry,
176  &mbox->Lock);
177 
179 }
180 
181 u32_t
183 {
184  LARGE_INTEGER LargeTimeout, PreWaitTime, PostWaitTime;
185  UINT64 TimeDiff;
187  PVOID Message;
188  PLWIP_MESSAGE_CONTAINER Container;
190  KIRQL OldIrql;
191  PVOID WaitObjects[] = {&mbox->Event, &TerminationEvent};
192 
193  LargeTimeout.QuadPart = Int32x32To64(timeout, -10000);
194 
195  KeQuerySystemTime(&PreWaitTime);
196 
198  WaitObjects,
199  WaitAny,
200  Executive,
201  KernelMode,
202  FALSE,
203  timeout != 0 ? &LargeTimeout : NULL,
204  NULL);
205 
206  if (Status == STATUS_WAIT_0)
207  {
210  ASSERT(Entry);
211  if (IsListEmpty(&mbox->ListHead))
214 
215  Container = CONTAINING_RECORD(Entry, LWIP_MESSAGE_CONTAINER, ListEntry);
216  Message = Container->Message;
217  ExFreePool(Container);
218 
219  if (msg)
220  *msg = Message;
221 
222  KeQuerySystemTime(&PostWaitTime);
223  TimeDiff = PostWaitTime.QuadPart - PreWaitTime.QuadPart;
224  TimeDiff /= 10000;
225 
226  return TimeDiff;
227  }
228  else if (Status == STATUS_WAIT_1)
229  {
230  /* DON'T remove ourselves from the thread list! */
232 
233  /* We should never get here! */
234  ASSERT(FALSE);
235 
236  return 0;
237  }
238 
239  return SYS_ARCH_TIMEOUT;
240 }
241 
242 u32_t
244 {
246  return 0;
247  else
248  return SYS_MBOX_EMPTY;
249 }
250 
251 err_t
253 {
255 
256  return ERR_OK;
257 }
258 
259 VOID
260 NTAPI
262 {
263  thread_t Container = (thread_t)Context;
264  KIRQL OldIrql;
265 
267 
268  Container->ThreadFunction(Container->ThreadContext);
269 
271  RemoveEntryList(&Container->ListEntry);
273 
274  ExFreePool(Container);
275 
277 }
278 
280 sys_thread_new(const char *name, lwip_thread_fn thread, void *arg, int stacksize, int prio)
281 {
282  thread_t Container;
284 
285  Container = ExAllocatePool(NonPagedPool, sizeof(*Container));
286  if (!Container)
287  return 0;
288 
289  Container->ThreadFunction = thread;
290  Container->ThreadContext = arg;
291 
292  Status = PsCreateSystemThread(&Container->Handle,
294  NULL,
295  NULL,
296  NULL,
298  Container);
299 
300  if (!NT_SUCCESS(Status))
301  {
302  ExFreePool(Container);
303  return 0;
304  }
305 
306  return 0;
307 }
308 
309 void
310 sys_init(void)
311 {
314 
316 
318 
320  NULL,
321  NULL,
322  0,
323  sizeof(struct lwip_callback_msg),
325  0);
326 
328  NULL,
329  NULL,
330  0,
331  sizeof(QUEUE_ENTRY),
333  0);
334 }
335 
336 void
338 {
339  PLIST_ENTRY CurrentEntry;
340  thread_t Container;
341 
342  /* Set the termination event */
344 
345  /* Loop through the thread list and wait for each to die */
346  while ((CurrentEntry = ExInterlockedRemoveHeadList(&ThreadListHead, &ThreadListLock)))
347  {
348  Container = CONTAINING_RECORD(CurrentEntry, struct _thread_t, ListEntry);
349 
350  if (Container->ThreadFunction)
351  {
352  KeWaitForSingleObject(Container->Handle,
353  Executive,
354  KernelMode,
355  FALSE,
356  NULL);
357 
358  ZwClose(Container->Handle);
359  }
360  }
361 
364 }
#define KeQuerySystemTime(t)
Definition: env_spec_w32.h:570
HANDLE Handle
Definition: sys_arch.c:22
void sys_sem_free(sys_sem_t *sem)
Definition: sys_arch.c:77
void sys_shutdown(void)
Definition: sys_arch.c:337
#define THREAD_ALL_ACCESS
Definition: nt_native.h:1339
#define KeRaiseIrql(irql, oldIrql)
Definition: env_spec_w32.h:597
KEVENT Event
Definition: sys_arch.h:14
#define KeLowerIrql(oldIrql)
Definition: env_spec_w32.h:602
LIST_ENTRY ListEntry
Definition: sys_arch.h:25
struct LOOKASIDE_ALIGN _NPAGED_LOOKASIDE_LIST NPAGED_LOOKASIDE_LIST
LIST_ENTRY ListHead
Definition: sys_arch.h:13
struct png_info_def **typedef void(__cdecl typeof(png_destroy_read_struct))(struct png_struct_def **
Definition: typeof.h:49
void * ThreadContext
Definition: sys_arch.c:24
u32_t sys_thread_t
Definition: sys_arch.h:20
struct _Entry Entry
Definition: kefuncs.h:640
err_t sys_sem_new(sys_sem_t *sem, u8_t count)
Definition: sys_arch.c:51
NTSYSAPI NTSTATUS NTAPI ZwClose(_In_ HANDLE Handle)
u32_t sys_now(void)
Definition: sys_arch.c:28
GLuint GLuint GLsizei count
Definition: gl.h:1545
void sys_sem_set_invalid(sys_sem_t *sem)
Definition: sys_arch.c:71
PLIST_ENTRY NTAPI ExInterlockedInsertTailList(IN OUT PLIST_ENTRY ListHead, IN OUT PLIST_ENTRY ListEntry, IN OUT PKSPIN_LOCK Lock)
Definition: interlocked.c:140
LONG NTSTATUS
Definition: precomp.h:26
u32_t sys_arch_sem_wait(sys_sem_t *sem, u32_t timeout)
Definition: sys_arch.c:91
#define SYS_ARCH_TIMEOUT
Definition: sys.h:78
NPAGED_LOOKASIDE_LIST MessageLookasideList
Definition: sys_arch.c:15
void * arg
Definition: msvc.h:10
Definition: dhcpd.h:245
KIRQL sys_prot_t
Definition: sys_arch.h:18
#define LWIP_QUEUE_TAG
Definition: rosip.h:12
LONG NTAPI KeSetEvent(IN PKEVENT Event, IN KPRIORITY Increment, IN BOOLEAN Wait)
Definition: eventobj.c:159
static LARGE_INTEGER StartTime
Definition: sys_arch.c:18
NTSTATUS NTAPI KeWaitForSingleObject(IN PVOID Object, IN KWAIT_REASON WaitReason, IN KPROCESSOR_MODE WaitMode, IN BOOLEAN Alertable, IN PLARGE_INTEGER Timeout OPTIONAL)
Definition: wait.c:416
_Must_inspect_result_ FORCEINLINE BOOLEAN IsListEmpty(_In_ const LIST_ENTRY *ListHead)
Definition: rtlfuncs.h:57
FORCEINLINE BOOLEAN RemoveEntryList(_In_ PLIST_ENTRY Entry)
Definition: rtlfuncs.h:105
DWORD ThreadFunction
Definition: mmdrv.h:85
err_t sys_mbox_new(sys_mbox_t *mbox, int size)
Definition: sys_arch.c:133
void sys_init(void)
Definition: sys_arch.c:310
UCHAR KIRQL
Definition: env_spec_w32.h:591
#define STATUS_WAIT_1
Definition: ntstatus.h:71
PLIST_ENTRY NTAPI ExInterlockedInsertHeadList(IN OUT PLIST_ENTRY ListHead, IN OUT PLIST_ENTRY ListEntry, IN OUT PKSPIN_LOCK Lock)
Definition: interlocked.c:114
#define STATUS_WAIT_0
Definition: ntstatus.h:223
NTSTATUS(* NTAPI)(IN PFILE_FULL_EA_INFORMATION EaBuffer, IN ULONG EaLength, OUT PULONG ErrorOffset)
Definition: IoEaTest.cpp:117
Definition: rosip.h:17
u32_t sys_arch_mbox_fetch(sys_mbox_t *mbox, void **msg, u32_t timeout)
Definition: sys_arch.c:182
err_t sys_mbox_trypost(sys_mbox_t *mbox, void *msg)
Definition: sys_arch.c:252
FORCEINLINE VOID KeInitializeSpinLock(_Out_ PKSPIN_LOCK SpinLock)
Definition: kefuncs.h:251
PLIST_ENTRY NTAPI ExInterlockedRemoveHeadList(IN OUT PLIST_ENTRY ListHead, IN OUT PKSPIN_LOCK Lock)
Definition: interlocked.c:166
smooth NULL
Definition: ftsmooth.c:416
FORCEINLINE PLIST_ENTRY RemoveHeadList(_Inout_ PLIST_ENTRY ListHead)
Definition: rtlfuncs.h:128
s8_t err_t
Definition: err.h:47
KEVENT TerminationEvent
Definition: sys_arch.c:14
PFLT_MESSAGE_WAITER_QUEUE CONTAINING_RECORD(Csq, DEVICE_EXTENSION, IrpQueue)) -> WaiterQ.mLock) _IRQL_raises_(DISPATCH_LEVEL) VOID NTAPI FltpAcquireMessageWaiterLock(_In_ PIO_CSQ Csq, _Out_ PKIRQL Irql)
Definition: Messaging.c:560
#define SYS_MBOX_EMPTY
Definition: sys.h:83
#define ERR_OK
Definition: err.h:52
void(* lwip_thread_fn)(void *arg)
Definition: sys.h:89
int Valid
Definition: sys_arch.h:15
u32_t sys_arch_mbox_tryfetch(sys_mbox_t *mbox, void **msg)
Definition: sys_arch.c:243
GLsizeiptr size
Definition: glext.h:5919
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:222
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
unsigned long u32_t
Definition: cc.h:25
#define KeAcquireSpinLock(sl, irql)
Definition: env_spec_w32.h:609
void sys_sem_signal(sys_sem_t *sem)
Definition: sys_arch.c:85
sys_thread_t sys_thread_new(const char *name, lwip_thread_fn thread, void *arg, int stacksize, int prio)
Definition: sys_arch.c:280
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
ASSERT((InvokeOnSuccess||InvokeOnError||InvokeOnCancel) ?(CompletionRoutine !=NULL) :TRUE)
CHAR Message[80]
Definition: alive.c:5
static HANDLE thread
Definition: service.c:33
LIST_ENTRY ListEntry
Definition: sys_arch.c:25
_Requires_lock_held_ Interrupt _Releases_lock_ Interrupt _In_ _IRQL_restores_ KIRQL OldIrql
Definition: kefuncs.h:803
void sys_arch_unprotect(sys_prot_t lev)
Definition: sys_arch.c:45
#define ExAllocatePool(type, size)
Definition: fbtusb.h:44
void sys_mbox_set_invalid(sys_mbox_t *mbox)
Definition: sys_arch.c:151
int sys_mbox_valid(sys_mbox_t *mbox)
Definition: sys_arch.c:146
Definition: typedefs.h:117
Status
Definition: gdiplustypes.h:24
#define DISPATCH_LEVEL
Definition: env_spec_w32.h:696
int sys_sem_valid(sys_sem_t *sem)
Definition: sys_arch.c:66
void sys_mbox_post(sys_mbox_t *mbox, void *msg)
Definition: sys_arch.c:165
#define KeInitializeEvent(pEvt, foo, foo2)
Definition: env_spec_w32.h:477
#define LWIP_MESSAGE_TAG
Definition: rosip.h:11
NPAGED_LOOKASIDE_LIST QueueEntryLookasideList
Definition: sys_arch.c:16
static HANDLE sem
Definition: sync.c:677
#define InitializeListHead(ListHead)
Definition: env_spec_w32.h:944
NTSTATUS NTAPI PsTerminateSystemThread(IN NTSTATUS ExitStatus)
Definition: kill.c:1144
ULONG KSPIN_LOCK
Definition: env_spec_w32.h:72
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
unsigned char u8_t
Definition: cc.h:23
void(* ThreadFunction)(void *arg)
Definition: sys_arch.c:23
static KSPIN_LOCK ThreadListLock
Definition: sys_arch.c:12
#define KeReleaseSpinLock(sl, irql)
Definition: env_spec_w32.h:627
struct _thread_t * thread_t
void sys_arch_protect(sys_prot_t *lev)
Definition: sys_arch.c:38
#define msg(x)
Definition: auth_time.c:54
VOID NTAPI LwipThreadMain(PVOID Context)
Definition: sys_arch.c:261
Definition: name.c:38
static LIST_ENTRY ThreadListHead
Definition: sys_arch.c:11
#define IO_NO_INCREMENT
Definition: iotypes.h:566
unsigned long long UINT64
VOID NTAPI KeClearEvent(IN PKEVENT Event)
Definition: eventobj.c:22
return STATUS_SUCCESS
Definition: btrfs.c:2938
VOID NTAPI ExDeleteNPagedLookasideList(IN PNPAGED_LOOKASIDE_LIST Lookaside)
Definition: lookas.c:174
static sys_mbox_t mbox
Definition: tcpip.c:56
base of all file and directory entries
Definition: entries.h:82
#define Int32x32To64(a, b)
#define ExFreePool(addr)
Definition: env_spec_w32.h:352
KSPIN_LOCK Lock
Definition: sys_arch.h:12
LONGLONG QuadPart
Definition: typedefs.h:112
void sys_mbox_free(sys_mbox_t *mbox)
Definition: sys_arch.c:157