scc  2022.4.0
SystemC components library
ordered_semaphore.h
1 /*******************************************************************************
2  * Copyright 2019 MINRES Technologies GmbH
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *******************************************************************************/
16 
17 #pragma once
18 
19 #include "sysc/communication/sc_semaphore_if.h"
20 #include "sysc/kernel/sc_event.h"
21 #include "sysc/kernel/sc_object.h"
22 #include "traceable.h"
23 #include <array>
24 #include <deque>
25 #include <sysc/kernel/sc_process_handle.h>
26 
27 #ifndef SC_API
28 #define SC_API
29 #endif
30 
36 namespace scc {
44 class SC_API ordered_semaphore : public sc_core::sc_semaphore_if, public sc_core::sc_object, public scc::traceable {
45 public:
54  explicit ordered_semaphore(unsigned init_value = 1);
63  ordered_semaphore(const char* name, unsigned init_value = 1, bool value_traceable = false);
64 
65  ordered_semaphore(const ordered_semaphore&) = delete;
66 
67  ordered_semaphore& operator=(const ordered_semaphore&) = delete;
74  int wait() override { return wait(0); }
81  int wait(unsigned priority);
88  int trywait() override;
95  int post() override;
101  unsigned get_capacity() { return capacity; }
108  void set_capacity(unsigned capacity);
115  int get_value() const override { return value; }
122  const char* kind() const override { return "sc_semaphore_ordered"; }
129  void trace(sc_core::sc_trace_file* tf) const override;
136  bool is_trace_enabled() const override { return value_traceable; }
144  struct lock {
152  : sem(sem) {
153  sem.wait();
154  }
155  lock(scc::ordered_semaphore& sem, unsigned prio)
156  : sem(sem) {
157  sem.wait(prio);
158  }
164  ~lock() { release(); }
170  void release() {
171  if(owned)
172  sem.post();
173  owned = false;
174  }
175 
176  private:
178  bool owned{true};
179  };
180 
181 protected:
182  // support methods
183  bool in_use() {
184  if(value > 0) {
185  if(queue[1].size()) {
186  if(queue[1].front() == sc_core::sc_get_current_process_handle()) {
187  queue[1].pop_front();
188  return false;
189  }
190  } else {
191  if(queue[0].front() == sc_core::sc_get_current_process_handle()) {
192  queue[0].pop_front();
193  return false;
194  }
195  }
196  }
197  return true;
198  }
199 
200  // error reporting
201  void report_error(const char* id, const char* add_msg = 0) const;
202 
203 protected:
204  sc_core::sc_event free_evt; // event to block on when m_value is negative
205  int value; // current value of the semaphore
206  unsigned capacity;
207  bool value_traceable = false;
208  std::array<std::deque<sc_core::sc_process_handle>, 2> queue;
209 };
210 
211 template <unsigned CAPACITY> struct ordered_semaphore_t : public ordered_semaphore {
212  explicit ordered_semaphore_t()
213  : ordered_semaphore(CAPACITY) {}
214  ordered_semaphore_t(const char* name_)
215  : ordered_semaphore(name_, CAPACITY) {}
216 };
217 
218 } // namespace scc // end of scc-sysc
The ordered_semaphore primitive channel class.
int get_value() const override
get the value of the semaphore
unsigned get_capacity()
retrieve the initial capacity of the semaphore
int post() override
unlock (give) the semaphore
const char * kind() const override
kind of this SastemC object
bool is_trace_enabled() const override
returns of this component shall be traced
int wait() override
lock (take) the semaphore, block if not available
ordered_semaphore(unsigned init_value=1)
constructor of an un-named semaphore
interface defining a traceable component
Definition: traceable.h:32
SCC SystemC utilities.
a lock for the semaphore
~lock()
destructor, unlock the semaphore if still locked
void release()
unlock the semaphore
lock(scc::ordered_semaphore &sem)
lock the given semahore, wait if not free