Source code of Windows XP (NT5)
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.

65 lines
1.6 KiB

  1. #include <nt.h>
  2. #include <ntrtl.h>
  3. #include <nturtl.h>
  4. #include <precomp.h>
  5. #include <locks.h>
  6. #ifdef _WIN32_WINNT
  7. #if _WIN32_WINNT > 0x0400
  8. ReaderWriter::ReaderWriter(bool can_throw) : initialized_(false), can_throw_(can_throw)
  9. {
  10. C_ASSERT(sizeof(::RTL_RESOURCE)==sizeof(ReaderWriter::RTL_RESOURCE));
  11. __try
  12. {
  13. RtlInitializeResource((::RTL_RESOURCE*)&lock_);
  14. initialized_ = true;
  15. }
  16. __except(GetExceptionCode() == STATUS_NO_MEMORY)
  17. {
  18. }
  19. if (initialized_ != true)
  20. raise_exception();
  21. }
  22. ReaderWriter::~ReaderWriter (void)
  23. { if (initialized_) RtlDeleteResource((::RTL_RESOURCE*)&lock_);}
  24. bool
  25. ReaderWriter::acquire (void)
  26. { return (RtlAcquireResourceExclusive((::RTL_RESOURCE*)&lock_,TRUE)==TRUE);}
  27. bool
  28. ReaderWriter::tryacquire (void)
  29. { return (RtlAcquireResourceExclusive((::RTL_RESOURCE*)&lock_,FALSE)==TRUE);}
  30. bool
  31. ReaderWriter::release (void)
  32. { RtlReleaseResource((::RTL_RESOURCE*)&lock_); return 0;}
  33. bool
  34. ReaderWriter::acquire_read (void)
  35. { return (RtlAcquireResourceShared((::RTL_RESOURCE*)&lock_,TRUE)==TRUE);}
  36. bool
  37. ReaderWriter::acquire_write (void)
  38. { return (RtlAcquireResourceExclusive((::RTL_RESOURCE*)&lock_,TRUE)==TRUE);}
  39. bool
  40. ReaderWriter::tryacquire_read (void)
  41. { return (RtlAcquireResourceShared((::RTL_RESOURCE*)&lock_,FALSE)==TRUE);}
  42. bool
  43. ReaderWriter::tryacquire_write (void)
  44. { return (RtlAcquireResourceExclusive((::RTL_RESOURCE*)&lock_,FALSE)==TRUE);}
  45. void
  46. ReaderWriter::downgrade()
  47. { RtlConvertExclusiveToShared((::RTL_RESOURCE*)&lock_);}
  48. void
  49. ReaderWriter::upgrade()
  50. { RtlConvertSharedToExclusive((::RTL_RESOURCE*)&lock_);}
  51. #endif
  52. #endif