1. # A reentrant lock mechanism with condition variable support.
  2. my class X::Lock::ConditionVariable::New is Exception {
  3. method message() {
  4. "Cannot directly create a ConditionVariable; use the 'condition' method on a lock"
  5. }
  6. }
  7. my class Lock {
  8. class ConditionVariable is repr('ConditionVariable') {
  9. method new() {
  10. X::Lock::ConditionVariable::New.new.throw
  11. }
  12. method wait() { nqp::condwait(self) }
  13. method signal() { nqp::condsignalone(self) }
  14. method signal_all() { nqp::condsignalall(self) }
  15. }
  16. method lock(Lock:D:) { nqp::lock(self) }
  17. method unlock(Lock:D:) { nqp::unlock(self) }
  18. method protect(Lock:D: &code) {
  19. nqp::lock(self);
  20. LEAVE nqp::unlock(self);
  21. code()
  22. }
  23. method condition(Lock:D:) {
  24. nqp::getlockcondvar(self, ConditionVariable)
  25. }
  26. }