public inbox for ecos-discuss@sourceware.org
 help / color / mirror / Atom feed
* [ECOS] condition variables and mutexes
@ 2001-08-09 14:08 Trenton D. Adams
  2001-08-10  3:00 ` Nick Garnett
  0 siblings, 1 reply; 7+ messages in thread
From: Trenton D. Adams @ 2001-08-09 14:08 UTC (permalink / raw)
  To: 'eCos Disuss'

Are the mutexes used with condition variables customarily used for
synchronization with other consumer threads only, or does the producer
thread use the mutexes as well?

I would imagine that the producer thread would generally not use the
mutex associated with a condition variable.  I would think that if a
mutex is needed, I should probably create a separate one for my use,
right?

Do I even care about the mutex associated with the condition variable if
I only have one consumer, and one producer?


Here's my situation in particular.

I've created a circular buffer that will hold three times the amount of
data required for each cycle.  Cycles usually occur once every second.
I've created three condition/mutex variable pairs for each of the three
slots in the circular buffer.  

Producer thread
- fill the first slot of the buffer with the data, and signal the
consumer thread  (condition 1)  
- fill the second slot of the buffer with the data, and signal the
consumer thread (condition 2)
- fill the third slot of the buffer with the data, and signal the
consumer thread  (condition 3)

Consumer thread
loop indefinately
- wait for signal 1
  - COPY data to another buffer
  - do something with data
- wait for signal 2
  - COPY data to another buffer
  - do something with data
- wait for signal 3
  - COPY data to another buffer
  - do something with data

I need a way of making sure that the head pointer of the circular buffer
doesn't change to point to the next slot before the consumer thread has
copied it.  I was thinking of using a mutex for this purpose.

Trenton D. Adams
Extreme Engineering
#17, 6025 - 12 St. SE
Calgary, Alberta, Canada
T2H 2K1

Phone: 403 640 9494 ext-208
Fax: 403 640 9599

http://www.extremeeng.com

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [ECOS] condition variables and mutexes
  2001-08-09 14:08 [ECOS] condition variables and mutexes Trenton D. Adams
@ 2001-08-10  3:00 ` Nick Garnett
  2001-08-10  7:37   ` Trenton D. Adams
  2001-08-10  7:52   ` Trenton D. Adams
  0 siblings, 2 replies; 7+ messages in thread
From: Nick Garnett @ 2001-08-10  3:00 UTC (permalink / raw)
  To: ecos-discuss

"Trenton D. Adams" <tadams@theone.dnsalias.com> writes:

> Are the mutexes used with condition variables customarily used for
> synchronization with other consumer threads only, or does the producer
> thread use the mutexes as well?
> 
> I would imagine that the producer thread would generally not use the
> mutex associated with a condition variable.  I would think that if a
> mutex is needed, I should probably create a separate one for my use,
> right?
> 
> Do I even care about the mutex associated with the condition variable if
> I only have one consumer, and one producer?
> 
> 
> Here's my situation in particular.
> 
> I've created a circular buffer that will hold three times the amount of
> data required for each cycle.  Cycles usually occur once every second.
> I've created three condition/mutex variable pairs for each of the three
> slots in the circular buffer.  
> 
> Producer thread
> - fill the first slot of the buffer with the data, and signal the
> consumer thread  (condition 1)  
> - fill the second slot of the buffer with the data, and signal the
> consumer thread (condition 2)
> - fill the third slot of the buffer with the data, and signal the
> consumer thread  (condition 3)
> 
> Consumer thread
> loop indefinately
> - wait for signal 1
>   - COPY data to another buffer
>   - do something with data
> - wait for signal 2
>   - COPY data to another buffer
>   - do something with data
> - wait for signal 3
>   - COPY data to another buffer
>   - do something with data
> 
> I need a way of making sure that the head pointer of the circular buffer
> doesn't change to point to the next slot before the consumer thread has
> copied it.  I was thinking of using a mutex for this purpose.
> 

Yes, a mutex is the right thing to use here. In a producer/consumer
situation they should normally be used in the following way:

Producer thread:
  loop indefinitely
    acquire/wait for data
    lock mutex
    copy data to buffer
    update buffer pointers
    signal condition variable
    unlock mutex

Consumer thread:
  loop indefinitely
    lock mutex
    while buffer empty
      wait for condition variable
    copy data out of buffer
    update buffer pointers
    unlock mutex
    do something with data


The important points here are that neither thread should access the
buffer unless it has the mutex locked, and the consumer should base
its decision on whether there is data in the buffer by looking at the
buffer pointers, and not on whether the signal has occured or not.

Hope this helps

-- 
Nick Garnett, eCos Kernel Architect
Red Hat, Cambridge, UK

^ permalink raw reply	[flat|nested] 7+ messages in thread

