Sha256: 3db5dbef68c7423f6b8d3484eb82bb394db30c936fa905166264d008aec49e6c
Contents?: true
Size: 1.86 KB
Versions: 1
Compression:
Stored size: 1.86 KB
Contents
# whiteboard.rb contains Whiteboard class require 'thread' require 'pp' # Whiteboard is a pretty simple messaging class, that can be used for # different (local) threads. It's meant for use in storing incoming messages # for later processing. # # The normal usage looks like this: # w=Whiteboard.new # <spawn thread1 and thread2> # thread1: a=w.get(:myKey) # <thread1 is suspended, because myKey is not there> # thread2: w.put(:myKey,:myValue) # <thread1 is woken up and has a==:myValue> # # This is NOT Possible: # Whiteboard.new.put(:myKey,:myValue) # because nobody's listening for myKey # # This is NOT possible either: # w=Whiteboard.new # thread1: w.get(:myValue) # thread2: w.get(:myValue) # because thread1 is already waiting for this value # # in practise this is not a problem, because the values are transaction-ids within node communication should not be duplicated. class Whiteboard def initialize() @cvs={} @values={} @mutex=Mutex.new end # sends +value+ directly to the listening thread waiting for +name+ # if there's no waiting thread an exception is raised def put(name,value) @mutex.synchronize { raise "Conflict in put" if @values[name] @values[name]=value raise "No one listening" if @cvs[name].nil? @cvs[name].wakeup @cvs.delete(name) } end # waits for name to be put into the local store then returns the value def get(name,timeout=nil) value=nil measureTime(7) { @mutex.synchronize { raise "Already value there" if @values[name] raise "Already listening" if @cvs[name] @cvs[name]=Thread.current } if timeout.nil? Thread.stop else # FIXME:wait until timeout Thread.stop end @mutex.synchronize { value=@values[name] @values.delete(name) } } value end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
appswarm-0.0.1 | lib/appswarm/whiteboard.rb |