public Buffer pinNew ( String fileName , PageFormatter fmtr ) { if ( pinnedBuffers . size ( ) == BUFFER_POOL_SIZE ) throw new BufferAbortException ( ) ; try { Buffer buff ; long timestamp = System . currentTimeMillis ( ) ; boolean waitedBeforeGotBuffer = false ; // Try to pin a buffer or the pinned buffer for the given BlockId buff = bufferPool . pinNew ( fileName , fmtr ) ; // If there is no such buffer or no available buffer, // wait for it if ( buff == null ) { waitedBeforeGotBuffer = true ; synchronized ( bufferPool ) { waitingThreads . add ( Thread . currentThread ( ) ) ; while ( buff == null && ! waitingTooLong ( timestamp ) ) { bufferPool . wait ( MAX_TIME ) ; if ( waitingThreads . get ( 0 ) . equals ( Thread . currentThread ( ) ) ) buff = bufferPool . pinNew ( fileName , fmtr ) ; } waitingThreads . remove ( Thread . currentThread ( ) ) ; } } // If it still has no buffer after a long wait, // release and re-pin all buffers it has if ( buff == null ) { repin ( ) ; buff = pinNew ( fileName , fmtr ) ; } else { pinnedBuffers . put ( buff . block ( ) , new PinnedBuffer ( buff ) ) ; } // TODO: Add some comment here if ( waitedBeforeGotBuffer ) { synchronized ( bufferPool ) { bufferPool . notifyAll ( ) ; } } return buff ; } catch ( InterruptedException e ) { throw new BufferAbortException ( ) ; } }
Pins a buffer to a new block in the specified file potentially waiting until a buffer becomes available . If no buffer becomes available within a fixed time period then repins all currently holding blocks .