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
|
//=============================================================================
/**
* @file Tokens_Test.cpp
*
* This application tests the ACE Token library including local
* and remote readers/writer and mutex locks, and token
* collections. This is accomplished with the ACE Token Invariant
* utilities that allow and application to check that
* readers/writer and mutex lock invariants are always satisfied.
* Throughout this test, ACE_TEST_ASSERTs are used in conjunction with
* Token Invariant operations, so that errors are reported using
* the ACE tests convention. This application performs a local
* test and then fork_execs a token server and performs the same
* test remotely.
*
* @author Tim Harrison <harrison@cs.wustl.edu>
*/
//=============================================================================
#include "test_config.h"
#include "ace/Process.h"
#include "ace/Get_Opt.h"
#include "ace/Local_Tokens.h"
#include "ace/Token_Collection.h"
#include "ace/Remote_Tokens.h"
#include "ace/Thread_Manager.h"
#include "ace/Token_Invariants.h"
#include "ace/Barrier.h"
#include "ace/OS_NS_unistd.h"
#if defined (ACE_HAS_THREADS) && defined (ACE_HAS_TOKENS_LIBRARY)
typedef ACE_Token_Invariant_Manager TOKEN_INVARIANTS;
static const ACE_TCHAR *server_host = ACE_DEFAULT_SERVER_HOST;
static const int server_port = 23456;
// Synchronize starts of threads, so that they all start before one
// has a chance to finish and clean up the TSS objects. To avoid
// creating a static object, it is dynamically allocated, before
// spawning any threads.
static ACE_Barrier *thread_start;
struct Test_Params
{
public:
ACE_Token_Proxy *token1_;
ACE_Token_Proxy *token2_;
const ACE_TCHAR *collection_name_;
};
static void *
run_thread (void *vp)
{
Test_Params *tp = (Test_Params *) vp;
ACE_Token_Collection collection (1, tp->collection_name_);
collection.insert (*(tp->token1_));
collection.insert (*(tp->token2_));
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("(%t) new thread.\n")));
thread_start->wait ();
int count = 50;
while (count--)
{
if (collection.acquire () == -1)
{
if (ACE_OS::last_error () == EDEADLK)
{
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("deadlock detected in acquire")));
continue;
}
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("(%t) %p acquire failed\n"),
ACE_TEXT ("run_thread")));
return (void *) -1;
}
ACE_TEST_ASSERT ((TOKEN_INVARIANTS::instance ()->acquired (tp->token1_) == 1) ||
(TOKEN_INVARIANTS::instance ()->acquired (tp->token2_) == 1));
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("(%t) %s acquired.\n"),
collection.name ()));
TOKEN_INVARIANTS::instance ()->releasing (tp->token1_);
TOKEN_INVARIANTS::instance ()->releasing (tp->token2_);
if (collection.renew () == -1)
{
if (ACE_OS::last_error () == EDEADLK)
{
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("deadlock detected")));
goto deadlock;
}
ACE_ERROR ((LM_ERROR, ACE_TEXT ("(%t) %p renew failed\n"),
ACE_TEXT ("run_thread")));
return (void *) -1;
}
ACE_TEST_ASSERT (TOKEN_INVARIANTS::instance ()->acquired (tp->token1_) == 1 ||
TOKEN_INVARIANTS::instance ()->acquired (tp->token2_) == 1);
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("(%t) %s renewed.\n"),
collection.name ()));
deadlock:
TOKEN_INVARIANTS::instance ()->releasing (tp->token1_);
TOKEN_INVARIANTS::instance ()->releasing (tp->token2_);
if (collection.release () == -1)
{
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("(%t) %p release failed\n"),
ACE_TEXT ("run_thread")));
return (void *) -1;
}
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("(%t) %s released.\n"),
collection.name ()));
}
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("(%t) thread finished.\n")));
return 0;
}
static int
run_test (ACE_Token_Proxy *A,
ACE_Token_Proxy *B,
ACE_Token_Proxy *R,
ACE_Token_Proxy *W)
{
// Parameters to be passed to the threads.
Test_Params tp1, tp2, tp3;
// tp1 and tp2 can run concurrently. Neither tp1 or tp3 can run
// when tp2 is running.
tp1.collection_name_ = ACE_TEXT ("A and Reader");
tp1.token1_ = A;
tp1.token2_ = R;
tp2.collection_name_ = ACE_TEXT ("A and Writer");
tp2.token1_ = A;
tp2.token2_ = W;
tp3.collection_name_ = ACE_TEXT ("B and Reader");
tp3.token1_ = B;
tp3.token2_ = R;
// Spawn off three threads.
ACE_Thread_Manager *mgr = ACE_Thread_Manager::instance ();
if (mgr->spawn (ACE_THR_FUNC (run_thread),
(void *) &tp1,
THR_BOUND) == -1)
ACE_ERROR_RETURN ((LM_DEBUG,
ACE_TEXT ("%p\n"),
ACE_TEXT ("spawn 1 failed")),
-1);
if (mgr->spawn (ACE_THR_FUNC (run_thread),
(void *) &tp2,
THR_BOUND) == -1)
ACE_ERROR_RETURN ((LM_DEBUG,
ACE_TEXT ("%p\n"),
ACE_TEXT ("spawn 2 failed")),
-1);
if (mgr->spawn (ACE_THR_FUNC (run_thread),
(void *) &tp3,
THR_BOUND) == -1)
ACE_ERROR_RETURN ((LM_DEBUG,
ACE_TEXT ("%p\n"),
ACE_TEXT ("spawn 3 failed")), -1);
// Wait for all threads to exit.
mgr->wait ();
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("Test finished.\n")));
return 0;
}
#endif /* ACE_HAS_THREADS && ACE_HAS_TOKENS_LIBRARY */
int
run_main (int, ACE_TCHAR *[])
{
ACE_START_TEST (ACE_TEXT ("Tokens_Test"));
#if defined (ACE_HAS_THREADS)
#if defined (ACE_HAS_TOKENS_LIBRARY)
ACE_Token_Proxy *A = 0, *B = 0, *R = 0, *W = 0;
ACE_NEW_RETURN (A,
ACE_Local_Mutex (ACE_TEXT ("L Mutex A"), 0, 0),
-1);
ACE_NEW_RETURN (B,
ACE_Local_Mutex (ACE_TEXT ("L Mutex B"), 0, 0),
-1);
ACE_NEW_RETURN (R,
ACE_Local_RLock (ACE_TEXT ("L Reader Lock"), 0, 0),
-1);
ACE_NEW_RETURN (W,
ACE_Local_WLock (ACE_TEXT ("L Writer Lock"), 0, 0),
-1);
ACE_NEW_RETURN (thread_start,
ACE_Barrier (3),
-1);
run_test (A, B, R, W);
const ACE_TCHAR *token_exe =
ACE_TEXT ("..") ACE_DIRECTORY_SEPARATOR_STR
ACE_TEXT ("netsvcs") ACE_DIRECTORY_SEPARATOR_STR
ACE_TEXT ("servers") ACE_DIRECTORY_SEPARATOR_STR
ACE_TEXT ("main") ACE_PLATFORM_EXE_SUFFIX;
int status = ACE_OS::access (token_exe, F_OK);
if (status == -1)
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("%p\n"),
token_exe));
else
{
const ACE_TCHAR *cl =
ACE_TEXT ("..") ACE_DIRECTORY_SEPARATOR_STR
ACE_TEXT ("netsvcs") ACE_DIRECTORY_SEPARATOR_STR
ACE_TEXT ("servers") ACE_DIRECTORY_SEPARATOR_STR
ACE_TEXT ("main") ACE_PLATFORM_EXE_SUFFIX
ACE_TEXT (" -f ") ACE_PLATFORM
ACE_TEXT ("tokens.conf");
ACE_Process_Options options;
options.command_line (cl);
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("Forking Token Service.\n")));
// Start up the token server for the remote test.
ACE_Process new_process;
if (new_process.spawn (options) == -1)
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("%n; %p (%s), will not run remote test.\n"),
ACE_TEXT ("Server fork failed"),
cl));
else
{
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("Server forked with pid = %d.\n"),
new_process.getpid ()));
// Wait for the server to start.
ACE_OS::sleep (3);
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("Using Token Server on %s at port %d.\n"),
server_host,
server_port));
ACE_Remote_Mutex::set_server_address (ACE_INET_Addr (server_port,
server_host));
delete A;
delete B;
delete R;
delete W;
ACE_NEW_RETURN (A,
ACE_Remote_Mutex (ACE_TEXT ("R Mutex A"), 0, 1),
-1);
ACE_NEW_RETURN (B,
ACE_Remote_Mutex (ACE_TEXT ("R Mutex B"), 0, 1),
-1);
ACE_NEW_RETURN (R,
ACE_Remote_RLock (ACE_TEXT ("R Reader Lock"), 0, 1),
-1);
ACE_NEW_RETURN (W,
ACE_Remote_WLock (ACE_TEXT ("R Writer Lock"), 0, 1),
-1);
run_test (A, B, R, W);
// Wait for the server to finish.
ACE_OS::sleep (3);
// Kill the token server.
if (new_process.terminate () == -1)
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("Kill failed.\n")),
-1);
}
delete thread_start;
thread_start = 0;
delete A;
delete B;
delete R;
delete W;
}
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("(%t) main thread exiting.\n")));
#else
ACE_ERROR ((LM_INFO,
ACE_TEXT ("ACE must be compiled with -DACE_HAS_TOKENS_LIBRARY to run this test\n")));
#endif /* ACE_HAS_TOKENS_LIBRARY */
#else
ACE_ERROR ((LM_INFO,
ACE_TEXT ("threads not supported on this platform\n")));
#endif /* ACE_HAS_THREADS */
ACE_END_TEST;
return 0;
}
|