Introduce an API for some shared splitable resource, like memory.
It's going to be used by backup. Backup uses both read/write io and
copy_range. copy_range may consume memory implictly, so new API is
abstract: it don't allocate any real memory but only handling out
tickets.

The idea is that we have some total amount of something and callers
should wait in coroutine queue if there is not enough of the resource
at the moment.

Signed-off-by: Vladimir Sementsov-Ogievskiy <vsement...@virtuozzo.com>
---
 include/qemu/co-shared-amount.h | 66 ++++++++++++++++++++++++++++
 util/qemu-co-shared-amount.c    | 77 +++++++++++++++++++++++++++++++++
 util/Makefile.objs              |  1 +
 3 files changed, 144 insertions(+)
 create mode 100644 include/qemu/co-shared-amount.h
 create mode 100644 util/qemu-co-shared-amount.c

diff --git a/include/qemu/co-shared-amount.h b/include/qemu/co-shared-amount.h
new file mode 100644
index 0000000000..e2dbc43dfd
--- /dev/null
+++ b/include/qemu/co-shared-amount.h
@@ -0,0 +1,66 @@
+/*
+ * Generic shared amount for coroutines
+ *
+ * Copyright (c) 2019 Virtuozzo International GmbH
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to 
deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#ifndef QEMU_CO_SHARED_AMOUNT_H
+#define QEMU_CO_SHARED_AMOUNT_H
+
+
+typedef struct QemuCoSharedAmount QemuCoSharedAmount;
+
+/*
+ * Create QemuCoSharedAmount structure
+ *
+ * @total: total amount to be shared between clients
+ *
+ * Note: this API is not thread-safe as it originally designed to be used in
+ * backup block-job and called from one aio context. If multiple threads 
support
+ * is needed it should be implemented (for ex., protect QemuCoSharedAmount by
+ * mutex).
+ */
+QemuCoSharedAmount *qemu_co_shared_amount_new(uint64_t total);
+
+/*
+ * Release QemuCoSharedAmount structure
+ */
+void qemu_co_shared_amount_free(QemuCoSharedAmount *s);
+
+/*
+ * Try to get n peaces. If not enough free peaces returns false, otherwise 
true.
+ */
+bool qemu_co_try_get_amount(QemuCoSharedAmount *s, uint64_t n);
+
+/*
+ * Get n peaces. If not enough yields. Return on success.
+ */
+void coroutine_fn qemu_co_get_amount(QemuCoSharedAmount *s, uint64_t n);
+
+/*
+ * Put n peaces. Client must not put more than it gets, still it may put in
+ * split: for example, get(5) and then put(3), put(2). All peaces must be put
+ * back before qemu_co_shared_amount_free call.
+ */
+void coroutine_fn qemu_co_put_amount(QemuCoSharedAmount *s, uint64_t n);
+
+
+#endif /* QEMU_CO_SHARED_AMOUNT_H */
diff --git a/util/qemu-co-shared-amount.c b/util/qemu-co-shared-amount.c
new file mode 100644
index 0000000000..8855ce5705
--- /dev/null
+++ b/util/qemu-co-shared-amount.c
@@ -0,0 +1,77 @@
+/*
+ * Generic shared amount for coroutines
+ *
+ * Copyright (c) 2019 Virtuozzo International GmbH
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to 
deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#include "qemu/osdep.h"
+#include "qemu/coroutine.h"
+#include "qemu/co-shared-amount.h"
+
+struct QemuCoSharedAmount {
+    uint64_t total;
+    uint64_t taken;
+
+    CoQueue queue;
+};
+
+QemuCoSharedAmount *qemu_co_shared_amount_new(uint64_t total)
+{
+    QemuCoSharedAmount *s = g_new0(QemuCoSharedAmount, 1);
+
+    s->total = total;
+    qemu_co_queue_init(&s->queue);
+
+    return s;
+}
+
+void qemu_co_shared_amount_free(QemuCoSharedAmount *s)
+{
+    assert(s->taken == 0);
+    g_free(s);
+}
+
+bool qemu_co_try_get_amount(QemuCoSharedAmount *s, uint64_t n)
+{
+    if (n < s->total && s->total - n >= s->taken) {
+        s->taken += n;
+        return true;
+    }
+
+    return false;
+}
+
+void coroutine_fn qemu_co_get_amount(QemuCoSharedAmount *s, uint64_t n)
+{
+    assert(n < s->total);
+    while (s->total - n < s->taken) {
+        qemu_co_queue_wait(&s->queue, NULL);
+    }
+
+    assert(qemu_co_try_get_amount(s, n));
+}
+
+void coroutine_fn qemu_co_put_amount(QemuCoSharedAmount *s, uint64_t n)
+{
+    assert(n <= s->taken);
+    s->taken -= n;
+    qemu_co_queue_restart_all(&s->queue);
+}
diff --git a/util/Makefile.objs b/util/Makefile.objs
index 41bf59d127..65ae18993a 100644
--- a/util/Makefile.objs
+++ b/util/Makefile.objs
@@ -37,6 +37,7 @@ util-obj-y += rcu.o
 util-obj-$(CONFIG_MEMBARRIER) += sys_membarrier.o
 util-obj-y += qemu-coroutine.o qemu-coroutine-lock.o qemu-coroutine-io.o
 util-obj-y += qemu-coroutine-sleep.o
+util-obj-y += qemu-co-shared-amount.o
 util-obj-y += coroutine-$(CONFIG_COROUTINE_BACKEND).o
 util-obj-y += buffer.o
 util-obj-y += timed-average.o
-- 
2.21.0


Reply via email to