docs: interesting idea for fast rw locks
authorWim Taymans <wim.taymans@collabora.co.uk>
Thu, 19 Mar 2009 10:46:11 +0000 (11:46 +0100)
committerWim Taymans <wim.taymans@collabora.co.uk>
Thu, 19 Mar 2009 10:46:11 +0000 (11:46 +0100)
--

docs/random/wtay/rwlocks [new file with mode: 0644]

diff --git a/docs/random/wtay/rwlocks b/docs/random/wtay/rwlocks
new file mode 100644 (file)
index 0000000..07a1821
--- /dev/null
@@ -0,0 +1,33 @@
+***********************************************
+
+typedef struct {
+  int pre_count;
+  int post_count;
+  Mutex *lock;
+} RWLock;
+
+
+reader:
+
+  retry:
+    post = atomic_get (lock->post_count);
+
+    ... do read ...
+
+    if (atomic_get (lock->pre_count) != post) {
+      /* wait for writer to finish then retry */
+      lock (lock->mutex);
+      unlock (lock->mutex);
+      goto retry;
+    }
+
+
+writer:
+
+   lock (lock->mutex);
+   atomic_inc (lock->pre_count);
+
+   ... update ...
+
+   atomic_inc (lock->post_count);
+   unlock (lock->mutex);