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_join.cpp -- Join Process Synchronization Implementation
23
24  Original Author: Andy Goodrich, Forte Design Systems, 5 May 2003
25
26 CHANGE LOG APPEARS AT THE END OF THE FILE
27 *****************************************************************************/
28
29
30#include <cassert>
31#include <cstdlib>
32#include <cstddef>
33
34#include "sysc/kernel/sc_process_handle.h"
35#include "sysc/kernel/sc_simcontext.h"
36#include "sysc/kernel/sc_simcontext_int.h"
37#include "sysc/kernel/sc_kernel_ids.h"
38#include "sysc/kernel/sc_thread_process.h"
39#include "sysc/kernel/sc_join.h"
40
41namespace sc_core {
42
43//------------------------------------------------------------------------------
44//"sc_join::sc_join"
45//
46// This is the object instance constructor for this class.
47//------------------------------------------------------------------------------
48sc_join::sc_join()
49  : m_join_event( (std::string(SC_KERNEL_EVENT_PREFIX)+"_join_event").c_str() )
50  , m_threads_n(0)
51{}
52
53//------------------------------------------------------------------------------
54//"sc_join::add_process - sc_process_b*"
55//
56// This method adds a process to this join object instance. This consists of
57// incrementing the count of processes in the join process and adding this
58// object instance to the supplied thread's monitoring queue.
59//     process_p -> thread to be monitored.
60//------------------------------------------------------------------------------
61void sc_join::add_process( sc_process_b* process_p )
62{
63    sc_thread_handle handle = DCAST<sc_thread_handle>(process_p);
64    assert( handle != 0 );
65    m_threads_n++;
66    handle->add_monitor( this );
67}
68
69
70//------------------------------------------------------------------------------
71//"sc_join::add_process - sc_process_handle"
72//
73// This method adds a process to this join object instance. This consists of
74// incrementing the count of processes in the join process and adding this
75// object instance to the supplied thread's monitoring queue.
76//     process_h = handle for process to be monitored.
77//------------------------------------------------------------------------------
78void sc_join::add_process( sc_process_handle process_h )
79{
80    sc_thread_handle thread_p; // Thread within process_h.
81
82    thread_p = process_h.operator sc_thread_handle();
83    if ( thread_p )
84    {
85        m_threads_n++;
86        thread_p->add_monitor( this );
87    }
88    else
89    {
90        SC_REPORT_ERROR( SC_ID_JOIN_ON_METHOD_HANDLE_, 0 );
91    }
92}
93
94
95//------------------------------------------------------------------------------
96//"sc_join::signal"
97//
98// This virtual method is called when a process being monitored by this object
99// instance sends a signal. If the signal type is spm_exit and the count of
100// threads that we are waiting to terminate on goes to zero we fire our join
101// event.
102//     thread_p -> thread that is signalling.
103//     type     =  type of signal being sent.
104//------------------------------------------------------------------------------
105void sc_join::signal(sc_thread_handle thread_p, int type)
106{
107    switch ( type )
108    {
109      case sc_process_monitor::spm_exit:
110        thread_p->remove_monitor(this);
111        if ( --m_threads_n == 0 ) m_join_event.notify();
112        break;
113    }
114}
115
116} // namespace sc_core
117
118// $Log: sc_join.cpp,v $
119// Revision 1.7  2011/08/26 21:45:00  acg
120//  Andy Goodrich: fix internal event naming.
121//
122// Revision 1.6  2011/08/26 20:46:09  acg
123//  Andy Goodrich: moved the modification log to the end of the file to
124//  eliminate source line number skew when check-ins are done.
125//
126// Revision 1.5  2011/02/18 20:27:14  acg
127//  Andy Goodrich: Updated Copyrights.
128//
129// Revision 1.4  2011/02/13 21:47:37  acg
130//  Andy Goodrich: update copyright notice.
131//
132// Revision 1.3  2009/07/28 01:10:53  acg
133//  Andy Goodrich: updates for 2.3 release candidate.
134//
135// Revision 1.2  2008/05/22 17:06:25  acg
136//  Andy Goodrich: updated copyright notice to include 2008.
137//
138// Revision 1.1.1.1  2006/12/15 20:20:05  acg
139// SystemC 2.3
140//
141// Revision 1.3  2006/01/13 18:44:29  acg
142// Added $Log to record CVS changes into the source.
143//
144