lib/volt/utils/read_write_lock.rb in volt-0.9.1 vs lib/volt/utils/read_write_lock.rb in volt-0.9.2
- old
+ new
@@ -66,13 +66,13 @@
release_write_lock
result
end
def acquire_read_lock
- while(true)
+ loop do
c = @counter.value
- raise "Too many reader threads!" if (c & MAX_READERS) == MAX_READERS
+ fail 'Too many reader threads!' if (c & MAX_READERS) == MAX_READERS
# If a writer is waiting when we first queue up, we need to wait
if c >= WAITING_WRITER
# But it is possible that the writer could finish and decrement @counter right here...
@reader_mutex.synchronize do
@@ -80,49 +80,49 @@
@reader_q.wait(@reader_mutex) if @counter.value >= WAITING_WRITER
end
# after a reader has waited once, they are allowed to "barge" ahead of waiting writers
# but if a writer is *running*, the reader still needs to wait (naturally)
- while(true)
+ loop do
c = @counter.value
if c >= RUNNING_WRITER
@reader_mutex.synchronize do
@reader_q.wait(@reader_mutex) if @counter.value >= RUNNING_WRITER
end
else
- return if @counter.compare_and_swap(c,c+1)
+ return if @counter.compare_and_swap(c, c + 1)
end
end
else
- break if @counter.compare_and_swap(c,c+1)
+ break if @counter.compare_and_swap(c, c + 1)
end
end
end
def release_read_lock
- while(true)
+ loop do
c = @counter.value
- if @counter.compare_and_swap(c,c-1)
+ if @counter.compare_and_swap(c, c - 1)
# If one or more writers were waiting, and we were the last reader, wake a writer up
if c >= WAITING_WRITER && (c & MAX_READERS) == 1
@writer_mutex.synchronize { @writer_q.signal }
end
break
end
end
end
def acquire_write_lock
- while(true)
+ loop do
c = @counter.value
- raise "Too many writers!" if (c & MAX_WRITERS) == MAX_WRITERS
+ fail 'Too many writers!' if (c & MAX_WRITERS) == MAX_WRITERS
if c == 0 # no readers OR writers running
# if we successfully swap the RUNNING_WRITER bit on, then we can go ahead
- break if @counter.compare_and_swap(0,RUNNING_WRITER)
- elsif @counter.compare_and_swap(c,c+WAITING_WRITER)
- while(true)
+ break if @counter.compare_and_swap(0, RUNNING_WRITER)
+ elsif @counter.compare_and_swap(c, c + WAITING_WRITER)
+ loop do
# Now we have successfully incremented, so no more readers will be able to increment
# (they will wait instead)
# However, readers OR writers could decrement right here, OR another writer could increment
@writer_mutex.synchronize do
# So we have to do another check inside the synchronized section
@@ -136,37 +136,35 @@
# Then we are OK to stop waiting and go ahead
# Otherwise go back and wait again
c = @counter.value
break if (c < RUNNING_WRITER) &&
((c & MAX_READERS) == 0) &&
- @counter.compare_and_swap(c,c+RUNNING_WRITER-WAITING_WRITER)
+ @counter.compare_and_swap(c, c + RUNNING_WRITER - WAITING_WRITER)
end
break
end
end
end
def release_write_lock
- while(true)
+ loop do
c = @counter.value
- if @counter.compare_and_swap(c,c-RUNNING_WRITER)
+ if @counter.compare_and_swap(c, c - RUNNING_WRITER)
@reader_mutex.synchronize { @reader_q.broadcast }
- if (c & MAX_WRITERS) > 0 # if any writers are waiting...
- @writer_mutex.synchronize { @writer_q.signal }
- end
+ @writer_mutex.synchronize { @writer_q.signal } if (c & MAX_WRITERS) > 0 # if any writers are waiting...
break
end
end
end
def to_s
c = @counter.value
s = if c >= RUNNING_WRITER
- "1 writer running, "
- elsif (c & MAX_READERS) > 0
- "#{c & MAX_READERS} readers running, "
- else
- ""
+ '1 writer running, '
+ elsif (c & MAX_READERS) > 0
+ "#{c & MAX_READERS} readers running, "
+ else
+ ''
end
"#<ReadWriteLock:#{object_id.to_s(16)} #{s}#{(c & MAX_WRITERS) / WAITING_WRITER} writers waiting>"
end
end