Skip to contentSkip to conversation

coroutine: Add CoRwlock support

PATCH In review Aneesh Kumar K.V 2 participants 4 messages Jul 27, 2011 – Jul 29, 2011

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 coroutine: Add CoRwlock support

---
 qemu-coroutine-lock.c |   44 ++++++++++++++++++++++++++++++++++++++++++++
 qemu-coroutine.h      |   12 ++++++++++++
 2 files changed, 56 insertions(+), 0 deletions(-)
qemu-coroutine-lock.c +44 -0
index 5071fb8..b7d01d1 100644
@@ -115,3 +115,47 @@ void qemu_co_mutex_unlock(CoMutex *mutex)
 
     trace_qemu_co_mutex_unlock_return(mutex, self);
 }
+
+void qemu_co_rwlock_init(CoRwlock *lock)
+{
+    memset(lock, 0, sizeof(*lock));
+    qemu_co_queue_init(&lock->queue);
+}
+
+void qemu_co_rwlock_rdlock(CoRwlock *lock)
+{
+    while (lock->writer) {
+        qemu_co_queue_wait(&lock->queue);
+    }
+    lock->reader++;
+}
+
+void qemu_co_rwlock_unlock(CoRwlock *lock)
+{
+    assert(qemu_in_coroutine());
+    if (lock->writer) {
+        lock->writer = false;
+        while (!qemu_co_queue_empty(&lock->queue)) {
+            /*
+             * Wakeup every body. This will include some
+             * writers too.
+             */
+            qemu_co_queue_next(&lock->queue);
+        }
+    } else {
+        lock->reader--;
+        assert(lock->reader >= 0);
+        /* Wakeup only one waiting writer */
+        if (!lock->reader) {
+            qemu_co_queue_next(&lock->queue);
+        }
+    }
+}
+
+void qemu_co_rwlock_wrlock(CoRwlock *lock)
+{
+    while (lock->writer || lock->reader) {
+        qemu_co_queue_wait(&lock->queue);
+    }
+    lock->writer = true;
+}
qemu-coroutine.h +12 -0
index 71cfa5a..a9735fb 100644
@@ -17,6 +17,7 @@
 #include <stdbool.h>
 
 #include "qemu-queue.h"
+#include "qemu-thread.h"
 
 /**
  * Coroutines are a mechanism for stack switching and can be used for
@@ -114,4 +115,15 @@ void qemu_co_mutex_init(CoMutex *mutex);
 void qemu_co_mutex_lock(CoMutex *mutex);
 void qemu_co_mutex_unlock(CoMutex *mutex);
 
+typedef struct CoRwlock {
+    bool writer;
+    int reader;
+    CoQueue queue;
+} CoRwlock;
+
+void qemu_co_rwlock_init(CoRwlock *lock);
+void qemu_co_rwlock_rdlock(CoRwlock *lock);
+void qemu_co_rwlock_wrlock(CoRwlock *lock);
+void qemu_co_rwlock_unlock(CoRwlock *lock);
+
 #endif /* QEMU_COROUTINE_H */
Signature · 1 line
1.7.4.1
#1 lore raw

Am 27.07.2011 18:21, schrieb Aneesh Kumar K.V:

Quoted 5 lines
---
 qemu-coroutine-lock.c |   44 ++++++++++++++++++++++++++++++++++++++++++++
 qemu-coroutine.h      |   12 ++++++++++++
 2 files changed, 56 insertions(+), 0 deletions(-)

Looks good to me.

What's your plan with getting this merged? Are you going to just include it with another series that you'll send once the basic coroutine support is in?

Kevin

#2 lore raw
Quoted 7 lines · Kevin Wolf

Am 27.07.2011 18:21, schrieb Aneesh Kumar K.V:

Quoted 5 lines
---
 qemu-coroutine-lock.c |   44 ++++++++++++++++++++++++++++++++++++++++++++
 qemu-coroutine.h      |   12 ++++++++++++
 2 files changed, 56 insertions(+), 0 deletions(-)
Quoted 5 lines

Looks good to me.

What's your plan with getting this merged? Are you going to just include it with another series that you'll send once the basic coroutine support is in?

Since the patch already is dependent on CoQueue, it would be nice if you can push it through your tree. I will stage the dependent VirtFS patch after that get merged upstream.

-aneesh

#3 lore raw

Am 29.07.2011 16:24, schrieb Aneesh Kumar K.V:

Quoted 10 lines · Kevin Wolf
Quoted 9 lines

Am 27.07.2011 18:21, schrieb Aneesh Kumar K.V:

Quoted 5 lines
---
 qemu-coroutine-lock.c |   44 ++++++++++++++++++++++++++++++++++++++++++++
 qemu-coroutine.h      |   12 ++++++++++++
 2 files changed, 56 insertions(+), 0 deletions(-)

Looks good to me.

Quoted 7 lines
Quoted 3 lines

What's your plan with getting this merged? Are you going to just include it with another series that you'll send once the basic coroutine support is in?

Since the patch already is dependent on CoQueue, it would be nice if you can push it through your tree. I will stage the dependent VirtFS patch after that get merged upstream.

Okay, I applied it to my branch so that I won't forget about it, but while merging I noticed two things that you could fix: The patch adds an #include "qemu-thread.h" to qemu-coroutine.h for apparently no reason, and it would be nice to have comments in qemu-coroutine.h like all other functions have.

Kevin