1 /* 2 * Helper functionality for distributing a fixed total amount of 3 * an abstract resource among multiple coroutines. 4 * 5 * Copyright (c) 2019 Virtuozzo International GmbH 6 * 7 * Permission is hereby granted, free of charge, to any person obtaining a copy 8 * of this software and associated documentation files (the "Software"), to deal 9 * in the Software without restriction, including without limitation the rights 10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 * copies of the Software, and to permit persons to whom the Software is 12 * furnished to do so, subject to the following conditions: 13 * 14 * The above copyright notice and this permission notice shall be included in 15 * all copies or substantial portions of the Software. 16 * 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 * THE SOFTWARE. 24 */ 25 26 #ifndef QEMU_CO_SHARED_RESOURCE_H 27 #define QEMU_CO_SHARED_RESOURCE_H 28 29 /* Accesses to co-shared-resource API are thread-safe */ 30 typedef struct SharedResource SharedResource; 31 32 /* 33 * Create SharedResource structure 34 * 35 * @total: total amount of some resource to be shared between clients 36 */ 37 SharedResource *shres_create(uint64_t total); 38 39 /* 40 * Release SharedResource structure 41 * 42 * This function may only be called once everything allocated by all 43 * clients has been deallocated. 44 */ 45 void shres_destroy(SharedResource *s); 46 47 /* 48 * Allocate an amount of @n, and, if necessary, yield until 49 * that becomes possible. 50 */ 51 void coroutine_fn co_get_from_shres(SharedResource *s, uint64_t n); 52 53 /* 54 * Deallocate an amount of @n. The total amount allocated by a caller 55 * does not need to be deallocated/released with a single call, but may 56 * be split over several calls. For example, get(4), get(3), and then 57 * put(5), put(2). 58 */ 59 void coroutine_fn co_put_to_shres(SharedResource *s, uint64_t n); 60 61 62 #endif /* QEMU_CO_SHARED_RESOURCE_H */ 63