thread.c 14.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * SHLWAPI thread and MT synchronisation functions
 *
 * Copyright 2002 Juergen Schmied
 * Copyright 2002 Jon Griffiths
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20
 */
21
#include <stdarg.h>
22 23
#include <string.h>

24 25
#define COBJMACROS

26
#include "windef.h"
27
#include "winbase.h"
28
#include "winnls.h"
29
#include "winuser.h"
30 31 32 33 34 35
#define NO_SHLWAPI_REG
#define NO_SHLWAPI_PATH
#define NO_SHLWAPI_GDI
#define NO_SHLWAPI_STREAM
#define NO_SHLWAPI_USER
#include "shlwapi.h"
36 37
#include "shlobj.h"
#include "wine/debug.h"
38 39 40

WINE_DEFAULT_DEBUG_CHANNEL(shell);

41 42
extern DWORD SHLWAPI_ThreadRef_index;  /* Initialised in shlwapi_main.c */

43
INT WINAPI SHStringFromGUIDA(REFGUID,LPSTR,INT);
44 45

/**************************************************************************
46
 *      CreateAllAccessSecurityAttributes       [SHLWAPI.356]
47 48 49 50 51 52 53 54 55 56 57 58
 *
 * Initialise security attributes from a security descriptor.
 *
 * PARAMS
 *  lpAttr [O] Security attributes
 *  lpSec  [I] Security descriptor
 *
 * RETURNS
 *  Success: lpAttr, initialised using lpSec.
 *  Failure: NULL, if any parameters are invalid.
 *
 * NOTES
59
 *  This function always returns NULL if the underlying OS version
60 61 62
 *  Wine is impersonating does not use security descriptors (i.e. anything
 *  before Windows NT).
 */
63
LPSECURITY_ATTRIBUTES WINAPI CreateAllAccessSecurityAttributes(
64
	LPSECURITY_ATTRIBUTES lpAttr,
65 66
	PSECURITY_DESCRIPTOR lpSec,
        DWORD p3)
67 68 69 70
{
  /* This function is used within SHLWAPI only to create security attributes
   * for shell semaphores. */

71
  TRACE("(%p,%p,%08x)\n", lpAttr, lpSec, p3);
72

73
  if (!(GetVersion() & 0x80000000))  /* NT */
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
  {
    if (!lpSec || !lpAttr)
      return NULL;

    if (InitializeSecurityDescriptor(lpSec, 1))
    {
      if (SetSecurityDescriptorDacl(lpSec, TRUE, NULL, FALSE))
      {
         lpAttr->nLength = sizeof(SECURITY_ATTRIBUTES);
         lpAttr->lpSecurityDescriptor = lpSec;
         lpAttr->bInheritHandle = FALSE;
         return lpAttr;
      }
    }
  }
  return NULL;
}

/*************************************************************************
 *      _SHGetInstanceExplorer	[SHLWAPI.@]
 *
 * Get an interface to the shell explorer.
 *
 * PARAMS
Jon Griffiths's avatar
Jon Griffiths committed
98
 *  lppUnknown [O] Destination for explorers IUnknown interface.
99 100 101 102 103 104 105 106 107
 *
 * RETURNS
 *  Success: S_OK. lppUnknown contains the explorer interface.
 *  Failure: An HRESULT error code.
 */
HRESULT WINAPI _SHGetInstanceExplorer(IUnknown **lppUnknown)
{
  /* This function is used within SHLWAPI only to hold the IE reference
   * for threads created with the CTF_PROCESS_REF flag set. */
108
    return SHGetInstanceExplorer(lppUnknown);
109 110 111 112 113 114 115 116 117 118 119 120 121 122
}

/* Internal thread information structure */
typedef struct tagSHLWAPI_THREAD_INFO
{
  LPTHREAD_START_ROUTINE pfnThreadProc; /* Thread start */
  LPTHREAD_START_ROUTINE pfnCallback;   /* Thread initialisation */
  PVOID     pData;                      /* Application specific data */
  BOOL      bInitCom;                   /* Initialise COM for the thread? */
  HANDLE    hEvent;                     /* Signal for creator to continue */
  IUnknown *refThread;                  /* Reference to thread creator */
  IUnknown *refIE;                      /* Reference to the IE process */
} SHLWAPI_THREAD_INFO, *LPSHLWAPI_THREAD_INFO;

