ole32: Fix some races in the global interface table implementation.
[wine/testsucceed.git] / dlls / ole32 / git.c
blobb51990dc7fe4fbfeb2ff5fd73cb332cd3f74737e
1 /*
2 * Implementation of the StdGlobalInterfaceTable object
4 * The GlobalInterfaceTable (GIT) object is used to marshal interfaces between
5 * threading apartments (contexts). When you want to pass an interface but not
6 * as a parameter, it wouldn't get marshalled automatically, so you can use this
7 * object to insert the interface into a table, and you get back a cookie.
8 * Then when it's retrieved, it'll be unmarshalled into the right apartment.
10 * Copyright 2003 Mike Hearn <mike@theoretic.com>
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; either
15 * version 2.1 of the License, or (at your option) any later version.
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27 #include <stdarg.h>
29 #define COBJMACROS
30 #define NONAMELESSUNION
31 #define NONAMELESSSTRUCT
33 #include "windef.h"
34 #include "winbase.h"
35 #include "winuser.h"
36 #include "objbase.h"
37 #include "ole2.h"
38 #include "winerror.h"
40 #include "compobj_private.h"
42 #include "wine/list.h"
43 #include "wine/debug.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(ole);
47 /****************************************************************************
48 * StdGlobalInterfaceTable definition
50 * This class implements IGlobalInterfaceTable and is a process-wide singleton
51 * used for marshalling interfaces between threading apartments using cookies.
54 /* Each entry in the linked list of GIT entries */
55 typedef struct StdGITEntry
57 DWORD cookie;
58 IID iid; /* IID of the interface */
59 IStream* stream; /* Holds the marshalled interface */
61 struct list entry;
62 } StdGITEntry;
64 /* Class data */
65 typedef struct StdGlobalInterfaceTableImpl
67 const IGlobalInterfaceTableVtbl *lpVtbl;
69 ULONG ref;
70 struct list list;
71 ULONG nextCookie;
73 } StdGlobalInterfaceTableImpl;
75 void* StdGlobalInterfaceTableInstance;
77 static CRITICAL_SECTION git_section;
78 static CRITICAL_SECTION_DEBUG critsect_debug =
80 0, 0, &git_section,
81 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
82 0, 0, { (DWORD_PTR)(__FILE__ ": global interface table") }
84 static CRITICAL_SECTION git_section = { &critsect_debug, -1, 0, 0, 0, 0 };
87 /** This destroys it again. It should revoke all the held interfaces first **/
88 static void StdGlobalInterfaceTable_Destroy(void* self)
90 TRACE("(%p)\n", self);
91 FIXME("Revoke held interfaces here\n");
93 HeapFree(GetProcessHeap(), 0, self);
94 StdGlobalInterfaceTableInstance = NULL;
97 /***
98 * A helper function to traverse the list and find the entry that matches the cookie.
99 * Returns NULL if not found. Must be called inside git_section critical section.
101 static StdGITEntry*
102 StdGlobalInterfaceTable_FindEntry(IGlobalInterfaceTable* iface, DWORD cookie)
104 StdGlobalInterfaceTableImpl* const self = (StdGlobalInterfaceTableImpl*) iface;
105 StdGITEntry* e;
107 TRACE("iface=%p, cookie=0x%x\n", iface, (UINT)cookie);
109 LIST_FOR_EACH_ENTRY(e, &self->list, StdGITEntry, entry) {
110 if (e->cookie == cookie)
111 return e;
114 TRACE("Entry not found\n");
115 return NULL;
118 /***
119 * Here's the boring boilerplate stuff for IUnknown
122 static HRESULT WINAPI
123 StdGlobalInterfaceTable_QueryInterface(IGlobalInterfaceTable* iface,
124 REFIID riid, void** ppvObject)
126 /* Make sure silly coders can't crash us */
127 if (ppvObject == 0) return E_INVALIDARG;
129 *ppvObject = 0; /* assume we don't have the interface */
131 /* Do we implement that interface? */
132 if (IsEqualIID(&IID_IUnknown, riid) ||
133 IsEqualIID(&IID_IGlobalInterfaceTable, riid))
134 *ppvObject = iface;
135 else
136 return E_NOINTERFACE;
138 /* Now inc the refcount */
139 IGlobalInterfaceTable_AddRef(iface);
140 return S_OK;
143 static ULONG WINAPI
144 StdGlobalInterfaceTable_AddRef(IGlobalInterfaceTable* iface)
146 StdGlobalInterfaceTableImpl* const self = (StdGlobalInterfaceTableImpl*) iface;
148 /* InterlockedIncrement(&self->ref); */
149 return self->ref;
152 static ULONG WINAPI
153 StdGlobalInterfaceTable_Release(IGlobalInterfaceTable* iface)
155 StdGlobalInterfaceTableImpl* const self = (StdGlobalInterfaceTableImpl*) iface;
157 /* InterlockedDecrement(&self->ref); */
158 if (self->ref == 0) {
159 /* Hey ho, it's time to go, so long again 'till next weeks show! */
160 StdGlobalInterfaceTable_Destroy(self);
161 return 0;
164 return self->ref;
167 /***
168 * Now implement the actual IGlobalInterfaceTable interface
171 static HRESULT WINAPI
172 StdGlobalInterfaceTable_RegisterInterfaceInGlobal(
173 IGlobalInterfaceTable* iface, IUnknown* pUnk,
174 REFIID riid, DWORD* pdwCookie)
176 StdGlobalInterfaceTableImpl* const self = (StdGlobalInterfaceTableImpl*) iface;
177 IStream* stream = NULL;
178 HRESULT hres;
179 StdGITEntry* entry;
180 LARGE_INTEGER zero;
182 TRACE("iface=%p, pUnk=%p, riid=%s, pdwCookie=0x%p\n", iface, pUnk, debugstr_guid(riid), pdwCookie);
184 if (pUnk == NULL) return E_INVALIDARG;
186 /* marshal the interface */
187 TRACE("About to marshal the interface\n");
189 hres = CreateStreamOnHGlobal(0, TRUE, &stream);
190 if (hres) return hres;
191 hres = CoMarshalInterface(stream, riid, pUnk, MSHCTX_INPROC, NULL, MSHLFLAGS_TABLESTRONG);
192 if (hres)
194 IStream_Release(stream);
195 return hres;
198 zero.QuadPart = 0;
199 IStream_Seek(stream, zero, STREAM_SEEK_SET, NULL);
201 entry = HeapAlloc(GetProcessHeap(), 0, sizeof(StdGITEntry));
202 if (entry == NULL) return E_OUTOFMEMORY;
204 EnterCriticalSection(&git_section);
206 entry->iid = *riid;
207 entry->stream = stream;
208 entry->cookie = self->nextCookie;
209 self->nextCookie++; /* inc the cookie count */
211 /* insert the new entry at the end of the list */
212 list_add_tail(&self->list, &entry->entry);
214 /* and return the cookie */
215 *pdwCookie = entry->cookie;
217 LeaveCriticalSection(&git_section);
219 TRACE("Cookie is 0x%x\n", entry->cookie);
220 return S_OK;
223 static HRESULT WINAPI
224 StdGlobalInterfaceTable_RevokeInterfaceFromGlobal(
225 IGlobalInterfaceTable* iface, DWORD dwCookie)
227 StdGITEntry* entry;
228 HRESULT hr;
230 TRACE("iface=%p, dwCookie=0x%x\n", iface, (UINT)dwCookie);
232 EnterCriticalSection(&git_section);
234 entry = StdGlobalInterfaceTable_FindEntry(iface, dwCookie);
235 if (entry == NULL) {
236 TRACE("Entry not found\n");
237 LeaveCriticalSection(&git_section);
238 return E_INVALIDARG; /* not found */
241 list_remove(&entry->entry);
243 LeaveCriticalSection(&git_section);
245 /* Free the stream */
246 hr = CoReleaseMarshalData(entry->stream);
247 if (hr != S_OK)
249 WARN("Failed to release marshal data, hr = 0x%08x\n", hr);
250 return hr;
252 IStream_Release(entry->stream);
254 HeapFree(GetProcessHeap(), 0, entry);
255 return S_OK;
258 static HRESULT WINAPI
259 StdGlobalInterfaceTable_GetInterfaceFromGlobal(
260 IGlobalInterfaceTable* iface, DWORD dwCookie,
261 REFIID riid, void **ppv)
263 StdGITEntry* entry;
264 HRESULT hres;
265 LPUNKNOWN lpUnk;
266 IStream *stream;
268 TRACE("dwCookie=0x%x, riid=%s, ppv=%p\n", dwCookie, debugstr_guid(riid), ppv);
270 EnterCriticalSection(&git_section);
272 entry = StdGlobalInterfaceTable_FindEntry(iface, dwCookie);
273 if (entry == NULL) {
274 LeaveCriticalSection(&git_section);
275 return E_INVALIDARG;
278 if (!IsEqualIID(&entry->iid, riid)) {
279 LeaveCriticalSection(&git_section);
280 WARN("entry->iid (%s) != riid\n", debugstr_guid(&entry->iid));
281 return E_INVALIDARG;
283 TRACE("entry=%p\n", entry);
285 hres = IStream_Clone(entry->stream, &stream);
287 LeaveCriticalSection(&git_section);
289 if (hres) {
290 WARN("Failed to clone stream with error 0x%08x\n", hres);
291 return hres;
294 /* unmarshal the interface */
295 hres = CoUnmarshalInterface(stream, riid, ppv);
296 IStream_Release(stream);
298 if (hres) {
299 WARN("Failed to unmarshal stream\n");
300 return hres;
303 /* addref it */
304 lpUnk = *ppv;
305 IUnknown_AddRef(lpUnk);
306 TRACE("ppv=%p\n", *ppv);
307 return S_OK;
310 /* Classfactory definition - despite what MSDN says, some programs need this */
312 static HRESULT WINAPI
313 GITCF_QueryInterface(LPCLASSFACTORY iface,REFIID riid, LPVOID *ppv)
315 *ppv = NULL;
316 if (IsEqualIID(riid,&IID_IUnknown) ||
317 IsEqualIID(riid,&IID_IGlobalInterfaceTable))
319 *ppv = (LPVOID)iface;
320 return S_OK;
322 return E_NOINTERFACE;
325 static ULONG WINAPI GITCF_AddRef(LPCLASSFACTORY iface)
327 return 2;
330 static ULONG WINAPI GITCF_Release(LPCLASSFACTORY iface)
332 return 1;
335 static HRESULT WINAPI
336 GITCF_CreateInstance(LPCLASSFACTORY iface, LPUNKNOWN pUnk,
337 REFIID riid, LPVOID *ppv)
339 if (IsEqualIID(riid,&IID_IGlobalInterfaceTable)) {
340 if (StdGlobalInterfaceTableInstance == NULL)
341 StdGlobalInterfaceTableInstance = StdGlobalInterfaceTable_Construct();
342 return IGlobalInterfaceTable_QueryInterface( (IGlobalInterfaceTable*) StdGlobalInterfaceTableInstance, riid, ppv);
345 FIXME("(%s), not supported.\n",debugstr_guid(riid));
346 return E_NOINTERFACE;
349 static HRESULT WINAPI GITCF_LockServer(LPCLASSFACTORY iface, BOOL fLock)
351 FIXME("(%d), stub!\n",fLock);
352 return S_OK;
355 static const IClassFactoryVtbl GITClassFactoryVtbl = {
356 GITCF_QueryInterface,
357 GITCF_AddRef,
358 GITCF_Release,
359 GITCF_CreateInstance,
360 GITCF_LockServer
363 static const IClassFactoryVtbl *PGITClassFactoryVtbl = &GITClassFactoryVtbl;
365 HRESULT StdGlobalInterfaceTable_GetFactory(LPVOID *ppv)
367 *ppv = &PGITClassFactoryVtbl;
368 TRACE("Returning GIT classfactory\n");
369 return S_OK;
372 /* Virtual function table */
373 static const IGlobalInterfaceTableVtbl StdGlobalInterfaceTableImpl_Vtbl =
375 StdGlobalInterfaceTable_QueryInterface,
376 StdGlobalInterfaceTable_AddRef,
377 StdGlobalInterfaceTable_Release,
378 StdGlobalInterfaceTable_RegisterInterfaceInGlobal,
379 StdGlobalInterfaceTable_RevokeInterfaceFromGlobal,
380 StdGlobalInterfaceTable_GetInterfaceFromGlobal
383 /** This function constructs the GIT. It should only be called once **/
384 void* StdGlobalInterfaceTable_Construct(void)
386 StdGlobalInterfaceTableImpl* newGIT;
388 newGIT = HeapAlloc(GetProcessHeap(), 0, sizeof(StdGlobalInterfaceTableImpl));
389 if (newGIT == 0) return newGIT;
391 newGIT->lpVtbl = &StdGlobalInterfaceTableImpl_Vtbl;
392 newGIT->ref = 1; /* Initialise the reference count */
393 list_init(&newGIT->list);
394 newGIT->nextCookie = 0xf100; /* that's where windows starts, so that's where we start */
395 TRACE("Created the GIT at %p\n", newGIT);
397 return (void*)newGIT;