SystemC  2.3.2
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 */
33 #ifndef SC_HOST_SEMAPHORE_H_INCLUDED_
34 #define SC_HOST_SEMAPHORE_H_INCLUDED_
35 
36 #ifndef SC_INCLUDE_WINDOWS_H
37 # define SC_INCLUDE_WINDOWS_H // include Windows.h, if needed
38 #endif
39 #include "sysc/kernel/sc_cmnhdr.h"
41 
42 #if !defined(WIN32) && !defined(_WIN32) // use POSIX semaphore
43 #include <semaphore.h>
44 #endif
45 
46 namespace sc_core {
47 
48 // ----------------------------------------------------------------------------
49 // CLASS : sc_host_semaphore
50 //
51 // Wrapping an OS semaphore on the simulation host
52 // ----------------------------------------------------------------------------
53 
55 {
56 #if defined(WIN32) || defined(_WIN32) // use Windows Semaphore
57 
58  typedef HANDLE underlying_type;
59 
60  void do_init(int init)
61  {
62  m_sem = CreateSemaphore( NULL, init, LONG_MAX, NULL );
63  sc_assert( m_sem != NULL );
64  }
65  void do_wait()
66  { WaitForSingleObject( m_sem, INFINITE ); }
67  bool do_trywait()
68  { return ( WaitForSingleObject( m_sem, 0 ) == WAIT_OBJECT_0 ); }
69  void do_post()
70  { ReleaseSemaphore( m_sem, 1, NULL ); }
71  void do_destroy()
72  { CloseHandle( m_sem ); }
73 
74 #else // use POSIX semaphore
75 
76  typedef sem_t underlying_type;
77 
78  void do_init(int init) { sem_init( &m_sem, 0, init ); }
79  void do_wait() { sem_wait( &m_sem ); }
80  bool do_trywait() { return ( sem_trywait( &m_sem ) == 0 ); }
81  void do_post() { sem_post( &m_sem ); }
82  void do_destroy() { sem_close( &m_sem ); }
83 
84 #endif // platform-specific implementation
85 
86 public:
87 
88  // constructors and destructor
89 
90  explicit sc_host_semaphore(int init = 0) : m_sem()
91  { do_init(init); }
93  { do_destroy(); }
94 
95  // interface methods
96 
97  // lock (take) the semaphore, block if not available
98  virtual int wait()
99  { do_wait(); return 0; }
100 
101  // lock (take) the semaphore, return -1 if not available
102  virtual int trywait()
103  { return do_trywait() ? 0 : -1; }
104 
105  // unlock (give) the semaphore
106  virtual int post()
107  { do_post(); return 0; }
108 
109  // get the value of the semaphore (not supported)
110  virtual int get_value() const
111  { sc_assert(false && "not supported on sc_host_semaphore"); return -1; }
112 
113 private:
114  underlying_type m_sem;
115 };
116 
117 } // namespace sc_core
118 
119 #endif // SC_HOST_SEMAPHORE_H_INCLUDED_
120 // Taf!
#define sc_assert(expr)
Definition: sc_report.h:270
The sc_semaphore_if interface class.
The sc_semaphore_if interface class.
virtual int get_value() const
#define SC_API
Definition: sc_cmnhdr.h:168