<!-- Copyright (C) 2003 Red Hat, Inc. -->
|
<!-- Copyright (C) 2003 Red Hat, Inc. -->
|
<!-- This material may be distributed only subject to the terms -->
|
<!-- This material may be distributed only subject to the terms -->
|
<!-- and conditions set forth in the Open Publication License, v1.0 -->
|
<!-- and conditions set forth in the Open Publication License, v1.0 -->
|
<!-- or later (the latest version is presently available at -->
|
<!-- or later (the latest version is presently available at -->
|
<!-- http://www.opencontent.org/openpub/). -->
|
<!-- http://www.opencontent.org/openpub/). -->
|
<!-- Distribution of the work or derivative of the work in any -->
|
<!-- Distribution of the work or derivative of the work in any -->
|
<!-- standard (paper) book form is prohibited unless prior -->
|
<!-- standard (paper) book form is prohibited unless prior -->
|
<!-- permission is obtained from the copyright holder. -->
|
<!-- permission is obtained from the copyright holder. -->
|
<HTML
|
<HTML
|
><HEAD
|
><HEAD
|
><TITLE
|
><TITLE
|
>Condition Variables</TITLE
|
>Condition Variables</TITLE
|
><meta name="MSSmartTagsPreventParsing" content="TRUE">
|
><meta name="MSSmartTagsPreventParsing" content="TRUE">
|
<META
|
<META
|
NAME="GENERATOR"
|
NAME="GENERATOR"
|
CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+
|
CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+
|
"><LINK
|
"><LINK
|
REL="HOME"
|
REL="HOME"
|
TITLE="eCos Reference Manual"
|
TITLE="eCos Reference Manual"
|
HREF="ecos-ref.html"><LINK
|
HREF="ecos-ref.html"><LINK
|
REL="UP"
|
REL="UP"
|
TITLE="The eCos Kernel"
|
TITLE="The eCos Kernel"
|
HREF="kernel.html"><LINK
|
HREF="kernel.html"><LINK
|
REL="PREVIOUS"
|
REL="PREVIOUS"
|
TITLE="Mutexes"
|
TITLE="Mutexes"
|
HREF="kernel-mutexes.html"><LINK
|
HREF="kernel-mutexes.html"><LINK
|
REL="NEXT"
|
REL="NEXT"
|
TITLE="Semaphores"
|
TITLE="Semaphores"
|
HREF="kernel-semaphores.html"></HEAD
|
HREF="kernel-semaphores.html"></HEAD
|
><BODY
|
><BODY
|
CLASS="REFENTRY"
|
CLASS="REFENTRY"
|
BGCOLOR="#FFFFFF"
|
BGCOLOR="#FFFFFF"
|
TEXT="#000000"
|
TEXT="#000000"
|
LINK="#0000FF"
|
LINK="#0000FF"
|
VLINK="#840084"
|
VLINK="#840084"
|
ALINK="#0000FF"
|
ALINK="#0000FF"
|
><DIV
|
><DIV
|
CLASS="NAVHEADER"
|
CLASS="NAVHEADER"
|
><TABLE
|
><TABLE
|
SUMMARY="Header navigation table"
|
SUMMARY="Header navigation table"
|
WIDTH="100%"
|
WIDTH="100%"
|
BORDER="0"
|
BORDER="0"
|
CELLPADDING="0"
|
CELLPADDING="0"
|
CELLSPACING="0"
|
CELLSPACING="0"
|
><TR
|
><TR
|
><TH
|
><TH
|
COLSPAN="3"
|
COLSPAN="3"
|
ALIGN="center"
|
ALIGN="center"
|
>eCos Reference Manual</TH
|
>eCos Reference Manual</TH
|
></TR
|
></TR
|
><TR
|
><TR
|
><TD
|
><TD
|
WIDTH="10%"
|
WIDTH="10%"
|
ALIGN="left"
|
ALIGN="left"
|
VALIGN="bottom"
|
VALIGN="bottom"
|
><A
|
><A
|
HREF="kernel-mutexes.html"
|
HREF="kernel-mutexes.html"
|
ACCESSKEY="P"
|
ACCESSKEY="P"
|
>Prev</A
|
>Prev</A
|
></TD
|
></TD
|
><TD
|
><TD
|
WIDTH="80%"
|
WIDTH="80%"
|
ALIGN="center"
|
ALIGN="center"
|
VALIGN="bottom"
|
VALIGN="bottom"
|
></TD
|
></TD
|
><TD
|
><TD
|
WIDTH="10%"
|
WIDTH="10%"
|
ALIGN="right"
|
ALIGN="right"
|
VALIGN="bottom"
|
VALIGN="bottom"
|
><A
|
><A
|
HREF="kernel-semaphores.html"
|
HREF="kernel-semaphores.html"
|
ACCESSKEY="N"
|
ACCESSKEY="N"
|
>Next</A
|
>Next</A
|
></TD
|
></TD
|
></TR
|
></TR
|
></TABLE
|
></TABLE
|
><HR
|
><HR
|
ALIGN="LEFT"
|
ALIGN="LEFT"
|
WIDTH="100%"></DIV
|
WIDTH="100%"></DIV
|
><H1
|
><H1
|
><A
|
><A
|
NAME="KERNEL-CONDITION-VARIABLES">Condition Variables</H1
|
NAME="KERNEL-CONDITION-VARIABLES">Condition Variables</H1
|
><DIV
|
><DIV
|
CLASS="REFNAMEDIV"
|
CLASS="REFNAMEDIV"
|
><A
|
><A
|
NAME="AEN1232"
|
NAME="AEN1232"
|
></A
|
></A
|
><H2
|
><H2
|
>Name</H2
|
>Name</H2
|
>cyg_cond_init, cyg_cond_destroy, cyg_cond_wait, cyg_cond_timed_wait, cyg_cond_signal, cyg_cond_broadcast -- Synchronization primitive</DIV
|
>cyg_cond_init, cyg_cond_destroy, cyg_cond_wait, cyg_cond_timed_wait, cyg_cond_signal, cyg_cond_broadcast -- Synchronization primitive</DIV
|
><DIV
|
><DIV
|
CLASS="REFSYNOPSISDIV"
|
CLASS="REFSYNOPSISDIV"
|
><A
|
><A
|
NAME="AEN1240"><H2
|
NAME="AEN1240"><H2
|
>Synopsis</H2
|
>Synopsis</H2
|
><DIV
|
><DIV
|
CLASS="FUNCSYNOPSIS"
|
CLASS="FUNCSYNOPSIS"
|
><A
|
><A
|
NAME="AEN1241"><P
|
NAME="AEN1241"><P
|
></P
|
></P
|
><TABLE
|
><TABLE
|
BORDER="5"
|
BORDER="5"
|
BGCOLOR="#E0E0F0"
|
BGCOLOR="#E0E0F0"
|
WIDTH="70%"
|
WIDTH="70%"
|
><TR
|
><TR
|
><TD
|
><TD
|
><PRE
|
><PRE
|
CLASS="FUNCSYNOPSISINFO"
|
CLASS="FUNCSYNOPSISINFO"
|
>#include <cyg/kernel/kapi.h>
|
>#include <cyg/kernel/kapi.h>
|
</PRE
|
</PRE
|
></TD
|
></TD
|
></TR
|
></TR
|
></TABLE
|
></TABLE
|
><P
|
><P
|
><CODE
|
><CODE
|
><CODE
|
><CODE
|
CLASS="FUNCDEF"
|
CLASS="FUNCDEF"
|
>void cyg_cond_init</CODE
|
>void cyg_cond_init</CODE
|
>(cyg_cond_t* cond, cyg_mutex_t* mutex);</CODE
|
>(cyg_cond_t* cond, cyg_mutex_t* mutex);</CODE
|
></P
|
></P
|
><P
|
><P
|
><CODE
|
><CODE
|
><CODE
|
><CODE
|
CLASS="FUNCDEF"
|
CLASS="FUNCDEF"
|
>void cyg_cond_destroy</CODE
|
>void cyg_cond_destroy</CODE
|
>(cyg_cond_t* cond);</CODE
|
>(cyg_cond_t* cond);</CODE
|
></P
|
></P
|
><P
|
><P
|
><CODE
|
><CODE
|
><CODE
|
><CODE
|
CLASS="FUNCDEF"
|
CLASS="FUNCDEF"
|
>cyg_bool_t cyg_cond_wait</CODE
|
>cyg_bool_t cyg_cond_wait</CODE
|
>(cyg_cond_t* cond);</CODE
|
>(cyg_cond_t* cond);</CODE
|
></P
|
></P
|
><P
|
><P
|
><CODE
|
><CODE
|
><CODE
|
><CODE
|
CLASS="FUNCDEF"
|
CLASS="FUNCDEF"
|
>cyg_bool_t cyg_cond_timed_wait</CODE
|
>cyg_bool_t cyg_cond_timed_wait</CODE
|
>(cyg_cond_t* cond, cyg_tick_count_t abstime);</CODE
|
>(cyg_cond_t* cond, cyg_tick_count_t abstime);</CODE
|
></P
|
></P
|
><P
|
><P
|
><CODE
|
><CODE
|
><CODE
|
><CODE
|
CLASS="FUNCDEF"
|
CLASS="FUNCDEF"
|
>void cyg_cond_signal</CODE
|
>void cyg_cond_signal</CODE
|
>(cyg_cond_t* cond);</CODE
|
>(cyg_cond_t* cond);</CODE
|
></P
|
></P
|
><P
|
><P
|
><CODE
|
><CODE
|
><CODE
|
><CODE
|
CLASS="FUNCDEF"
|
CLASS="FUNCDEF"
|
>void cyg_cond_broadcast</CODE
|
>void cyg_cond_broadcast</CODE
|
>(cyg_cond_t* cond);</CODE
|
>(cyg_cond_t* cond);</CODE
|
></P
|
></P
|
><P
|
><P
|
></P
|
></P
|
></DIV
|
></DIV
|
></DIV
|
></DIV
|
><DIV
|
><DIV
|
CLASS="REFSECT1"
|
CLASS="REFSECT1"
|
><A
|
><A
|
NAME="KERNEL-CONDITION-VARIABLES-DESCRIPTION"
|
NAME="KERNEL-CONDITION-VARIABLES-DESCRIPTION"
|
></A
|
></A
|
><H2
|
><H2
|
>Description</H2
|
>Description</H2
|
><P
|
><P
|
>Condition variables are used in conjunction with mutexes to implement
|
>Condition variables are used in conjunction with mutexes to implement
|
long-term waits for some condition to become true. For example
|
long-term waits for some condition to become true. For example
|
consider a set of functions that control access to a pool of
|
consider a set of functions that control access to a pool of
|
resources:
|
resources:
|
</P
|
</P
|
><TABLE
|
><TABLE
|
BORDER="5"
|
BORDER="5"
|
BGCOLOR="#E0E0F0"
|
BGCOLOR="#E0E0F0"
|
WIDTH="70%"
|
WIDTH="70%"
|
><TR
|
><TR
|
><TD
|
><TD
|
><PRE
|
><PRE
|
CLASS="PROGRAMLISTING"
|
CLASS="PROGRAMLISTING"
|
> cyg_mutex_t res_lock;
|
> cyg_mutex_t res_lock;
|
res_t res_pool[RES_MAX];
|
res_t res_pool[RES_MAX];
|
int res_count = RES_MAX;
|
int res_count = RES_MAX;
|
|
|
void res_init(void)
|
void res_init(void)
|
{
|
{
|
cyg_mutex_init(&res_lock);
|
cyg_mutex_init(&res_lock);
|
<fill pool with resources>
|
<fill pool with resources>
|
}
|
}
|
|
|
res_t res_allocate(void)
|
res_t res_allocate(void)
|
{
|
{
|
res_t res;
|
res_t res;
|
|
|
cyg_mutex_lock(&res_lock); // lock the mutex
|
cyg_mutex_lock(&res_lock); // lock the mutex
|
|
|
if( res_count == 0 ) // check for free resource
|
if( res_count == 0 ) // check for free resource
|
res = RES_NONE; // return RES_NONE if none
|
res = RES_NONE; // return RES_NONE if none
|
else
|
else
|
{
|
{
|
res_count--; // allocate a resources
|
res_count--; // allocate a resources
|
res = res_pool[res_count];
|
res = res_pool[res_count];
|
}
|
}
|
|
|
cyg_mutex_unlock(&res_lock); // unlock the mutex
|
cyg_mutex_unlock(&res_lock); // unlock the mutex
|
|
|
return res;
|
return res;
|
}
|
}
|
|
|
void res_free(res_t res)
|
void res_free(res_t res)
|
{
|
{
|
cyg_mutex_lock(&res_lock); // lock the mutex
|
cyg_mutex_lock(&res_lock); // lock the mutex
|
|
|
res_pool[res_count] = res; // free the resource
|
res_pool[res_count] = res; // free the resource
|
res_count++;
|
res_count++;
|
|
|
cyg_mutex_unlock(&res_lock); // unlock the mutex
|
cyg_mutex_unlock(&res_lock); // unlock the mutex
|
}
|
}
|
</PRE
|
</PRE
|
></TD
|
></TD
|
></TR
|
></TR
|
></TABLE
|
></TABLE
|
><P
|
><P
|
>These routines use the variable <TT
|
>These routines use the variable <TT
|
CLASS="VARNAME"
|
CLASS="VARNAME"
|
>res_count</TT
|
>res_count</TT
|
> to keep
|
> to keep
|
track of the resources available. If there are none then
|
track of the resources available. If there are none then
|
<TT
|
<TT
|
CLASS="FUNCTION"
|
CLASS="FUNCTION"
|
>res_allocate</TT
|
>res_allocate</TT
|
> returns <TT
|
> returns <TT
|
CLASS="LITERAL"
|
CLASS="LITERAL"
|
>RES_NONE</TT
|
>RES_NONE</TT
|
>,
|
>,
|
which the caller must check for and take appropriate error handling
|
which the caller must check for and take appropriate error handling
|
actions.
|
actions.
|
</P
|
</P
|
><P
|
><P
|
>Now suppose that we do not want to return
|
>Now suppose that we do not want to return
|
<TT
|
<TT
|
CLASS="LITERAL"
|
CLASS="LITERAL"
|
>RES_NONE</TT
|
>RES_NONE</TT
|
> when there are no resources, but want to
|
> when there are no resources, but want to
|
wait for one to become available. This is where a condition variable
|
wait for one to become available. This is where a condition variable
|
can be used:
|
can be used:
|
</P
|
</P
|
><TABLE
|
><TABLE
|
BORDER="5"
|
BORDER="5"
|
BGCOLOR="#E0E0F0"
|
BGCOLOR="#E0E0F0"
|
WIDTH="70%"
|
WIDTH="70%"
|
><TR
|
><TR
|
><TD
|
><TD
|
><PRE
|
><PRE
|
CLASS="PROGRAMLISTING"
|
CLASS="PROGRAMLISTING"
|
> cyg_mutex_t res_lock;
|
> cyg_mutex_t res_lock;
|
cyg_cond_t res_wait;
|
cyg_cond_t res_wait;
|
res_t res_pool[RES_MAX];
|
res_t res_pool[RES_MAX];
|
int res_count = RES_MAX;
|
int res_count = RES_MAX;
|
|
|
void res_init(void)
|
void res_init(void)
|
{
|
{
|
cyg_mutex_init(&res_lock);
|
cyg_mutex_init(&res_lock);
|
cyg_cond_init(&res_wait, &res_lock);
|
cyg_cond_init(&res_wait, &res_lock);
|
<fill pool with resources>
|
<fill pool with resources>
|
}
|
}
|
|
|
res_t res_allocate(void)
|
res_t res_allocate(void)
|
{
|
{
|
res_t res;
|
res_t res;
|
|
|
cyg_mutex_lock(&res_lock); // lock the mutex
|
cyg_mutex_lock(&res_lock); // lock the mutex
|
|
|
while( res_count == 0 ) // wait for a resources
|
while( res_count == 0 ) // wait for a resources
|
cyg_cond_wait(&res_wait);
|
cyg_cond_wait(&res_wait);
|
|
|
res_count--; // allocate a resource
|
res_count--; // allocate a resource
|
res = res_pool[res_count];
|
res = res_pool[res_count];
|
|
|
cyg_mutex_unlock(&res_lock); // unlock the mutex
|
cyg_mutex_unlock(&res_lock); // unlock the mutex
|
|
|
return res;
|
return res;
|
}
|
}
|
|
|
void res_free(res_t res)
|
void res_free(res_t res)
|
{
|
{
|
cyg_mutex_lock(&res_lock); // lock the mutex
|
cyg_mutex_lock(&res_lock); // lock the mutex
|
|
|
res_pool[res_count] = res; // free the resource
|
res_pool[res_count] = res; // free the resource
|
res_count++;
|
res_count++;
|
|
|
cyg_cond_signal(&res_wait); // wake up any waiting allocators
|
cyg_cond_signal(&res_wait); // wake up any waiting allocators
|
|
|
cyg_mutex_unlock(&res_lock); // unlock the mutex
|
cyg_mutex_unlock(&res_lock); // unlock the mutex
|
}
|
}
|
</PRE
|
</PRE
|
></TD
|
></TD
|
></TR
|
></TR
|
></TABLE
|
></TABLE
|
><P
|
><P
|
>In this version of the code, when <TT
|
>In this version of the code, when <TT
|
CLASS="FUNCTION"
|
CLASS="FUNCTION"
|
>res_allocate</TT
|
>res_allocate</TT
|
>
|
>
|
detects that there are no resources it calls
|
detects that there are no resources it calls
|
<TT
|
<TT
|
CLASS="FUNCTION"
|
CLASS="FUNCTION"
|
>cyg_cond_wait</TT
|
>cyg_cond_wait</TT
|
>. This does two things: it unlocks
|
>. This does two things: it unlocks
|
the mutex, and puts the calling thread to sleep on the condition
|
the mutex, and puts the calling thread to sleep on the condition
|
variable. When <TT
|
variable. When <TT
|
CLASS="FUNCTION"
|
CLASS="FUNCTION"
|
>res_free</TT
|
>res_free</TT
|
> is eventually called, it
|
> is eventually called, it
|
puts a resource back into the pool and calls
|
puts a resource back into the pool and calls
|
<TT
|
<TT
|
CLASS="FUNCTION"
|
CLASS="FUNCTION"
|
>cyg_cond_signal</TT
|
>cyg_cond_signal</TT
|
> to wake up any thread waiting on
|
> to wake up any thread waiting on
|
the condition variable. When the waiting thread eventually gets to run again,
|
the condition variable. When the waiting thread eventually gets to run again,
|
it will re-lock the mutex before returning from
|
it will re-lock the mutex before returning from
|
<TT
|
<TT
|
CLASS="FUNCTION"
|
CLASS="FUNCTION"
|
>cyg_cond_wait</TT
|
>cyg_cond_wait</TT
|
>.
|
>.
|
</P
|
</P
|
><P
|
><P
|
>There are two important things to note about the way in which this
|
>There are two important things to note about the way in which this
|
code works. The first is that the mutex unlock and wait in
|
code works. The first is that the mutex unlock and wait in
|
<TT
|
<TT
|
CLASS="FUNCTION"
|
CLASS="FUNCTION"
|
>cyg_cond_wait</TT
|
>cyg_cond_wait</TT
|
> are atomic: no other thread can run
|
> are atomic: no other thread can run
|
between the unlock and the wait. If this were not the case then a call
|
between the unlock and the wait. If this were not the case then a call
|
to <TT
|
to <TT
|
CLASS="FUNCTION"
|
CLASS="FUNCTION"
|
>res_free</TT
|
>res_free</TT
|
> by that thread would release the
|
> by that thread would release the
|
resource but the call to <TT
|
resource but the call to <TT
|
CLASS="FUNCTION"
|
CLASS="FUNCTION"
|
>cyg_cond_signal</TT
|
>cyg_cond_signal</TT
|
> would be
|
> would be
|
lost, and the first thread would end up waiting when there were
|
lost, and the first thread would end up waiting when there were
|
resources available.
|
resources available.
|
</P
|
</P
|
><P
|
><P
|
>The second feature is that the call to
|
>The second feature is that the call to
|
<TT
|
<TT
|
CLASS="FUNCTION"
|
CLASS="FUNCTION"
|
>cyg_cond_wait</TT
|
>cyg_cond_wait</TT
|
> is in a <TT
|
> is in a <TT
|
CLASS="LITERAL"
|
CLASS="LITERAL"
|
>while</TT
|
>while</TT
|
>
|
>
|
loop and not a simple <TT
|
loop and not a simple <TT
|
CLASS="LITERAL"
|
CLASS="LITERAL"
|
>if</TT
|
>if</TT
|
> statement. This is because
|
> statement. This is because
|
of the need to re-lock the mutex in <TT
|
of the need to re-lock the mutex in <TT
|
CLASS="FUNCTION"
|
CLASS="FUNCTION"
|
>cyg_cond_wait</TT
|
>cyg_cond_wait</TT
|
>
|
>
|
when the signalled thread reawakens. If there are other threads
|
when the signalled thread reawakens. If there are other threads
|
already queued to claim the lock then this thread must wait. Depending
|
already queued to claim the lock then this thread must wait. Depending
|
on the scheduler and the queue order, many other threads may have
|
on the scheduler and the queue order, many other threads may have
|
entered the critical section before this one gets to run. So the
|
entered the critical section before this one gets to run. So the
|
condition that it was waiting for may have been rendered false. Using
|
condition that it was waiting for may have been rendered false. Using
|
a loop around all condition variable wait operations is the only way
|
a loop around all condition variable wait operations is the only way
|
to guarantee that the condition being waited for is still true after
|
to guarantee that the condition being waited for is still true after
|
waiting.
|
waiting.
|
</P
|
</P
|
><P
|
><P
|
>Before a condition variable can be used it must be initialized with a
|
>Before a condition variable can be used it must be initialized with a
|
call to <TT
|
call to <TT
|
CLASS="FUNCTION"
|
CLASS="FUNCTION"
|
>cyg_cond_init</TT
|
>cyg_cond_init</TT
|
>. This requires two
|
>. This requires two
|
arguments, memory for the data structure and a pointer to an existing
|
arguments, memory for the data structure and a pointer to an existing
|
mutex. This mutex will not be initialized by
|
mutex. This mutex will not be initialized by
|
<TT
|
<TT
|
CLASS="FUNCTION"
|
CLASS="FUNCTION"
|
>cyg_cond_init</TT
|
>cyg_cond_init</TT
|
>, instead a separate call to
|
>, instead a separate call to
|
<TT
|
<TT
|
CLASS="FUNCTION"
|
CLASS="FUNCTION"
|
>cyg_mutex_init</TT
|
>cyg_mutex_init</TT
|
> is required. If a condition
|
> is required. If a condition
|
variable is no longer required and there are no threads waiting on it
|
variable is no longer required and there are no threads waiting on it
|
then <TT
|
then <TT
|
CLASS="FUNCTION"
|
CLASS="FUNCTION"
|
>cyg_cond_destroy</TT
|
>cyg_cond_destroy</TT
|
> can be used.
|
> can be used.
|
</P
|
</P
|
><P
|
><P
|
>When a thread needs to wait for a condition to be satisfied it can
|
>When a thread needs to wait for a condition to be satisfied it can
|
call <TT
|
call <TT
|
CLASS="FUNCTION"
|
CLASS="FUNCTION"
|
>cyg_cond_wait</TT
|
>cyg_cond_wait</TT
|
>. The thread must have already
|
>. The thread must have already
|
locked the mutex that was specified in the
|
locked the mutex that was specified in the
|
<TT
|
<TT
|
CLASS="FUNCTION"
|
CLASS="FUNCTION"
|
>cyg_cond_init</TT
|
>cyg_cond_init</TT
|
> call. This mutex will be unlocked
|
> call. This mutex will be unlocked
|
and the current thread will be suspended in an atomic operation. When
|
and the current thread will be suspended in an atomic operation. When
|
some other thread performs a signal or broadcast operation the current
|
some other thread performs a signal or broadcast operation the current
|
thread will be woken up and automatically reclaim ownership of the mutex
|
thread will be woken up and automatically reclaim ownership of the mutex
|
again, allowing it to examine global state and determine whether or
|
again, allowing it to examine global state and determine whether or
|
not the condition is now satisfied. The kernel supplies a variant of
|
not the condition is now satisfied. The kernel supplies a variant of
|
this function, <TT
|
this function, <TT
|
CLASS="FUNCTION"
|
CLASS="FUNCTION"
|
>cyg_cond_timed_wait</TT
|
>cyg_cond_timed_wait</TT
|
>, which can be
|
>, which can be
|
used to wait on the condition variable or until some number of clock
|
used to wait on the condition variable or until some number of clock
|
ticks have occurred. The mutex will always be reclaimed before
|
ticks have occurred. The mutex will always be reclaimed before
|
<TT
|
<TT
|
CLASS="FUNCTION"
|
CLASS="FUNCTION"
|
>cyg_cond_timed_wait</TT
|
>cyg_cond_timed_wait</TT
|
> returns, regardless of
|
> returns, regardless of
|
whether it was a result of a signal operation or a timeout.
|
whether it was a result of a signal operation or a timeout.
|
</P
|
</P
|
><P
|
><P
|
>There is no <TT
|
>There is no <TT
|
CLASS="FUNCTION"
|
CLASS="FUNCTION"
|
>cyg_cond_trywait</TT
|
>cyg_cond_trywait</TT
|
> function because
|
> function because
|
this would not serve any purpose. If a thread has locked the mutex and
|
this would not serve any purpose. If a thread has locked the mutex and
|
determined that the condition is satisfied, it can just release the
|
determined that the condition is satisfied, it can just release the
|
mutex and return. There is no need to perform any operation on the
|
mutex and return. There is no need to perform any operation on the
|
condition variable.
|
condition variable.
|
</P
|
</P
|
><P
|
><P
|
>When a thread changes shared state that may affect some other thread
|
>When a thread changes shared state that may affect some other thread
|
blocked on a condition variable, it should call either
|
blocked on a condition variable, it should call either
|
<TT
|
<TT
|
CLASS="FUNCTION"
|
CLASS="FUNCTION"
|
>cyg_cond_signal</TT
|
>cyg_cond_signal</TT
|
> or
|
> or
|
<TT
|
<TT
|
CLASS="FUNCTION"
|
CLASS="FUNCTION"
|
>cyg_cond_broadcast</TT
|
>cyg_cond_broadcast</TT
|
>. These calls do not require
|
>. These calls do not require
|
ownership of the mutex, but usually the mutex will have been claimed
|
ownership of the mutex, but usually the mutex will have been claimed
|
before updating the shared state. A signal operation only wakes up the
|
before updating the shared state. A signal operation only wakes up the
|
first thread that is waiting on the condition variable, while a
|
first thread that is waiting on the condition variable, while a
|
broadcast wakes up all the threads. If there are no threads waiting on
|
broadcast wakes up all the threads. If there are no threads waiting on
|
the condition variable at the time, then the signal or broadcast will
|
the condition variable at the time, then the signal or broadcast will
|
have no effect: past signals are not counted up or remembered in any
|
have no effect: past signals are not counted up or remembered in any
|
way. Typically a signal should be used when all threads will check the
|
way. Typically a signal should be used when all threads will check the
|
same condition and at most one thread can continue running. A
|
same condition and at most one thread can continue running. A
|
broadcast should be used if threads check slightly different
|
broadcast should be used if threads check slightly different
|
conditions, or if the change to the global state might allow multiple
|
conditions, or if the change to the global state might allow multiple
|
threads to proceed.
|
threads to proceed.
|
</P
|
</P
|
></DIV
|
></DIV
|
><DIV
|
><DIV
|
CLASS="REFSECT1"
|
CLASS="REFSECT1"
|
><A
|
><A
|
NAME="KERNEL-CONDITION-VARIABLES-CONTEXT"
|
NAME="KERNEL-CONDITION-VARIABLES-CONTEXT"
|
></A
|
></A
|
><H2
|
><H2
|
>Valid contexts</H2
|
>Valid contexts</H2
|
><P
|
><P
|
><TT
|
><TT
|
CLASS="FUNCTION"
|
CLASS="FUNCTION"
|
>cyg_cond_init</TT
|
>cyg_cond_init</TT
|
> is typically called during system
|
> is typically called during system
|
initialization but may also be called in thread context. The same
|
initialization but may also be called in thread context. The same
|
applies to <TT
|
applies to <TT
|
CLASS="FUNCTION"
|
CLASS="FUNCTION"
|
>cyg_cond_delete</TT
|
>cyg_cond_delete</TT
|
>.
|
>.
|
<TT
|
<TT
|
CLASS="FUNCTION"
|
CLASS="FUNCTION"
|
>cyg_cond_wait</TT
|
>cyg_cond_wait</TT
|
> and
|
> and
|
<TT
|
<TT
|
CLASS="FUNCTION"
|
CLASS="FUNCTION"
|
>cyg_cond_timedwait</TT
|
>cyg_cond_timedwait</TT
|
> may only be called from thread
|
> may only be called from thread
|
context since they may block. <TT
|
context since they may block. <TT
|
CLASS="FUNCTION"
|
CLASS="FUNCTION"
|
>cyg_cond_signal</TT
|
>cyg_cond_signal</TT
|
> and
|
> and
|
<TT
|
<TT
|
CLASS="FUNCTION"
|
CLASS="FUNCTION"
|
>cyg_cond_broadcast</TT
|
>cyg_cond_broadcast</TT
|
> may be called from thread or
|
> may be called from thread or
|
DSR context.
|
DSR context.
|
</P
|
</P
|
></DIV
|
></DIV
|
><DIV
|
><DIV
|
CLASS="NAVFOOTER"
|
CLASS="NAVFOOTER"
|
><HR
|
><HR
|
ALIGN="LEFT"
|
ALIGN="LEFT"
|
WIDTH="100%"><TABLE
|
WIDTH="100%"><TABLE
|
SUMMARY="Footer navigation table"
|
SUMMARY="Footer navigation table"
|
WIDTH="100%"
|
WIDTH="100%"
|
BORDER="0"
|
BORDER="0"
|
CELLPADDING="0"
|
CELLPADDING="0"
|
CELLSPACING="0"
|
CELLSPACING="0"
|
><TR
|
><TR
|
><TD
|
><TD
|
WIDTH="33%"
|
WIDTH="33%"
|
ALIGN="left"
|
ALIGN="left"
|
VALIGN="top"
|
VALIGN="top"
|
><A
|
><A
|
HREF="kernel-mutexes.html"
|
HREF="kernel-mutexes.html"
|
ACCESSKEY="P"
|
ACCESSKEY="P"
|
>Prev</A
|
>Prev</A
|
></TD
|
></TD
|
><TD
|
><TD
|
WIDTH="34%"
|
WIDTH="34%"
|
ALIGN="center"
|
ALIGN="center"
|
VALIGN="top"
|
VALIGN="top"
|
><A
|
><A
|
HREF="ecos-ref.html"
|
HREF="ecos-ref.html"
|
ACCESSKEY="H"
|
ACCESSKEY="H"
|
>Home</A
|
>Home</A
|
></TD
|
></TD
|
><TD
|
><TD
|
WIDTH="33%"
|
WIDTH="33%"
|
ALIGN="right"
|
ALIGN="right"
|
VALIGN="top"
|
VALIGN="top"
|
><A
|
><A
|
HREF="kernel-semaphores.html"
|
HREF="kernel-semaphores.html"
|
ACCESSKEY="N"
|
ACCESSKEY="N"
|
>Next</A
|
>Next</A
|
></TD
|
></TD
|
></TR
|
></TR
|
><TR
|
><TR
|
><TD
|
><TD
|
WIDTH="33%"
|
WIDTH="33%"
|
ALIGN="left"
|
ALIGN="left"
|
VALIGN="top"
|
VALIGN="top"
|
>Mutexes</TD
|
>Mutexes</TD
|
><TD
|
><TD
|
WIDTH="34%"
|
WIDTH="34%"
|
ALIGN="center"
|
ALIGN="center"
|
VALIGN="top"
|
VALIGN="top"
|
><A
|
><A
|
HREF="kernel.html"
|
HREF="kernel.html"
|
ACCESSKEY="U"
|
ACCESSKEY="U"
|
>Up</A
|
>Up</A
|
></TD
|
></TD
|
><TD
|
><TD
|
WIDTH="33%"
|
WIDTH="33%"
|
ALIGN="right"
|
ALIGN="right"
|
VALIGN="top"
|
VALIGN="top"
|
>Semaphores</TD
|
>Semaphores</TD
|
></TR
|
></TR
|
></TABLE
|
></TABLE
|
></DIV
|
></DIV
|
></BODY
|
></BODY
|
></HTML
|
></HTML
|
|
|