112027Sjungma@eit.uni-kl.de/*****************************************************************************
212027Sjungma@eit.uni-kl.de
312027Sjungma@eit.uni-kl.de  Licensed to Accellera Systems Initiative Inc. (Accellera) under one or
412027Sjungma@eit.uni-kl.de  more contributor license agreements.  See the NOTICE file distributed
512027Sjungma@eit.uni-kl.de  with this work for additional information regarding copyright ownership.
612027Sjungma@eit.uni-kl.de  Accellera licenses this file to you under the Apache License, Version 2.0
712027Sjungma@eit.uni-kl.de  (the "License"); you may not use this file except in compliance with the
812027Sjungma@eit.uni-kl.de  License.  You may obtain a copy of the License at
912027Sjungma@eit.uni-kl.de
1012027Sjungma@eit.uni-kl.de    http://www.apache.org/licenses/LICENSE-2.0
1112027Sjungma@eit.uni-kl.de
1212027Sjungma@eit.uni-kl.de  Unless required by applicable law or agreed to in writing, software
1312027Sjungma@eit.uni-kl.de  distributed under the License is distributed on an "AS IS" BASIS,
1412027Sjungma@eit.uni-kl.de  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
1512027Sjungma@eit.uni-kl.de  implied.  See the License for the specific language governing
1612027Sjungma@eit.uni-kl.de  permissions and limitations under the License.
1712027Sjungma@eit.uni-kl.de
1812027Sjungma@eit.uni-kl.de *****************************************************************************/
1912027Sjungma@eit.uni-kl.de
2012027Sjungma@eit.uni-kl.de/*****************************************************************************
2112027Sjungma@eit.uni-kl.de
2212027Sjungma@eit.uni-kl.de  sc_cor_pthread.h -- Coroutine implementation with pthreads.
2312027Sjungma@eit.uni-kl.de
2412027Sjungma@eit.uni-kl.de  Original Author: Andy Goodrich, Forte Design Systems, 2002-11-10
2512027Sjungma@eit.uni-kl.de
2612027Sjungma@eit.uni-kl.de  CHANGE LOG AT THE END OF THE FILE
2712027Sjungma@eit.uni-kl.de *****************************************************************************/
2812027Sjungma@eit.uni-kl.de
2912027Sjungma@eit.uni-kl.de
3012027Sjungma@eit.uni-kl.de#ifndef SC_COR_PTHREAD_H
3112027Sjungma@eit.uni-kl.de#define SC_COR_PTHREAD_H
3212027Sjungma@eit.uni-kl.de
3312027Sjungma@eit.uni-kl.de
3412027Sjungma@eit.uni-kl.de#if defined(SC_USE_PTHREADS)
3512027Sjungma@eit.uni-kl.de
3612027Sjungma@eit.uni-kl.de#include "sysc/kernel/sc_cor.h"
3712027Sjungma@eit.uni-kl.de#include "sysc/kernel/sc_cmnhdr.h"
3812027Sjungma@eit.uni-kl.de#include <pthread.h>
3912027Sjungma@eit.uni-kl.de
4012027Sjungma@eit.uni-kl.denamespace sc_core {
4112027Sjungma@eit.uni-kl.de
4212027Sjungma@eit.uni-kl.declass sc_cor_pkg_pthread;
4312027Sjungma@eit.uni-kl.detypedef sc_cor_pkg_pthread sc_cor_pkg_t;
4412027Sjungma@eit.uni-kl.de
4512027Sjungma@eit.uni-kl.de// ----------------------------------------------------------------------------
4612027Sjungma@eit.uni-kl.de//  CLASS : sc_cor_pthread
4712027Sjungma@eit.uni-kl.de//
4812027Sjungma@eit.uni-kl.de//  Coroutine class implemented with Posix Threads.
4912027Sjungma@eit.uni-kl.de//
5012027Sjungma@eit.uni-kl.de// Notes:
5112027Sjungma@eit.uni-kl.de//   (1) The thread creation mutex and the creation condition are used to
5212027Sjungma@eit.uni-kl.de//       suspend the thread creating another one until the created thread
5312027Sjungma@eit.uni-kl.de//       reaches its invoke_module_method. This allows us to get control of
5412027Sjungma@eit.uni-kl.de//       thread scheduling away from the pthread package.
5512027Sjungma@eit.uni-kl.de// ----------------------------------------------------------------------------
5612027Sjungma@eit.uni-kl.de
5712027Sjungma@eit.uni-kl.declass sc_cor_pthread : public sc_cor
5812027Sjungma@eit.uni-kl.de{
5912027Sjungma@eit.uni-kl.de  public:
6012027Sjungma@eit.uni-kl.de
6112027Sjungma@eit.uni-kl.de    // constructor
6212027Sjungma@eit.uni-kl.de    sc_cor_pthread();
6312027Sjungma@eit.uni-kl.de
6412027Sjungma@eit.uni-kl.de    // destructor
6512027Sjungma@eit.uni-kl.de    virtual ~sc_cor_pthread();
6612027Sjungma@eit.uni-kl.de
6712027Sjungma@eit.uni-kl.de	// module method invocator (starts thread execution)
6812027Sjungma@eit.uni-kl.de	static void* invoke_module_method( void* context_p );
6912027Sjungma@eit.uni-kl.de
7012027Sjungma@eit.uni-kl.de  public:
7112027Sjungma@eit.uni-kl.de	static sc_cor_pthread* m_active_cor_p;	   // Active coroutine.
7212027Sjungma@eit.uni-kl.de
7312027Sjungma@eit.uni-kl.de  public:
7412027Sjungma@eit.uni-kl.de	sc_cor_fn*          m_cor_fn;		// Core function.
7512027Sjungma@eit.uni-kl.de	void*               m_cor_fn_arg;	// Core function argument.
7612027Sjungma@eit.uni-kl.de	pthread_mutex_t     m_mutex;        // Mutex to suspend thread on.
7712027Sjungma@eit.uni-kl.de    sc_cor_pkg_pthread* m_pkg_p;        // the creating coroutine package
7812027Sjungma@eit.uni-kl.de	pthread_cond_t      m_pt_condition; // Condition waiting for.
7912027Sjungma@eit.uni-kl.de	pthread_t           m_thread;       // Our pthread storage.
8012027Sjungma@eit.uni-kl.de
8112027Sjungma@eit.uni-kl.deprivate:
8212027Sjungma@eit.uni-kl.de
8312027Sjungma@eit.uni-kl.de    // disabled
8412027Sjungma@eit.uni-kl.de    sc_cor_pthread( const sc_cor_pthread& );
8512027Sjungma@eit.uni-kl.de    sc_cor_pthread& operator = ( const sc_cor_pthread& );
8612027Sjungma@eit.uni-kl.de};
8712027Sjungma@eit.uni-kl.de
8812027Sjungma@eit.uni-kl.de
8912027Sjungma@eit.uni-kl.de// ----------------------------------------------------------------------------
9012027Sjungma@eit.uni-kl.de//  CLASS : sc_cor_pkg_pthread
9112027Sjungma@eit.uni-kl.de//
9212027Sjungma@eit.uni-kl.de//  Coroutine package class implemented with Posix Threads.
9312027Sjungma@eit.uni-kl.de// ----------------------------------------------------------------------------
9412027Sjungma@eit.uni-kl.de
9512027Sjungma@eit.uni-kl.declass sc_cor_pkg_pthread
9612027Sjungma@eit.uni-kl.de: public sc_cor_pkg
9712027Sjungma@eit.uni-kl.de{
9812027Sjungma@eit.uni-kl.depublic:
9912027Sjungma@eit.uni-kl.de
10012027Sjungma@eit.uni-kl.de    // constructor
10112027Sjungma@eit.uni-kl.de    sc_cor_pkg_pthread( sc_simcontext* simc );
10212027Sjungma@eit.uni-kl.de
10312027Sjungma@eit.uni-kl.de    // destructor
10412027Sjungma@eit.uni-kl.de    virtual ~sc_cor_pkg_pthread();
10512027Sjungma@eit.uni-kl.de
10612027Sjungma@eit.uni-kl.de    // create a new coroutine
10712027Sjungma@eit.uni-kl.de    virtual sc_cor* create( std::size_t stack_size, sc_cor_fn* fn, void* arg );
10812027Sjungma@eit.uni-kl.de
10912027Sjungma@eit.uni-kl.de    // yield to the next coroutine
11012027Sjungma@eit.uni-kl.de    virtual void yield( sc_cor* next_cor );
11112027Sjungma@eit.uni-kl.de
11212027Sjungma@eit.uni-kl.de    // abort the current coroutine (and resume the next coroutine)
11312027Sjungma@eit.uni-kl.de    virtual void abort( sc_cor* next_cor );
11412027Sjungma@eit.uni-kl.de
11512027Sjungma@eit.uni-kl.de    // get the main coroutine
11612027Sjungma@eit.uni-kl.de    virtual sc_cor* get_main();
11712027Sjungma@eit.uni-kl.de
11812027Sjungma@eit.uni-kl.deprivate:
11912027Sjungma@eit.uni-kl.de
12012027Sjungma@eit.uni-kl.de    static int instance_count;
12112027Sjungma@eit.uni-kl.de
12212027Sjungma@eit.uni-kl.deprivate:
12312027Sjungma@eit.uni-kl.de
12412027Sjungma@eit.uni-kl.de    // disabled
12512027Sjungma@eit.uni-kl.de    sc_cor_pkg_pthread();
12612027Sjungma@eit.uni-kl.de    sc_cor_pkg_pthread( const sc_cor_pkg_pthread& );
12712027Sjungma@eit.uni-kl.de    sc_cor_pkg_pthread& operator = ( const sc_cor_pkg_pthread& );
12812027Sjungma@eit.uni-kl.de};
12912027Sjungma@eit.uni-kl.de
13012027Sjungma@eit.uni-kl.de} // namespace sc_core
13112027Sjungma@eit.uni-kl.de
13212027Sjungma@eit.uni-kl.de#endif
13312027Sjungma@eit.uni-kl.de
13412027Sjungma@eit.uni-kl.de// $Log: sc_cor_pthread.h,v $
13512027Sjungma@eit.uni-kl.de// Revision 1.5  2011/08/26 20:46:09  acg
13612027Sjungma@eit.uni-kl.de//  Andy Goodrich: moved the modification log to the end of the file to
13712027Sjungma@eit.uni-kl.de//  eliminate source line number skew when check-ins are done.
13812027Sjungma@eit.uni-kl.de//
13912027Sjungma@eit.uni-kl.de// Revision 1.4  2011/02/18 20:27:14  acg
14012027Sjungma@eit.uni-kl.de//  Andy Goodrich: Updated Copyrights.
14112027Sjungma@eit.uni-kl.de//
14212027Sjungma@eit.uni-kl.de// Revision 1.3  2011/02/13 21:47:37  acg
14312027Sjungma@eit.uni-kl.de//  Andy Goodrich: update copyright notice.
14412027Sjungma@eit.uni-kl.de//
14512027Sjungma@eit.uni-kl.de// Revision 1.2  2008/05/22 17:06:25  acg
14612027Sjungma@eit.uni-kl.de//  Andy Goodrich: updated copyright notice to include 2008.
14712027Sjungma@eit.uni-kl.de//
14812027Sjungma@eit.uni-kl.de// Revision 1.1.1.1  2006/12/15 20:20:05  acg
14912027Sjungma@eit.uni-kl.de// SystemC 2.3
15012027Sjungma@eit.uni-kl.de//
15112027Sjungma@eit.uni-kl.de// Revision 1.3  2006/01/13 18:44:29  acg
15212027Sjungma@eit.uni-kl.de// Added $Log to record CVS changes into the source.
15312027Sjungma@eit.uni-kl.de//
15412027Sjungma@eit.uni-kl.de
15512027Sjungma@eit.uni-kl.de#endif // defined(SC_USE_PTHREADS)
15612027Sjungma@eit.uni-kl.de
15712027Sjungma@eit.uni-kl.de// Taf!
158