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)
9 static ACE_Token_Proxy
*global_rlock
;
10 static ACE_Token_Proxy
*global_wlock
;
12 static char *server_host
= ACE_DEFAULT_SERVER_HOST
;
13 static int server_port
= ACE_DEFAULT_SERVER_PORT
;
14 static int ignore_deadlock
= 0;
15 static int threads
= 2;
16 static int iterations
= 50;
18 static int remote
= 0;
20 static int write_sleep
= 0;
25 for (int x
= 0; x
< iterations
; x
++)
28 for (; y
< reads
; y
++)
30 if (global_rlock
->acquire () == -1)
32 if (ACE_Log_Msg::instance ()->errnum () == EDEADLK
)
34 ACE_DEBUG ((LM_DEBUG
, "rlock deadlock detected\n"));
41 ACE_DEBUG ((LM_DEBUG
, "(%t) rlock acquired.\n"));
47 if (global_rlock
->release () == 0)
48 ACE_DEBUG ((LM_DEBUG
, "(%t) r-released.\n"));
51 if (global_wlock
->acquire () == -1)
53 ACE_DEBUG ((LM_DEBUG
, "wlock deadlock detected\n"));
59 ACE_DEBUG ((LM_DEBUG
, "\t\t(%t) wlock acquired.\n"));
60 if (global_wlock
->release () == 0)
61 ACE_DEBUG ((LM_DEBUG
, "\t\t(%t) w-released.\n"));
65 ACE_DEBUG ((LM_DEBUG
, "(%t) thread exiting.\n"));
70 parse_args (int argc
, ACE_TCHAR
*argv
[])
72 ACE_LOG_MSG
->open (argv
[0], ACE_Log_Msg::STDERR
); // | ACE_Log_Msg::VERBOSE);
74 ACE_Get_Opt
get_opt (argc
, argv
, ACE_TEXT("t:iun:drR:sp:h:"), 1);
76 for (int c
; (c
= get_opt ()) != -1; )
80 case 'h': // specify the host machine on which the server is running
81 server_host
= get_opt
.opt_arg ();
83 case 'p': // specify the port on which the server is running
84 server_port
= ACE_OS::atoi (get_opt
.opt_arg ());
87 threads
= ACE_OS::atoi (get_opt
.opt_arg ());
90 reads
= ACE_OS::atoi (get_opt
.opt_arg ());
102 iterations
= ACE_OS::atoi (get_opt
.opt_arg ());
108 // usage same as unknown.
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 */