test02.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 test02.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_if interface -- mutex case 39 40#include "systemc.h" 41 42SC_MODULE( mod_a ) 43{ 44 sc_port<sc_semaphore_if> 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 SC_CTOR( mod_a ) 79 { 80 SC_THREAD( proc_a ); 81 } 82}; 83 84SC_MODULE( mod_b ) 85{ 86 sc_port<sc_semaphore_if> semaphore; 87 88 void write( const char* msg ) 89 { 90 cout << sc_time_stamp() << " " << msg << endl; 91 } 92 93 void proc_b() 94 { 95 while( true ) { 96 wait( 2, SC_NS ); 97 write( "proc_b - lock requested" ); 98 semaphore->wait(); 99 write( "proc_b - lock obtained" ); 100 wait( 4, SC_NS ); 101 if( semaphore->post() == 0 ) { 102 write( "proc_b - unlock successful" ); 103 } else { 104 write( "proc_b - unlock failed" ); 105 } 106 wait( 3, SC_NS ); 107 if( semaphore->trywait() == 0 ) { 108 write( "proc_b - trylock successful" ); 109 } else { 110 write( "proc_b - trylock failed" ); 111 } 112 if( semaphore->post() == 0 ) { 113 write( "proc_b - unlock successful" ); 114 } else { 115 write( "proc_b - unlock failed" ); 116 } 117 } 118 } 119 120 SC_CTOR( mod_b ) 121 { 122 SC_THREAD( proc_b ); 123 } 124}; 125 126int 127sc_main( int, char*[] ) 128{ 129 mod_a a( "a" ); 130 mod_b b( "b" ); 131 sc_semaphore semaphore( "semaphore", 1 ); 132 133 a.semaphore( semaphore ); 134 b.semaphore( semaphore ); 135 136 sc_start( 40, SC_NS ); 137 138 return 0; 139} 140