Class: ThreadLocalBuffer

Inherits:
Object
  • Object
show all
Defined in:
lib/ec2/amitools/util.rb

Overview

A thread local buffer manager. Provide’s each thread with a single pre-allocated IO buffer. IMPORTANT: as these buffers are indexed in a hashtable they will not be freed until the application closes. If a thread needs to free the memory its buffer is using, it must call delete_buffer and ensure it has no references to the buffer.

Constant Summary collapse

POWER =

Log SIZE base 2

12
SIZE =

Size of the buffer.

2**POWER
@@buffers =
{}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.get_bufferObject

Return the thread’s buffer.



38
39
40
41
42
43
# File 'lib/ec2/amitools/util.rb', line 38

def ThreadLocalBuffer.get_buffer
  @@buffers.synchronize do
    @@buffers[Thread.current] = new_buffer unless @@buffers.has_key?( Thread.current )
    @@buffers[Thread.current]
  end
end

Instance Method Details

#delete_buffer(buffer) ⇒ Object

Delete the threads buffer.



48
49
50
# File 'lib/ec2/amitools/util.rb', line 48

def delete_buffer buffer
  @@buffers.delete buffer
end