* RE: [ECOS] condition variables and mutexes
  2001-08-10  3:00 ` Nick Garnett
@ 2001-08-10  7:37   ` Trenton D. Adams
  2001-08-10  7:52   ` Trenton D. Adams
  1 sibling, 0 replies; 7+ messages in thread
From: Trenton D. Adams @ 2001-08-10  7:37 UTC (permalink / raw)
  To: 'Nick Garnett', ecos-discuss

That's great, thanks.  That clears a lot of conceptual problems I was
having! :)

-----Original Message-----
From: ecos-discuss-owner@sources.redhat.com
[ mailto:ecos-discuss-owner@sources.redhat.com ] On Behalf Of Nick Garnett
Sent: Friday, August 10, 2001 3:52 AM
To: ecos-discuss@sources.redhat.com
Subject: Re: [ECOS] condition variables and mutexes


"Trenton D. Adams" <tadams@theone.dnsalias.com> writes:

> Are the mutexes used with condition variables customarily used for
> synchronization with other consumer threads only, or does the producer
> thread use the mutexes as well?
> 
> I would imagine that the producer thread would generally not use the
> mutex associated with a condition variable.  I would think that if a
> mutex is needed, I should probably create a separate one for my use,
> right?
> 
> Do I even care about the mutex associated with the condition variable
if
> I only have one consumer, and one producer?
> 
> 
> Here's my situation in particular.
> 
> I've created a circular buffer that will hold three times the amount
of
> data required for each cycle.  Cycles usually occur once every second.
> I've created three condition/mutex variable pairs for each of the
three
> slots in the circular buffer.  
> 
> Producer thread
> - fill the first slot of the buffer with the data, and signal the
> consumer thread  (condition 1)  
> - fill the second slot of the buffer with the data, and signal the
> consumer thread (condition 2)
> - fill the third slot of the buffer with the data, and signal the
> consumer thread  (condition 3)
> 
> Consumer thread
> loop indefinately
> - wait for signal 1
>   - COPY data to another buffer
>   - do something with data
> - wait for signal 2
>   - COPY data to another buffer
>   - do something with data
> - wait for signal 3
>   - COPY data to another buffer
>   - do something with data
> 
> I need a way of making sure that the head pointer of the circular
buffer
> doesn't change to point to the next slot before the consumer thread
has
> copied it.  I was thinking of using a mutex for this purpose.
> 

Yes, a mutex is the right thing to use here. In a producer/consumer
situation they should normally be used in the following way:

Producer thread:
  loop indefinitely
    acquire/wait for data
    lock mutex
    copy data to buffer
    update buffer pointers
    signal condition variable
    unlock mutex

Consumer thread:
  loop indefinitely
    lock mutex
    while buffer empty
      wait for condition variable
    copy data out of buffer
    update buffer pointers
    unlock mutex
    do something with data


The important points here are that neither thread should access the
buffer unless it has the mutex locked, and the consumer should base
its decision on whether there is data in the buffer by looking at the
buffer pointers, and not on whether the signal has occured or not.

Hope this helps

-- 
Nick Garnett, eCos Kernel Architect
Red Hat, Cambridge, UK

^ permalink raw reply	[flat|nested] 7+ messages in thread

* RE: [ECOS] condition variables and mutexes
  2001-08-10  3:00 ` Nick Garnett
  2001-08-10  7:37   ` Trenton D. Adams
@ 2001-08-10  7:52   ` Trenton D. Adams
  2001-08-10  8:06     ` Nick Garnett
  2001-08-10  8:39     ` Robin Farine
  1 sibling, 2 replies; 7+ messages in thread
From: Trenton D. Adams @ 2001-08-10  7:52 UTC (permalink / raw)
  To: 'Nick Garnett', ecos-discuss

Do I have to do the "while buffer empty" part if I've only got one
consumer thread?  Because I figured the condition variable would only
get signaled once each round, and the buffer would never be empty except
for when the transmit thread is waiting on the condition variable.  No?

-----Original Message-----
From: ecos-discuss-owner@sources.redhat.com
[ mailto:ecos-discuss-owner@sources.redhat.com ] On Behalf Of Nick Garnett
Sent: Friday, August 10, 2001 3:52 AM
To: ecos-discuss@sources.redhat.com
Subject: Re: [ECOS] condition variables and mutexes


"Trenton D. Adams" <tadams@theone.dnsalias.com> writes:

