Fixed a race condition on RPC worker thread creation, and a typo.
[wine] / dlls / ole32 / git.c
1 /*
2  * Implementation of the StdGlobalInterfaceTable object
3  *
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.
9  *
10  * Copyright 2003 Mike Hearn <mike@theoretic.com>
11  *
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.
16  *
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.
21  *
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
25  */
26
27 #include "config.h"
28
29 #include <assert.h>
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <string.h>
33
34 #include "windef.h"
35 #include "objbase.h"
36 #include "ole2.h"
37 #include "winbase.h"
38 #include "winerror.h"
39 #include "winternl.h"
40
41 #include "compobj_private.h" 
42
43 #include "wine/debug.h"
44
45 WINE_DEFAULT_DEBUG_CHANNEL(ole);
46
47 /****************************************************************************
48  * StdGlobalInterfaceTable definition
49  *
50  * This class implements IGlobalInterfaceTable and is a process-wide singleton
51  * used for marshalling interfaces between threading apartments using cookies.
52  */
53
54 /* Each entry in the linked list of GIT entries */
55 typedef struct StdGITEntry
56 {
57   DWORD cookie;
58   IID iid;      /* IID of the interface */
59   IStream* stream; /* Holds the marshalled interface */
60
61   struct StdGITEntry* next;
62   struct StdGITEntry* prev;  
63 } StdGITEntry;
64
65 /* Class data */
66 typedef struct StdGlobalInterfaceTableImpl
67 {
68   ICOM_VFIELD(IGlobalInterfaceTable);
69
70   ULONG ref;
71   struct StdGITEntry* firstEntry;
72   struct StdGITEntry* lastEntry;
73   ULONG nextCookie;
74   
75 } StdGlobalInterfaceTableImpl;
76
77
78 /* IUnknown */
79 static HRESULT WINAPI StdGlobalInterfaceTable_QueryInterface(IGlobalInterfaceTable* iface, REFIID riid, void** ppvObject);
80 static ULONG   WINAPI StdGlobalInterfaceTable_AddRef(IGlobalInterfaceTable* iface);
81 static ULONG   WINAPI StdGlobalInterfaceTable_Release(IGlobalInterfaceTable* iface);
82 /* IGlobalInterfaceTable */
83 static HRESULT WINAPI StdGlobalInterfaceTable_RegisterInterfaceInGlobal(IGlobalInterfaceTable* iface, IUnknown* pUnk, REFIID riid, DWORD* pdwCookie);
84 static HRESULT WINAPI StdGlobalInterfaceTable_RevokeInterfaceFromGlobal(IGlobalInterfaceTable* iface, DWORD dwCookie);
85 static HRESULT WINAPI StdGlobalInterfaceTable_GetInterfaceFromGlobal(IGlobalInterfaceTable* iface, DWORD dwCookie, REFIID riid, void **ppv);
86
87 /* Virtual function table */
88 static ICOM_VTABLE(IGlobalInterfaceTable) StdGlobalInterfaceTableImpl_Vtbl =
89 {
90   ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
91   StdGlobalInterfaceTable_QueryInterface,
92   StdGlobalInterfaceTable_AddRef,
93   StdGlobalInterfaceTable_Release,
94   StdGlobalInterfaceTable_RegisterInterfaceInGlobal,
95   StdGlobalInterfaceTable_RevokeInterfaceFromGlobal,
96   StdGlobalInterfaceTable_GetInterfaceFromGlobal
97 };
98
99 /***
100  * Let's go! Here is the constructor and destructor for the class.
101  *
102  */
103
104 /** This function constructs the GIT. It should only be called once **/
105 void* StdGlobalInterfaceTable_Construct() {
106   StdGlobalInterfaceTableImpl* newGIT;
107
108   newGIT = HeapAlloc(GetProcessHeap(), 0, sizeof(StdGlobalInterfaceTableImpl));
109   if (newGIT == 0) return newGIT;
110
111   newGIT->lpVtbl = &StdGlobalInterfaceTableImpl_Vtbl;
112   newGIT->ref = 0;      /* Initialise the reference count */
113   newGIT->firstEntry = NULL; /* we start with an empty table   */
114   newGIT->lastEntry  = NULL;
115   newGIT->nextCookie = 0xf100; /* that's where windows starts, so that's where we start */
116   TRACE("Created the GIT at %p\n", newGIT);
117
118   return (void*)newGIT;
119 }
120
121 /** This destroys it again. It should revoke all the held interfaces first **/
122 void StdGlobalInterfaceTable_Destroy(void* self) {
123   TRACE("(%p)\n", self);
124   FIXME("Revoke held interfaces here\n");
125   
126   HeapFree(GetProcessHeap(), 0, self);
127 }
128
129 /***
130  * A helper function to traverse the list and find the entry that matches the cookie.
131  * Returns NULL if not found
132  */
133 StdGITEntry* StdGlobalInterfaceTable_FindEntry(IGlobalInterfaceTable* iface, DWORD cookie) {
134   StdGlobalInterfaceTableImpl* const self = (StdGlobalInterfaceTableImpl*) iface;
135   StdGITEntry* e;
136
137   TRACE("iface=%p, cookie=0x%x\n", iface, (UINT)cookie);
138   
139   e = self->firstEntry;
140   while (e != NULL) {
141     if (e->cookie == cookie) return e;
142     e = e->next;
143   }
144   return NULL;
145 }
146
147 /***
148  * Here's the boring boilerplate stuff for IUnknown
149  */
150
151 HRESULT WINAPI StdGlobalInterfaceTable_QueryInterface(IGlobalInterfaceTable* iface, REFIID riid, void** ppvObject) {
152   StdGlobalInterfaceTableImpl* const self = (StdGlobalInterfaceTableImpl*) iface;
153
154   /* Make sure silly coders can't crash us */
155   if (ppvObject == 0) return E_INVALIDARG;
156
157   *ppvObject = 0; /* assume we don't have the interface */
158
159   /* Do we implement that interface? */
160   if (IsEqualIID(&IID_IUnknown, riid)) {
161     *ppvObject = (IGlobalInterfaceTable*) self;
162   } else if (IsEqualIID(&IID_IGlobalInterfaceTable, riid)) {
163     *ppvObject = (IGlobalInterfaceTable*) self;
164   } else return E_NOINTERFACE;
165
166   /* Now inc the refcount */
167   /* we don't use refcounts for now: StdGlobalInterfaceTable_AddRef(iface); */
168   return S_OK;
169 }
170
171 ULONG WINAPI StdGlobalInterfaceTable_AddRef(IGlobalInterfaceTable* iface) {
172   StdGlobalInterfaceTableImpl* const self = (StdGlobalInterfaceTableImpl*) iface;
173
174   self->ref++;
175   return self->ref;
176 }
177
178 ULONG WINAPI StdGlobalInterfaceTable_Release(IGlobalInterfaceTable* iface) {
179   StdGlobalInterfaceTableImpl* const self = (StdGlobalInterfaceTableImpl*) iface;
180
181   self->ref--;
182   if (self->ref == 0) {
183     /* Hey ho, it's time to go, so long again 'till next weeks show! */
184     StdGlobalInterfaceTable_Destroy(self);
185     return 0;
186   }
187
188   return self->ref;
189 }
190
191 /***
192  * Now implement the actual IGlobalInterfaceTable interface
193  */
194
195 HRESULT WINAPI StdGlobalInterfaceTable_RegisterInterfaceInGlobal(IGlobalInterfaceTable* iface, IUnknown* pUnk, REFIID riid, DWORD* pdwCookie) {
196   StdGlobalInterfaceTableImpl* const self = (StdGlobalInterfaceTableImpl*) iface;
197   IStream* stream = NULL;
198   HRESULT hres;
199   StdGITEntry* entry;
200
201   TRACE("iface=%p, pUnk=%p, riid=%s, pdwCookie=%p\n", iface, pUnk, debugstr_guid(riid), pdwCookie);
202
203   if (pUnk == NULL) return E_INVALIDARG;
204   
205   /* marshal the interface */
206   hres = CoMarshalInterThreadInterfaceInStream(riid, pUnk, &stream);
207   if (hres) return hres;
208   entry = HeapAlloc(GetProcessHeap(), 0, sizeof(StdGITEntry));
209   if (entry == NULL) return E_OUTOFMEMORY;
210   
211   entry->iid = *riid;
212   entry->stream = stream;
213   entry->cookie = self->nextCookie;
214   self->nextCookie++; /* inc the cookie count */
215
216   /* insert the new entry at the end of the list */
217   entry->next = NULL;
218   entry->prev = self->lastEntry;
219   if (entry->prev) entry->prev->next = entry;
220   else self->firstEntry = entry;
221   self->lastEntry = entry;
222
223   /* and return the cookie */
224   *pdwCookie = entry->cookie;
225   return S_OK;
226 }
227
228 HRESULT WINAPI StdGlobalInterfaceTable_RevokeInterfaceFromGlobal(IGlobalInterfaceTable* iface, DWORD dwCookie) {
229   StdGlobalInterfaceTableImpl* const self = (StdGlobalInterfaceTableImpl*) iface;
230   StdGITEntry* entry;
231
232   TRACE("iface=%p, dwCookie=0x%x\n", iface, (UINT)dwCookie);
233   
234   entry = StdGlobalInterfaceTable_FindEntry(iface, dwCookie);
235   if (entry == NULL) {
236     TRACE("Entry not found\n");
237     return E_INVALIDARG; /* not found */
238   }
239
240   /* chop entry out of the list, and free the memory */
241   if (entry->prev) entry->prev->next = entry->next;
242   else self->firstEntry = entry->next;
243   if (entry->next) entry->next->prev = entry->prev;
244   else self->lastEntry = entry->prev;
245   HeapFree(GetProcessHeap(), 0, entry);
246   return S_OK;
247 }
248
249 HRESULT WINAPI StdGlobalInterfaceTable_GetInterfaceFromGlobal(IGlobalInterfaceTable* iface, DWORD dwCookie, REFIID riid, void **ppv) {
250   StdGITEntry* entry;
251   HRESULT hres;
252   
253   entry = StdGlobalInterfaceTable_FindEntry(iface, dwCookie);
254   if (entry == NULL) return E_INVALIDARG;
255   if (!IsEqualIID(&entry->iid, &riid)) return E_INVALIDARG;
256
257   /* unmarshal the interface */
258   hres = CoGetInterfaceAndReleaseStream(entry->stream, riid, *ppv);
259   if (hres) return hres;
260   
261   return S_OK;
262 }