syslocks.h 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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/lock.h>
  15. namespace Nim {
  16. struct SysLock;
  17. struct SysCond;
  18. }
  19. struct Nim::SysLock
  20. {
  21. Genode::Lock _lock_a, _lock_b;
  22. bool _locked;
  23. void acquireSys()
  24. {
  25. _lock_a.lock();
  26. _locked = true;
  27. _lock_a.unlock();
  28. _lock_b.lock();
  29. }
  30. bool tryAcquireSys()
  31. {
  32. if (_locked)
  33. return false;
  34. _lock_a.lock();
  35. if (_locked) {
  36. _lock_a.unlock();
  37. return false;
  38. } else {
  39. _locked = true;
  40. _lock_b.lock();
  41. _lock_a.unlock();
  42. return true;
  43. }
  44. }
  45. void releaseSys()
  46. {
  47. _locked = false;
  48. _lock_a.unlock();
  49. _lock_b.unlock();
  50. }
  51. };
  52. struct Nim::SysCond
  53. {
  54. Genode::Semaphore _semaphore;
  55. void waitSysCond(SysLock &syslock)
  56. {
  57. syslock.releaseSys();
  58. _semaphore.down();
  59. syslock.acquireSys();
  60. }
  61. void signalSysCond()
  62. {
  63. _semaphore.up();
  64. }
  65. };
  66. #endif