Re: Locking and synchronization primitives
Re: Locking and synchronization primitives
- Subject: Re: Locking and synchronization primitives
- From: Wally Crooze <email@hidden>
- Date: Mon, 12 May 2003 18:31:28 +1000
Quinn wrote:
At 17:29 +1000 12/5/03, Wally Crooze wrote:
Note. I am writing a filesystem... so I can't avoid using BSD headers.
If you're working within the BSD portions of the kernel, your code
should rely on the kernel funnels as its primary synchronisation
mechanism. Kernel funnels are described in DTS Technote 2028
"Threading Architectures".
<http://developer.apple.com/technotes/tn/tn2028.html#MacOSXKernelThreading>
Any code running under a funnel is automatically single threaded.
Thus, there's no need for locking for simple operations, like global
+= 1. However, locking does come into play if your code blocks. When
you block you automatically release the funnel you're holding, and
other code might run and modify state that you care about. You have
to make sure that any state you depend on is preserved across blocking.
Given that you're working on a file system, the primary locking
mechanism you should use is the vnode locking mechanism
(vn_lock--which calls VOP_LOCK--and VOP_UNLOCK). If you need to lock
something other than vnodes, you would typically use the BSD locks
provided by <sys/lock.h>.
Remember that vnode locks must be taken in the correct order (from
root to leaf) in order to avoid deadlocks.
This system will be multithreaded as it is required to do work in the
background and keep synchronized with each other as well as normal vfs /
vnode requests. So I will have to look more into sys/lock.h and read the
TechNote.
Thanks
--
Wally Crooze
+-------------------------------------------------------+
| ,-_|\ Wally Crooze <email@hidden> |
| / \ |
| \_,-._/ |
| v |
+-------------------------------------------------------+
_______________________________________________
darwin-kernel mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/darwin-kernel
Do not post admin requests to the list. They will be ignored.