public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* readers-writers mutex
@ 2006-04-05 22:21 Joshua Hudson
  2006-04-06  1:06 ` Arjan van de Ven
  2006-04-06  1:15 ` Jeff V. Merkey
  0 siblings, 2 replies; 4+ messages in thread
From: Joshua Hudson @ 2006-04-05 22:21 UTC (permalink / raw)
  To: linux-kernel

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

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

end of thread, other threads:[~2006-04-06  1:59 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2006-04-05 22:21 readers-writers mutex Joshua Hudson
2006-04-06  1:06 ` Arjan van de Ven
2006-04-06  1:59   ` Joshua Hudson
2006-04-06  1:15 ` Jeff V. Merkey

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox