2008-02-25 22:33:07 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								! Copyright (C) 2008 Slava Pestov.  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								! See http://factorcode.org/license.txt for BSD license.  
						 
					
						
							
								
									
										
										
										
											2015-11-27 07:15:17 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								USING:  calendar  concurrency.conditions  help.markup  help.syntax  ;
  
						 
					
						
							
								
									
										
										
										
											2008-02-25 22:33:07 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								IN:  concurrency.flags  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								HELP:  flag  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{ $class-description "A flag allows one thread to notify another when a condition is satisfied."  } ;
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								HELP:  <flag>  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{ $values { "flag"  flag } }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{ $description "Creates a new flag."  } ;
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-11-27 07:15:17 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								HELP:  lower-flag  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{ $values { "flag"  flag } }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{ $description "Attempts to lower a flag. If the flag has been raised previously, returns immediately, otherwise waits for it to be raised first."  } ;
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2008-02-25 22:33:07 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								HELP:  raise-flag  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{ $values { "flag"  flag } }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{ $description "Raises a flag, notifying any threads waiting on it. Does nothing if the flag has already been raised."  } ;
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2008-02-27 18:15:52 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								HELP:  wait-for-flag  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{ $values { "flag"  flag } }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{ $description "Waits for a flag to be raised. If the flag has already been raised, returns immediately."  } ;
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-11-27 07:15:17 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								HELP:  wait-for-flag-timeout  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{ $values { "flag"  flag } { "timeout"  duration } }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{ $description "Waits for a flag to be raised or throws a "  { $link timed-out-error } " if the flag wasn't raised in time."  } ;
 
							 
						 
					
						
							
								
									
										
										
										
											2008-02-25 22:33:07 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								ARTICLE: "concurrency.flags"  "Flags" 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								"A "  { $emphasis "flag"  } " is a condition notification device which can be in one of two states: "  { $emphasis "lowered"  } " (the initial state) or "  { $emphasis "raised"  } "."  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								$nl
							 
						 
					
						
							
								
									
										
										
										
											2008-02-27 17:31:13 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								"The flag can be raised at any time; raising a raised flag does nothing. Lowering a flag if it has not been raised yet will wait for another thread to raise the flag."  
						 
					
						
							
								
									
										
										
										
											2008-02-25 22:33:07 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								$nl
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								"Essentially, a flag can be thought of as a counting semaphore where the count never goes above one."  
						 
					
						
							
								
									
										
										
										
											2009-10-01 15:56:36 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								{ $subsections
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    flag
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    flag?
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
									
										
										
										
											2008-02-27 18:15:52 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								"Waiting for a flag to be raised:"  
						 
					
						
							
								
									
										
										
										
											2009-10-01 15:56:36 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								{ $subsections
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    raise-flag
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    wait-for-flag
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    lower-flag
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								} ;
 
							 
						 
					
						
							
								
									
										
										
										
											2008-02-25 22:33:07 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								ABOUT: "concurrency.flags"