Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

54 lines
1.7 KiB

  1. ///////////////////////////////////////////////////////////////////////////////
  2. /* File: thdsync.cpp
  3. Description: Contains classes for managing thread synchronization in
  4. Win32 programs. Most of the work is to provide automatic unlocking
  5. of synchronization primities on object destruction. The work on
  6. monitors and condition variables is strongly patterned after
  7. work in "Multithreaded Programming with Windows NT" by Pham and Garg.
  8. Revision History:
  9. Date Description Programmer
  10. -------- --------------------------------------------------- ----------
  11. 09/22/97 Initial creation. BrianAu
  12. */
  13. ///////////////////////////////////////////////////////////////////////////////
  14. #include "pch.h"
  15. #pragma hdrstop
  16. #include "thdsync.h"
  17. CMutex::CMutex(
  18. BOOL bInitialOwner
  19. ) : CWin32SyncObj(CreateMutex(NULL, bInitialOwner, NULL))
  20. {
  21. if (NULL == Handle())
  22. throw CSyncException(CSyncException::mutex, CSyncException::create);
  23. }
  24. //
  25. // Wait on a Win32 mutex object.
  26. // Throw an exception if the mutex has been abandoned or the wait has timed out.
  27. //
  28. void
  29. AutoLockMutex::Wait(
  30. DWORD dwTimeout
  31. )
  32. {
  33. DWORD dwStatus = WaitForSingleObject(m_hMutex, dwTimeout);
  34. switch(dwStatus)
  35. {
  36. case WAIT_ABANDONED:
  37. throw CSyncException(CSyncException::mutex, CSyncException::abandoned);
  38. break;
  39. case WAIT_TIMEOUT:
  40. throw CSyncException(CSyncException::mutex, CSyncException::timeout);
  41. break;
  42. default:
  43. break;
  44. }
  45. }