9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
* [9fans] qunlock
@ 2004-06-22  6:04 dvd
  2004-06-22  8:06 ` C H Forsyth
  0 siblings, 1 reply; 20+ messages in thread
From: dvd @ 2004-06-22  6:04 UTC (permalink / raw)
  To: 9fans

calling qunlock on an unlocked QLock is no-op, isn't it?



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

* Re: [9fans] qunlock
  2004-06-22  6:04 [9fans] qunlock dvd
@ 2004-06-22  8:06 ` C H Forsyth
  2004-06-22  9:09   ` C H Forsyth
  0 siblings, 1 reply; 20+ messages in thread
From: C H Forsyth @ 2004-06-22  8:06 UTC (permalink / raw)
  To: 9fans

[-- Attachment #1: Type: text/plain, Size: 109 bytes --]

i'd have thought it best to regard it as best avoided.
does it happen, or was it something you wanted to do?

[-- Attachment #2: Type: message/rfc822, Size: 2595 bytes --]

From: dvd@davidashen.net
To: 9fans@cse.psu.edu
Subject: [9fans] qunlock
Date: Tue, 22 Jun 2004 11:04:20 +0500
Message-ID: <259e5ab496f281b48d4e6ad53a58891d@davidashen.net>

calling qunlock on an unlocked QLock is no-op, isn't it?

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

* Re: [9fans] qunlock
  2004-06-22  8:06 ` C H Forsyth
@ 2004-06-22  9:09   ` C H Forsyth
  2004-06-22 14:19     ` ron minnich
  2004-06-23 20:11     ` dvd
  0 siblings, 2 replies; 20+ messages in thread
From: C H Forsyth @ 2004-06-22  9:09 UTC (permalink / raw)
  To: 9fans

[-- Attachment #1: Type: text/plain, Size: 179 bytes --]

actually, i'd have said such an effect was unusable (or relying on it
was pointless) in general, at least with more than one process, so i'm
now more curious how you'd use it.

[-- Attachment #2: Type: message/rfc822, Size: 5134 bytes --]

[-- Attachment #2.1.1: Type: text/plain, Size: 109 bytes --]

i'd have thought it best to regard it as best avoided.
does it happen, or was it something you wanted to do?

[-- Attachment #2.1.2: Type: message/rfc822, Size: 2595 bytes --]

From: dvd@davidashen.net
To: 9fans@cse.psu.edu
Subject: [9fans] qunlock
Date: Tue, 22 Jun 2004 11:04:20 +0500
Message-ID: <259e5ab496f281b48d4e6ad53a58891d@davidashen.net>

calling qunlock on an unlocked QLock is no-op, isn't it?

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

* Re: [9fans] qunlock
  2004-06-22  9:09   ` C H Forsyth
@ 2004-06-22 14:19     ` ron minnich
  2004-06-22 14:38       ` boyd, rounin
  2004-06-23 20:11     ` dvd
  1 sibling, 1 reply; 20+ messages in thread
From: ron minnich @ 2004-06-22 14:19 UTC (permalink / raw)
  To: Fans of the OS Plan 9 from Bell Labs

On Tue, 22 Jun 2004, C H Forsyth wrote:

> actually, i'd have said such an effect was unusable (or relying on it
> was pointless) in general, at least with more than one process, so i'm
> now more curious how you'd use it.


to me qunlock of an unlock'ed thing should be a cause for panic.

ron



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

* Re: [9fans] qunlock
  2004-06-22 14:19     ` ron minnich
@ 2004-06-22 14:38       ` boyd, rounin
  2004-06-22 15:13         ` Rob Pike
  0 siblings, 1 reply; 20+ messages in thread
From: boyd, rounin @ 2004-06-22 14:38 UTC (permalink / raw)
  To: Fans of the OS Plan 9 from Bell Labs

> to me qunlock of an unlock'ed thing should be a cause for panic.

seconded.



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

* Re: [9fans] qunlock
  2004-06-22 14:38       ` boyd, rounin
@ 2004-06-22 15:13         ` Rob Pike
  0 siblings, 0 replies; 20+ messages in thread
From: Rob Pike @ 2004-06-22 15:13 UTC (permalink / raw)
  To: Fans of the OS Plan 9 from Bell Labs

if it's already unlocked, don't unlock it.

-rob


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

