9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
* [9fans] rwlocks on plan9
@ 2011-09-22 11:22 Steve Simon
  2011-09-22 11:42 ` Sape Mullender
  2011-09-22 11:46 ` Charles Forsyth
  0 siblings, 2 replies; 4+ messages in thread
From: Steve Simon @ 2011-09-22 11:22 UTC (permalink / raw)
  To: 9fans

Its quite possible that I'am doing somthing foolish,
but just in case its in the design...

It appears to me that when using rwlocks on plan9
I have to release the read lock before I can take the write lock
in a process - i.e. a single process cannot hold both.

This means that when updating a data structure I do this:

	rlock()
	search_structure()
	if(found){
		runlock()
		return
	}
	runlock()

	wlock()
	search_structure()	# search a seccond time
	if(found){			# lost the race
		wunlock()
		return
	}
	add_entry()			# won the race
	wlock()
	return

If I could hold both locks I wouldn't need to do the seccond
search.

is this how its susposed to work?

-Steve




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

* Re: [9fans] rwlocks on plan9
  2011-09-22 11:22 [9fans] rwlocks on plan9 Steve Simon
@ 2011-09-22 11:42 ` Sape Mullender
  2011-09-22 11:46 ` Charles Forsyth
  1 sibling, 0 replies; 4+ messages in thread
From: Sape Mullender @ 2011-09-22 11:42 UTC (permalink / raw)
  To: 9fans

It's how read and write locks are supposed to work, but your
example isn't necessarily how they should be used.

The idea is that a piece of data can be kept consistent by *either*
• allowing any number of simultaneous readers *or*
• a single writer.
You can't have simultaneous readers and writers because the
readers might not read the data consistently if they're allowed
to read while writing is in progress.

The locks reflect the notion that we'd like to achieve maximum
concurrency.

Now, it wouldn't hurt, one would think, if one process takes a
read lock and then adds a write lock.  The locking system could
recognize it's one and the same process and allow it.  When
adding the write lock (or converting to the write lock), the
process doing it would have to wait until all other readers have
released their locks.

The alternative is also possible: after makinga  change to the object,
a process could convert its write lock to a read lock and "know" that
it's now reading the data produced moments ago.

The problem is that this (and many other activities involving
more than one lock) can lead to deadlock.

In your example, the recommended approach is that, if you're thinking
of modifying an object, you use a write lock, even if the change
doesn't have to occur because some pattern wasn't found.

It's okay to read a file with a write lock.  Think of it as
an exclusive lock rather than a write lock.

	Sape

> Its quite possible that I'am doing somthing foolish,
> but just in case its in the design...
>
> It appears to me that when using rwlocks on plan9
> I have to release the read lock before I can take the write lock
> in a process - i.e. a single process cannot hold both.
>
> This means that when updating a data structure I do this:
>
> 	rlock()
> 	search_structure()
> 	if(found){
> 		runlock()
> 		return
> 	}
> 	runlock()
>
> 	wlock()
> 	search_structure()	# search a seccond time
> 	if(found){			# lost the race
> 		wunlock()
> 		return
> 	}
> 	add_entry()			# won the race
> 	wlock()
> 	return
>
> If I could hold both locks I wouldn't need to do the seccond
> search.
>
> is this how its susposed to work?
>
> -Steve




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

* Re: [9fans] rwlocks on plan9
  2011-09-22 11:22 [9fans] rwlocks on plan9 Steve Simon
  2011-09-22 11:42 ` Sape Mullender
@ 2011-09-22 11:46 ` Charles Forsyth
  1 sibling, 0 replies; 4+ messages in thread
From: Charles Forsyth @ 2011-09-22 11:46 UTC (permalink / raw)
  To: 9fans

>is this how its susposed to work?

yes. the queued locks are associated with data, not with a process:
one process can acquire the lock and another release it.
that probably isn't used often at all, but it's the best way to think of it.
spin locks have a tighter link with a process because (after all)
they're intended for short sections of code without rescheduling.



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

* Re: [9fans] rwlocks on plan9
@ 2011-09-22 19:33 Steve Simon
  0 siblings, 0 replies; 4+ messages in thread
From: Steve Simon @ 2011-09-22 19:33 UTC (permalink / raw)
  To: 9fans

Thanks to all who replied, I understand rwlocks
a little better now.

-Steve



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

end of thread, other threads:[~2011-09-22 19:33 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-09-22 11:22 [9fans] rwlocks on plan9 Steve Simon
2011-09-22 11:42 ` Sape Mullender
2011-09-22 11:46 ` Charles Forsyth
2011-09-22 19:33 Steve Simon

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