1f16ba00dSVladimir Sementsov-Ogievskiy /*
2f16ba00dSVladimir Sementsov-Ogievskiy  * Helper functionality for distributing a fixed total amount of
3f16ba00dSVladimir Sementsov-Ogievskiy  * an abstract resource among multiple coroutines.
4f16ba00dSVladimir Sementsov-Ogievskiy  *
5f16ba00dSVladimir Sementsov-Ogievskiy  * Copyright (c) 2019 Virtuozzo International GmbH
6f16ba00dSVladimir Sementsov-Ogievskiy  *
7f16ba00dSVladimir Sementsov-Ogievskiy  * Permission is hereby granted, free of charge, to any person obtaining a copy
8f16ba00dSVladimir Sementsov-Ogievskiy  * of this software and associated documentation files (the "Software"), to deal
9f16ba00dSVladimir Sementsov-Ogievskiy  * in the Software without restriction, including without limitation the rights
10f16ba00dSVladimir Sementsov-Ogievskiy  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11f16ba00dSVladimir Sementsov-Ogievskiy  * copies of the Software, and to permit persons to whom the Software is
12f16ba00dSVladimir Sementsov-Ogievskiy  * furnished to do so, subject to the following conditions:
13f16ba00dSVladimir Sementsov-Ogievskiy  *
14f16ba00dSVladimir Sementsov-Ogievskiy  * The above copyright notice and this permission notice shall be included in
15f16ba00dSVladimir Sementsov-Ogievskiy  * all copies or substantial portions of the Software.
16f16ba00dSVladimir Sementsov-Ogievskiy  *
17f16ba00dSVladimir Sementsov-Ogievskiy  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18f16ba00dSVladimir Sementsov-Ogievskiy  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19f16ba00dSVladimir Sementsov-Ogievskiy  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20f16ba00dSVladimir Sementsov-Ogievskiy  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21f16ba00dSVladimir Sementsov-Ogievskiy  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22f16ba00dSVladimir Sementsov-Ogievskiy  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23f16ba00dSVladimir Sementsov-Ogievskiy  * THE SOFTWARE.
24f16ba00dSVladimir Sementsov-Ogievskiy  */
25f16ba00dSVladimir Sementsov-Ogievskiy 
26f16ba00dSVladimir Sementsov-Ogievskiy #include "qemu/osdep.h"
27f16ba00dSVladimir Sementsov-Ogievskiy #include "qemu/coroutine.h"
28f16ba00dSVladimir Sementsov-Ogievskiy #include "qemu/co-shared-resource.h"
29f16ba00dSVladimir Sementsov-Ogievskiy 
30f16ba00dSVladimir Sementsov-Ogievskiy struct SharedResource {
31*55fa54a7SEmanuele Giuseppe Esposito     uint64_t total; /* Set in shres_create() and not changed anymore */
32f16ba00dSVladimir Sementsov-Ogievskiy 
33*55fa54a7SEmanuele Giuseppe Esposito     /* State fields protected by lock */
34*55fa54a7SEmanuele Giuseppe Esposito     uint64_t available;
35f16ba00dSVladimir Sementsov-Ogievskiy     CoQueue queue;
36*55fa54a7SEmanuele Giuseppe Esposito 
37*55fa54a7SEmanuele Giuseppe Esposito     QemuMutex lock;
38f16ba00dSVladimir Sementsov-Ogievskiy };
39f16ba00dSVladimir Sementsov-Ogievskiy 
shres_create(uint64_t total)40f16ba00dSVladimir Sementsov-Ogievskiy SharedResource *shres_create(uint64_t total)
41f16ba00dSVladimir Sementsov-Ogievskiy {
42f16ba00dSVladimir Sementsov-Ogievskiy     SharedResource *s = g_new0(SharedResource, 1);
43f16ba00dSVladimir Sementsov-Ogievskiy 
44f16ba00dSVladimir Sementsov-Ogievskiy     s->total = s->available = total;
45f16ba00dSVladimir Sementsov-Ogievskiy     qemu_co_queue_init(&s->queue);
46*55fa54a7SEmanuele Giuseppe Esposito     qemu_mutex_init(&s->lock);
47f16ba00dSVladimir Sementsov-Ogievskiy 
48f16ba00dSVladimir Sementsov-Ogievskiy     return s;
49f16ba00dSVladimir Sementsov-Ogievskiy }
50f16ba00dSVladimir Sementsov-Ogievskiy 
shres_destroy(SharedResource * s)51f16ba00dSVladimir Sementsov-Ogievskiy void shres_destroy(SharedResource *s)
52f16ba00dSVladimir Sementsov-Ogievskiy {
53f16ba00dSVladimir Sementsov-Ogievskiy     assert(s->available == s->total);
54*55fa54a7SEmanuele Giuseppe Esposito     qemu_mutex_destroy(&s->lock);
55f16ba00dSVladimir Sementsov-Ogievskiy     g_free(s);
56f16ba00dSVladimir Sementsov-Ogievskiy }
57f16ba00dSVladimir Sementsov-Ogievskiy 
58*55fa54a7SEmanuele Giuseppe Esposito /* Called with lock held. */
co_try_get_from_shres_locked(SharedResource * s,uint64_t n)59*55fa54a7SEmanuele Giuseppe Esposito static bool co_try_get_from_shres_locked(SharedResource *s, uint64_t n)
60f16ba00dSVladimir Sementsov-Ogievskiy {
61f16ba00dSVladimir Sementsov-Ogievskiy     if (s->available >= n) {
62f16ba00dSVladimir Sementsov-Ogievskiy         s->available -= n;
63f16ba00dSVladimir Sementsov-Ogievskiy         return true;
64f16ba00dSVladimir Sementsov-Ogievskiy     }
65f16ba00dSVladimir Sementsov-Ogievskiy 
66f16ba00dSVladimir Sementsov-Ogievskiy     return false;
67f16ba00dSVladimir Sementsov-Ogievskiy }
68f16ba00dSVladimir Sementsov-Ogievskiy 
co_try_get_from_shres(SharedResource * s,uint64_t n)69*55fa54a7SEmanuele Giuseppe Esposito bool co_try_get_from_shres(SharedResource *s, uint64_t n)
70*55fa54a7SEmanuele Giuseppe Esposito {
71*55fa54a7SEmanuele Giuseppe Esposito     QEMU_LOCK_GUARD(&s->lock);
72*55fa54a7SEmanuele Giuseppe Esposito     return co_try_get_from_shres_locked(s, n);
73*55fa54a7SEmanuele Giuseppe Esposito }
74*55fa54a7SEmanuele Giuseppe Esposito 
co_get_from_shres(SharedResource * s,uint64_t n)75f16ba00dSVladimir Sementsov-Ogievskiy void coroutine_fn co_get_from_shres(SharedResource *s, uint64_t n)
76f16ba00dSVladimir Sementsov-Ogievskiy {
77f16ba00dSVladimir Sementsov-Ogievskiy     assert(n <= s->total);
78*55fa54a7SEmanuele Giuseppe Esposito     QEMU_LOCK_GUARD(&s->lock);
79*55fa54a7SEmanuele Giuseppe Esposito     while (!co_try_get_from_shres_locked(s, n)) {
80*55fa54a7SEmanuele Giuseppe Esposito         qemu_co_queue_wait(&s->queue, &s->lock);
81f16ba00dSVladimir Sementsov-Ogievskiy     }
82f16ba00dSVladimir Sementsov-Ogievskiy }
83f16ba00dSVladimir Sementsov-Ogievskiy 
co_put_to_shres(SharedResource * s,uint64_t n)84f16ba00dSVladimir Sementsov-Ogievskiy void coroutine_fn co_put_to_shres(SharedResource *s, uint64_t n)
85f16ba00dSVladimir Sementsov-Ogievskiy {
86*55fa54a7SEmanuele Giuseppe Esposito     QEMU_LOCK_GUARD(&s->lock);
87f16ba00dSVladimir Sementsov-Ogievskiy     assert(s->total - s->available >= n);
88f16ba00dSVladimir Sementsov-Ogievskiy     s->available += n;
89f16ba00dSVladimir Sementsov-Ogievskiy     qemu_co_queue_restart_all(&s->queue);
90f16ba00dSVladimir Sementsov-Ogievskiy }
91