1 /* $Id: work.c,v 1.9 2000/10/07 13:41:50 dwelch Exp $
3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS kernel
5 * FILE: mkernel/kernel/work.c
6 * PURPOSE: Manage system work queues
7 * PROGRAMMER: David Welch (welch@mcmail.com)
12 /* INCLUDES ******************************************************************/
14 #include <ddk/ntddk.h>
16 #include <internal/ps.h>
18 #include <internal/debug.h>
20 /* DEFINES *******************************************************************/
22 #define NUMBER_OF_WORKER_THREADS (5)
24 /* TYPES *********************************************************************/
26 typedef struct _WORK_QUEUE
29 * PURPOSE: Head of the list of waiting work items
34 * PURPOSE: Sychronize access to the work queue
39 * PURPOSE: Worker threads with nothing to do wait on this event
44 * PURPOSE: Thread associated with work queue
46 HANDLE Thread
[NUMBER_OF_WORKER_THREADS
];
47 } WORK_QUEUE
, *PWORK_QUEUE
;
49 /* GLOBALS *******************************************************************/
52 * PURPOSE: Queue of items waiting to be processed at normal priority
54 WORK_QUEUE EiNormalWorkQueue
;
56 WORK_QUEUE EiCriticalWorkQueue
;
58 WORK_QUEUE EiHyperCriticalWorkQueue
;
60 /* FUNCTIONS ****************************************************************/
62 static NTSTATUS
ExWorkerThreadEntryPoint(PVOID context
)
64 * FUNCTION: Entry point for a worker thread
66 * context = Parameters
68 * NOTE: To kill a worker thread you must queue an item whose callback
69 * calls PsTerminateSystemThread
72 PWORK_QUEUE queue
= (PWORK_QUEUE
)context
;
73 PWORK_QUEUE_ITEM item
;
78 current
= ExInterlockedRemoveHeadList(&queue
->Head
,
82 item
= CONTAINING_RECORD(current
,WORK_QUEUE_ITEM
,Entry
);
83 item
->Routine(item
->Context
);
87 KeWaitForSingleObject((PVOID
)&queue
->Sem
,
92 DPRINT1("Woke from wait\n");
97 static VOID
ExInitializeWorkQueue(PWORK_QUEUE WorkQueue
,
103 InitializeListHead(&WorkQueue
->Head
);
104 KeInitializeSpinLock(&WorkQueue
->Lock
);
105 KeInitializeSemaphore(&WorkQueue
->Sem
,
108 for (i
=0; i
<NUMBER_OF_WORKER_THREADS
; i
++)
110 PsCreateSystemThread(&WorkQueue
->Thread
[i
],
115 ExWorkerThreadEntryPoint
,
117 ObReferenceObjectByHandle(WorkQueue
->Thread
[i
],
123 KeSetPriorityThread(&Thread
->Tcb
,
125 ObDereferenceObject(Thread
);
129 VOID
ExInitializeWorkerThreads(VOID
)
131 ExInitializeWorkQueue(&EiNormalWorkQueue
,
133 ExInitializeWorkQueue(&EiCriticalWorkQueue
,
134 LOW_REALTIME_PRIORITY
);
135 ExInitializeWorkQueue(&EiHyperCriticalWorkQueue
,
140 ExQueueWorkItem (PWORK_QUEUE_ITEM WorkItem
,
141 WORK_QUEUE_TYPE QueueType
)
143 * FUNCTION: Inserts a work item in a queue for one of the system worker
146 * WorkItem = Item to insert
147 * QueueType = Queue to insert it in
150 assert(WorkItem
!=NULL
);
151 ASSERT_IRQL(DISPATCH_LEVEL
);
154 * Insert the item in the appropiate queue and wake up any thread
155 * waiting for something to do
159 case DelayedWorkQueue
:
160 ExInterlockedInsertTailList(&EiNormalWorkQueue
.Head
,
162 &EiNormalWorkQueue
.Lock
);
163 KeReleaseSemaphore(&EiNormalWorkQueue
.Sem
,
169 case CriticalWorkQueue
:
170 ExInterlockedInsertTailList(&EiCriticalWorkQueue
.Head
,
172 &EiCriticalWorkQueue
.Lock
);
173 KeReleaseSemaphore(&EiCriticalWorkQueue
.Sem
,
179 case HyperCriticalWorkQueue
:
180 ExInterlockedInsertTailList(&EiHyperCriticalWorkQueue
.Head
,
182 &EiHyperCriticalWorkQueue
.Lock
);
183 KeReleaseSemaphore(&EiHyperCriticalWorkQueue
.Sem
,