Program Listing for File cmb_resourceguard.h

Return to documentation for file (include/cmb_resourceguard.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_RESOURCEGUARD_H
#define CIMBA_CMB_RESOURCEGUARD_H

#include <stdbool.h>
#include <stdint.h>

#include "cmb_process.h"

#include "cmi_hashheap.h"
#include "cmi_slist.h"

struct cmb_resourceguard {
    struct cmi_hashheap priority_queue;
    struct cmi_resourcebase *guarded_resource;
    struct cmi_slist_head observers;
};

typedef bool (cmb_resourceguard_demand_func)(const struct cmi_resourcebase *rbp,
                                             const struct cmb_process *pp,
                                             const void *ctx);

extern void cmb_resourceguard_initialize(struct cmb_resourceguard *rgp,
                                         struct cmi_resourcebase *rbp);

extern void cmb_resourceguard_terminate(struct cmb_resourceguard *rgp);

extern int64_t cmb_resourceguard_wait(struct cmb_resourceguard *rgp,
                                      cmb_resourceguard_demand_func *demand,
                                      const void *ctx);

extern bool cmb_resourceguard_signal(struct cmb_resourceguard *rgp);

extern bool cmb_resourceguard_cancel(struct cmb_resourceguard *rgp,
                                     struct cmb_process *pp);

extern bool cmb_resourceguard_remove(struct cmb_resourceguard *rgp,
                                     const struct cmb_process *pp);

extern void cmb_resourceguard_register(struct cmb_resourceguard *rgp,
                                       struct cmb_resourceguard *obs);

extern bool cmb_resourceguard_unregister(struct cmb_resourceguard *rgp,
                                         const struct cmb_resourceguard *obs);

#endif /* CIMBA_CMB_RESOURCEGUARD_H */