* Re: [9fans] qunlock
  2004-06-22  9:09   ` C H Forsyth
  2004-06-22 14:19     ` ron minnich
@ 2004-06-23 20:11     ` dvd
  2004-06-23 20:24       ` Rob Pike
  1 sibling, 1 reply; 20+ messages in thread
From: dvd @ 2004-06-23 20:11 UTC (permalink / raw)
  To: 9fans

> > calling qunlock on an unlocked QLock is no-op, isn't it?
> 
> i'd have thought it best to regard it as best avoided.
> does it happen, or was it something you wanted to do?
> 

I need to release a qlock in  a criticial session guarded by another lock,

QLock p,q;

void x() {
	qlock(&p);

	if(...)
		qunlock(&q);

	qunlock(&p);
}

void y() {
	qlock(&q);
	qlock(&p);

	qunlock(&p);
}

The question is whether the above is legal or should I write

if(...){
	canqlock(&q);
	qunlock(&q);
}

instead?



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

* Re: [9fans] qunlock
  2004-06-23 20:11     ` dvd
@ 2004-06-23 20:24       ` Rob Pike
  2004-06-23 21:00         ` dvd
  0 siblings, 1 reply; 20+ messages in thread
From: Rob Pike @ 2004-06-23 20:24 UTC (permalink / raw)
  To: Fans of the OS Plan 9 from Bell Labs

you can't do that. what if you do the first unlock,
then someone else locks it in the interim. you'll
unlock his lock. bad bad bad.

-rob


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

* Re: [9fans] qunlock
  2004-06-23 20:24       ` Rob Pike
@ 2004-06-23 21:00         ` dvd
  2004-06-23 21:12           ` boyd, rounin
                             ` (2 more replies)
  0 siblings, 3 replies; 20+ messages in thread
From: dvd @ 2004-06-23 21:00 UTC (permalink / raw)
  To: 9fans

> you can't do that. what if you do the first unlock,
> then someone else locks it in the interim. you'll
> unlock his lock. bad bad bad.
> 
> -rob

It happens in a critical section guarded by another mutex. 
Noone else can unlock my lock.

Should I uncomment the commented-out calls below?


void
A()
{
againA:
	qlock(a);
	qlock(q);
		if(x) {
   			qunlock(q);
			goto againA;
		}
	/* canqlock(b); */
	qunlock(b);
	qunlock(q);
	qunlock(a);
}

void
B()
{
againB:
	qlock(b);
	qlock(q);
		if(y){
			qunlock(q);
			goto againB;
		}
	/* canqlock(a); */
	qunlock(a);
	qunlock(q);
	qunlock(b);
}



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

* Re: [9fans] qunlock
  2004-06-23 21:00         ` dvd
@ 2004-06-23 21:12           ` boyd, rounin
  2004-06-24  7:19           ` Fco. J. Ballesteros
  2004-06-24  7:34           ` Fco. J. Ballesteros
  2 siblings, 0 replies; 20+ messages in thread
From: boyd, rounin @ 2004-06-23 21:12 UTC (permalink / raw)
  To: Fans of the OS Plan 9 from Bell Labs

restructure the code and make sure you lock/unlock in the same 'order':

    lock:    A B C D E
    unlock: E D C B A

you could use canlock, but i think yer inviting races.

and get _rid_ of those gotos!



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

* Re: [9fans] qunlock
  2004-06-23 21:00         ` dvd
  2004-06-23 21:12           ` boyd, rounin
@ 2004-06-24  7:19           ` Fco. J. Ballesteros
  2004-06-24 10:58             ` dvd
  2004-06-24  7:34           ` Fco. J. Ballesteros
  2 siblings, 1 reply; 20+ messages in thread
From: Fco. J. Ballesteros @ 2004-06-24  7:19 UTC (permalink / raw)
  To: 9fans

Maybe it's just that it's too early and I got soo few coffee, but,
do meant this, didn't you?

