/synch/mutex.d

http://github.com/wilkie/djehuty · D · 46 lines · 29 code · 13 blank · 4 comment · 0 complexity · a28b27b0487b0ce5d360b83d4a5355ad MD5 · raw file

  1. module synch.mutex;
  2. import platform.vars.mutex;
  3. import synch.condition;
  4. import scaffold.thread;
  5. // Section: Core/Synchronization
  6. // Description: This class provides a simple mutex, also known as a binary semaphore. This is provided as a means to manually lock critical sections. It is initially unlocked.
  7. class Mutex {
  8. this() {
  9. MutexInit(_pfvars);
  10. }
  11. ~this() {
  12. MutexUninit(_pfvars);
  13. }
  14. // Description: This function will lock the mutex. This could be used to enter a critical section.
  15. void lock() {
  16. MutexLock(_pfvars);
  17. }
  18. // Description: This function will unlock a locked mutex. This could be used to leave a critical section.
  19. void unlock() {
  20. MutexUnlock(_pfvars);
  21. }
  22. void lock(uint milliseconds) {
  23. MutexLock(_pfvars, milliseconds);
  24. }
  25. void wait(Condition cond) {
  26. cond.wait(_pfvars);
  27. }
  28. void wait(Waitable forObject) {
  29. wait(forObject.waitCondition());
  30. }
  31. protected:
  32. MutexPlatformVars _pfvars;
  33. }