SystemC 3.0.0
Accellera SystemC proof-of-concept library
sc_cor_pthread.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_cor_pthread.h -- Coroutine implementation with pthreads.
23
24 Original Author: Andy Goodrich, Forte Design Systems, 2002-11-10
25
26 CHANGE LOG AT THE END OF THE FILE
27 *****************************************************************************/
28
29
30#ifndef SC_COR_PTHREAD_H
31#define SC_COR_PTHREAD_H
32
33
34#if defined(SC_USE_PTHREADS)
35
36#include "sysc/kernel/sc_cor.h"
38#include <pthread.h>
39
40namespace sc_core {
41
42class sc_cor_pkg_pthread;
43typedef sc_cor_pkg_pthread sc_cor_pkg_t;
44
45// ----------------------------------------------------------------------------
46// CLASS : sc_cor_pthread
47//
48// Coroutine class implemented with Posix Threads.
49//
50// ----------------------------------------------------------------------------
51
52class sc_cor_pthread : public sc_cor
53{
54 public:
55
56 // constructor
57 sc_cor_pthread();
58
59 // destructor
60 virtual ~sc_cor_pthread();
61
62 // module method invocator (starts thread execution)
63 static void* invoke_module_method( void* context_p );
64
65 public:
66 sc_cor_fn* m_cor_fn; // Core function.
67 void* m_cor_fn_arg; // Core function argument.
68 pthread_mutex_t m_mutex; // Mutex to suspend thread on.
69 sc_cor_pkg_pthread* m_pkg_p; // the creating coroutine package
70 pthread_cond_t m_pt_condition; // Condition waiting for.
71 pthread_t m_thread; // Our pthread storage.
72
73private:
74
75 // disabled
76 sc_cor_pthread( const sc_cor_pthread& );
77 sc_cor_pthread& operator = ( const sc_cor_pthread& );
78};
79
80
81// ----------------------------------------------------------------------------
82// CLASS : sc_cor_pkg_pthread
83//
84// Coroutine package class implemented with Posix Threads.
85//
86// Notes:
87// (1) The thread creation mutex and the creation condition are used to
88// suspend the thread creating another one until the created thread
89// reaches its invoke_module_method. This allows us to get control of
90// thread scheduling away from the pthread package.
91// ----------------------------------------------------------------------------
92
93class sc_cor_pkg_pthread
94 : public sc_cor_pkg
95{
96 friend void* sc_cor_pthread::invoke_module_method( void* context_p );
97public:
98
99 // constructor
100 sc_cor_pkg_pthread( sc_simcontext* simc );
101
102 // destructor
103 virtual ~sc_cor_pkg_pthread();
104
105 // create a new coroutine
106 virtual sc_cor* create( std::size_t stack_size, sc_cor_fn* fn, void* arg );
107
108 // yield to the next coroutine
109 virtual void yield( sc_cor* next_cor );
110
111 // abort the current coroutine (and resume the next coroutine)
112 virtual void abort( sc_cor* next_cor );
113
114 // get the main coroutine
115 virtual sc_cor* get_main();
116
117private:
118
119 sc_cor_pthread m_main_cor; // Main coroutine
120 sc_cor_pthread* m_curr_cor; // Active coroutine
121 pthread_mutex_t m_create_mtx; // See note (1) above
122 pthread_cond_t m_create_cond; // See note (1) above
123
124private:
125
126 // disabled
127 sc_cor_pkg_pthread();
128 sc_cor_pkg_pthread( const sc_cor_pkg_pthread& );
129 sc_cor_pkg_pthread& operator = ( const sc_cor_pkg_pthread& );
130};
131
132} // namespace sc_core
133
134#endif
135
136// $Log: sc_cor_pthread.h,v $
137// Revision 1.5 2011/08/26 20:46:09 acg
138// Andy Goodrich: moved the modification log to the end of the file to
139// eliminate source line number skew when check-ins are done.
140//
141// Revision 1.4 2011/02/18 20:27:14 acg
142// Andy Goodrich: Updated Copyrights.
143//
144// Revision 1.3 2011/02/13 21:47:37 acg
145// Andy Goodrich: update copyright notice.
146//
147// Revision 1.2 2008/05/22 17:06:25 acg
148// Andy Goodrich: updated copyright notice to include 2008.
149//
150// Revision 1.1.1.1 2006/12/15 20:20:05 acg
151// SystemC 2.3
152//
153// Revision 1.3 2006/01/13 18:44:29 acg
154// Added $Log to record CVS changes into the source.
155//
156
157#endif // defined(SC_USE_PTHREADS)
158
159// Taf!
class SC_API sc_simcontext
Definition: sc_object.h:50
sc_cor_pkg_qt sc_cor_pkg_t
Definition: sc_cor_qt.h:42
void() sc_cor_fn(void *)
Definition: sc_cor.h:48