target/ppc: 405: Alignment exception cleanup
[qemu.git] / include / qemu / co-shared-resource.h
blob78ca5850f8f5cf3fdc1a901938514c99f83a3ecd
1 /*
2 * Helper functionality for distributing a fixed total amount of
3 * an abstract resource among multiple coroutines.
5 * Copyright (c) 2019 Virtuozzo International GmbH
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:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
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.
26 #ifndef QEMU_CO_SHARED_RESOURCE_H
27 #define QEMU_CO_SHARED_RESOURCE_H
29 /* Accesses to co-shared-resource API are thread-safe */
30 typedef struct SharedResource SharedResource;
33 * Create SharedResource structure
35 * @total: total amount of some resource to be shared between clients
37 SharedResource *shres_create(uint64_t total);
40 * Release SharedResource structure
42 * This function may only be called once everything allocated by all
43 * clients has been deallocated.
45 void shres_destroy(SharedResource *s);
48 * Try to allocate an amount of @n. Return true on success, and false
49 * if there is too little left of the collective resource to fulfill
50 * the request.
52 bool co_try_get_from_shres(SharedResource *s, uint64_t n);
55 * Allocate an amount of @n, and, if necessary, yield until
56 * that becomes possible.
58 void coroutine_fn co_get_from_shres(SharedResource *s, uint64_t n);
61 * Deallocate an amount of @n. The total amount allocated by a caller
62 * does not need to be deallocated/released with a single call, but may
63 * be split over several calls. For example, get(4), get(3), and then
64 * put(5), put(2).
66 void coroutine_fn co_put_to_shres(SharedResource *s, uint64_t n);
69 #endif /* QEMU_CO_SHARED_RESOURCE_H */