Class Semaphore

Simple coroutine synchronization device.

class Semaphore( [initValue] )

more...

Summary

post()Increments the count of the semaphore.
wait()Waits on a semaphore.

Detailed description

class Semaphore( [initValue] )

initValueInitial value for the semaphore; if not given, 0 will be assumed.

Simple coroutine synchronization device.

The semaphore is a simple synchronization object that is used by coroutines to communicate each others about relevant changes in the status of the application.

Decrements the value of the semaphore, and eventually waits for the value to be > 0. When a Semaphore.wait method is called on a semaphore, two things may happen: if the value of the semaphore is greater than zero, the value is decremented and the coroutine can proceed. If it's zero, the coroutine is swapped out until the semaphore gets greater than zero again. When this happens, the coroutine decrements the value of the semaphore and proceeds. If a timeout parameter is given, in case the semaphore wasn't posted before the given timeout the function will return false.

The order by which coroutines are resumed is the same by which they asked to wait on a semaphore. In this sense, Semaphore.wait method is implemented as a fair wait routine.

The Semaphore.post method will raise the count of the semaphore by the given parameter (1 is the default if the parameter is not given). However, the calling coroutine won't necessarily be swapped out until a yield is called.

By default, the semaphore is initialized to zero; this means that the first wait will block the waiting coroutine, unless a Semaphore.post is issued first.

Methods

post()

Increments the count of the semaphore.

Semaphore.post( [count] )

countThe amount by which the semaphore will be incremented (1 by default).

This method will increment the count of the semaphore by 1 or by a specified amount, allowing the same number of waiting coroutines to proceed.

However, the calling coroutine won't necessarily be swapped out until a yield is called.

wait()

Waits on a semaphore.

Semaphore.wait( [timeout] )

timeoutOptional maximum wait in seconds.

Decrements the value of the semaphore, and eventually waits for the value to be greater than zero.


Made with faldoc 2.2.0