Valid contexts – Comtrol eCos User Manual

Page 76

Advertising
background image

Semaphores

cyg_semaphore_post

is called when an event has occurs. This increments the counter and wakes up

the first thread waiting on the semaphore (if any). Usually that thread will then continue running inside

cyg_semaphore_wait

and decrement the counter again. However other scenarioes are possible. For example the

thread calling

cyg_semaphore_post

may be running at high priority, some other thread running at medium

priority may be about to call

cyg_semaphore_wait

when it next gets a chance to run, and a low priority thread

may be waiting on the semaphore. What will happen is that the current high priority thread continues running until
it is descheduled for some reason, then the medium priority thread runs and its call to

cyg_semaphore_wait

succeeds immediately, and later on the low priority thread runs again, discovers a counter value of 0, and blocks
until another event is posted. If there are multiple threads blocked on a semaphore then the configuration option

CYGIMP_KERNEL_SCHED_SORTED_QUEUES

determines which one will be woken up by a post operation.

cyg_semaphore_wait

returns a boolean. Normally it will block until it has successfully decremented the

counter, retrying as necessary, and return success. However the wait operation may be aborted by a call to

cyg_thread_release

, and

cyg_semaphore_wait

will then return false.

cyg_semaphore_timed_wait

is a variant of

cyg_semaphore_wait

. It can be used to wait until either an event

has occurred or a number of clock ticks have happened. The function returns success if the semaphore wait op-
eration succeeded, or false if the operation timed out or was aborted by

cyg_thread_release

. If support for

the real-time clock has been removed from the current configuration then this function will not be available.

cyg_semaphore_trywait

is another variant which will always return immediately rather than block, again re-

turning success or failure.

cyg_semaphore_peek

can be used to get hold of the current counter value. This function is rarely useful except

for debugging purposes since the counter value may change at any time if some other thread or a DSR performs a
semaphore operation.

Valid contexts

cyg_semaphore_init

is normally called during initialization but may also be called from thread context.

cyg_semaphore_wait

and

cyg_semaphore_timed_wait

may only be called from thread context because these

operations may block.

cyg_semaphore_trywait

,

cyg_semaphore_post

and

cyg_semaphore_peek

may be

called from thread or DSR context.

76

Advertising