SystemC  2.3.1
Accellera SystemC proof-of-concept library
sc_cor_pthread.h
Go to the documentation of this file.
1 /*****************************************************************************
2 
3  The following code is derived, directly or indirectly, from the SystemC
4  source code Copyright (c) 1996-2014 by all Contributors.
5  All Rights reserved.
6 
7  The contents of this file are subject to the restrictions and limitations
8  set forth in the SystemC Open Source License (the "License");
9  You may not use this file except in compliance with such restrictions and
10  limitations. You may obtain instructions on how to receive a copy of the
11  License at http://www.accellera.org/. Software distributed by Contributors
12  under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF
13  ANY KIND, either express or implied. See the License for the specific
14  language governing rights and limitations under the License.
15 
16  *****************************************************************************/
17 
18 /*****************************************************************************
19 
20  sc_cor_pthread.h -- Coroutine implementation with pthreads.
21 
22  Original Author: Andy Goodrich, Forte Design Systems, 2002-11-10
23 
24  CHANGE LOG AT THE END OF THE FILE
25  *****************************************************************************/
26 
27 
28 #ifndef SC_COR_PTHREAD_H
29 #define SC_COR_PTHREAD_H
30 
31 
32 #if defined(SC_USE_PTHREADS)
33 
34 #include "sysc/kernel/sc_cor.h"
35 #include "sysc/kernel/sc_cmnhdr.h"
36 #include <pthread.h>
37 
38 namespace sc_core {
39 
40 class sc_cor_pkg_pthread;
41 typedef sc_cor_pkg_pthread sc_cor_pkg_t;
42 
43 // ----------------------------------------------------------------------------
44 // CLASS : sc_cor_pthread
45 //
46 // Coroutine class implemented with Posix Threads.
47 //
48 // Notes:
49 // (1) The thread creation mutex and the creation condition are used to
50 // suspend the thread creating another one until the created thread
51 // reaches its invoke_module_method. This allows us to get control of
52 // thread scheduling away from the pthread package.
53 // ----------------------------------------------------------------------------
54 
55 class sc_cor_pthread : public sc_cor
56 {
57  public:
58 
59  // constructor
60  sc_cor_pthread();
61 
62  // destructor
63  virtual ~sc_cor_pthread();
64 
65  // module method invocator (starts thread execution)
66  static void* invoke_module_method( void* context_p );
67 
68  public:
69  static sc_cor_pthread* m_active_cor_p; // Active coroutine.
70 
71  public:
72  sc_cor_fn* m_cor_fn; // Core function.
73  void* m_cor_fn_arg; // Core function argument.
74  pthread_mutex_t m_mutex; // Mutex to suspend thread on.
75  sc_cor_pkg_pthread* m_pkg_p; // the creating coroutine package
76  pthread_cond_t m_pt_condition; // Condition waiting for.
77  pthread_t m_thread; // Our pthread storage.
78 
79 private:
80 
81  // disabled
82  sc_cor_pthread( const sc_cor_pthread& );
83  sc_cor_pthread& operator = ( const sc_cor_pthread& );
84 };
85 
86 
87 // ----------------------------------------------------------------------------
88 // CLASS : sc_cor_pkg_pthread
89 //
90 // Coroutine package class implemented with Posix Threads.
91 // ----------------------------------------------------------------------------
92 
93 class sc_cor_pkg_pthread
94 : public sc_cor_pkg
95 {
96 public:
97 
98  // constructor
99  sc_cor_pkg_pthread( sc_simcontext* simc );
100 
101  // destructor
102  virtual ~sc_cor_pkg_pthread();
103 
104  // create a new coroutine
105  virtual sc_cor* create( std::size_t stack_size, sc_cor_fn* fn, void* arg );
106 
107  // yield to the next coroutine
108  virtual void yield( sc_cor* next_cor );
109 
110  // abort the current coroutine (and resume the next coroutine)
111  virtual void abort( sc_cor* next_cor );
112 
113  // get the main coroutine
114  virtual sc_cor* get_main();
115 
116 private:
117 
118  static int instance_count;
119 
120 private:
121 
122  // disabled
123  sc_cor_pkg_pthread();
124  sc_cor_pkg_pthread( const sc_cor_pkg_pthread& );
125  sc_cor_pkg_pthread& operator = ( const sc_cor_pkg_pthread& );
126 };
127 
128 } // namespace sc_core
129 
130 #endif
131 
132 // $Log: sc_cor_pthread.h,v $
133 // Revision 1.5 2011/08/26 20:46:09 acg
134 // Andy Goodrich: moved the modification log to the end of the file to
135 // eliminate source line number skew when check-ins are done.
136 //
137 // Revision 1.4 2011/02/18 20:27:14 acg
138 // Andy Goodrich: Updated Copyrights.
139 //
140 // Revision 1.3 2011/02/13 21:47:37 acg
141 // Andy Goodrich: update copyright notice.
142 //
143 // Revision 1.2 2008/05/22 17:06:25 acg
144 // Andy Goodrich: updated copyright notice to include 2008.
145 //
146 // Revision 1.1.1.1 2006/12/15 20:20:05 acg
147 // SystemC 2.3
148 //
149 // Revision 1.3 2006/01/13 18:44:29 acg
150 // Added $Log to record CVS changes into the source.
151 //
152 
153 #endif // defined(SC_USE_PTHREADS)
154 
155 // Taf!
void( sc_cor_fn)(void *)
Definition: sc_cor.h:46
sc_cor_pkg_qt sc_cor_pkg_t
Definition: sc_cor_qt.h:39