2 //=============================================================================
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";
23 run_reader_writer (void *)
25 for (int x
= 0; x
< 50; x
++)
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"));
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"));
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"));
96 ACE_DEBUG ((LM_DEBUG
, "starting mutex tests 1 & 2\n"));
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"));
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"));
111 ACE_ERROR_RETURN ((LM_ERROR
, "mutex test 2 failed..\n"), 0);
114 ACE_DEBUG ((LM_DEBUG
, "starting rwlock tests 1 & 2\n"));
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);
127 if (ACE_TOKEN_INVARIANTS::instance ()->writer_acquired ("testing rwlock") == 0)
128 ACE_DEBUG ((LM_ERROR
, "rwlock test 1 succeded.\n"));
130 ACE_ERROR_RETURN ((LM_ERROR
, "rwlock test 1 failed...\n"), 0);
133 ACE_TOKEN_INVARIANTS::instance ()->rwlock_releasing ("testing rwlock 2");
134 ACE_TOKEN_INVARIANTS::instance ()->rwlock_releasing ("testing rwlock 2");
137 if (ACE_TOKEN_INVARIANTS::instance ()->writer_acquired ("testing rwlock 2") == 0)
138 ACE_ERROR_RETURN ((LM_ERROR
, "rwlock test 2 failed....\n"), 0);
141 if (ACE_TOKEN_INVARIANTS::instance ()->reader_acquired ("testing rwlock 2") == 0)
142 ACE_DEBUG ((LM_DEBUG
, "rwlock test 2 succeeded.\n"));
144 ACE_ERROR_RETURN ((LM_ERROR
, "rwlock test 2 failed.....\n"), 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
),
157 THR_NEW_LWP
| THR_DETACHED
) == -1)
158 ACE_ERROR_RETURN ((LM_DEBUG
, "%p\n", "spawn failed"), -1);
165 if (mgr
.spawn_n (2, ACE_THR_FUNC (run_mutex
),
167 THR_NEW_LWP
| THR_DETACHED
) == -1)
168 ACE_ERROR_RETURN ((LM_DEBUG
, "%p\n", "spawn failed"), -1);
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 */