Class: Mutex
Instance Method Summary collapse
- 
  
    
      #initialize  ⇒ Mutex 
    
    
  
  
  
    constructor
  
  
  
  
  
  
  
    A new instance of Mutex. 
- #lock ⇒ Object
- #locked? ⇒ Boolean
- #owned? ⇒ Boolean
- #synchronize ⇒ Object
- #try_lock ⇒ Object
- #unlock ⇒ Object
Constructor Details
#initialize ⇒ Mutex
Returns a new instance of Mutex.
| 148 149 150 151 152 | # File 'opal/stdlib/thread.rb', line 148 def initialize # We still keep the @locked state so any logic based on try_lock while # held yields reasonable results. @locked = false end | 
Instance Method Details
#lock ⇒ Object
| 154 155 156 157 158 | # File 'opal/stdlib/thread.rb', line 154 def lock raise ThreadError, 'Deadlock' if @locked @locked = true self end | 
#owned? ⇒ Boolean
| 164 165 166 167 | # File 'opal/stdlib/thread.rb', line 164 def owned? # Being the only "thread", we implicitly own any locked mutex. @locked end | 
#synchronize ⇒ Object
| 184 185 186 187 188 189 190 191 | # File 'opal/stdlib/thread.rb', line 184 def synchronize lock begin yield ensure unlock end end | 
#try_lock ⇒ Object
| 169 170 171 172 173 174 175 176 | # File 'opal/stdlib/thread.rb', line 169 def try_lock if locked? false else lock true end end | 
#unlock ⇒ Object
| 178 179 180 181 182 | # File 'opal/stdlib/thread.rb', line 178 def unlock raise ThreadError, 'Mutex not locked' unless @locked @locked = false self end |