开发者

Correct lock to use in linux character driver

开发者 https://www.devze.com 2023-01-08 01:57 出处:网络
I am writing a simple character device driver. (kernel 2.6.26) Multiple concurrent reader & writers are expected.

I am writing a simple character device driver. (kernel 2.6.26) Multiple concurrent reader & writers are expected.

I am开发者_如何学C not sure what type of lock is best used to synchronize a short access to internal structures.

Any advice will be most appreciated


Compare with http://www.kernel.org/pub/linux/kernel/people/rusty/kernel-locking/c214.html . An old document from before when mutexes existed, but given mutexes are a sleeping lock, they count towards user context.

spinlock — spinlock_bh — mutex — semaphore

If your data structures are only ever accessed by functions whose execution is triggered by userspace, all lock primitives are available to you. It depends on gut feeling of how short a "short access" is.

And then there is RCU as a fifth way of doing things, though it is somewhat not a locking primitive in its own right. (It is used together with one of the lock primitives.)


Start with a mutex. Once you've got it working you can think about reworking the locking.

0

精彩评论

暂无评论...
验证码 换一张
取 消