qemu/include/qemu/co-shared-resource.h
<<
>>
Prefs
   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 */
  30typedef struct SharedResource SharedResource;
  31
  32/*
  33 * Create SharedResource structure
  34 *
  35 * @total: total amount of some resource to be shared between clients
  36 */
  37SharedResource *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 */
  45void shres_destroy(SharedResource *s);
  46
  47/*
  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.
  51 */
  52bool co_try_get_from_shres(SharedResource *s, uint64_t n);
  53
  54/*
  55 * Allocate an amount of @n, and, if necessary, yield until
  56 * that becomes possible.
  57 */
  58void coroutine_fn co_get_from_shres(SharedResource *s, uint64_t n);
  59
  60/*
  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).
  65 */
  66void coroutine_fn co_put_to_shres(SharedResource *s, uint64_t n);
  67
  68
  69#endif /* QEMU_CO_SHARED_RESOURCE_H */
  70