Changes to attempt to silence bcc64x
[ACE_TAO.git] / ACE / netsvcs / clients / Tokens / invariant / invariant.cpp
blob47da7c085b6f4a24e0ef3fe0799f613364c75fa9
2 //=============================================================================
3 /**
4 * @file invariant.cpp
6 * @author Tim Harrison
7 */
8 //=============================================================================
10 #include "ace/Get_Opt.h"
11 #include "ace/Singleton.h"
12 #include "ace/Thread_Manager.h"
13 #include "ace/Token_Invariants.h"
15 #if defined (ACE_HAS_THREADS) && defined (ACE_HAS_THREADS_LIBRARY)
17 typedef ACE_Token_Invariant_Manager ACE_TOKEN_INVARIANTS;
19 static const char *rwname = "reader/writer";
20 static const char *mutexname = "mutex";
22 static void *
23 run_reader_writer (void *)
25 for (int x = 0; x < 50; x++)
27 int y = 0;
28 for (; y < 5; y++)
30 if (ACE_TOKEN_INVARIANTS::instance ()->reader_acquired (rwname) == 0)
31 ACE_ERROR_RETURN ((LM_ERROR, "reader acquire violated invariant.\n"), 0);
33 ACE_DEBUG ((LM_DEBUG, "(%t) rlock acquired.\n"));
36 ACE_TOKEN_INVARIANTS::instance ()->rwlock_releasing (rwname);
38 if (ACE_TOKEN_INVARIANTS::instance ()->reader_acquired (rwname) == 0)
39 ACE_ERROR_RETURN ((LM_ERROR, "reader renew violated invariant.\n"), 0);
41 ACE_DEBUG ((LM_DEBUG, "(%t) rlock renewed.\n"));
43 for (; y > 0; y--)
45 ACE_TOKEN_INVARIANTS::instance ()->rwlock_releasing (rwname);
46 ACE_DEBUG ((LM_DEBUG, "(%t) r-released.\n"));
49 if (ACE_TOKEN_INVARIANTS::instance ()->writer_acquired (rwname) == 0)
50 ACE_ERROR_RETURN ((LM_ERROR, "writer acquire violated invariant.\n"), 0);
52 ACE_DEBUG ((LM_DEBUG, "\t\t(%t) wlock acquired.\n"));
54 ACE_TOKEN_INVARIANTS::instance ()->rwlock_releasing (rwname);
56 if (ACE_TOKEN_INVARIANTS::instance ()->writer_acquired (rwname) == 0)
57 ACE_ERROR_RETURN ((LM_ERROR, "writer renew violated invariant.\n"), 0);
59 ACE_DEBUG ((LM_DEBUG, "(%t) rlock renewed.\n"));
61 ACE_TOKEN_INVARIANTS::instance ()->rwlock_releasing (rwname);
64 ACE_DEBUG ((LM_DEBUG, "(%t) thread exiting.\n"));
65 return 0;
68 static void *
69 run_mutex (void *)
71 for (int x = 0; x < 50; x++)
73 if (ACE_TOKEN_INVARIANTS::instance ()->mutex_acquired (mutexname) == 0)
74 ACE_ERROR_RETURN ((LM_ERROR, "mutex acquire violated invariant.\n"), 0);
76 ACE_DEBUG ((LM_DEBUG, "(%t) mutex acquired.\n"));
78 ACE_TOKEN_INVARIANTS::instance ()->mutex_releasing (mutexname);
80 if (ACE_TOKEN_INVARIANTS::instance ()->mutex_acquired (mutexname) == 0)
81 ACE_ERROR_RETURN ((LM_ERROR, "mutex renew violated invariant.\n"), 0);
83 ACE_DEBUG ((LM_DEBUG, "(%t) mutex renewed.\n"));
85 ACE_TOKEN_INVARIANTS::instance ()->mutex_releasing (mutexname);
86 ACE_DEBUG ((LM_DEBUG, "(%t) mutex released.\n"));
89 ACE_DEBUG ((LM_DEBUG, "(%t) thread exiting.\n"));
90 return 0;
93 static int
94 run_final_test ()
96 ACE_DEBUG ((LM_DEBUG, "starting mutex tests 1 & 2\n"));
98 // Mutex tests.
99 if (ACE_TOKEN_INVARIANTS::instance ()->mutex_acquired ("testing mutex") == 0)
100 ACE_ERROR_RETURN ((LM_ERROR, "mutex test 1 failed.\n"), 0);
101 if (ACE_TOKEN_INVARIANTS::instance ()->mutex_acquired ("testing mutex2") == 0)
102 ACE_ERROR_RETURN ((LM_ERROR, "mutex test 2 failed.\n"), 0);
103 if (ACE_TOKEN_INVARIANTS::instance ()->mutex_acquired ("testing mutex") == 0)
104 ACE_DEBUG ((LM_DEBUG, "mutex test 1 succeeded.\n"));
105 else
106 ACE_ERROR_RETURN ((LM_ERROR, "mutex test 1 failed..\n"), 0);
108 if (ACE_TOKEN_INVARIANTS::instance ()->mutex_acquired ("testing mutex2") == 0)
109 ACE_DEBUG ((LM_DEBUG, "mutex test 2 succeeded.\n"));
110 else
111 ACE_ERROR_RETURN ((LM_ERROR, "mutex test 2 failed..\n"), 0);
113 // RW tests.
114 ACE_DEBUG ((LM_DEBUG, "starting rwlock tests 1 & 2\n"));
116 // Multiple readers.
117 if (ACE_TOKEN_INVARIANTS::instance ()->reader_acquired ("testing rwlock") == 0)
118 ACE_ERROR_RETURN ((LM_ERROR, "rwlock test 1 failed.\n"), 0);
119 if (ACE_TOKEN_INVARIANTS::instance ()->reader_acquired ("testing rwlock 2") == 0)
120 ACE_ERROR_RETURN ((LM_ERROR, "rwlock test 2 failed.\n"), 0);
121 if (ACE_TOKEN_INVARIANTS::instance ()->reader_acquired ("testing rwlock") == 0)
122 ACE_ERROR_RETURN ((LM_ERROR, "rwlock test 1 failed..\n"), 0);
123 if (ACE_TOKEN_INVARIANTS::instance ()->reader_acquired ("testing rwlock 2") == 0)
124 ACE_ERROR_RETURN ((LM_ERROR, "rwlock test 2 failed..\n"), 0);
126 // Writer.
127 if (ACE_TOKEN_INVARIANTS::instance ()->writer_acquired ("testing rwlock") == 0)
128 ACE_DEBUG ((LM_ERROR, "rwlock test 1 succeded.\n"));
129 else
130 ACE_ERROR_RETURN ((LM_ERROR, "rwlock test 1 failed...\n"), 0);
132 // Releasing reader.
133 ACE_TOKEN_INVARIANTS::instance ()->rwlock_releasing ("testing rwlock 2");
134 ACE_TOKEN_INVARIANTS::instance ()->rwlock_releasing ("testing rwlock 2");
136 // Writer.
137 if (ACE_TOKEN_INVARIANTS::instance ()->writer_acquired ("testing rwlock 2") == 0)
138 ACE_ERROR_RETURN ((LM_ERROR, "rwlock test 2 failed....\n"), 0);
140 // Reader.
141 if (ACE_TOKEN_INVARIANTS::instance ()->reader_acquired ("testing rwlock 2") == 0)
142 ACE_DEBUG ((LM_DEBUG, "rwlock test 2 succeeded.\n"));
143 else
144 ACE_ERROR_RETURN ((LM_ERROR, "rwlock test 2 failed.....\n"), 0);
146 return 0;
150 ACE_TMAIN(int, ACE_TCHAR *[])
152 ACE_Thread_Manager mgr;
154 // Run reader/writer test
155 if (mgr.spawn_n (2, ACE_THR_FUNC (run_reader_writer),
156 (void *) 0,
157 THR_NEW_LWP | THR_DETACHED) == -1)
158 ACE_ERROR_RETURN ((LM_DEBUG, "%p\n", "spawn failed"), -1);
160 mgr.wait ();
162 ACE_OS::sleep (2);
164 // Run mutex test.
165 if (mgr.spawn_n (2, ACE_THR_FUNC (run_mutex),
166 (void *) 0,
167 THR_NEW_LWP | THR_DETACHED) == -1)
168 ACE_ERROR_RETURN ((LM_DEBUG, "%p\n", "spawn failed"), -1);
170 mgr.wait ();
172 ACE_OS::sleep (2);
174 run_final_test ();
176 return 0;
179 #else
181 ACE_TMAIN(int, ACE_TCHAR *[])
183 ACE_ERROR_RETURN ((LM_ERROR,
184 "threads not supported on this platform\n"), -1);
186 #endif /* ACE_HAS_THREADS */