17 #include "ordered_semaphore.h"
19 #include "sysc/communication/sc_communication_ids.h"
20 #include "sysc/kernel/sc_wait.h"
21 #include <util/strprintf.h>
25 using namespace sc_core;
35 auto gen_unique_event_name(
const char* modifier) -> std::string {
36 auto str = std::string(
"$$$$kernel_event$$$$_") +
"_" + modifier;
37 return std::string(sc_core::sc_gen_unique_name(str.c_str(),
false));
43 auto diff =
static_cast<int>(capacity) -
static_cast<int>(c);
47 free_evt.notify(sc_core::SC_ZERO_TIME);
49 SCCWARN(SCMOD) <<
"cannot resize fixed size ordered semaphore";
55 void ordered_semaphore::report_error(
const char*
id,
const char* add_msg)
const {
56 auto msg = add_msg ?
util::strprintf(
"semaphore '%s'", name()) :
util::strprintf(
"%s: semaphore '%s'", add_msg, name());
57 SC_REPORT_ERROR(
id, msg.c_str());
63 : sc_core::sc_object(sc_core::sc_gen_unique_name(
"semaphore"))
64 , free_evt(gen_unique_event_name(
"free_event").c_str())
66 , capacity(init_value_) {
68 report_error(sc_core::SC_ID_INVALID_SEMAPHORE_VALUE_);
74 , free_evt(gen_unique_event_name(
"free_event").c_str())
76 , capacity(init_value_)
77 , value_traceable(value_traceable) {}
83 queue.at(priority).push_back(sc_core::sc_get_current_process_handle());
85 sc_core::wait(free_evt);
104 if(capacity && value == capacity) {
105 SCCWARN(SCMOD) <<
"post() called on entirely free semaphore!";
109 free_evt.notify(sc_core::SC_ZERO_TIME);
The ordered_semaphore primitive channel class.
void trace(sc_core::sc_trace_file *tf) const override
adds internal variables to trace
int post() override
unlock (give) the semaphore
int wait() override
lock (take) the semaphore, block if not available
void set_capacity(unsigned capacity)
change the capacity
ordered_semaphore(unsigned init_value=1)
constructor of an un-named semaphore
int trywait() override
lock (take) the semaphore, return -1 if not available
std::string strprintf(const std::string format,...)
allocate and print to a string buffer