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