Count Down Latch

Count Down Latch
Count Down Latch is synchronization aid that allows one or more threads to wait until a set of operations being performed in other threads completes.
A Count Down Latch is initialized with a given count. The await method block until the currentcount reaches zero due to invocations of thecountDown method, after which all waiting threads are released and any subsequent invocations of await return immediately. This is a one-shot phenomenon -- the count cannot be reset. If you need a version that resets the count, consider using a Cyclic Barier.
 

RAM structure
The implementation given below uses an atomic counter which is initialized before the latch is used.The Latch is not a reusable synchronizer so the count can not be reset. The counter supports atomic decrement which will basically count down the “external” events.
One byte of RAM is needed to store the synchronization artifact - atomic counter.


 

{C} - the counter byte - up to 256 external events could be waited on


Cyclic Barrier similarities
While the Cyclic Barrier event is triggered when the number of threads reache the barrier count - all engaged in await function, the Count Down Latch event is triggered by an “external” thread that is not engaged with Latch’s await function.

While the Cyclic Barrier counts the number of threads reaching the barrier,the Count Down Latch counts the events happening as a result of some other thread(s) processing.

Simple Count Down Latch   
What follows is a simple implementation of a Count Down Latch in assembler. The latch state consits of a counter initialized to a positive number, representing the number of events to wait for.

;INIT part

 

1    ldi temp,@1                      ;load count byte
2    sts @0,temp                    ;save count byte in RAM


The countDown method decrements the counter, indicating that an event has occurred.

;COUNT DOWN part
;must be an atomic operation
1    cli  
3    lds temp,@0
4    tst temp
5    breq exit
6    dec temp
7    sts @0,temp  
exit:
8    sei



And await methods wait for the counter to reach zero, which happens when all the events have occurred.

;WAIT part
loop:    
1    lds temp,@0
2    tst temp    
3    breq exit      
4    _YIELD_TASK
5    rjmp loop
exit



A Count Down Latch initialized with a count of one serves as a simple on/off latch, or gate: all threads invokingawait() wait at the gate until it is opened by a thread invokingcountDown(). A CountDownLatch initialized to N can be used to make one thread wait until N threads have completed some action, or some action has been completed N times.

There is no order in threads leaving the latch as to the order they have arrived to the latch wait() - the latch is not fair.

The count down latch will be available in the next kernel release(2.1 and higher). In the context of ACORN kernel terms thread and task are used interchangeably.

 

 

 

 

Comments

Be the first to comment.




 Add Comment

  Comments 1 - 0 of 0  

Screen Name
Subject
Comment