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

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


  • To: minios-devel@xxxxxxxxxxxxx
  • From: Costin Lupu <costin.lupu@xxxxxxxxx>
  • Date: Mon, 9 Sep 2019 12:07:54 +0300
  • Cc: sharan.santhanam@xxxxxxxxx
  • Delivery-date: Mon, 09 Sep 2019 09:08:21 +0000
  • Ironport-phdr: 9a23:Pf3gDhP/W3bHQru+z8Il6mtUPXoX/o7sNwtQ0KIMzox0Iv7/rarrMEGX3/hxlliBBdydt6sezbOG7eu4CSQp2tWoiDg6aptCVhsI2409vjcLJ4q7M3D9N+PgdCcgHc5PBxdP9nC/NlVJSo6lPwWB6nK94iQPFRrhKAF7Ovr6GpLIj8Swyuu+54Dfbx9HiTagf79+Ngi6oAbQu8UZnYdvKbs6xwfUrHdPZ+lY335jK0iJnxb76Mew/Zpj/DpVtvk86cNOUrj0crohQ7BAAzsoL2465MvwtRneVgSP/WcTUn8XkhVTHQfI6gzxU4rrvSv7sup93zSaPdHzQLspVzmu87tnRRn1gyoBKjU38nzYitZogaxbvhyvuhJxzY3Tbo6XOvpzZb/Rcc8ASGZdRMtdSzBND4WhZIUPFeoBOuNYopHjqlsJthu+GQisBOXywTFOm3/2xbA62PkmHA7a2wwgBM4OsHXSrNnvMKcSTPi1zLTTwDrfdPNawy/96JXTfRw7u/GMWqt9fMzMwkcsDwPIlkicpIP4Mz+P1ekAs3KX4/R+We+tkWIqpRl9riWgy8sxkIXEhYIYxkra+Sh3zos5P8C0RUFlbdOiDZBerTuVN5FsTcMnW2xovSE6xaAYtpOjZygKzYgnxwbYa/yab4iE+hLjW/iVITd/nH9lZre/iAyz8Uik0OHzStK03ExSripYidbArGoN1xvL5siGTPty4Fuh1C6S2w3c9+1IO0M5mKrBJ5I/3LI9lIAfvErbEi/zgkr2jauWdks++uiv7uTqeqnpppiHN49oiwH+NL4imsiiAeQgLwgDRHSU+f+m2L374E32W69GjucxkqXBqpDVOdwbprKlAw9Syosj7he/DzGn0NQfhnkLNU9KdwyZj4f3P1HDO/T4Dfakg1Swizdn3f/HMaPnApnXKXjDirjhd65n60FA0Aoz0cxf55VMB7EaIPL8QFXxu8beDhMjKAy72eDnCMl91owEX2KAGKqZPbjUsVCW+u0vJ/ODa5QPtDnjNvh2r8Lp2Ho4n14aZuyl0IUabFi8H+96OAOJbHyqhc0OQkkQuQ9rZ+vxlFyEGRpOf2v6C6k7/S06DsSiEJ/ebou2xqSc1mGhGcsFNSh9FlmQHCKwJM2/UPAWZXfKLw==
  • Ironport-sdr: kGS0oZZ4BTORA8HOtZ8rQr5XEcTfWoKQtl3Ed+z7bM1l2Zf9CSTSb0PfiOZ8QebhGzrHlU6GDR NwFAWLHXDxDA==
  • 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..986fa6b2 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, 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®.