]> git.proxmox.com Git - mirror_lxc.git/commitdiff
lxccontainer: lxc_container_{get,put}()
authorChristian Brauner <christian.brauner@ubuntu.com>
Sat, 24 Feb 2018 14:15:54 +0000 (15:15 +0100)
committerChristian Brauner <christian.brauner@ubuntu.com>
Sat, 24 Feb 2018 20:38:11 +0000 (21:38 +0100)
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
src/lxc/lxccontainer.c

index 4858d971bec6c23010e9559469bb005a9ea0a222..1f6d33a6e3527ad7b5f84c75d23fd30de5f4c4fc 100644 (file)
@@ -281,19 +281,21 @@ static void lxc_container_free(struct lxc_container *c)
        free(c);
 }
 
-/*
- * Consider the following case:
-freer                         |    racing get()er
-==================================================================
-lxc_container_put()           |   lxc_container_get()
-\ lxclock(c->privlock)        |   c->numthreads < 1? (no)
-\ c->numthreads = 0           |   \ lxclock(c->privlock) -> waits
-\ lxcunlock()                 |   \
-\ lxc_container_free()        |   \ lxclock() returns
-                              |   \ c->numthreads < 1 -> return 0
-\ \ (free stuff)              |
-\ \ sem_destroy(privlock)     |
-
+/* Consider the following case:
+ *
+ * |====================================================================|
+ * | freer                         |    racing get()er                  |
+ * |====================================================================|
+ * | lxc_container_put()           |   lxc_container_get()              |
+ * | \ lxclock(c->privlock)        |   c->numthreads < 1? (no)          |
+ * | \ c->numthreads = 0           |   \ lxclock(c->privlock) -> waits  |
+ * | \ lxcunlock()                 |   \                                |
+ * | \ lxc_container_free()        |   \ lxclock() returns              |
+ * |                               |   \ c->numthreads < 1 -> return 0  |
+ * | \ \ (free stuff)              |                                    |
+ * | \ \ sem_destroy(privlock)     |                                    |
+ * |_______________________________|____________________________________|
+ *
  * When the get()er checks numthreads the first time, one of the following
  * is true:
  * 1. freer has set numthreads = 0.  get() returns 0
@@ -326,6 +328,7 @@ int lxc_container_get(struct lxc_container *c)
 
        c->numthreads++;
        container_mem_unlock(c);
+
        return 1;
 }
 
@@ -333,14 +336,18 @@ int lxc_container_put(struct lxc_container *c)
 {
        if (!c)
                return -1;
+
        if (container_mem_lock(c))
                return -1;
+
        if (--c->numthreads < 1) {
                container_mem_unlock(c);
                lxc_container_free(c);
                return 1;
        }
+
        container_mem_unlock(c);
+
        return 0;
 }