[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Minios-devel] [UNIKRAFT PATCH v3 4/4] lib/uklock: Make mutex recursive


  • To: minios-devel@xxxxxxxxxxxxx
  • From: Costin Lupu <costin.lupu@xxxxxxxxx>
  • Date: Mon, 9 Sep 2019 12:58:02 +0300
  • Cc: sharan.santhanam@xxxxxxxxx
  • Delivery-date: Mon, 09 Sep 2019 09:58:22 +0000
  • Ironport-phdr: 9a23:erpBSRZARr88U1d/42MccgX/LSx+4OfEezUN459isYplN5qZr86+bnLW6fgltlLVR4KTs6sC17OM9fmwBydZsd6oizMrSNR0TRgLiMEbzUQLIfWuLgnFFsPsdDEwB89YVVVorDmROElRH9viNRWJ+iXhpTEdFQ/iOgVrO+/7BpDdj9it1+C15pbffxhEiCCybL9vMhm6txjdu8oXjIdtN6o8yQbCr2dVdehR2W5mP0+YkQzm5se38p5j8iBQtOwk+sVdT6j0fLk2QKJBAjg+PG87+MPktR/YTQuS/XQcSXkZkgBJAwfe8h73WIr6vzbguep83CmaOtD2TawxVD+/4apnVAPkhSEaPDM/7WrZiNF/jLhDrRyhuRJx3oDaboKSOvV8cKPQZs8WSXZbU8pNTSFNHp+wYo0SBOQBJ+ZYqIz9qkMQoBu5HgmsGOLvyjlVjXHwwK06y/khGhzB0QM8GNIOq2jUrNTzNKsIVeC10bHIzSjYYvxKwjfx8pDIcg06rv2WR7JwdtPcxE8yHA3GllWdsZHpMjyI2ugXsGWW7/BsWfyxh2MlsQ18rCCjytojh4XUnI4YyVDJ+T9nzIs7K9C0UlN3bNG6HJZWqiqULZF5Qtk4TGFtoCs6z7oGtoOlcyUS05QnwgLfa+SAc4iV/hLvTOaRLil8hHJiYL+/mROy/lKhyu34TMW7zE1KojBdktnRrX8BzQbT6s+fRvt8+EeuxyqP2hjO5uxLPEw4j6nWJp47zrIuiJYes17PEyHulEXzlqCWd0Ek+uay6+TgZ7XrvoOTN4hvigHiM6QunNazAeAlMggWQmiW4viz1Kb58U3hXbVFlec6krPesJzCP8QUura5AxNJ0oYk8xu/Cjam0NIZnXkAN1JJYQ+IgJb3O17QJPD1FvO/g1W3kDd33PDKJLLgDYvLLnTZl7fhZ7l94VZGyAUv1dBf+45UCrYZLfLoWk/+rsbYAQU/MwCu2OboEtN91p8eWW2VBK+WKqbSvESO5u0xP+aMYJUaty3nJ/c7+v69xUM+zFoce6iuxt4bZW61GtxiIl6Fejz8j9FHFn0F7SQkS+m/o1qZTT9VL1KvR790sjo8E56nC8HHW5iwqLeamj+mFNtMYTYVWRi3DX70etDcCL83YyWIL5o5nw==
  • Ironport-sdr: MAXD1+Px+/SOheV9K6WNICiSIFah2bmwJolWGqQVmZ9cCVF/CXxqfZNKoCZmEHfwmiTyk1A2Qk oeLJLEsR5G9A==
  • List-id: Mini-os development list <minios-devel.lists.xenproject.org>

This patch changes the existing mutex implementation to support multiple locking
by the same thread.

Signed-off-by: Costin Lupu <costin.lupu@xxxxxxxxx>
---
 lib/uklock/include/uk/mutex.h | 36 ++++++++++++++++++++++++-----------
 lib/uklock/mutex.c            |  3 ++-
 2 files changed, 27 insertions(+), 12 deletions(-)

diff --git a/lib/uklock/include/uk/mutex.h b/lib/uklock/include/uk/mutex.h
index ff852b45..d4435ebd 100644
--- a/lib/uklock/include/uk/mutex.h
+++ b/lib/uklock/include/uk/mutex.h
@@ -54,49 +54,61 @@ extern "C" {
  * uses wait queues for threads
  */
 struct uk_mutex {
-       int locked;
+       int lock_count;
+       struct uk_thread *owner;
        struct uk_waitq wait;
 };
 
 #define        UK_MUTEX_INITIALIZER(name)                              \
-       { 0, __WAIT_QUEUE_INITIALIZER((name).wait) }
+       { 0, NULL, __WAIT_QUEUE_INITIALIZER((name).wait) }
 
 void uk_mutex_init(struct uk_mutex *m);
 
 static inline void uk_mutex_lock(struct uk_mutex *m)
 {
+       struct uk_thread *current;
        unsigned long irqf;
 
        UK_ASSERT(m);
 
+       current = uk_thread_current();
+
        for (;;) {
-               uk_waitq_wait_event(&m->wait, m->locked == 0);
+               uk_waitq_wait_event(&m->wait,
+                       m->lock_count == 0 || m->owner == current);
                irqf = ukplat_lcpu_save_irqf();
-               if (!m->locked)
+               if (m->lock_count == 0 || m->owner == current)
                        break;
                ukplat_lcpu_restore_irqf(irqf);
        }
-       m->locked = 1;
+       m->lock_count++;
+       m->owner = current;
        ukplat_lcpu_restore_irqf(irqf);
 }
 
 static inline int uk_mutex_trylock(struct uk_mutex *m)
 {
+       struct uk_thread *current;
        unsigned long irqf;
        int ret = 0;
 
        UK_ASSERT(m);
 
+       current = uk_thread_current();
+
        irqf = ukplat_lcpu_save_irqf();
-       if (!m->locked)
-               ret = m->locked = 1;
+       if (m->lock_count == 0 || m->owner == current) {
+               ret = 1;
+               m->lock_count++;
+               m->owner = current;
+       }
        ukplat_lcpu_restore_irqf(irqf);
        return ret;
 }
 
 static inline int uk_mutex_is_locked(struct uk_mutex *m)
 {
-       return m->locked;
+       return m->lock_count > 0;
 }
 
 static inline void uk_mutex_unlock(struct uk_mutex *m)
@@ -106,9 +118,11 @@ static inline void uk_mutex_unlock(struct uk_mutex *m)
        UK_ASSERT(m);
 
        irqf = ukplat_lcpu_save_irqf();
-       UK_ASSERT(m->locked);
-       m->locked = 0;
-       uk_waitq_wake_up(&m->wait);
+       UK_ASSERT(m->lock_count > 0);
+       if (--m->lock_count == 0) {
+               m->owner = NULL;
+               uk_waitq_wake_up(&m->wait);
+       }
        ukplat_lcpu_restore_irqf(irqf);
 }
 
diff --git a/lib/uklock/mutex.c b/lib/uklock/mutex.c
index 183a01db..5e5ec9a6 100644
--- a/lib/uklock/mutex.c
+++ b/lib/uklock/mutex.c
@@ -2,6 +2,7 @@
 
 void uk_mutex_init(struct uk_mutex *m)
 {
-       m->locked = 0;
+       m->lock_count = 0;
+       m->owner = NULL;
        uk_waitq_init(&m->wait);
 }
-- 
2.20.1


_______________________________________________
Minios-devel mailing list
Minios-devel@xxxxxxxxxxxxxxxxxxxx
https://lists.xenproject.org/mailman/listinfo/minios-devel

 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.