note
	description: "Summary description for {CANCELLABLE_EXECUTOR}."
	author: ""
	date: "$Date$"
	revision: "$Revision$"

class
	CANCELLABLE_EXECUTOR

create
	make

feature
	--events
 	execution_cancelled : EVENT_TYPE[TUPLE]
	execution_completed : EVENT_TYPE[TUPLE]

	make--(a_exec: separate EXECUTION_UNIT)
	do

		--exec := a_exec
		create token.empty
		create execution_completed
		create execution_cancelled
	end

	set_new_token(a_token : separate CANCELLATION_REQUEST)
	do
		token := a_token
	end

	run(a_unit: separate EXECUTION_UNIT)
	do
		live(a_unit)
	end
feature{NONE}
	live(a_unit : separate EXECUTION_UNIT)
	do
		from
		until
			is_done(a_unit)
		loop
			if(check_cancel_requested(token)) then
				cancel_requested := TRUE
				execution_cancelled.publish ([])
			else
				loop_once(a_unit)
			end
		end

		if(not cancel_requested) then
			execution_completed.publish ([])
		end
		cancel_requested:= FALSE
	end

	check_cancel_requested(a_token : separate CANCELLATION_REQUEST) : BOOLEAN
	do
		Result := a_token.cancellation_requested
	end

	is_done(a_exec : separate EXECUTION_UNIT) : BOOLEAN
	do
		if(cancel_requested) then
			Result := TRUE
		else
			Result := a_exec.is_done
		end
	end

	loop_once(a_exec : separate EXECUTION_UNIT)
	do
		a_exec.action;
	end

 	cancel_requested : BOOLEAN
	token : separate CANCELLATION_REQUEST

end