123 124
typedef struct
{
125
  IUnknown IUnknown_iface;
126 127 128
  LONG  *ref;
} threadref;

129 130 131 132 133
static inline threadref *impl_from_IUnknown(IUnknown *iface)
{
  return CONTAINING_RECORD(iface, threadref, IUnknown_iface);
}

134 135
static HRESULT WINAPI threadref_QueryInterface(IUnknown *iface, REFIID riid, LPVOID *ppvObj)
{
136
  threadref * This = impl_from_IUnknown(iface);
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156

  TRACE("(%p, %s, %p)\n", This, debugstr_guid(riid), ppvObj);

  if (ppvObj == NULL)
    return E_POINTER;

  if (IsEqualGUID(&IID_IUnknown, riid)) {
    TRACE("(%p)->(IID_IUnknown %p)\n", This, ppvObj);
    *ppvObj = This;
    IUnknown_AddRef((IUnknown*)*ppvObj);
    return S_OK;
  }

  *ppvObj = NULL;
  FIXME("(%p, %s, %p) interface not supported\n", This, debugstr_guid(riid), ppvObj);
  return E_NOINTERFACE;
}

static ULONG WINAPI threadref_AddRef(IUnknown *iface)
{
157
  threadref * This = impl_from_IUnknown(iface);
158 159 160 161 162 163 164 165

  TRACE("(%p)\n", This);
  return InterlockedIncrement(This->ref);
}

static ULONG WINAPI threadref_Release(IUnknown *iface)
{
  LONG refcount;
166
  threadref * This = impl_from_IUnknown(iface);
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206

  TRACE("(%p)\n", This);

  refcount = InterlockedDecrement(This->ref);
  if (!refcount)
      HeapFree(GetProcessHeap(), 0, This);

  return refcount;
}

/* VTable */
static const IUnknownVtbl threadref_vt =
{
  threadref_QueryInterface,
  threadref_AddRef,
  threadref_Release,
};

/*************************************************************************
 * SHCreateThreadRef [SHLWAPI.@]
 *
 * Create a per-thread IUnknown object
 *
 * PARAMS
 *   lprefcount [I] Pointer to a LONG to be used as refcount
 *   lppUnknown [O] Destination to receive the created object reference
 *
 * RETURNS
 *   Success: S_OK. lppUnknown is set to the object reference.
 *   Failure: E_INVALIDARG, if a parameter is NULL
 */
HRESULT WINAPI SHCreateThreadRef(LONG *lprefcount, IUnknown **lppUnknown)
{
  threadref * This;
  TRACE("(%p, %p)\n", lprefcount, lppUnknown);

  if (!lprefcount || !lppUnknown)
    return E_INVALIDARG;

  This = HeapAlloc(GetProcessHeap(), 0, sizeof(threadref));
207
  This->IUnknown_iface.lpVtbl = &threadref_vt;
208 209 210
  This->ref = lprefcount;

  *lprefcount = 1;
211
  *lppUnknown = &This->IUnknown_iface;
212 213 214
  TRACE("=> returning S_OK with %p\n", This);
  return S_OK;
}
215 216 217 218

/*************************************************************************
 * SHGetThreadRef	[SHLWAPI.@]
 *
Jon Griffiths's avatar
Jon Griffiths committed
219
 * Get a per-thread object reference set by SHSetThreadRef().
220 221 222 223 224
 *
 * PARAMS
 *   lppUnknown [O] Destination to receive object reference
 *
 * RETURNS
Jon Griffiths's avatar
Jon Griffiths committed
225
 *   Success: S_OK. lppUnknown is set to the object reference.
226
 *   Failure: E_NOINTERFACE, if an error occurs or no object is set
227 228 229 230 231
 */
