SystemC 3.0.0
Accellera SystemC proof-of-concept library
sc_host_semaphore.h
Go to the documentation of this file.
1/*****************************************************************************
2
3 Licensed to Accellera Systems Initiative Inc. (Accellera) under one or
4 more contributor license agreements. See the NOTICE file distributed
5 with this work for additional information regarding copyright ownership.
6 Accellera licenses this file to you under the Apache License, Version 2.0
7 (the "License"); you may not use this file except in compliance with the
8 License. You may obtain a copy of the License at
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
15 implied. See the License for the specific language governing
16 permissions and limitations under the License.
17
18 *****************************************************************************/
19
20/*****************************************************************************
21
22 sc_host_semaphore.h -- A "real" semaphore for the underlying host system
23
24 Original Author: Philipp A. Hartmann, Intel
25
26 CHANGE LOG AT THE END OF THE FILE
27 *****************************************************************************/
28
29#ifndef SC_HOST_SEMAPHORE_H_INCLUDED_
30#define SC_HOST_SEMAPHORE_H_INCLUDED_
31
32#include "sysc/kernel/sc_cmnhdr.h" // SC_CPLUSPLUS
34
35#include <mutex>
36#include <condition_variable>
37
38#if defined(_MSC_VER) && !defined(SC_WIN_DLL_WARN)
39#pragma warning(disable: 4251) // DLL import for std::mutex
40#endif
41
42namespace sc_core {
43
44// ----------------------------------------------------------------------------
45// CLASS : sc_host_semaphore
46//
47// Wrapping an OS semaphore on the simulation host
48// ----------------------------------------------------------------------------
49
51{
52public:
53
54 // constructors and destructor
55
56 explicit sc_host_semaphore(int init = 0) : m_value(init) {}
57 virtual ~sc_host_semaphore() = default;
58
59 // interface methods
60
61 // lock (take) the semaphore, block if not available
62 virtual int wait()
63 {
64 std::unique_lock lock(m_mtx);
65 while (m_value <= 0) {
66 m_cond_var.wait(lock);
67 }
68 --m_value;
69 return 0;
70 }
71
72 // lock (take) the semaphore, return -1 if not available
73 virtual int trywait()
74 {
75 std::unique_lock lock(m_mtx);
76 if (m_value <= 0)
77 return -1;
78 --m_value;
79 return 0;
80 }
81
82 // unlock (give) the semaphore
83 virtual int post()
84 {
85 std::unique_lock lock(m_mtx);
86 ++m_value;
87 m_cond_var.notify_one();
88 return 0;
89 }
90
91 // get the value of the semaphore
92 virtual int get_value() const
93 {
94 return m_value;
95 }
96
97private:
98 std::mutex m_mtx;
99 std::condition_variable m_cond_var;
100 int m_value = 0;
101};
102
103} // namespace sc_core
104
105#endif // SC_HOST_SEMAPHORE_H_INCLUDED_
106// Taf!
#define SC_API
Definition: sc_cmnhdr.h:148
virtual int get_value() const
virtual ~sc_host_semaphore()=default