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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
|
// ============================================================================
// $Id$
//
// = LIBRARY
// examples
//
// = FILENAME
// manual.cpp
//
// = DESCRIPTION
// Allows manual operations on local and remote tokens.
//
// = AUTHOR
// Tim Harrison
//
// ============================================================================
#include "ace/OS.h"
#include "ace/Get_Opt.h"
#include "ace/Local_Tokens.h"
#include "ace/Remote_Tokens.h"
#include "ace/Thread_Manager.h"
#include "ace/Token_Invariants.h"
#include "ace/Token_Collection.h"
#include "ace/Map_Manager.h"
#include "ace/Service_Config.h"
typedef ACE_Token_Invariant_Manager ACE_TOKEN_INVARIANTS;
class STDIN_Token : public ACE_Event_Handler
// = TITLE
// STDIN Token
//
// = DESCRIPTION
// Translates STDIN commands to ACE Token commands.
{
public:
STDIN_Token (void);
// Construction.
int parse_args (int argc, char *argv[]);
// Parse command-line arguments.
int open (int argc, char *argv[]);
// Register with whatever event dispatcher is needed and run.
// = Event_Handler methods.
int handle_input (ACE_HANDLE);
int handle_exception (ACE_HANDLE);
private:
void display_menu (void);
// Display options.
ACE_Token_Proxy *get_proxy (const char *tid, const char *token, char type);
// Get or make a proxy to <token> with a <tid> client id.
ACE_Token_Proxy *create_proxy (const char *token, char type);
// Create a proxy to <token> with a <tid> client id.
typedef ACE_CString TID;
// = Mapping from tid to Token_Collection.
typedef ACE_Map_Manager<TID, ACE_Token_Collection *, ACE_Null_Mutex>
COLLECTIONS;
// COLLECTION maintains a mapping from tid to a collection.
typedef ACE_Map_Iterator<TID, ACE_Token_Collection *, ACE_Null_Mutex>
COLLECTIONS_ITERATOR;
// Allows iterations through collections_.
typedef ACE_Map_Entry<TID, ACE_Token_Collection *>
COLLECTIONS_ENTRY;
// Allows iterations through collections_.
COLLECTIONS collections_;
// A collection for each <tid>.
char *server_host_;
int server_port_;
int ignore_deadlock_;
int debug_;
int remote_;
};
STDIN_Token::STDIN_Token (void)
: server_host_ (ACE_DEFAULT_SERVER_HOST),
server_port_ (ACE_DEFAULT_SERVER_PORT),
ignore_deadlock_ (0),
debug_ (0),
remote_ (0)
{
}
int
STDIN_Token::parse_args (int argc, char *argv[])
{
ACE_LOG_MSG->open (argv[0], ACE_Log_Msg::STDERR);
ACE_Get_Opt get_opt (argc, argv, "h:p:diu", 1);
for (int c; (c = get_opt ()) != -1; )
{
switch (c)
{
case 'h': // specify the host machine on which the server is running
server_host_ = get_opt.optarg;
remote_ = 1;
break;
case 'p': // specify the port on which the server is running
server_port_ = ACE_OS::atoi (get_opt.optarg);
remote_ = 1;
break;
case 'd':
debug_ = 1;
break;
case 'i':
ignore_deadlock_ = 1;
break;
case 'u':
// usage: fallthrough
default:
ACE_ERROR_RETURN ((LM_ERROR,
"%n:\n"
"[-h <remote host>]\n"
"[-p <remote port>]\n"
"[-i ignore deadlock]\n"
"[-d debug]\n", 1), -1);
break;
}
}
if (remote_)
ACE_Remote_Mutex::set_server_address (ACE_INET_Addr (server_port_, server_host_));
return 0;
}
int
STDIN_Token::open (int argc, char *argv[])
{
if (this->parse_args (argc, argv) == -1)
return -1;
// Register for signals.
if (ACE_Service_Config::reactor ()->register_handler
(SIGINT, this) == -1)
ACE_DEBUG ((LM_DEBUG, "Can't register signal handler\n"));
#if (ACE_WIN32)
#else
// Register for STDIN events with Reactor.
if (ACE_Service_Config::reactor ()->register_handler
(ACE_STDIN, this, ACE_Event_Handler::READ_MASK) == -1)
ACE_ERROR_RETURN ((LM_DEBUG, "Can't register signal handler\n"), 0);
#endif /* ACE_WIN32 */
this->display_menu ();
#if (ACE_WIN32)
#else
ACE_Service_Config::run_reactor_event_loop ();
#endif /* ACE_WIN32 */
ACE_OS::printf ("Exiting...\n");
return 0;
}
int
STDIN_Token::handle_input (ACE_HANDLE fd)
{
char tid[BUFSIZ];
char token[BUFSIZ];
char type[16];
char operation[16];
if (::scanf ("%s %s %s %s", tid, token, type, operation) <= 0)
{
ACE_OS::printf ("Try again.\n");
return 0;
}
ACE_Token_Proxy *proxy =
this->get_proxy (tid, token, type[0]);
if (proxy == 0)
return -1;
switch (operation[0])
{
case 'a':
case 'A':
if (proxy->acquire () == 0)
{
ACE_OS::printf ("Succeeded.\n");
if (ACE_TOKEN_INVARIANTS::instance ()->acquired (proxy) == 0)
ACE_OS::printf ("Violated invariant.\n");
}
else
ACE_ERROR ((LM_ERROR, "%p.\n", "Acquire failed"));
break;
case 'n':
case 'N':
ACE_TOKEN_INVARIANTS::instance ()->releasing (proxy);
if (proxy->renew () == 0)
{
ACE_OS::printf ("Succeeded.\n");
if (ACE_TOKEN_INVARIANTS::instance ()->acquired (proxy) == 0)
ACE_OS::printf ("Violated invariant.\n");
}
else
ACE_ERROR ((LM_ERROR, "%p.\n", "Renew failed"));
break;
case 'r':
case 'R':
ACE_TOKEN_INVARIANTS::instance ()->releasing (proxy);
if (proxy->release () == 0)
ACE_OS::printf ("Succeeded.\n");
else
ACE_ERROR ((LM_ERROR, "%p.\n", "Release failed"));
break;
case 't':
case 'T':
if (proxy->tryacquire () == 0)
{
ACE_OS::printf ("Succeeded.\n");
if (ACE_TOKEN_INVARIANTS::instance ()->acquired (proxy) == 0)
ACE_OS::printf ("Violated invariant.\n");
}
else
ACE_ERROR ((LM_ERROR, "%p.\n", "Tryacquire failed"));
break;
}
this->display_menu ();
return 0;
}
void
STDIN_Token::display_menu (void)
{
ACE_OS::printf ("<tid> <token> <type> <operation>\n");
}
int
STDIN_Token::handle_exception (ACE_HANDLE fd)
{
ACE_Service_Config::run_reactor_event_loop ();
return -1;
}
ACE_Token_Proxy *
STDIN_Token::get_proxy (const char *_tid, const char *token, char type)
{
ACE_Token_Collection *proxy_collection;
TID tid (_tid);
if (collections_.find (tid, proxy_collection) == -1)
// We did not find a proxy_collection.
{
// Make one.
proxy_collection = new ACE_Token_Collection (debug_, "no name collection");
// Put it in the collections.
if (collections_.bind (tid, proxy_collection) == -1)
{
delete proxy_collection;
return 0;
}
}
// Either way, we have a proxy_collection now.
// See if the proxy already exists in the collection.
ACE_Token_Proxy *proxy = proxy_collection->is_member (token);
// If not, create one.
if (proxy == 0)
{
proxy = this->create_proxy (token, type);
// Put the new_proxy in this tid's collection.
if (proxy_collection->insert (*proxy) == -1)
ACE_ERROR_RETURN ((LM_ERROR, "insert failed\n"), 0);
// Delete our copy (one was created in the collection).
delete proxy;
proxy = proxy_collection->is_member (token);
if (proxy == 0)
ACE_ERROR_RETURN ((LM_ERROR, "is_member failed\n"), 0);
// Set the client_id (it was set to 1 since we're
// single-threaded.
proxy->client_id (_tid);
}
return proxy;
}
ACE_Token_Proxy *
STDIN_Token::create_proxy (const char *token, char type)
{
switch (type)
{
case 'm':
case 'M':
if (remote_)
return new ACE_Remote_Mutex (token, ignore_deadlock_, debug_);
else
return new ACE_Local_Mutex (token, ignore_deadlock_, debug_);
case 'r':
case 'R':
if (remote_)
return new ACE_Remote_RLock (token, ignore_deadlock_, debug_);
else
return new ACE_Local_RLock (token, ignore_deadlock_, debug_);
case 'w':
case 'W':
if (remote_)
return new ACE_Remote_WLock (token, ignore_deadlock_, debug_);
else
return new ACE_Local_WLock (token, ignore_deadlock_, debug_);
}
}
int
main (int argc, char* argv[])
{
STDIN_Token st;
return st.open (argc, argv);
}
|