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
  30typedef struct SharedResource SharedResource;
  31
  32/*
  33 * Create SharedResource structure
  34 *
  35 * @total: total amount of some resource to be shared between clients
  36 *
  37 * Note: this API is not thread-safe.
  38 */
  39SharedResource *shres_create(uint64_t total);
  40
  41/*
  42 * Release SharedResource structure
  43 *
  44 * This function may only be called once everything allocated by all
  45 * clients has been deallocated.
  46 */
  47void shres_destroy(SharedResource *s);
  48
  49/*
  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.
  53 */
  54bool co_try_get_from_shres(SharedResource *s, uint64_t n);
  55
  56/*
  57 * Allocate an amount of @n, and, if necessary, yield until
  58 * that becomes possible.
  59 */
  60void coroutine_fn co_get_from_shres(SharedResource *s, uint64_t n);
  61
  62/*
  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).
  67 */
  68void coroutine_fn co_put_to_shres(SharedResource *s, uint64_t n);
  69
  70
  71#endif /* QEMU_CO_SHARED_RESOURCE_H */
  72