Messages in this thread Patch in this message | | | Date | Wed, 5 Apr 2006 15:21:01 -0700 | From | "Joshua Hudson" <> | Subject | readers-writers mutex |
| |
Since we are moving from semaphores to mutex, there should be a mutex_rw. I had a try at creating one (might as well convert from sem_rw). I'll bet somebody here can do a lot better, but this will do if need be.
--- linux-2.6.16.1-stock/include/linux/mutex_rw.h 1969-12-31 16:00:00.000000000 -0800 +++ linux-2.6.16.1-nvl/include/linux/mutex_rw.h 2006-04-04 18:11:56.000000000 -0700 @@ -0,0 +1,60 @@ +/* Linux RW mutex + * This file: GNU GPL v2 or later, Joshua Hudson <joshudson@gmail.com> + * + * Somebody else can make this fast. I just made this work. + * + * DANGER! Change of this file will break module binaries if any + * rw mutex is shared between main kernel and modules or between + * modules with a different version. + */ + +#ifndef __KERNEL_MUTEX_RW +#define __KERNEL_MUTEX_RW +#ifdef __KERNEL__ + +#include <linux/mutex.h> + +struct rw_mutex { + struct mutex r_mutex; + struct mutex w_mutex; + unsigned n_readers; +}; + +static inline void rw_mutex_init(struct rw_mutex *rw) +{ + mutex_init(&rw->r_mutex); + mutex_init(&rw->w_mutex); + rw->n_readers = 0; +} + +static inline void rw_mutex_destroy(struct rw_mutex *rw) +{ + mutex_destroy(&rw->r_mutex); + mutex_destroy(&rw->w_mutex); +} + +static inline void mutex_lock_w(struct rw_mutex *rw) +{ + mutex_lock(&rw->w_mutex); +} + +static inline void mutex_unlock_w(struct rw_mutex *rw) +{ + mutex_unlock(&rw->w_mutex); +} + +static inline void mutex_lock_r(struct rw_mutex *rw) +{ + mutex_lock(&rw->r_mutex); + if (++rw->n_readers == 1) + mutex_lock(&rw->w_mutex); + mutex_unlock(&rw->r_mutex); +} + +static inline void mutex_unlock_r(struct rw_mutex *rw) +{ + mutex_lock(&rw->r_mutex); + if (--rw->n_readers == 0) + mutex_unlock(&rw->w_mutex); + mutex_unlock(&rw->r_mutex); +} + +#endif +#endif - To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/
| |