note
	description: "[
				Condition variables allow threads to synchronize based on the content of a shared data, whereas
				mutexes only synchronize access to the data. In other words, a condition variable is a
				synchronization object that enables threads to wait until a particular condition occurs.
				
				When a thread executes a wait call on a condition variable, it must hold an associated mutex
				(used for checking that condition). Then, it is immediately suspended and put into the waiting
				queue. The thread is suspended and is waiting for the condition to occur.
		
				Eventually, when the condition has occurred, a thread will signal it. Two possible scenarios:
				- if there are threads waiting, then one of the waiting thread will resume its execution and
				  will get the mutex in a locked state.
				- if there are no threads waiting, nothing is done
		
				For the simple usage of a condition variable, it is very similar to using a semaphore.
				
				In addition you have broadcast that will resume all waiting threads at once, and
				wait_with_timeout that will wait only a certain amount of time before abandonning the wait.
				
				The signal and broadcast routines can be called by a thread whether or not it currently owns
				the mutex that threads calling wait or wait_with_timeout have associated with the condition
				variable during their waits. If, however, predictable scheduling behavior is required, then that
				mutex should be locked by the thread prior to calling signal or broadcast.
				
				Assuming shared_data an INTEGER initially set to zero, then a typical usage of condition variable
				to wait until shared_data becomes one, could be written as followed in thread A:
				
					mutex.lock
					from
					until
						shared_data = 1
					loop
						condition_variable.wait (mutex)
					end
					mutex.unlock
					
				and in thread B:
				
					mutex.lock
					shared_data := 1
					condition_variable.signal
					mutex.unlock
					
				Thread A will be blocked until thread B signal that now shared_data is 1.
	]"
	legal: "See notice at end of class."
	status: "See notice at end of class."
	date: "$Date: 2017-05-25 15:02:28 +0000 (Thu, 25 May 2017) $"
	revision: "$Revision: 100436 $"

class interface
	CONDITION_VARIABLE

create 
	make

feature -- Access

	is_set: BOOLEAN
			-- Is condition variable initialized?
	
feature -- Status setting

	signal
			-- Unblock one thread blocked on the current condition variable.
		require
			is_set: is_set

	broadcast
			-- Unblock all threads blocked on the current condition variable.
		require
			is_set: is_set

	wait (a_mutex: MUTEX)
			-- Block calling thread on current condition variable.
		require
			is_set: is_set
			a_mutex_not_void: a_mutex /= Void

	wait_with_timeout (a_mutex: MUTEX; a_timeout_ms: INTEGER_32): BOOLEAN
			-- Block calling thread on current condition variable for at most a_timeout milliseconds.
			-- Return True is we got the condition variable on time, otherwise return False
		require
			is_set: is_set
			a_mutex_not_void: a_mutex /= Void
			timeout_positive: a_timeout_ms >= 0

	destroy
			-- Destroy condition variable.
		require
			is_set: is_set
		ensure
			not_set: not is_set
	
feature -- Removal

	dispose
			-- Called by the garbage collector when the condition
			-- variable is collected.
	
note
	copyright: "Copyright (c) 1984-2017, Eiffel Software and others"
	license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)"
	source: "[
		Eiffel Software
		5949 Hollister Ave., Goleta, CA 93117 USA
		Telephone 805-685-1006, Fax 805-685-6869
		Website http://www.eiffel.com
		Customer support http://support.eiffel.com
	]"

end -- class CONDITION_VARIABLE

Generated by ISE EiffelStudio