* Use semaphore for producer/consumer case...
@ 2001-03-23 12:07 Stelian Pop
2001-03-23 23:52 ` Nigel Gamble
0 siblings, 1 reply; 7+ messages in thread
From: Stelian Pop @ 2001-03-23 12:07 UTC (permalink / raw)
To: linux-kernel
Hi,
I want to use a semaphore for the classic producer/consumer case
(put the consumer to wait until X items are produced, where X != 1).
If X is 1, the semaphore is a simple MUTEX, ok.
But if the consumer wants to wait for several items, it doesn't
seem to work (or something is bad in my code).
What is wrong in the following ?
DECLARE_MUTEX(sem);
producer() {
/* One item produced */
up(&sem);
}
consumer() {
/* Let's wait for 10 items */
atomic_set(&sem->count, -10);
/* This starts the producers, they will call producer()
some time in the future */
start_producers();
/* Wait for completion */
down(&sem);
}
I can get around this by using a mutex and a separate atomic_t
'count' variable, but it's more a workaround that a solution...
Thanks,
Stelian.
--
Stelian Pop <stelian.pop@fr.alcove.com>
|------------- Ingénieur Informatique Libre --------------|
| Alcôve - http://www.alcove.com - Tel: +33 1 49 22 68 00 |
|----------- Alcôve, l'informatique est libre ------------|
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Use semaphore for producer/consumer case...
@ 2001-03-23 18:34 Manfred Spraul
2001-03-26 14:54 ` Stelian Pop
0 siblings, 1 reply; 7+ messages in thread
From: Manfred Spraul @ 2001-03-23 18:34 UTC (permalink / raw)
To: stelian.pop; +Cc: linux-kernel
> consumer()
> /* Let's wait for 10 items */
> atomic_set(&sem->count, -10);
That doesn't work, at least the i386 semaphore implementation doesn't
support semaphore counts < 0.
--
Manfred
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Use semaphore for producer/consumer case...
2001-03-23 12:07 Stelian Pop
@ 2001-03-23 23:52 ` Nigel Gamble
2001-03-26 14:52 ` Stelian Pop
0 siblings, 1 reply; 7+ messages in thread
From: Nigel Gamble @ 2001-03-23 23:52 UTC (permalink / raw)
To: Stelian Pop; +Cc: linux-kernel
On Fri, 23 Mar 2001, Stelian Pop wrote:
> I want to use a semaphore for the classic producer/consumer case
> (put the consumer to wait until X items are produced, where X != 1).
>
> If X is 1, the semaphore is a simple MUTEX, ok.
>
> But if the consumer wants to wait for several items, it doesn't
> seem to work (or something is bad in my code).
>
> What is wrong in the following ?
>
> DECLARE_MUTEX(sem);
For the producer/consumer case, you want to initialize the semaphore to
0, not 1 which DECLARE_MUTEX(sem) does. So I would use
__DECLARE_SEMAPHORE_GENERIC(sem, 0)
The count is then the number of items produced but not yet consumed.
> producer() {
> /* One item produced */
> up(&sem);
> }
>
> consumer() {
> /* Let's wait for 10 items */
> atomic_set(&sem->count, -10);
>
> /* This starts the producers, they will call producer()
> some time in the future */
> start_producers();
>
> /* Wait for completion */
> down(&sem);
> }
Then consumer could be:
consumer()
{
int i;
start_producers();
/* Wait for 10 items to be produced */
for (i = 0; i < 10; i++)
down(&sem);
}
Nigel Gamble nigel@nrg.org
Mountain View, CA, USA. http://www.nrg.org/
MontaVista Software nigel@mvista.com
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Use semaphore for producer/consumer case...
2001-03-23 23:52 ` Nigel Gamble
@ 2001-03-26 14:52 ` Stelian Pop
0 siblings, 0 replies; 7+ messages in thread
From: Stelian Pop @ 2001-03-26 14:52 UTC (permalink / raw)
To: Nigel Gamble; +Cc: linux-kernel
On Fri, Mar 23, 2001 at 03:52:54PM -0800, Nigel Gamble wrote:
> For the producer/consumer case, you want to initialize the semaphore to
> 0, not 1 which DECLARE_MUTEX(sem) does. So I would use
>
> __DECLARE_SEMAPHORE_GENERIC(sem, 0)
You're right that the DECLARE_MUTEX does not (entirely) do the job,
but I set manually the value of sem->count:
> > atomic_set(&sem->count, -10);
> Then consumer could be:
> /* Wait for 10 items to be produced */
> for (i = 0; i < 10; i++)
> down(&sem);
IMHO, this will not work, because the producer could be issuing
more than one 'up(&sem)' at the time...
Stelian.
--
Stelian Pop <stelian.pop@fr.alcove.com>
|------------- Ingénieur Informatique Libre --------------|
| Alcôve - http://www.alcove.com - Tel: +33 1 49 22 68 00 |
|----------- Alcôve, l'informatique est libre ------------|
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Use semaphore for producer/consumer case...
2001-03-23 18:34 Use semaphore for producer/consumer case Manfred Spraul
@ 2001-03-26 14:54 ` Stelian Pop
2001-03-26 17:12 ` Manfred Spraul
0 siblings, 1 reply; 7+ messages in thread
From: Stelian Pop @ 2001-03-26 14:54 UTC (permalink / raw)
To: Manfred Spraul; +Cc: linux-kernel
On Fri, Mar 23, 2001 at 07:34:07PM +0100, Manfred Spraul wrote:
> > consumer()
>
> > /* Let's wait for 10 items */
> > atomic_set(&sem->count, -10);
>
> That doesn't work, at least the i386 semaphore implementation doesn't
> support semaphore counts < 0.
Does that mean that kernel semaphore can not be used for something
else than mutual exclusion ?
Stelian.
--
Stelian Pop <stelian.pop@fr.alcove.com>
|------------- Ingénieur Informatique Libre --------------|
| Alcôve - http://www.alcove.com - Tel: +33 1 49 22 68 00 |
|----------- Alcôve, l'informatique est libre ------------|
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Use semaphore for producer/consumer case...
2001-03-26 14:54 ` Stelian Pop
@ 2001-03-26 17:12 ` Manfred Spraul
2001-03-27 7:57 ` Stelian Pop
0 siblings, 1 reply; 7+ messages in thread
From: Manfred Spraul @ 2001-03-26 17:12 UTC (permalink / raw)
To: Stelian Pop; +Cc: linux-kernel
From: "Stelian Pop" <stelian.pop@fr.alcove.com>
> >
> > That doesn't work, at least the i386 semaphore implementation
doesn't
> > support semaphore counts < 0.
>
> Does that mean that kernel semaphore can not be used for something
> else than mutual exclusion ?
>
It's a bit better: counts >= 0 are supported, i.e. you can call up()
before down(), and that's used in several places.
The for loop that Nigel proposed should solve your problem. Multiple
up's are handled correctly.
--
Manfred
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Use semaphore for producer/consumer case...
2001-03-26 17:12 ` Manfred Spraul
@ 2001-03-27 7:57 ` Stelian Pop
0 siblings, 0 replies; 7+ messages in thread
From: Stelian Pop @ 2001-03-27 7:57 UTC (permalink / raw)
To: Manfred Spraul; +Cc: linux-kernel
On Mon, Mar 26, 2001 at 07:12:55PM +0200, Manfred Spraul wrote:
> > > That doesn't work, at least the i386 semaphore implementation
> doesn't
> > > support semaphore counts < 0.
> >
> > Does that mean that kernel semaphore can not be used for something
> > else than mutual exclusion ?
> >
> It's a bit better: counts >= 0 are supported, i.e. you can call up()
> before down(), and that's used in several places.
I see... it's somewhat different than the classical semaphore
implementation, but usable anyway.
> The for loop that Nigel proposed should solve your problem. Multiple
> up's are handled correctly.
Now I understand his suggestion. Thanks to both of you.
Stelian.
--
Stelian Pop <stelian.pop@fr.alcove.com>
|------------- Ingénieur Informatique Libre --------------|
| Alcôve - http://www.alcove.com - Tel: +33 1 49 22 68 00 |
|----------- Alcôve, l'informatique est libre ------------|
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2001-03-27 7:58 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2001-03-23 18:34 Use semaphore for producer/consumer case Manfred Spraul
2001-03-26 14:54 ` Stelian Pop
2001-03-26 17:12 ` Manfred Spraul
2001-03-27 7:57 ` Stelian Pop
-- strict thread matches above, loose matches on Subject: below --
2001-03-23 12:07 Stelian Pop
2001-03-23 23:52 ` Nigel Gamble
2001-03-26 14:52 ` Stelian Pop
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox