syslocks.h 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. *
  3. * Nim's Runtime Library
  4. * (c) Copyright 2017 Emery Hemingway
  5. *
  6. * See the file "copying.txt", included in this
  7. * distribution, for details about the copyright.
  8. *
  9. */
  10. #ifndef _GENODE_CPP__SYSLOCKS_H_
  11. #define _GENODE_CPP__SYSLOCKS_H_
  12. /* Genode includes */
  13. #include <base/semaphore.h>
  14. #include <base/mutex.h>
  15. namespace Nim {
  16. struct SysLock;
  17. struct SysCond;
  18. }
  19. struct Nim::SysLock
  20. {
  21. Genode::Mutex _mutex_a, _mutex_b;
  22. bool _locked;
  23. void acquireSys()
  24. {
  25. Genode::Mutex::Guard guard(_mutex_a);
  26. _locked = true;
  27. _mutex_b.acquire();
  28. }
  29. bool tryAcquireSys()
  30. {
  31. if (_locked)
  32. return false;
  33. Genode::Mutex::Guard guard(_mutex_a);
  34. if (_locked) {
  35. return false;
  36. } else {
  37. _locked = true;
  38. _mutex_b.acquire();
  39. return true;
  40. }
  41. }
  42. void releaseSys()
  43. {
  44. Genode::Mutex::Guard guard(_mutex_a);
  45. _locked = false;
  46. _mutex_b.release();
  47. }
  48. };
  49. struct Nim::SysCond
  50. {
  51. Genode::Semaphore _semaphore;
  52. void waitSysCond(SysLock &syslock)
  53. {
  54. syslock.releaseSys();
  55. _semaphore.down();
  56. syslock.acquireSys();
  57. }
  58. void signalSysCond()
  59. {
  60. _semaphore.up();
  61. }
  62. void broadcastSysCond()
  63. {
  64. _semaphore.up();
  65. }
  66. };
  67. #endif