def acquire_write_lock
      while(true)
        c = @counter.value
        raise ResourceLimitError, 'Too many writer threads' if max_writers?(c)

        if c == 0
          break if @counter.compare_and_swap(0,RUNNING_WRITER)
        elsif @counter.compare_and_swap(c,c+WAITING_WRITER)
          while(true)
            @writer_mutex.synchronize do
              c = @counter.value
              if running_writer?(c) || running_readers?(c)
                @writer_q.wait(@writer_mutex)
              end
            end

            c = @counter.value
            break if !running_writer?(c) && !running_readers?(c) &&
              @counter.compare_and_swap(c,c+RUNNING_WRITER-WAITING_WRITER)
          end
          break
        end
      end
      true
    end
def acquire_write_lock
      while(true)
        c = @counter.value
        raise ResourceLimitError, 'Too many writer threads' if max_writers?(c)

        if c == 0
          break if @counter.compare_and_swap(0,RUNNING_WRITER)
        elsif @counter.compare_and_swap(c,c+WAITING_WRITER)
          while(true)
            @writer_mutex.synchronize do
              c = @counter.value
              if running_writer?(c) || running_readers?(c)
                @writer_q.wait(@writer_mutex)
              end
            end

            c = @counter.value
            break if !running_writer?(c) && !running_readers?(c) &&
              @counter.compare_and_swap(c,c+RUNNING_WRITER-WAITING_WRITER)
          end
          break
        end
      end
      true
    end
Acquire a write lock. Will block and wait for all active readers and
 writers.

 @return [Boolean]
   True if the lock is successfully acquired.

 @raise [Garcon::ResourceLimitError]
   If the maximum number of writers is exceeded.