> Are the mutexes used with condition variables customarily used for
> synchronization with other consumer threads only, or does the producer
> thread use the mutexes as well?
> 
> I would imagine that the producer thread would generally not use the
> mutex associated with a condition variable.  I would think that if a
> mutex is needed, I should probably create a separate one for my use,
> right?
> 
> Do I even care about the mutex associated with the condition variable
if
> I only have one consumer, and one producer?
> 
> 
> Here's my situation in particular.
> 
> I've created a circular buffer that will hold three times the amount
of
> data required for each cycle.  Cycles usually occur once every second.
> I've created three condition/mutex variable pairs for each of the
three
> slots in the circular buffer.  
> 
> Producer thread
> - fill the first slot of the buffer with the data, and signal the
> consumer thread  (condition 1)  
> - fill the second slot of the buffer with the data, and signal the
> consumer thread (condition 2)
> - fill the third slot of the buffer with the data, and signal the
> consumer thread  (condition 3)
> 
> Consumer thread
> loop indefinately
> - wait for signal 1
>   - COPY data to another buffer
>   - do something with data
> - wait for signal 2
>   - COPY data to another buffer
>   - do something with data
> - wait for signal 3
>   - COPY data to another buffer
>   - do something with data
> 
> I need a way of making sure that the head pointer of the circular
buffer
> doesn't change to point to the next slot before the consumer thread
has
> copied it.  I was thinking of using a mutex for this purpose.
> 

Yes, a mutex is the right thing to use here. In a producer/consumer
situation they should normally be used in the following way:

Producer thread:
  loop indefinitely
    acquire/wait for data
    lock mutex
    copy data to buffer
    update buffer pointers
    signal condition variable
    unlock mutex

Consumer thread:
  loop indefinitely
    lock mutex
    while buffer empty
      wait for condition variable
    copy data out of buffer
    update buffer pointers
    unlock mutex
    do something with data


The important points here are that neither thread should access the
buffer unless it has the mutex locked, and the consumer should base
its decision on whether there is data in the buffer by looking at the
buffer pointers, and not on whether the signal has occured or not.

Hope this helps

-- 
Nick Garnett, eCos Kernel Architect
Red Hat, Cambridge, UK

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [ECOS] condition variables and mutexes
  2001-08-10  7:52   ` Trenton D. Adams
@ 2001-08-10  8:06     ` Nick Garnett
  2001-08-10  8:39     ` Robin Farine
  1 sibling, 0 replies; 7+ messages in thread
From: Nick Garnett @ 2001-08-10  8:06 UTC (permalink / raw)
  To: Trenton D. Adams; +Cc: ecos-discuss

"Trenton D. Adams" <tadams@theone.dnsalias.com> writes:

> Do I have to do the "while buffer empty" part if I've only got one
> consumer thread?  Because I figured the condition variable would only
> get signaled once each round, and the buffer would never be empty except
> for when the transmit thread is waiting on the condition variable.  No?

It is not good practice to rely on the semantics of the condition
variable signal operation to always wake up exactly one waiter. We
explicitly permit condition variables to generate spurious wakeups. So
it can never be guaranteed that when a thread awakes from the wait,
that the condition is still true, it must always check the state by
independent means.

Now, you might get away with it in your case, but as a general rule,
the loop should always be used.


-- 
Nick Garnett, eCos Kernel Architect
Red Hat, Cambridge, UK

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [ECOS] condition variables and mutexes
  2001-08-10  7:52   ` Trenton D. Adams
  2001-08-10  8:06     ` Nick Garnett
@ 2001-08-10  8:39     ` Robin Farine
  2001-08-10  8:50       ` Robin Farine
  1 sibling, 1 reply; 7+ messages in thread
From: Robin Farine @ 2001-08-10  8:39 UTC (permalink / raw)
  To: Trenton D. Adams; +Cc: ecos-discuss

"Trenton D. Adams" <tadams@theone.dnsalias.com> writes:

> Do I have to do the "while buffer empty" part if I've only got one
> consumer thread?  Because I figured the condition variable would only
> get signaled once each round, and the buffer would never be empty except
> for when the transmit thread is waiting on the condition variable.  No?

If you have exactly one producer and one consumer then you can a simple solution
that uses a ring buffer and a cardinal semaphore that counts the items in the
buffer. The producer waits for the semaphore and uses the buffer's in_index
while the consumer uses the out_index and signals the semaphore.

[...]

Robin

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [ECOS] condition variables and mutexes
  2001-08-10  8:39     ` Robin Farine
@ 2001-08-10  8:50       ` Robin Farine
  0 siblings, 0 replies; 7+ messages in thread
From: Robin Farine @ 2001-08-10  8:50 UTC (permalink / raw)
  To: ecos-discuss

Heu, ahem ... correction:

The producer uses the buffer's in_index and *signals* the semaphore while the
consumer *waits* for the semaphore and uses the out_index.

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2001-08-10  8:50 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-08-09 14:08 [ECOS] condition variables and mutexes Trenton D. Adams
2001-08-10  3:00 ` Nick Garnett
2001-08-10  7:37   ` Trenton D. Adams
2001-08-10  7:52   ` Trenton D. Adams
2001-08-10  8:06     ` Nick Garnett
2001-08-10  8:39     ` Robin Farine
2001-08-10  8:50       ` Robin Farine

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).