/** Moves contents of matrix num_rows down. Avoids a System.arraycopy(). Caller must hold the lock. */ @GuardedBy("lock") protected void move(int num_rows) { if(num_rows <= 0 || num_rows > matrix.length) return; int target_index=0; for(int i=num_rows; i < matrix.length; i++) matrix[target_index++]=matrix[i]; for(int i=matrix.length - num_rows; i < matrix.length; i++) matrix[i]=null; num_moves++; }
/** Computes and returns the row index for seqno. The caller must hold the lock. */ // Note that seqno-offset is never > Integer.MAX_VALUE and thus doesn't overflow into a negative long, // as offset is always adjusted in resize() or compact(). Even if it was negative, callers of computeRow() will // ignore the result or throw an ArrayIndexOutOfBound exception @GuardedBy("lock") protected int computeRow(long seqno) { int diff=(int)(seqno-offset); if(diff < 0) return diff; return diff / elements_per_row; }
@GuardedBy("lock") protected boolean decrement(long credits) { if(credits_left - credits >= 0) { credits_left-=credits; return true; } return false; } }
@GuardedBy("deliverySet") private View getAndSetView(View newView) { View oldView = currentView; currentView = newView; return oldView; }
/** Computes and returns the index within a row for seqno */ // Note that seqno-offset is never > Integer.MAX_VALUE and thus doesn't overflow into a negative long, // as offset is always adjusted in resize() or compact(). Even if it was negative, callers of computeIndex() will // ignore the result or throw an ArrayIndexOutOfBound exception @GuardedBy("lock") protected int computeIndex(long seqno) { int diff=(int)(seqno - offset); if(diff < 0) return diff; return diff & (elements_per_row - 1); // same as mod, but (apparently, I'm told) more efficient }
@GuardedBy("lock") protected int size() { return write_pos-read_pos; }
@GuardedBy("lock") protected int remaining() { return buf.length - write_pos; }
/** * Returns a row. Creates a new row and inserts it at index if the row at index doesn't exist * @param index * @return A row */ @SuppressWarnings("unchecked") @GuardedBy("lock") protected T[] getRow(int index) { T[] row=matrix[index]; if(row == null) { row=(T[])new Object[elements_per_row]; matrix[index]=row; } return row; }
@GuardedBy("lock") protected boolean setProcessing(boolean flag) { boolean do_signal=processing && !flag; processing=flag; if(do_signal) processing_done.signalAll(); return flag; }
@GuardedBy("lock") protected List<PingData> toList() { List<PingData> retval=new ArrayList<>(); retval.addAll(Arrays.asList(ping_rsps).subList(0, index)); return retval; }
@GuardedBy("lock") protected void clearMessages() { msgs.values().stream().filter(Objects::nonNull).forEach(List::clear); }
@GuardedBy("deliverySet") private void notifyIfNeeded() { if (!deliverySet.isEmpty() && deliverySet.first().isReadyToDeliver()) { deliverySet.notify(); } }
@GuardedBy("lock") protected Responses _done() { if(!done) { done=true; cond.signal(true); } return this; }
@GuardedBy("this") public void replaceConnection(Address address, Connection conn) { Connection previous=conns.put(address, conn); Util.close(previous); // closes previous connection (if present) }
@GuardedBy("lock") protected boolean block(long seqno) { while(running && seqno - low > capacity()) { try { buffer_full.await(); } catch(InterruptedException e) { } } return running; }
@GuardedBy("lock") protected void addMessage(Message msg, long size) { Address dest=msg.getDest(); List<Message> tmp=msgs.computeIfAbsent(dest, k -> new ArrayList<>(5)); tmp.add(msg); count+=size; } }
protected TP transport; protected final ReentrantLock lock=new ReentrantLock(); protected @GuardedBy("lock") long count; // current number of bytes accumulated protected ByteArrayDataOutputStream output; protected Log log;
@GuardedBy("lock") protected void advanceLow() { while(low < high) { int index=index(low); if(buffer[index] != null) break; low++; } }
/** Iterate from low to hr and add up non-null values. Caller must hold the lock. */ @GuardedBy("lock") public int computeSize() { return (int)stream().filter(Objects::nonNull).count(); }
@GuardedBy("lock") protected int find(PingData data) { if(data == null) return -1; for(int i=0; i < index; i++) { if(data.equals(ping_rsps[i])) return i; } return -1; }