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";
59 void ordered_semaphore::report_error(
const char*
id,
const char* add_msg)
const {
60 auto msg = add_msg ?
util::strprintf(
"semaphore '%s'", name()) :
util::strprintf(
"%s: semaphore '%s'", add_msg, name());
61 SC_REPORT_ERROR(
id, msg.c_str());
66 : sc_core::sc_object(sc_core::sc_gen_unique_name(
"semaphore"))
67 , free_evt(gen_unique_event_name(
"free_event").c_str())
69 , capacity(init_value_) {
71 report_error(sc_core::SC_ID_INVALID_SEMAPHORE_VALUE_);
77 , free_evt(gen_unique_event_name(
"free_event").c_str())
79 , capacity(init_value_)
80 , value_traceable(value_traceable) {
82 report_error(sc_core::SC_ID_INVALID_SEMAPHORE_VALUE_);
92 queue.at(priority).push_back(sc_core::sc_get_current_process_handle());
94 sc_core::wait(free_evt);
117 if(capacity && value == capacity) {
118 SCCWARN(SCMOD) <<
"post() called on entirely free semaphore!";
125 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
the sc_ref_variable for a particular plain data type. This marks an existing C++ variable as discover...