Program Listing for File cmb_resource.h

Return to documentation for file (include/cmb_resource.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_RESOURCE_H
#define CIMBA_CMB_RESOURCE_H

#include <stdint.h>

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

#include "cmi_holdable.h"

struct cmb_resource {
    struct cmi_holdable core;
    struct cmb_resourceguard guard;
    struct cmb_process *holder;
    bool is_recording;
    struct cmb_timeseries history;
};

extern struct cmb_resource *cmb_resource_create(void);

extern void cmb_resource_initialize(struct cmb_resource *rp,
                                    const char *name);

extern void cmb_resource_terminate(struct cmb_resource *rp);

extern void cmb_resource_destroy(struct cmb_resource *rp);

extern int64_t cmb_resource_acquire(struct cmb_resource *rp);

extern void cmb_resource_release(struct cmb_resource *rp);


extern int64_t cmb_resource_preempt(struct cmb_resource *rp);

[[maybe_unused]]
static inline const char *cmb_resource_name(const struct cmb_resource *rp)
{
    cmb_assert_debug(rp != NULL);

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

    return rbp->name;
}

[[maybe_unused]]
static inline uint64_t cmb_resource_in_use(const struct cmb_resource *rp)
{
    cmb_assert_debug(rp != NULL);
    cmb_assert_release(((struct cmi_resourcebase *)rp)->cookie == CMI_INITIALIZED);

    return (rp->holder != NULL) ? 1u : 0u;
}

[[maybe_unused]]
static inline uint64_t cmb_resource_available(const struct cmb_resource *rp)
{
    cmb_assert_debug(rp != NULL);
    cmb_assert_release(((struct cmi_resourcebase *)rp)->cookie == CMI_INITIALIZED);

    return (rp->holder == NULL) ? 1u : 0u;
}

[[maybe_unused]]
static inline uint64_t cmb_resource_held_by_process(const struct cmb_resource *rp,
                                                    const struct cmb_process *pp) {
    cmb_assert_debug(rp != NULL);
    cmb_assert_release(((struct cmi_resourcebase *)rp)->cookie == CMI_INITIALIZED);

    return (rp->holder == pp) ? 1u : 0u;
}


extern void cmb_resource_start_recording(struct cmb_resource *rp);

extern void cmb_resource_stop_recording(struct cmb_resource *rp);

extern struct cmb_timeseries *cmb_resource_history(struct cmb_resource *rp);

extern void cmb_resource_print_report(struct cmb_resource *rp, FILE *fp);


#endif /* CIMBA_CMB_RESOURCE_H */