HRESULT WINAPI SHGetThreadRef(IUnknown **lppUnknown)
{
  TRACE("(%p)\n", lppUnknown);

232
  if (SHLWAPI_ThreadRef_index == TLS_OUT_OF_INDEXES)
233 234
    return E_NOINTERFACE;

235
  *lppUnknown = TlsGetValue(SHLWAPI_ThreadRef_index);
236 237 238 239 240 241 242 243 244 245 246 247 248 249
  if (!*lppUnknown)
    return E_NOINTERFACE;

  /* Add a reference. Caller will Release() us when finished */
  IUnknown_AddRef(*lppUnknown);
  return S_OK;
}

/*************************************************************************
 * SHSetThreadRef	[SHLWAPI.@]
 *
 * Store a per-thread object reference.
 *
 * PARAMS
Jon Griffiths's avatar
Jon Griffiths committed
250
 *   lpUnknown [I] Object reference to store
251 252
 *
 * RETURNS
Jon Griffiths's avatar
Jon Griffiths committed
253
 *   Success: S_OK. lpUnknown is stored and can be retrieved by SHGetThreadRef()
254
 *   Failure: E_NOINTERFACE, if an error occurs
255 256 257 258 259
 */
HRESULT WINAPI SHSetThreadRef(IUnknown *lpUnknown)
{
  TRACE("(%p)\n", lpUnknown);

260
  if (SHLWAPI_ThreadRef_index == TLS_OUT_OF_INDEXES)
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
    return E_NOINTERFACE;

  TlsSetValue(SHLWAPI_ThreadRef_index, lpUnknown);
  return S_OK;
}

/*************************************************************************
 * SHReleaseThreadRef	[SHLWAPI.@]
 *
 * Release a per-thread object reference.
 *
 * PARAMS
 *  None.
 *
 * RETURNS
Jon Griffiths's avatar
Jon Griffiths committed
276
 *   Success: S_OK. The threads object reference is released.
277 278
 *   Failure: An HRESULT error code.
 */
279
HRESULT WINAPI SHReleaseThreadRef(void)
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
{
  FIXME("() - stub!\n");
  return S_OK;
}

/*************************************************************************
 * SHLWAPI_ThreadWrapper
 *
 * Internal wrapper for executing user thread functions from SHCreateThread.
 */
static DWORD WINAPI SHLWAPI_ThreadWrapper(PVOID pTi)
{
  SHLWAPI_THREAD_INFO ti;
  HRESULT hCom = E_FAIL;
  DWORD dwRet;

296
  TRACE("(%p)\n", pTi);
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337

  /* We are now executing in the context of the newly created thread.
   * So we copy the data passed to us (it is on the stack of the function
   * that called us, which is waiting for us to signal an event before
   * returning). */
  memcpy(&ti, pTi, sizeof(SHLWAPI_THREAD_INFO));

  /* Initialise COM for the thread, if desired */
  if (ti.bInitCom)
  {
    hCom = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED|COINIT_DISABLE_OLE1DDE);

    if (FAILED(hCom))
      hCom = CoInitializeEx(NULL, COINIT_DISABLE_OLE1DDE);
  }

  /* Execute the callback function before returning */
  if (ti.pfnCallback)
    ti.pfnCallback(ti.pData);

  /* Signal the thread that created us; it can return now */
  SetEvent(ti.hEvent);

  /* Execute the callers start code */
  dwRet = ti.pfnThreadProc(ti.pData);

  /* Release references to the caller and IE process, if held */
  if (ti.refThread)
    IUnknown_Release(ti.refThread);

  if (ti.refIE)
    IUnknown_Release(ti.refIE);

  if (SUCCEEDED(hCom))
    CoUninitialize();

  /* Return the users thread return value */
  return dwRet;
}

/*************************************************************************
Jon Griffiths's avatar
Jon Griffiths committed
338
 *      SHCreateThread	[SHLWAPI.16]
339 340 341 342 343 344
 *
 * Create a new thread.
 *
 * PARAMS
 *   pfnThreadProc [I] Function to execute in new thread
 *   pData         [I] Application specific data passed to pfnThreadProc
Jon Griffiths's avatar
Jon Griffiths committed
345
 *   dwFlags       [I] CTF_ flags from "shlwapi.h"
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
 *   pfnCallback   [I] Function to execute before pfnThreadProc
 *
 * RETURNS
 *   Success: TRUE. pfnThreadProc was executed.
 *   Failure: FALSE. pfnThreadProc was not executed.
 *
 * NOTES
 *   If the thread cannot be created, pfnCallback is NULL, and dwFlags
 *   has bit CTF_INSIST set, pfnThreadProc will be executed synchronously.
 */
