protected void doSend(byte[] data, int offset, int length, boolean acquire_lock, boolean flush) throws Exception { out.writeInt(length); // write the length of the data buffer first out.write(data, offset, length); if (!flush || (acquire_lock && send_lock.hasQueuedThreads())) return; // don't flush as some of the waiting threads will do the flush, or flush is false out.flush(); // may not be very efficient (but safe) }
@JRubyMethod public synchronized IRubyObject unlock(ThreadContext context) { Ruby runtime = context.getRuntime(); if (!lock.isLocked()) { throw runtime.newThreadError("Mutex is not locked"); } if (!lock.isHeldByCurrentThread()) { throw runtime.newThreadError("Mutex is not owned by calling thread"); } boolean hasQueued = lock.hasQueuedThreads(); context.getThread().unlock(lock); return hasQueued ? context.nil : this; }