* Re: [9fans] semaphores
@ 2002-01-25 14:53 Russ Cox
0 siblings, 0 replies; 10+ messages in thread
From: Russ Cox @ 2002-01-25 14:53 UTC (permalink / raw)
To: 9fans
> Should that be `typedef struct Semaphore Sema;' in order for s->data to
> work?
probably struct Sema, but yeah.
i haven't actually compiled the code.
like i said, as soon as i wrote it
i discovered i didn't need it.
^ permalink raw reply [flat|nested] 10+ messages in thread
* [9fans] Semaphores
@ 2006-10-22 23:05 Joel Salomon
2006-10-22 23:21 ` Russ Cox
2006-10-22 23:29 ` Steve Simon
0 siblings, 2 replies; 10+ messages in thread
From: Joel Salomon @ 2006-10-22 23:05 UTC (permalink / raw)
To: Fans of the OS Plan 9 from Bell Labs
Homework help: Could someone point me to an explanation of semaphore
implementation? I'm supposed to implement something very like
semacquire(2) using TAS spinlocks (the professor says it's OK for me
to use lock(2), but not qlock), but in user mode. He suggests using
signals for wakeup.
I'm looking at /sys/src/9/port/sysproc.c and /sys/doc/sleep.* not
quite understanding either. Is there a readable description of
queueing on a semaphore in the literature somewhere? Google's mostly
turning up assembler-heavy linux kernel one-upsmanship.
--Joel
--
In googlis non est, ergo non est.
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [9fans] semaphores
@ 2002-01-16 7:48 nigel
0 siblings, 0 replies; 10+ messages in thread
From: nigel @ 2002-01-16 7:48 UTC (permalink / raw)
To: 9fans
[-- Attachment #1: Type: text/plain, Size: 39 bytes --]
As does the Lock module in Inferno.
[-- Attachment #2: Type: message/rfc822, Size: 1435 bytes --]
From: Scott Schwartz <schwartz@bio.cse.psu.edu>
To: 9fans@cse.psu.edu
Subject: Re: [9fans] semaphores
Date: Tue, 15 Jan 2002 22:54:56 -0500
Message-ID: <20020116035456.13985.qmail@g.bio.cse.psu.edu>
A previous edition's malloc used a pipe with a byte in it as a semaphore.
I thought that was a lovely solution, given shared file descriptors.
^ permalink raw reply [flat|nested] 10+ messages in thread
[parent not found: <rsc@plan9.bell-labs.com>]
* [9fans] semaphores
@ 2002-01-16 3:44 ` Russ Cox
2002-01-16 3:54 ` Scott Schwartz
2002-01-25 10:30 ` Ralph Corderoy
0 siblings, 2 replies; 10+ messages in thread
From: Russ Cox @ 2002-01-16 3:44 UTC (permalink / raw)
To: 9fans
I just wrote these for a porting project and
then discovered that I didn't need them.
However, they seem a little subtle to me
(I gave up the first couple times I tried)
so I figured I'd toss them out for others
and to see if anyone noticed any problems.
Russ
/*
* Semaphore support.
*
* The data QLock could be a Lock if you care;
* it protects the n and need values.
*
* The wait QLock manages the line of people waiting to
* acquire the semaphore. If you manage to qlock it,
* it's your turn. If n isn't big enough for you, record
* your needs and rendezvous on the Sema; once incsema
* makes n big enough, it will rendezvous with you.
*/
typedef struct Sema Sema;
struct Semaphore {
QLock data;
QLock wait;
int n;
int need;
};
void
incsema(Sema *s, int c)
{
qlock(&s->data);
s->n += c;
if(s->need > 0 && s->n >= s->need){
rendezvous(s, 0);
s->need = 0;
}
qunlock(&s->data);
}
void
decsema(Sema *s, int c)
{
qlock(&s->wait);
qlock(&s->data);
if(s->n < c){
s->need = c;
qunlock(&s->data);
rendezvous(s, 0);
qlock(&s->data);
}
assert(s->n >= c);
s->n -= c;
qunlock(&s->data);
qunlock(&s->wait);
}
int
candecsema(Sema *s, int c)
{
int ret;
if(!canqlock(&s->wait))
return 0;
qlock(&s->data); /* won't block for long */
if(s->n >= c){
ret = 1;
s->n -= c;
}else
ret = 0;
qunlock(&s->data);
qunlock(&s->wait);
return ret;
}
^ permalink raw reply [flat|nested] 10+ messages in thread
end of thread, other threads:[~2006-10-22 23:29 UTC | newest]
Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-01-25 14:53 [9fans] semaphores Russ Cox
-- strict thread matches above, loose matches on Subject: below --
2006-10-22 23:05 [9fans] Semaphores Joel Salomon
2006-10-22 23:21 ` Russ Cox
2006-10-22 23:29 ` Steve Simon
2002-01-16 7:48 [9fans] semaphores nigel
[not found] <rsc@plan9.bell-labs.com>
2002-01-16 3:44 ` Russ Cox
2002-01-16 3:54 ` Scott Schwartz
2002-01-16 11:14 ` Boyd Roberts
2002-01-16 15:47 ` Ronald G Minnich
2002-01-25 10:30 ` Ralph Corderoy
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 NNTP newsgroup(s).