Sha256: b6e310c26f67b60bf48f237439658e76fd72f14271a8a2853bcdcb7187f76661

Contents?: true

Size: 1.22 KB

Versions: 8

Compression:

Stored size: 1.22 KB

Contents

#--
#            DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
#                    Version 2, December 2004
#
#            DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
#   TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
#
#  0. You just DO WHAT THE FUCK YOU WANT TO.
#++

require 'thread/channel'

# A process should only interact with the outside the outside through messages,
# it still uses a thread, but it should make it safer to use than sharing and
# locks.
class Thread::Process
	def self.all
		@@processes ||= {}
	end

	def self.register (name, process)
		all[name] = process
	end

	def self.unregister (name)
		all.delete(name)
	end

	def self.[] (name)
		all[name]
	end

	# Create a new process executing the block.
	def initialize (&block)
		@channel = Thread::Channel.new

		Thread.new {
			instance_eval(&block)

			@channel = nil
		}
	end

	# Send a message to the process.
	def send (what)
		unless @channel
			raise RuntimeError, 'the process has terminated'
		end

		@channel.send(what)

		self
	end

	alias << send

private
	def receive
		@channel.receive
	end

	def receive!
		@channel.receive!
	end
end

class Thread
	# Helper to create a process.
	def self.process (&block)
		Thread::Process.new(&block)
	end
end

Version data entries

8 entries across 8 versions & 2 rubygems

Version Path
thread-0.1.3 lib/thread/process.rb
thread-0.1.2 lib/thread/process.rb
vinted-thread-0.1.1 lib/thread/process.rb
thread-0.1.1 lib/thread/process.rb
thread-0.1.0 lib/thread/process.rb
thread-0.0.8.1 lib/thread/process.rb
thread-0.0.8 lib/thread/process.rb
thread-0.0.7 lib/thread/process.rb