1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
|
/*****************************************************************************
Licensed to Accellera Systems Initiative Inc. (Accellera) under one or
more contributor license agreements. See the NOTICE file distributed
with this work for additional information regarding copyright ownership.
Accellera licenses this file to you under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with the
License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied. See the License for the specific language governing
permissions and limitations under the License.
*****************************************************************************/
/*****************************************************************************
sc_event.cpp --
Original Author: Martin Janssen, Synopsys, Inc., 2001-05-21
CHANGE LOG APPEARS AT THE END OF THE FILE
*****************************************************************************/
#include <stdlib.h>
#include <string.h>
#include "sysc/kernel/sc_event.h"
#include "sysc/kernel/sc_kernel_ids.h"
#include "sysc/kernel/sc_phase_callback_registry.h"
#include "sysc/kernel/sc_process.h"
#include "sysc/kernel/sc_process_handle.h"
#include "sysc/kernel/sc_simcontext_int.h"
#include "sysc/kernel/sc_object_manager.h"
#include "sysc/utils/sc_utils_ids.h"
namespace sc_core {
// ----------------------------------------------------------------------------
// CLASS : sc_event
//
// The event class.
// ----------------------------------------------------------------------------
const char*
sc_event::basename() const
{
const char* p = strrchr( m_name.c_str(), SC_HIERARCHY_CHAR );
return p ? (p + 1) : m_name.c_str();
}
void
sc_event::cancel()
{
// cancel a delta or timed notification
switch( m_notify_type ) {
case DELTA: {
// remove this event from the delta events set
m_simc->remove_delta_event( this );
m_notify_type = NONE;
break;
}
case TIMED: {
// remove this event from the timed events set
sc_assert( m_timed != 0 );
m_timed->m_event = 0;
m_timed = 0;
m_notify_type = NONE;
break;
}
default:
;
}
}
void
sc_event::notify()
{
// immediate notification
if(
// coming from sc_prim_channel::update
m_simc->update_phase()
#if SC_HAS_PHASE_CALLBACKS_
// coming from phase callbacks
|| m_simc->notify_phase()
#endif
)
{
SC_REPORT_ERROR( SC_ID_IMMEDIATE_NOTIFICATION_, "" );
return;
}
cancel();
trigger();
}
void
sc_event::notify( const sc_time& t )
{
if( m_notify_type == DELTA ) {
return;
}
if( t == SC_ZERO_TIME ) {
# if SC_HAS_PHASE_CALLBACKS_
if( SC_UNLIKELY_( m_simc->get_status()
& (SC_END_OF_UPDATE|SC_BEFORE_TIMESTEP) ) )
{
std::stringstream msg;
msg << m_simc->get_status()
<< ":\n\t delta notification of `"
<< name() << "' ignored";
SC_REPORT_WARNING( SC_ID_PHASE_CALLBACK_FORBIDDEN_
, msg.str().c_str() );
return;
}
# endif
if( m_notify_type == TIMED ) {
// remove this event from the timed events set
sc_assert( m_timed != 0 );
m_timed->m_event = 0;
m_timed = 0;
}
// add this event to the delta events set
m_delta_event_index = m_simc->add_delta_event( this );
m_notify_type = DELTA;
return;
}
# if SC_HAS_PHASE_CALLBACKS_
if( SC_UNLIKELY_( m_simc->get_status()
& (SC_END_OF_UPDATE|SC_BEFORE_TIMESTEP) ) )
{
std::stringstream msg;
msg << m_simc->get_status()
<< ":\n\t timed notification of `"
<< name() << "' ignored";
SC_REPORT_WARNING( SC_ID_PHASE_CALLBACK_FORBIDDEN_
, msg.str().c_str() );
return;
}
# endif
if( m_notify_type == TIMED ) {
sc_assert( m_timed != 0 );
if( m_timed->m_notify_time <= m_simc->time_stamp() + t ) {
return;
}
// remove this event from the timed events set
m_timed->m_event = 0;
m_timed = 0;
}
// add this event to the timed events set
sc_event_timed* et = new sc_event_timed( this, m_simc->time_stamp() + t );
m_simc->add_timed_event( et );
m_timed = et;
m_notify_type = TIMED;
}
static void sc_warn_notify_delayed()
{
static bool warn_notify_delayed=true;
if ( warn_notify_delayed )
{
warn_notify_delayed = false;
SC_REPORT_INFO(SC_ID_IEEE_1666_DEPRECATION_,
"notify_delayed(...) is deprecated, use notify(sc_time) instead" );
}
}
void
sc_event::notify_delayed()
{
sc_warn_notify_delayed();
if( m_notify_type != NONE ) {
SC_REPORT_ERROR( SC_ID_NOTIFY_DELAYED_, 0 );
}
// add this event to the delta events set
m_delta_event_index = m_simc->add_delta_event( this );
m_notify_type = DELTA;
}
void
sc_event::notify_delayed( const sc_time& t )
{
sc_warn_notify_delayed();
if( m_notify_type != NONE ) {
SC_REPORT_ERROR( SC_ID_NOTIFY_DELAYED_, 0 );
}
if( t == SC_ZERO_TIME ) {
// add this event to the delta events set
m_delta_event_index = m_simc->add_delta_event( this );
m_notify_type = DELTA;
} else {
// add this event to the timed events set
sc_event_timed* et = new sc_event_timed( this,
m_simc->time_stamp() + t );
m_simc->add_timed_event( et );
m_timed = et;
m_notify_type = TIMED;
}
}
// +----------------------------------------------------------------------------
// |"sc_event::register_event"
// |
// | This method sets the name of this object instance and optionally adds
// | it to the object manager's hierarchy. The object instance will be
// | inserted into the object manager's hierarchy if one of the following is
// | true:
// | (a) the leaf name is non-null and does not start with
// | SC_KERNEL_EVENT_PREFIX.
// | (b) the event is being created before the start of simulation.
// |
// | Arguments:
// | leaf_name = leaf name of the object or NULL.
// +----------------------------------------------------------------------------
void sc_event::register_event( const char* leaf_name )
{
sc_object_manager* object_manager = m_simc->get_object_manager();
m_parent_p = m_simc->active_object();
// No name provided, if we are not executing then create a name:
if( !leaf_name || !leaf_name[0] )
{
if ( sc_is_running( m_simc ) ) return;
leaf_name = sc_gen_unique_name("event");
}
// Create a hierarchichal name and place it into the object manager if
// its not a kernel event:
object_manager->create_name( leaf_name ).swap( m_name );
if ( strncmp( leaf_name, SC_KERNEL_EVENT_PREFIX,
strlen(SC_KERNEL_EVENT_PREFIX) ) )
{
object_manager->insert_event(m_name, this);
if ( m_parent_p )
m_parent_p->add_child_event( this );
else
m_simc->add_child_event( this );
}
}
void
sc_event::reset()
{
m_notify_type = NONE;
m_delta_event_index = -1;
m_timed = 0;
// clear the dynamic sensitive methods
m_methods_dynamic.resize(0);
// clear the dynamic sensitive threads
m_threads_dynamic.resize(0);
}
// +----------------------------------------------------------------------------
// |"sc_event::sc_event(name)"
// |
// | This is the object instance constructor for named sc_event instances.
// | If the name is non-null or the this is during elaboration add the
// | event to the object hierarchy.
// |
// | Arguments:
// | name = name of the event.
// +----------------------------------------------------------------------------
sc_event::sc_event( const char* name ) :
m_name(),
m_parent_p(NULL),
m_simc( sc_get_curr_simcontext() ),
m_notify_type( NONE ),
m_delta_event_index( -1 ),
m_timed( 0 ),
m_methods_static(),
m_methods_dynamic(),
m_threads_static(),
m_threads_dynamic()
{
// Skip simulator's internally defined events.
register_event( name );
}
// +----------------------------------------------------------------------------
// |"sc_event::sc_event(name)"
// |
// | This is the object instance constructor for non-named sc_event instances.
// | If this is during elaboration add create a name and add it to the object
// | hierarchy.
// +----------------------------------------------------------------------------
sc_event::sc_event() :
m_name(),
m_parent_p(NULL),
m_simc( sc_get_curr_simcontext() ),
m_notify_type( NONE ),
m_delta_event_index( -1 ),
m_timed( 0 ),
m_methods_static(),
m_methods_dynamic(),
m_threads_static(),
m_threads_dynamic()
{
register_event( NULL );
}
// +----------------------------------------------------------------------------
// |"sc_event::~sc_event"
// |
// | This is the object instance destructor for this class. It cancels any
// | outstanding waits and removes the event from the object manager's
// | instance table if it has a name.
// +----------------------------------------------------------------------------
sc_event::~sc_event()
{
cancel();
if ( m_name.length() != 0 )
{
sc_object_manager* object_manager_p = m_simc->get_object_manager();
object_manager_p->remove_event( m_name );
}
}
// +----------------------------------------------------------------------------
// |"sc_event::trigger"
// |
// | This method "triggers" this object instance. This consists of scheduling
// | for execution all the processes that are schedulable and waiting on this
// | event.
// +----------------------------------------------------------------------------
void
sc_event::trigger()
{
int last_i; // index of last element in vector now accessing.
int size; // size of vector now accessing.
// trigger the static sensitive methods
if( ( size = m_methods_static.size() ) != 0 )
{
sc_method_handle* l_methods_static = &m_methods_static[0];
int i = size - 1;
do {
sc_method_handle method_h = l_methods_static[i];
method_h->trigger_static();
} while( -- i >= 0 );
}
// trigger the dynamic sensitive methods
if( ( size = m_methods_dynamic.size() ) != 0 )
{
last_i = size - 1;
sc_method_handle* l_methods_dynamic = &m_methods_dynamic[0];
for ( int i = 0; i <= last_i; i++ )
{
sc_method_handle method_h = l_methods_dynamic[i];
if ( method_h->trigger_dynamic( this ) )
{
l_methods_dynamic[i] = l_methods_dynamic[last_i];
last_i--;
i--;
}
}
m_methods_dynamic.resize(last_i+1);
}
// trigger the static sensitive threads
if( ( size = m_threads_static.size() ) != 0 )
{
sc_thread_handle* l_threads_static = &m_threads_static[0];
int i = size - 1;
do {
sc_thread_handle thread_h = l_threads_static[i];
thread_h->trigger_static();
} while( -- i >= 0 );
}
// trigger the dynamic sensitive threads
if( ( size = m_threads_dynamic.size() ) != 0 )
{
last_i = size - 1;
sc_thread_handle* l_threads_dynamic = &m_threads_dynamic[0];
for ( int i = 0; i <= last_i; i++ )
{
sc_thread_handle thread_h = l_threads_dynamic[i];
if ( thread_h->trigger_dynamic( this ) )
{
l_threads_dynamic[i] = l_threads_dynamic[last_i];
i--;
last_i--;
}
}
m_threads_dynamic.resize(last_i+1);
}
m_notify_type = NONE;
m_delta_event_index = -1;
m_timed = 0;
}
bool
sc_event::remove_static( sc_method_handle method_h_ ) const
{
int size;
if ( ( size = m_methods_static.size() ) != 0 ) {
sc_method_handle* l_methods_static = &m_methods_static[0];
for( int i = size - 1; i >= 0; -- i ) {
if( l_methods_static[i] == method_h_ ) {
l_methods_static[i] = l_methods_static[size - 1];
m_methods_static.resize(size-1);
return true;
}
}
}
return false;
}
bool
sc_event::remove_static( sc_thread_handle thread_h_ ) const
{
int size;
if ( ( size = m_threads_static.size() ) != 0 ) {
sc_thread_handle* l_threads_static = &m_threads_static[0];
for( int i = size - 1; i >= 0; -- i ) {
if( l_threads_static[i] == thread_h_ ) {
l_threads_static[i] = l_threads_static[size - 1];
m_threads_static.resize(size-1);
return true;
}
}
}
return false;
}
bool
sc_event::remove_dynamic( sc_method_handle method_h_ ) const
{
int size;
if ( ( size = m_methods_dynamic.size() ) != 0 ) {
sc_method_handle* l_methods_dynamic = &m_methods_dynamic[0];
for( int i = size - 1; i >= 0; -- i ) {
if( l_methods_dynamic[i] == method_h_ ) {
l_methods_dynamic[i] = l_methods_dynamic[size - 1];
m_methods_dynamic.resize(size-1);
return true;
}
}
}
return false;
}
bool
sc_event::remove_dynamic( sc_thread_handle thread_h_ ) const
{
int size;
if ( ( size= m_threads_dynamic.size() ) != 0 ) {
sc_thread_handle* l_threads_dynamic = &m_threads_dynamic[0];
for( int i = size - 1; i >= 0; -- i ) {
if( l_threads_dynamic[i] == thread_h_ ) {
l_threads_dynamic[i] = l_threads_dynamic[size - 1];
m_threads_dynamic.resize(size-1);
return true;
}
}
}
return false;
}
// ----------------------------------------------------------------------------
// CLASS : sc_event_timed
//
// Class for storing the time to notify a timed event.
// ----------------------------------------------------------------------------
// dedicated memory management; not MT-Safe
union sc_event_timed_u
{
sc_event_timed_u* next;
char dummy[sizeof( sc_event_timed )];
};
static
sc_event_timed_u* free_list = 0;
void*
sc_event_timed::allocate()
{
const int ALLOC_SIZE = 64;
if( free_list == 0 ) {
free_list = (sc_event_timed_u*) malloc( ALLOC_SIZE *
sizeof( sc_event_timed ) );
int i = 0;
for( ; i < ALLOC_SIZE - 1; ++ i ) {
free_list[i].next = &free_list[i + 1];
}
free_list[i].next = 0;
}
sc_event_timed_u* q = free_list;
free_list = free_list->next;
return q;
}
void
sc_event_timed::deallocate( void* p )
{
if( p != 0 ) {
sc_event_timed_u* q = RCAST<sc_event_timed_u*>( p );
q->next = free_list;
free_list = q;
}
}
// ----------------------------------------------------------------------------
// CLASS : sc_event_list
//
// Base class for lists of events.
// ----------------------------------------------------------------------------
void
sc_event_list::push_back( const sc_event& e )
{
// make sure e is not already in the list
if ( m_events.size() != 0 ) {
const sc_event** l_events = &m_events[0];
for( int i = m_events.size() - 1; i >= 0; -- i ) {
if( &e == l_events[i] ) {
// event already in the list; ignore
return;
}
}
}
m_events.push_back( &e );
}
void
sc_event_list::push_back( const sc_event_list& el )
{
m_events.reserve( size() + el.size() );
for ( int i = el.m_events.size() - 1; i >= 0; --i )
{
push_back( *el.m_events[i] );
}
el.auto_delete();
}
void
sc_event_list::add_dynamic( sc_method_handle method_h ) const
{
m_busy++;
if ( m_events.size() != 0 ) {
const sc_event* const * l_events = &m_events[0];
for( int i = m_events.size() - 1; i >= 0; -- i ) {
l_events[i]->add_dynamic( method_h );
}
}
}
void
sc_event_list::add_dynamic( sc_thread_handle thread_h ) const
{
m_busy++;
if ( m_events.size() != 0 ) {
const sc_event* const* l_events = &m_events[0];
for( int i = m_events.size() - 1; i >= 0; -- i ) {
l_events[i]->add_dynamic( thread_h );
}
}
}
void
sc_event_list::remove_dynamic( sc_method_handle method_h,
const sc_event* e_not ) const
{
if ( m_events.size() != 0 ) {
const sc_event* const* l_events = &m_events[0];
for( int i = m_events.size() - 1; i >= 0; -- i ) {
const sc_event* e = l_events[i];
if( e != e_not ) {
e->remove_dynamic( method_h );
}
}
}
}
void
sc_event_list::remove_dynamic( sc_thread_handle thread_h,
const sc_event* e_not ) const
{
if ( m_events.size() != 0 ) {
const sc_event* const* l_events = &m_events[0];
for( int i = m_events.size() - 1; i >= 0; -- i ) {
const sc_event* e = l_events[i];
if( e != e_not ) {
e->remove_dynamic( thread_h );
}
}
}
}
void
sc_event_list::report_premature_destruction() const
{
// TDB: reliably detect premature destruction
//
// If an event list is used as a member of a module,
// its lifetime may (correctly) end, although there
// are processes currently waiting for it.
//
// Detecting (and ignoring) this corner-case is quite
// difficult for similar reasons to the sc_is_running()
// return value during the destruction of the module
// hierarchy.
//
// Ignoring the lifetime checks for now, if no process
// is currently running (which is only part of the story):
if( sc_get_current_process_handle().valid() ) {
// FIXME: improve error-handling
sc_assert( false && "sc_event_list prematurely destroyed" );
}
}
void
sc_event_list::report_invalid_modification() const
{
// FIXME: improve error-handling
sc_assert( false && "sc_event_list modfied while being waited on" );
}
// ----------------------------------------------------------------------------
// Deprecated functional notation for notifying events.
// ----------------------------------------------------------------------------
static void sc_warn_notify()
{
static bool warn_notify=true;
if ( warn_notify )
{
SC_REPORT_INFO(SC_ID_IEEE_1666_DEPRECATION_,
"the notify() function is deprecated use sc_event::notify()" );
warn_notify = false;
}
}
void
notify( sc_event& e )
{
sc_warn_notify();
e.notify();
}
void
notify( const sc_time& t, sc_event& e )
{
sc_warn_notify();
e.notify( t );
}
void
notify( double v, sc_time_unit tu, sc_event& e )
{
sc_warn_notify();
e.notify( v, tu );
}
} // namespace sc_core
// $Log: sc_event.cpp,v $
// Revision 1.17 2011/08/26 20:46:09 acg
// Andy Goodrich: moved the modification log to the end of the file to
// eliminate source line number skew when check-ins are done.
//
// Revision 1.16 2011/08/24 22:05:50 acg
// Torsten Maehne: initialization changes to remove warnings.
//
// Revision 1.15 2011/03/12 21:07:51 acg
// Andy Goodrich: changes to kernel generated event support.
//
// Revision 1.14 2011/03/06 15:55:52 acg
// Andy Goodrich: changes for named events.
//
// Revision 1.13 2011/03/05 01:39:21 acg
// Andy Goodrich: changes for named events.
//
// Revision 1.12 2011/02/19 08:33:25 acg
// Andy Goodrich: remove }'s that should have been removed before.
//
// Revision 1.11 2011/02/19 08:30:53 acg
// Andy Goodrich: Moved process queueing into trigger_static from
// sc_event::notify.
//
// Revision 1.10 2011/02/18 20:27:14 acg
// Andy Goodrich: Updated Copyrights.
//
// Revision 1.9 2011/02/17 19:49:51 acg
// Andy Goodrich:
// (1) Changed signature of trigger_dynamic() to return a bool again.
// (2) Moved process run queue processing into trigger_dynamic().
//
// Revision 1.8 2011/02/16 22:37:30 acg
// Andy Goodrich: clean up to remove need for ps_disable_pending.
//
// Revision 1.7 2011/02/13 21:47:37 acg
// Andy Goodrich: update copyright notice.
//
// Revision 1.6 2011/02/01 21:02:28 acg
// Andy Goodrich: new return code for trigger_dynamic() calls.
//
// Revision 1.5 2011/01/18 20:10:44 acg
// Andy Goodrich: changes for IEEE1666_2011 semantics.
//
// Revision 1.4 2011/01/06 18:04:05 acg
// Andy Goodrich: added code to leave disabled processes on the dynamic
// method and thread queues.
//
// Revision 1.3 2008/05/22 17:06:25 acg
// Andy Goodrich: updated copyright notice to include 2008.
//
// Revision 1.2 2007/01/17 22:44:30 acg
// Andy Goodrich: fix for Microsoft compiler.
//
// Revision 1.7 2006/04/11 23:13:20 acg
// Andy Goodrich: Changes for reduced reset support that only includes
// sc_cthread, but has preliminary hooks for expanding to method and thread
// processes also.
//
// Revision 1.6 2006/01/25 00:31:19 acg
// Andy Goodrich: Changed over to use a standard message id of
// SC_ID_IEEE_1666_DEPRECATION for all deprecation messages.
//
// Revision 1.5 2006/01/24 20:59:11 acg
// Andy Goodrich: fix up of CVS comments, new version roll.
//
// Revision 1.4 2006/01/24 20:48:14 acg
// Andy Goodrich: added deprecation warnings for notify_delayed(). Added two
// new implementation-dependent methods, notify_next_delta() & notify_internal()
// to replace calls to notify_delayed() from within the simulator. These two
// new methods are simpler than notify_delayed() and should speed up simulations
//
// Revision 1.3 2006/01/13 18:44:29 acg
// Added $Log to record CVS changes into the source.
// Taf!
|