A little library for making it easier to get to PASSIVE_LEVEL. Just contains
authorArt Yerkes <art.yerkes@gmail.com>
Sun, 11 Dec 2005 08:26:16 +0000 (08:26 +0000)
committerArt Yerkes <art.yerkes@gmail.com>
Sun, 11 Dec 2005 08:26:16 +0000 (08:26 +0000)
boilerplate for using work items.

svn path=/trunk/; revision=20055

reactos/drivers/lib/chew/chew.xml [new file with mode: 0644]
reactos/drivers/lib/chew/workqueue.c [new file with mode: 0644]
reactos/include/chew/chew.h [new file with mode: 0644]

diff --git a/reactos/drivers/lib/chew/chew.xml b/reactos/drivers/lib/chew/chew.xml
new file mode 100644 (file)
index 0000000..1598417
--- /dev/null
@@ -0,0 +1,7 @@
+<module name="chew" type="staticlibrary">\r
+       <define name="__USE_W32API" />\r
+       <define name="_NTOSKRNL_" />\r
+       <include base="chew">include</include>\r
+       <include base="reactos">w32api/include/ddk</include>\r
+       <file>workqueue.c</file>\r
+</module>
\ No newline at end of file
diff --git a/reactos/drivers/lib/chew/workqueue.c b/reactos/drivers/lib/chew/workqueue.c
new file mode 100644 (file)
index 0000000..1f041bc
--- /dev/null
@@ -0,0 +1,101 @@
+/*\r
+ * COPYRIGHT:   See COPYING in the top level directory\r
+ * PROJECT:     ReactOS TCP/IP protocol driver\r
+ * FILE:        tcpip/main.c\r
+ * PURPOSE:     Common Highlevel Executive Worker\r
+ * PROGRAMMERS: Art Yerkes\r
+ * REVISIONS:\r
+ *   CSH 10/12-2005 Created\r
+ */\r
+#include <ntddk.h>\r
+#include <chew/chew.h>\r
+\r
+//#define NDEBUG\r
+\r
+#define FOURCC(w,x,y,z) (((w) << 24) | ((x) << 16) | ((y) << 8) | (z))\r
+\r
+PDEVICE_OBJECT WorkQueueDevice;\r
+LIST_ENTRY     WorkQueue;\r
+KSPIN_LOCK     WorkQueueLock;\r
+\r
+typedef struct _WORK_ITEM {\r
+    LIST_ENTRY Entry;\r
+    PIO_WORKITEM WorkItem;\r
+    VOID (*Worker)( PVOID Data );\r
+    CHAR UserSpace[1];\r
+} WORK_ITEM, *PWORK_ITEM;\r
+\r
+VOID ChewInit( PDEVICE_OBJECT DeviceObject ) {\r
+    WorkQueueDevice = DeviceObject;\r
+    InitializeListHead( &WorkQueue );\r
+    KeInitializeSpinLock( &WorkQueueLock );\r
+}\r
+\r
+VOID ChewShutdown() {\r
+    KIRQL OldIrql;\r
+    PLIST_ENTRY Entry;\r
+    PWORK_ITEM WorkItem;\r
+\r
+    KeAcquireSpinLock( &WorkQueueLock, &OldIrql );\r
+    \r
+    while( !IsListEmpty( &WorkQueue ) ) {\r
+       Entry = RemoveHeadList( &WorkQueue );\r
+       WorkItem = CONTAINING_RECORD( Entry, WORK_ITEM, Entry );\r
+       IoFreeWorkItem( WorkItem->WorkItem );\r
+       ExFreePool( WorkItem );\r
+    }\r
+\r
+    KeReleaseSpinLock( &WorkQueueLock, OldIrql );\r
+}\r
+\r
+VOID STDCALL ChewWorkItem( PDEVICE_OBJECT DeviceObject, PVOID ChewItem ) {\r
+    PWORK_ITEM WorkItem = ChewItem;\r
+\r
+    if( WorkItem->Worker ) \r
+       WorkItem->Worker( WorkItem->UserSpace );\r
+\r
+    IoFreeWorkItem( WorkItem->WorkItem );\r
+    ExFreePool( WorkItem );\r
+}\r
+    \r
+BOOLEAN ChewCreate\r
+( PVOID *ItemPtr, UINT Bytes, VOID (*Worker)( PVOID ), PVOID UserSpace ) {\r
+    PWORK_ITEM Item;\r
+    if( KeGetCurrentIrql() == PASSIVE_LEVEL ) {\r
+       *ItemPtr = NULL;\r
+       Worker(UserSpace);\r
+       return TRUE;\r
+    } else {\r
+       Item = ExAllocatePoolWithTag\r
+           ( NonPagedPool, \r
+             sizeof( WORK_ITEM ) + Bytes - 1, \r
+             FOURCC('C','H','E','W') );\r
+       \r
+       if( Item ) {\r
+           Item->WorkItem = IoAllocateWorkItem( WorkQueueDevice );\r
+           if( !Item->WorkItem ) {\r
+               ExFreePool( Item );\r
+               return FALSE;\r
+           }\r
+           Item->Worker = Worker;\r
+           if( Bytes && UserSpace )\r
+               RtlCopyMemory( Item->UserSpace, UserSpace, Bytes );\r
+           \r
+           ExInterlockedInsertTailList\r
+               ( &WorkQueue, &Item->Entry, &WorkQueueLock );\r
+           IoQueueWorkItem\r
+               ( Item->WorkItem, ChewWorkItem, CriticalWorkQueue, Item );\r
+\r
+           *ItemPtr = Item;\r
+\r
+           return TRUE;\r
+       } else\r
+           return FALSE;\r
+    }\r
+}\r
+\r
+VOID ChewRemove( PVOID Item ) {\r
+    PWORK_ITEM WorkItem = Item;\r
+    IoFreeWorkItem( WorkItem->WorkItem );\r
+    ExFreePool( WorkItem );\r
+}\r
diff --git a/reactos/include/chew/chew.h b/reactos/include/chew/chew.h
new file mode 100644 (file)
index 0000000..d87eb82
--- /dev/null
@@ -0,0 +1,10 @@
+#ifndef _REACTOS_CHEW_H\r
+#define _REACTOS_CHEW_H\r
+\r
+VOID ChewInit( PDEVICE_OBJECT DeviceObject );\r
+VOID ChewShutdown();\r
+BOOLEAN ChewCreate\r
+( PVOID *Item, UINT Bytes, VOID (*Worker)(PVOID), PVOID UserSpace );\r
+VOID ChewRemove( PVOID Item );\r
+\r
+#endif/*_REACTOS_CHEW_H*/\r