Program Listing for File cmb_resourcepool.h

Return to documentation for file (include/cmb_resourcepool.h)

/*
 * Copyright (c) Asbjørn M. Bonvik 2025-26.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef CIMBA_CMB_RESOURCEPOOL_H
#define CIMBA_CMB_RESOURCEPOOL_H

#include <stdint.h>

#include "cmb_resourceguard.h"
#include "cmb_timeseries.h"

#include "cmi_holdable.h"

struct cmb_resourcepool {
    struct cmi_holdable core;
    struct cmb_resourceguard guard;
    struct cmi_hashheap holders;
    uint64_t capacity;
    uint64_t in_use;
    bool is_recording;
    struct cmb_timeseries history;
};

extern struct cmb_resourcepool *cmb_resourcepool_create(void);

extern void cmb_resourcepool_initialize(struct cmb_resourcepool *rpp,
                                         const char *name,
                                         uint64_t capacity);

extern void cmb_resourcepool_terminate(struct cmb_resourcepool *rpp);

extern void cmb_resourcepool_destroy(struct cmb_resourcepool *rpp);

extern uint64_t cmb_resourcepool_held_by_process(struct cmb_resourcepool *rpp,
                                                 const struct cmb_process *pp);

extern int64_t cmb_resourcepool_acquire(struct cmb_resourcepool *rpp,
                                        uint64_t req_amount);

extern int64_t cmb_resourcepool_preempt(struct cmb_resourcepool *rpp,
                                         uint64_t req_amount);

extern void cmb_resourcepool_release(struct cmb_resourcepool *rpp,
                                      uint64_t rel_amount);

[[maybe_unused]]
static inline const char *cmb_resourcepool_get_name(struct cmb_resourcepool *rsp)
{
    cmb_assert_debug(rsp != NULL);

    const struct cmi_resourcebase *rbp = (struct cmi_resourcebase *)rsp;
    cmb_assert_release(rbp->cookie == CMI_INITIALIZED);

    return rbp->name;
}

[[maybe_unused]]
static inline uint64_t cmb_resourcepool_in_use(struct cmb_resourcepool *rsp)
{
    cmb_assert_release(rsp != NULL);
    cmb_assert_release(((struct cmi_resourcebase *)rsp)->cookie == CMI_INITIALIZED);
    cmb_assert_debug(rsp->in_use <= rsp->capacity);

    return rsp->in_use;
}

[[maybe_unused]]
static inline uint64_t cmb_resourcepool_available(struct cmb_resourcepool *rsp)
{
    cmb_assert_release(rsp != NULL);
    cmb_assert_release(((struct cmi_resourcebase *)rsp)->cookie == CMI_INITIALIZED);
    cmb_assert_debug(rsp->in_use <= rsp->capacity);

    return (rsp->capacity - rsp->in_use);
}

extern void cmb_resourcepool_start_recording(struct cmb_resourcepool *rsp);

extern void cmb_resourcepool_stop_recording(struct cmb_resourcepool *rsp);

extern struct cmb_timeseries *cmb_resourcepool_get_history(struct cmb_resourcepool *rsp);

extern void cmb_resourcepool_print_report(struct cmb_resourcepool *rsp, FILE *fp);


#endif /* CIMBA_CMB_RESOURCEPOOL_H */