From: "Joshua Hudson" <joshudson@gmail.com>
To: linux-kernel@vger.kernel.org
Subject: readers-writers mutex
Date: Wed, 5 Apr 2006 15:21:01 -0700 [thread overview]
Message-ID: <bda6d13a0604051521o229de77dvb38992d6427a450c@mail.gmail.com> (raw)
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
next reply other threads:[~2006-04-05 22:21 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2006-04-05 22:21 Joshua Hudson [this message]
2006-04-06 1:06 ` Arjan van de Ven
2006-04-06 1:59 ` Joshua Hudson
2006-04-06 1:15 ` Jeff V. Merkey
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=bda6d13a0604051521o229de77dvb38992d6427a450c@mail.gmail.com \
--to=joshudson@gmail.com \
--cc=linux-kernel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®