BOOL WINAPI SHCreateThread(LPTHREAD_START_ROUTINE pfnThreadProc, VOID *pData,
                           DWORD dwFlags, LPTHREAD_START_ROUTINE pfnCallback)
{
  SHLWAPI_THREAD_INFO ti;
  BOOL bCalled = FALSE;

362
  TRACE("(%p,%p,0x%X,%p)\n", pfnThreadProc, pData, dwFlags, pfnCallback);
363 364 365 366 367

  /* Set up data to pass to the new thread (On our stack) */
  ti.pfnThreadProc = pfnThreadProc;
  ti.pfnCallback = pfnCallback;
  ti.pData = pData;
368
  ti.bInitCom = (dwFlags & CTF_COINIT) != 0;
369
  ti.hEvent = CreateEventW(NULL,FALSE,FALSE,NULL);
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392

  /* Hold references to the current thread and IE process, if desired */
  if(dwFlags & CTF_THREAD_REF)
    SHGetThreadRef(&ti.refThread);
  else
    ti.refThread = NULL;

  if(dwFlags & CTF_PROCESS_REF)
    _SHGetInstanceExplorer(&ti.refIE);
  else
    ti.refIE = NULL;

  /* Create the thread */
  if(ti.hEvent)
  {
    DWORD dwRetVal;
    HANDLE hThread;

    hThread = CreateThread(NULL, 0, SHLWAPI_ThreadWrapper, &ti, 0, &dwRetVal);

    if(hThread)
    {
      /* Wait for the thread to signal us to continue */
393
      WaitForSingleObject(ti.hEvent, INFINITE);
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
      CloseHandle(hThread);
      bCalled = TRUE;
    }
    CloseHandle(ti.hEvent);
  }

  if (!bCalled)
  {
    if (!ti.pfnCallback && dwFlags & CTF_INSIST)
    {
      /* Couldn't call, call synchronously */
      pfnThreadProc(pData);
      bCalled = TRUE;
    }
    else
    {
      /* Free references, since thread hasn't run to do so */
      if(ti.refThread)
        IUnknown_Release(ti.refThread);

      if(ti.refIE)
        IUnknown_Release(ti.refIE);
    }
  }
  return bCalled;
}

/*************************************************************************
422
 *      SHGlobalCounterGetValue	[SHLWAPI.223]
423 424 425 426 427 428 429 430 431
 *
 * Get the current count of a semaphore.
 *
 * PARAMS
 *  hSem [I] Semaphore handle
 *
 * RETURNS
 *  The current count of the semaphore.
 */
432
LONG WINAPI SHGlobalCounterGetValue(HANDLE hSem)
433
{
Jon Griffiths's avatar
Jon Griffiths committed
434
  LONG dwOldCount = 0;
435

436
  TRACE("(%p)\n", hSem);
437 438 439 440 441 442
  ReleaseSemaphore(hSem, 1, &dwOldCount); /* +1 */
  WaitForSingleObject(hSem, 0);           /* -1 */
  return dwOldCount;
}

/*************************************************************************
443
 *      SHGlobalCounterIncrement	[SHLWAPI.224]
444 445 446 447 448 449 450 451 452
 *
 * Claim a semaphore.
 *
 * PARAMS
 *  hSem [I] Semaphore handle
 *
 * RETURNS
 *  The new count of the semaphore.
 */
453
LONG WINAPI SHGlobalCounterIncrement(HANDLE hSem)
454
{
Jon Griffiths's avatar
Jon Griffiths committed
455
  LONG dwOldCount = 0;
456

457
  TRACE("(%p)\n", hSem);
458 459 460 461 462
  ReleaseSemaphore(hSem, 1, &dwOldCount);
  return dwOldCount + 1;
}

/*************************************************************************
463
 *      SHGlobalCounterDecrement	[SHLWAPI.424]
464 465 466 467 468 469 470 471 472
 *
 * Release a semaphore.
 *
 * PARAMS
 *  hSem [I] Semaphore handle
 *
 * RETURNS
 *  The new count of the semaphore.
 */
473
DWORD WINAPI SHGlobalCounterDecrement(HANDLE hSem)
474 475 476
{
  DWORD dwOldCount = 0;

477
  TRACE("(%p)\n", hSem);
478

479
  dwOldCount = SHGlobalCounterGetValue(hSem);
480 481 482 483 484
  WaitForSingleObject(hSem, 0);
  return dwOldCount - 1;
}

/*************************************************************************
485
 *      SHGlobalCounterCreateNamedW	[SHLWAPI.423]
486
 *
487
 * Unicode version of SHGlobalCounterCreateNamedA.
488
 */
489
HANDLE WINAPI SHGlobalCounterCreateNamedW(LPCWSTR lpszName, DWORD iInitial)
490 491 492 493 494 495 496 497 498
{
  static const WCHAR szPrefix[] = { 's', 'h', 'e', 'l', 'l', '.', '\0' };
  const int iPrefixLen = 6;
  WCHAR szBuff[MAX_PATH];
  const int iBuffLen = sizeof(szBuff)/sizeof(WCHAR);
  SECURITY_DESCRIPTOR sd;
  SECURITY_ATTRIBUTES sAttr, *pSecAttr;
  HANDLE hRet;

499
  TRACE("(%s,%d)\n", debugstr_w(lpszName), iInitial);
500 501 502 503 504 505 506

  /* Create Semaphore name */
  memcpy(szBuff, szPrefix, (iPrefixLen + 1) * sizeof(WCHAR));
  if (lpszName)
    StrCpyNW(szBuff + iPrefixLen, lpszName, iBuffLen - iPrefixLen);

  /* Initialise security attributes */
507
  pSecAttr = CreateAllAccessSecurityAttributes(&sAttr, &sd, 0);
508 509 510 511 512 513 514

  if (!(hRet = CreateSemaphoreW(pSecAttr , iInitial, MAXLONG, szBuff)))
    hRet = OpenSemaphoreW(SYNCHRONIZE|SEMAPHORE_MODIFY_STATE, 0, szBuff);
  return hRet;
}

/*************************************************************************
515
 *      SHGlobalCounterCreateNamedA	[SHLWAPI.422]
516 517 518 519 520 521 522 523 524 525
 *
 * Create a semaphore.
 *
 * PARAMS
 *  lpszName [I] Name of semaphore
 *  iInitial [I] Initial count for semaphore
 *
 * RETURNS
 *  A new semaphore handle.
 */
526
HANDLE WINAPI SHGlobalCounterCreateNamedA(LPCSTR lpszName, DWORD iInitial)
527 528 529
{
  WCHAR szBuff[MAX_PATH];

530
  TRACE("(%s,%d)\n", debugstr_a(lpszName), iInitial);
531 532

  if (lpszName)
533
    MultiByteToWideChar(CP_ACP, 0, lpszName, -1, szBuff, MAX_PATH);
534
  return SHGlobalCounterCreateNamedW(lpszName ? szBuff : NULL, iInitial);
535 536 537
}

/*************************************************************************
538
 *      SHGlobalCounterCreate	[SHLWAPI.222]
539 540 541 542 543 544 545 546 547 548 549 550
 *
 * Create a semaphore using the name of a GUID.
 *
 * PARAMS
 *  guid [I] GUID to use as semaphore name
 *
 * RETURNS
 *  A handle to the semaphore.
 *
 * NOTES
 *  The initial count of the semaphore is set to 0.
 */
551
HANDLE WINAPI SHGlobalCounterCreate (REFGUID guid)
552 553 554 555 556 557
{
  char szName[40];

  TRACE("(%s)\n", debugstr_guid(guid));

  /* Create a named semaphore using the GUID string */
558
  SHStringFromGUIDA(guid, szName, sizeof(szName) - 1);
559
  return SHGlobalCounterCreateNamedA(szName, 0);
560
}