wined3d: Fix a comment.
[wine/hramrach.git] / dlls / netapi32 / nbcmdqueue.c
blob720826d8cc8dc41f52191dd7bef4dca54a241714
1 /* Copyright (c) 2003 Juan Lang
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2.1 of the License, or (at your option) any later version.
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17 #include "config.h"
18 #include "wine/debug.h"
19 #include "nbcmdqueue.h"
21 WINE_DEFAULT_DEBUG_CHANNEL(netbios);
23 struct NBCmdQueue
25 HANDLE heap;
26 CRITICAL_SECTION cs;
27 PNCB head;
30 #define CANCEL_EVENT_PTR(ncb) (PHANDLE)((ncb)->ncb_reserve)
31 #define NEXT_PTR(ncb) (PNCB *)((ncb)->ncb_reserve + sizeof(HANDLE))
33 /* The reserved area of an ncb will be used for the following data:
34 * - a cancelled flag (BOOL, 4 bytes??)
35 * - a handle to an event that's set by a cancelled command on completion
36 * (HANDLE, 4 bytes)
37 * These members are used in the following way
38 * - on cancel, set the event member of the reserved field (with create event)
39 * - NBCmdComplete will delete the ncb from the queue of there's no event;
40 * otherwise it will set the event and not delete the ncb
41 * - cancel must lock the queue before finding the ncb in it, and can unlock it
42 * once it's set the event (and the cancelled flag)
43 * - NBCmdComplete must lock the queue before attempting to remove the ncb or
44 * check the event
45 * - NBCmdQueueCancelAll will lock the queue, and cancel all ncb's in the queue.
46 * It'll then unlock the queue, and wait on the event in the head of the queue
47 * until there's no more ncb's in the queue.
48 * Space optimization: use the handle as a boolean. NULL == 0 => not cancelled.
49 * Non-NULL == valid handle => cancelled. This allows storing a next pointer
50 * in the ncb's reserved field as well, avoiding a memory alloc for a new
51 * command (cool).
54 struct NBCmdQueue *NBCmdQueueCreate(HANDLE heap)
56 struct NBCmdQueue *queue;
58 if (heap == NULL)
59 heap = GetProcessHeap();
60 queue = HeapAlloc(heap, 0, sizeof(struct NBCmdQueue));
61 if (queue)
63 queue->heap = heap;
64 InitializeCriticalSection(&queue->cs);
65 queue->cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": NBCmdQueue.cs");
66 queue->head = NULL;
68 return queue;
71 UCHAR NBCmdQueueAdd(struct NBCmdQueue *queue, PNCB ncb)
73 UCHAR ret;
75 TRACE(": queue %p, ncb %p\n", queue, ncb);
77 if (!queue)
78 return NRC_BADDR;
79 if (!ncb)
80 return NRC_INVADDRESS;
82 *CANCEL_EVENT_PTR(ncb) = NULL;
83 EnterCriticalSection(&queue->cs);
84 *NEXT_PTR(ncb) = queue->head;
85 queue->head = ncb;
86 ret = NRC_GOODRET;
87 LeaveCriticalSection(&queue->cs);
88 TRACE("returning 0x%02x\n", ret);
89 return ret;
92 static PNCB *NBCmdQueueFindNBC(struct NBCmdQueue *queue, PNCB ncb)
94 PNCB *ret;
96 if (!queue || !ncb)
97 ret = NULL;
98 else
100 ret = &queue->head;
101 while (ret && *ret != ncb)
102 ret = NEXT_PTR(*ret);
104 return ret;
107 UCHAR NBCmdQueueCancel(struct NBCmdQueue *queue, PNCB ncb)
109 UCHAR ret;
110 PNCB *spot;
112 TRACE(": queue %p, ncb %p\n", queue, ncb);
114 if (!queue)
115 return NRC_BADDR;
116 if (!ncb)
117 return NRC_INVADDRESS;
119 EnterCriticalSection(&queue->cs);
120 spot = NBCmdQueueFindNBC(queue, ncb);
121 if (spot)
123 *CANCEL_EVENT_PTR(*spot) = CreateEventW(NULL, FALSE, FALSE, NULL);
124 WaitForSingleObject(*CANCEL_EVENT_PTR(*spot), INFINITE);
125 CloseHandle(*CANCEL_EVENT_PTR(*spot));
126 *spot = *NEXT_PTR(*spot);
127 if (ncb->ncb_retcode == NRC_CMDCAN)
128 ret = NRC_CMDCAN;
129 else
130 ret = NRC_CANOCCR;
132 else
133 ret = NRC_INVADDRESS;
134 LeaveCriticalSection(&queue->cs);
135 TRACE("returning 0x%02x\n", ret);
136 return ret;
139 UCHAR NBCmdQueueComplete(struct NBCmdQueue *queue, PNCB ncb, UCHAR retcode)
141 UCHAR ret;
142 PNCB *spot;
144 TRACE(": queue %p, ncb %p\n", queue, ncb);
146 if (!queue)
147 return NRC_BADDR;
148 if (!ncb)
149 return NRC_INVADDRESS;
151 EnterCriticalSection(&queue->cs);
152 spot = NBCmdQueueFindNBC(queue, ncb);
153 if (spot)
155 if (*CANCEL_EVENT_PTR(*spot))
156 SetEvent(*CANCEL_EVENT_PTR(*spot));
157 else
158 *spot = *NEXT_PTR(*spot);
159 ret = NRC_GOODRET;
161 else
162 ret = NRC_INVADDRESS;
163 LeaveCriticalSection(&queue->cs);
164 TRACE("returning 0x%02x\n", ret);
165 return ret;
168 UCHAR NBCmdQueueCancelAll(struct NBCmdQueue *queue)
170 UCHAR ret;
172 TRACE(": queue %p\n", queue);
174 if (!queue)
175 return NRC_BADDR;
177 EnterCriticalSection(&queue->cs);
178 while (queue->head)
180 TRACE(": waiting for ncb %p (command 0x%02x)\n", queue->head,
181 queue->head->ncb_command);
182 NBCmdQueueCancel(queue, queue->head);
184 LeaveCriticalSection(&queue->cs);
185 ret = NRC_GOODRET;
186 TRACE("returning 0x%02x\n", ret);
187 return ret;
190 void NBCmdQueueDestroy(struct NBCmdQueue *queue)
192 TRACE(": queue %p\n", queue);
194 if (queue)
196 NBCmdQueueCancelAll(queue);
197 queue->cs.DebugInfo->Spare[0] = 0;
198 DeleteCriticalSection(&queue->cs);
199 HeapFree(queue->heap, 0, queue);