1 #include "ace/Get_Opt.h"
2 #include "ace/Local_Tokens.h"
3 #include "ace/Remote_Tokens.h"
4 #include "ace/Thread_Manager.h"
6 #if defined (ACE_HAS_THREADS) && defined (ACE_HAS_THREADS_LIBRARY)
10 static ACE_Token_Proxy
*global_rlock
;
11 static ACE_Token_Proxy
*global_wlock
;
13 static char *server_host
= ACE_DEFAULT_SERVER_HOST
;
14 static int server_port
= ACE_DEFAULT_SERVER_PORT
;
15 static int ignore_deadlock
= 0;
16 static int threads
= 2;
17 static int iterations
= 50;
19 static int remote
= 0;
21 static int write_sleep
= 0;
26 for (int x
= 0; x
< iterations
; x
++)
29 for (; y
< reads
; y
++)
31 if (global_rlock
->acquire () == -1)
33 if (ACE_Log_Msg::instance ()->errnum () == EDEADLK
)
35 ACE_DEBUG ((LM_DEBUG
, "rlock deadlock detected\n"));
42 ACE_DEBUG ((LM_DEBUG
, "(%t) rlock acquired.\n"));
48 if (global_rlock
->release () == 0)
49 ACE_DEBUG ((LM_DEBUG
, "(%t) r-released.\n"));
52 if (global_wlock
->acquire () == -1)
54 ACE_DEBUG ((LM_DEBUG
, "wlock deadlock detected\n"));
60 ACE_DEBUG ((LM_DEBUG
, "\t\t(%t) wlock acquired.\n"));
61 if (global_wlock
->release () == 0)
62 ACE_DEBUG ((LM_DEBUG
, "\t\t(%t) w-released.\n"));
66 ACE_DEBUG ((LM_DEBUG
, "(%t) thread exiting.\n"));
71 parse_args (int argc
, ACE_TCHAR
*argv
[])
73 ACE_LOG_MSG
->open (argv
[0], ACE_Log_Msg::STDERR
); // | ACE_Log_Msg::VERBOSE);
75 ACE_Get_Opt
get_opt (argc
, argv
, ACE_TEXT("t:iun:drR:sp:h:"), 1);
77 for (int c
; (c
= get_opt ()) != -1; )
81 case 'h': // specify the host machine on which the server is running
82 server_host
= get_opt
.opt_arg ();
84 case 'p': // specify the port on which the server is running
85 server_port
= ACE_OS::atoi (get_opt
.opt_arg ());
88 threads
= ACE_OS::atoi (get_opt
.opt_arg ());
91 reads
= ACE_OS::atoi (get_opt
.opt_arg ());
103 iterations
= ACE_OS::atoi (get_opt
.opt_arg ());
109 // usage: fallthrough
111 ACE_ERROR_RETURN ((LM_ERROR
,
113 "[-i ignore deadlock]\n"
114 "[-n <iterations>]\n"
116 "[-r use remote locks]\n"
118 "[-s sleep during writes]\n"
119 "[-t <threads>\n", 1), -1);
128 ACE_TMAIN(int argc
, ACE_TCHAR
*argv
[])
130 if (parse_args (argc
, argv
) == -1)
135 ACE_Remote_Mutex::set_server_address (ACE_INET_Addr (server_port
, server_host
));
136 global_rlock
= (ACE_Token_Proxy
*) new
137 ACE_Remote_RLock ("THE_TOKEN", ignore_deadlock
, debug
);
138 global_wlock
= (ACE_Token_Proxy
*) new
139 ACE_Remote_WLock ("THE_TOKEN", ignore_deadlock
, debug
);
143 global_rlock
= (ACE_Token_Proxy
*) new
144 ACE_Local_RLock ("THE_TOKEN", ignore_deadlock
, debug
);
145 global_wlock
= (ACE_Token_Proxy
*) new
146 ACE_Local_WLock ("THE_TOKEN", ignore_deadlock
, debug
);
149 ACE_Thread_Manager mgr
;
151 if (mgr
.spawn_n (threads
, ACE_THR_FUNC (run_thread
),
153 THR_BOUND
| THR_SUSPENDED
) == -1)
154 ACE_ERROR_RETURN ((LM_DEBUG
, "%p\n", "spawn failed"), -1);
156 if (mgr
.resume_all () == -1)
157 ACE_ERROR_RETURN ((LM_DEBUG
, "%p\n", "resume failed"), -1);
166 ACE_TMAIN(int, ACE_TCHAR
*[])
168 ACE_ERROR_RETURN ((LM_ERROR
,
169 "threads not supported on this platform\n"), -1);
171 #endif /* ACE_HAS_THREADS */