tests/docker: remove travis container
[qemu/ar7.git] / include / qemu / co-shared-resource.h
blob4e4503004ca350ff9a9ba53df2918daf67991512
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
30 typedef struct SharedResource SharedResource;
33 * Create SharedResource structure
35 * @total: total amount of some resource to be shared between clients
37 * Note: this API is not thread-safe.
39 SharedResource *shres_create(uint64_t total);
42 * Release SharedResource structure
44 * This function may only be called once everything allocated by all
45 * clients has been deallocated.
47 void shres_destroy(SharedResource *s);
50 * Try to allocate an amount of @n. Return true on success, and false
51 * if there is too little left of the collective resource to fulfill
52 * the request.
54 bool co_try_get_from_shres(SharedResource *s, uint64_t n);
57 * Allocate an amount of @n, and, if necessary, yield until
58 * that becomes possible.
60 void coroutine_fn co_get_from_shres(SharedResource *s, uint64_t n);
63 * Deallocate an amount of @n. The total amount allocated by a caller
64 * does not need to be deallocated/released with a single call, but may
65 * be split over several calls. For example, get(4), get(3), and then
66 * put(5), put(2).
68 void coroutine_fn co_put_to_shres(SharedResource *s, uint64_t n);
71 #endif /* QEMU_CO_SHARED_RESOURCE_H */