test01.cpp revision 12855:588919e0e4aa
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  test01.cpp --
23
24  Original Author: Martin Janssen, Synopsys, Inc., 2002-02-15
25
26 *****************************************************************************/
27
28/*****************************************************************************
29
30  MODIFICATION LOG - modifiers, enter your name, affiliation, date and
31  changes you are making here.
32
33      Name, Affiliation, Date:
34  Description of Modification:
35
36 *****************************************************************************/
37
38// test of the sc_semaphore primitive channel -- mutex case
39
40#include "systemc.h"
41
42SC_MODULE( mod_a )
43{
44    sc_semaphore semaphore;
45
46    void write( const char* msg )
47    {
48        cout << sc_time_stamp() << " " << msg << endl;
49    }
50
51    void proc_a()
52    {
53        while( true ) {
54            wait( 1, SC_NS );
55            write( "proc_a - lock requested" );
56            semaphore.wait();
57            write( "proc_a - lock obtained" );
58            wait( 2, SC_NS );
59            if( semaphore.post() == 0 ) {
60                write( "proc_a - unlock successful" );
61            } else {
62                write( "proc_a - unlock failed" );
63            }
64            wait( 3, SC_NS );
65            if( semaphore.trywait() == 0 ) {
66                write( "proc_a - trylock successful" );
67            } else {
68                write( "proc_a - trylock failed" );
69            }
70            if( semaphore.post() == 0 ) {
71                write( "proc_a - unlock successful" );
72            } else {
73                write( "proc_a - unlock failed" );
74            }
75        }
76    }
77
78    void proc_b()
79    {
80        while( true ) {
81            wait( 2, SC_NS );
82            write( "proc_b - lock requested" );
83            semaphore.wait();
84            write( "proc_b - lock obtained" );
85            wait( 4, SC_NS );
86            if( semaphore.post() == 0 ) {
87                write( "proc_b - unlock successful" );
88            } else {
89                write( "proc_b - unlock failed" );
90            }
91            wait( 3, SC_NS );
92            if( semaphore.trywait() == 0 ) {
93                write( "proc_b - trylock successful" );
94            } else {
95                write( "proc_b - trylock failed" );
96            }
97            if( semaphore.post() == 0 ) {
98                write( "proc_b - unlock successful" );
99            } else {
100                write( "proc_b - unlock failed" );
101            }
102        }
103    }
104
105    SC_CTOR( mod_a )
106    : semaphore( 1 )
107    {
108        SC_THREAD( proc_a );
109        SC_THREAD( proc_b );
110    }
111};
112
113int
114sc_main( int, char*[] )
115{
116    mod_a a( "a" );
117
118    sc_start( 40, SC_NS );
119
120    return 0;
121}
122