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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 #include "wine/debug.h"
19 #include "nbcmdqueue.h"
21 WINE_DEFAULT_DEBUG_CHANNEL(netbios);
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
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
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
54 struct NBCmdQueue *NBCmdQueueCreate(HANDLE heap)
56 struct NBCmdQueue *queue;
59 heap = GetProcessHeap();
60 queue = (struct NBCmdQueue *)HeapAlloc(heap, 0, sizeof(struct NBCmdQueue));
64 InitializeCriticalSection(&queue->cs);
70 UCHAR NBCmdQueueAdd(struct NBCmdQueue *queue, PNCB ncb)
74 TRACE(": queue %p, ncb %p\n", queue, ncb);
79 return NRC_INVADDRESS;
81 *CANCEL_EVENT_PTR(ncb) = NULL;
82 EnterCriticalSection(&queue->cs);
83 *NEXT_PTR(ncb) = queue->head;
86 LeaveCriticalSection(&queue->cs);
87 TRACE("returning 0x%02x\n", ret);
91 static PNCB *NBCmdQueueFindNBC(struct NBCmdQueue *queue, PNCB ncb)
100 while (ret && *ret != ncb)
101 ret = NEXT_PTR(*ret);
106 UCHAR NBCmdQueueCancel(struct NBCmdQueue *queue, PNCB ncb)
111 TRACE(": queue %p, ncb %p\n", queue, ncb);
116 return NRC_INVADDRESS;
118 EnterCriticalSection(&queue->cs);
119 spot = NBCmdQueueFindNBC(queue, ncb);
122 *CANCEL_EVENT_PTR(*spot) = CreateEventW(NULL, FALSE, FALSE, NULL);
123 WaitForSingleObject(*CANCEL_EVENT_PTR(*spot), INFINITE);
124 CloseHandle(*CANCEL_EVENT_PTR(*spot));
125 *spot = *NEXT_PTR(*spot);
126 if (ncb->ncb_retcode == NRC_CMDCAN)
132 ret = NRC_INVADDRESS;
133 LeaveCriticalSection(&queue->cs);
134 TRACE("returning 0x%02x\n", ret);
138 UCHAR NBCmdQueueComplete(struct NBCmdQueue *queue, PNCB ncb, UCHAR retcode)
143 TRACE(": queue %p, ncb %p\n", queue, ncb);
148 return NRC_INVADDRESS;
150 EnterCriticalSection(&queue->cs);
151 spot = NBCmdQueueFindNBC(queue, ncb);
154 if (*CANCEL_EVENT_PTR(*spot))
155 SetEvent(*CANCEL_EVENT_PTR(*spot));
157 *spot = *NEXT_PTR(*spot);
161 ret = NRC_INVADDRESS;
162 LeaveCriticalSection(&queue->cs);
163 TRACE("returning 0x%02x\n", ret);
167 UCHAR NBCmdQueueCancelAll(struct NBCmdQueue *queue)
171 TRACE(": queue %p\n", queue);
176 EnterCriticalSection(&queue->cs);
179 TRACE(": waiting for ncb %p (command 0x%02x)\n", queue->head,
180 queue->head->ncb_command);
181 NBCmdQueueCancel(queue, queue->head);
183 LeaveCriticalSection(&queue->cs);
185 TRACE("returning 0x%02x\n", ret);
189 void NBCmdQueueDestroy(struct NBCmdQueue *queue)
191 TRACE(": queue %p\n", queue);
195 NBCmdQueueCancelAll(queue);
196 DeleteCriticalSection(&queue->cs);
197 HeapFree(queue->heap, 0, queue);