Program Listing for File cmb_condition.h

Return to documentation for file (include/cmb_condition.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_CONDITION_H
#define CIMBA_CMB_CONDITION_H

#include "cmi_resourcebase.h"
#include "cmb_resourceguard.h"

struct cmb_condition {
    struct cmi_resourcebase base;
    struct cmb_resourceguard guard;
};

typedef bool (cmb_condition_demand_func)(const struct cmb_condition *cnd,
                                         const struct cmb_process *prc,
                                         const void *ctx);

extern struct cmb_condition *cmb_condition_create(void);

extern void cmb_condition_initialize(struct cmb_condition *cvp,
                                     const char *name);

extern void cmb_condition_terminate(struct cmb_condition *cvp);

extern void cmb_condition_destroy(struct cmb_condition *cvp);

extern int64_t cmb_condition_wait(struct cmb_condition *cvp,
                                  cmb_condition_demand_func *dmnd,
                                  const void *ctx);

extern uint64_t cmb_condition_signal(struct cmb_condition *cvp);

extern bool cmb_condition_cancel(struct cmb_condition *cvp,
                                 struct cmb_process *pp);

extern bool cmb_condition_remove(struct cmb_condition *cvp,
                                 const struct cmb_process *pp);

[[maybe_unused]]
static inline void cmb_condition_subscribe(struct cmb_condition *cvp,
                                           struct cmb_resourceguard *rgp) {
    cmb_assert_release(cvp != NULL);
    cmb_assert_release(rgp != NULL);

    cmb_resourceguard_register(rgp, (struct cmb_resourceguard *)cvp);
}

[[maybe_unused]]
static inline bool cmb_condition_unsubscribe(struct cmb_condition *cvp,
                                             struct cmb_resourceguard *rgp) {
    cmb_assert_release(cvp != NULL);
    cmb_assert_release(rgp != NULL);

    return cmb_resourceguard_unregister(rgp, (struct cmb_resourceguard *)cvp);
}

#endif /* CIMBA_CMB_CONDITION_H */