> void
> A()
> {

	qlock(a);
againA:

> 	qlock(q);
> 		if(x) {
>    			qunlock(q);
> 			goto againA;
> 		}
....


> void
> B()
> {

 	qlock(b);
againB:

> 	qlock(q);
> 		if(y){
> 			qunlock(q);
> 			goto againB;
> 		}
....



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

* Re: [9fans] qunlock
  2004-06-23 21:00         ` dvd
  2004-06-23 21:12           ` boyd, rounin
  2004-06-24  7:19           ` Fco. J. Ballesteros
@ 2004-06-24  7:34           ` Fco. J. Ballesteros
  2004-06-24 11:02             ` dvd
  2 siblings, 1 reply; 20+ messages in thread
From: Fco. J. Ballesteros @ 2004-06-24  7:34 UTC (permalink / raw)
  To: 9fans

> 	/* canqlock(b); */
> 	qunlock(b);

This is very weird for me.
Can't you rework your scheme to avoid such things?
Perhaps this code is suggesting that your locking
conventions may need more work.



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

* Re: [9fans] qunlock
  2004-06-24  7:19           ` Fco. J. Ballesteros
@ 2004-06-24 10:58             ` dvd
  0 siblings, 0 replies; 20+ messages in thread
From: dvd @ 2004-06-24 10:58 UTC (permalink / raw)
  To: 9fans

> Maybe it's just that it's too early and I got soo few coffee, but,
> do meant this, didn't you?

No, I didn't; I mean that under certain condition each of the processes
steps aside and opens the way for the other one until the other one executes
at least once. 

The original code does exactly that.  Your changes just spin each of the processes once the corresponding condition becomes true.

> 
>> void
>> A()
>> {
> 
> 	qlock(a);
> againA:
> 
>> 	qlock(q);
>> 		if(x) {
>>    			qunlock(q);
>> 			goto againA;
>> 		}
> ....
> 
> 
>> void
>> B()
>> {
> 
>  	qlock(b);
> againB:
> 
>> 	qlock(q);
>> 		if(y){
>> 			qunlock(q);
>> 			goto againB;
>> 		}
> ....



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

* Re: [9fans] qunlock
  2004-06-24  7:34           ` Fco. J. Ballesteros
@ 2004-06-24 11:02             ` dvd
  2004-06-24 11:14               ` Fco. J. Ballesteros
  2004-06-24 13:42               ` boyd, rounin
  0 siblings, 2 replies; 20+ messages in thread
From: dvd @ 2004-06-24 11:02 UTC (permalink / raw)
  To: 9fans

>> 	/* canqlock(b); */
>> 	qunlock(b);
> 
> This is very weird for me.
> Can't you rework your scheme to avoid such things?
> Perhaps this code is suggesting that your locking
> conventions may need more work.

That's why I am asking. What exactly is weird? The code actually works
in SPIN (as a model), and in Python (but I check locked() before unlocking).

The purpose is to have multiple producers and consumers on an queue
with exclusive access, with priority always given to consumers if there
are waiting ones and the queue is not empty.

The implementation of qlock/qunlock allows to unlock when not locked;
I am asking whether it is by design or by a coincidence?

David



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

* Re: [9fans] qunlock
  2004-06-24 11:02             ` dvd
@ 2004-06-24 11:14               ` Fco. J. Ballesteros
  2004-06-24 12:18                 ` dvd
  2004-06-24 13:42               ` boyd, rounin
  1 sibling, 1 reply; 20+ messages in thread
From: Fco. J. Ballesteros @ 2004-06-24 11:14 UTC (permalink / raw)
  To: 9fans

> The purpose is to have multiple producers and consumers on an queue
> with exclusive access, with priority always given to consumers if there
> are waiting ones and the queue is not empty.


Why don't you use channels to implement your sleep/wakeup
mechanism. Using a lock to do so is weird for me, but perhaps others
are used to it.



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

* Re: [9fans] qunlock
  2004-06-24 11:14               ` Fco. J. Ballesteros
@ 2004-06-24 12:18                 ` dvd
  2004-06-24 13:09                   ` Fco. J. Ballesteros
                                     ` (2 more replies)
  0 siblings, 3 replies; 20+ messages in thread
From: dvd @ 2004-06-24 12:18 UTC (permalink / raw)
  To: 9fans

>> The purpose is to have multiple producers and consumers on an queue
>> with exclusive access, with priority always given to consumers if there
>> are waiting ones and the queue is not empty.
> 
> 
> Why don't you use channels to implement your sleep/wakeup
> mechanism. Using a lock to do so is weird for me, but perhaps others
> are used to it.

Channels and qlocks are the same thing, aren't they?



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

* Re: [9fans] qunlock
  2004-06-24 12:18                 ` dvd
@ 2004-06-24 13:09                   ` Fco. J. Ballesteros
  2004-06-24 13:30                   ` Sape Mullender
  2004-06-24 13:47                   ` boyd, rounin
  2 siblings, 0 replies; 20+ messages in thread
From: Fco. J. Ballesteros @ 2004-06-24 13:09 UTC (permalink / raw)
  To: 9fans

> Channels and qlocks are the same thing, aren't they?

In the end, all might be the same. But a qlock, for me, is
a lock. I acquire it, I release it later. A channel on the other
hand is something that lets me send/recv. That can be used
to do a sleep/wakeup approach, as well as a I want this, take that
one.



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

* Re: [9fans] qunlock
  2004-06-24 12:18                 ` dvd
  2004-06-24 13:09                   ` Fco. J. Ballesteros
@ 2004-06-24 13:30                   ` Sape Mullender
  2004-06-24 13:47                   ` boyd, rounin
  2 siblings, 0 replies; 20+ messages in thread
From: Sape Mullender @ 2004-06-24 13:30 UTC (permalink / raw)
  To: 9fans

> The purpose is to have multiple producers and consumers on an queue
> with exclusive access, with priority always given to consumers if there
> are waiting ones and the queue is not empty.

I can imagine one would want to give priority to consumers over producers,
but the danger is that the performance gained by such a thing is more than
offset by the cost of the mechanism.

Putting something on a queue or taking something off is not an expensive
operation.  The number of instructions needed is typically somewhere between
a dozen and a gross.

The queue must be protected by a lock (unless one uses a non-blocking queue
implementation, but that's another story).  QLocks themselves also have queues
and they, in turn, are protected by spin locks.

If your queue is fairly light weight, I would recommend just using lock() and
unlock() to protect them.  When you try to lock when it's already locked, you'll
waste cycles by spinning (but fewer than you might think), but when you use
qlocks — and especially when you use three of them — you'll end up wasting far
more cycles.

I'd say forget about giving priority to consumers; it's not worth it.

	Sape



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

* Re: [9fans] qunlock
  2004-06-24 11:02             ` dvd
  2004-06-24 11:14               ` Fco. J. Ballesteros
@ 2004-06-24 13:42               ` boyd, rounin
  1 sibling, 0 replies; 20+ messages in thread
From: boyd, rounin @ 2004-06-24 13:42 UTC (permalink / raw)
  To: Fans of the OS Plan 9 from Bell Labs

> The implementation of qlock/qunlock allows to unlock when not locked;
> I am asking whether it is by design or by a coincidence?

i'd say design.  unlocking things that are not locked is a BUG.



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

* Re: [9fans] qunlock
  2004-06-24 12:18                 ` dvd
  2004-06-24 13:09                   ` Fco. J. Ballesteros
  2004-06-24 13:30                   ` Sape Mullender
@ 2004-06-24 13:47                   ` boyd, rounin
  2 siblings, 0 replies; 20+ messages in thread
From: boyd, rounin @ 2004-06-24 13:47 UTC (permalink / raw)
  To: Fans of the OS Plan 9 from Bell Labs

> Channels and qlocks are the same thing, aren't they?

no



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

end of thread, other threads:[~2004-06-24 13:47 UTC | newest]

Thread overview: 20+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-06-22  6:04 [9fans] qunlock dvd
2004-06-22  8:06 ` C H Forsyth
2004-06-22  9:09   ` C H Forsyth
2004-06-22 14:19     ` ron minnich
2004-06-22 14:38       ` boyd, rounin
2004-06-22 15:13         ` Rob Pike
2004-06-23 20:11     ` dvd
2004-06-23 20:24       ` Rob Pike
2004-06-23 21:00         ` dvd
2004-06-23 21:12           ` boyd, rounin
2004-06-24  7:19           ` Fco. J. Ballesteros
2004-06-24 10:58             ` dvd
2004-06-24  7:34           ` Fco. J. Ballesteros
2004-06-24 11:02             ` dvd
2004-06-24 11:14               ` Fco. J. Ballesteros
2004-06-24 12:18                 ` dvd
2004-06-24 13:09                   ` Fco. J. Ballesteros
2004-06-24 13:30                   ` Sape Mullender
2004-06-24 13:47                   ` boyd, rounin
2004-06-24 13:42               ` boyd, rounin

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).