2 //=============================================================================
6 * test_rw_locks shows how to use ACE_Local_RLock, ACE_Local_WLock,
7 * ACE_Remote_RLock, and ACE_Remote_WLock.
11 //=============================================================================
14 #include "ace/Get_Opt.h"
15 #include "ace/Local_Tokens.h"
16 #include "ace/Remote_Tokens.h"
17 #include "ace/Thread_Manager.h"
18 #include "ace/Token_Invariants.h"
20 #if defined (ACE_HAS_THREADS) && defined (ACE_HAS_THREADS_LIBRARY)
23 typedef ACE_Token_Invariant_Manager ACE_TOKEN_INVARIANTS
;
25 static ACE_Token_Proxy
*global_rlock
;
26 static ACE_Token_Proxy
*global_wlock
;
28 static const char *server_host
= ACE_DEFAULT_SERVER_HOST
;
29 static int server_port
= ACE_DEFAULT_SERVER_PORT
;
30 static int ignore_deadlock
= 0;
31 static int threads
= 2;
32 static int iterations
= 50;
34 static int remote
= 0;
36 static int write_sleep
= 0;
42 for (int x
= 0; x
< iterations
; x
++)
45 for (; y
< reads
; y
++)
47 if (global_rlock
->acquire () == -1)
49 if (ACE_Log_Msg::instance ()->errnum () == EDEADLK
)
51 ACE_DEBUG ((LM_DEBUG
, "rlock deadlock detected\n"));
58 if (ACE_TOKEN_INVARIANTS::instance ()->acquired (global_rlock
) == 0)
59 ACE_ERROR_RETURN ((LM_ERROR
, "reader acquire violated invariant.\n"), 0);
61 ACE_DEBUG ((LM_DEBUG
, "(%t) rlock acquired.\n"));
66 ACE_TOKEN_INVARIANTS::instance ()->releasing (global_rlock
);
68 if (global_rlock
->renew () == -1)
70 if (ACE_Log_Msg::instance ()->errnum () == EDEADLK
)
72 ACE_DEBUG ((LM_DEBUG
, "rlock deadlock detected during renew\n"));
79 ACE_DEBUG ((LM_DEBUG
, "(%t) rlock renewed.\n"));
81 if (ACE_TOKEN_INVARIANTS::instance ()->acquired (global_rlock
) == 0)
82 ACE_ERROR_RETURN ((LM_ERROR
, "reader renew violated invariant.\n"), 0);
89 ACE_TOKEN_INVARIANTS::instance ()->releasing (global_rlock
);
90 if (global_rlock
->release () == 0)
91 ACE_DEBUG ((LM_DEBUG
, "(%t) r-released.\n"));
94 if (global_wlock
->acquire () == -1)
95 ACE_DEBUG ((LM_DEBUG
, "wlock deadlock detected\n"));
100 ACE_DEBUG ((LM_DEBUG
, "\t\t(%t) wlock acquired.\n"));
102 if (ACE_TOKEN_INVARIANTS::instance ()->acquired (global_wlock
) == 0)
103 ACE_ERROR_RETURN ((LM_ERROR
, "writer acquire violated invariant.\n"), 0);
107 ACE_TOKEN_INVARIANTS::instance ()->releasing (global_wlock
);
109 if (global_wlock
->renew () == -1)
111 if (ACE_Log_Msg::instance ()->errnum () == EDEADLK
)
113 ACE_DEBUG ((LM_DEBUG
, "wlock deadlock detected during renew\n"));
119 ACE_DEBUG ((LM_DEBUG
, "(%t) rlock renewed.\n"));
121 if (ACE_TOKEN_INVARIANTS::instance ()->acquired (global_wlock
) == 0)
122 ACE_ERROR_RETURN ((LM_ERROR
, "writer renew violated invariant.\n"), 0);
125 ACE_TOKEN_INVARIANTS::instance ()->releasing (global_wlock
);
127 if (global_wlock
->release () == 0)
128 ACE_DEBUG ((LM_DEBUG
, "\t\t(%t) w-released.\n"));
132 ACE_DEBUG ((LM_DEBUG
, "(%t) thread exiting.\n"));
137 parse_args (int argc
, ACE_TCHAR
*argv
[])
139 ACE_LOG_MSG
->open (argv
[0], ACE_Log_Msg::STDERR
); // | ACE_Log_Msg::VERBOSE);
141 ACE_Get_Opt
get_opt (argc
, argv
, ACE_TEXT("t:iun:dr:sp:h:R"), 1);
143 for (int c
; (c
= get_opt ()) != -1; )
147 case 'h': // specify the host machine on which the server is running
148 server_host
= get_opt
.opt_arg ();
151 case 'p': // specify the port on which the server is running
152 server_port
= ACE_OS::atoi (get_opt
.opt_arg ());
156 threads
= ACE_OS::atoi (get_opt
.opt_arg ());
162 reads
= ACE_OS::atoi (get_opt
.opt_arg ());
171 iterations
= ACE_OS::atoi (get_opt
.opt_arg ());
177 // usage same as unknown.
180 ACE_ERROR_RETURN ((LM_ERROR
,
182 "[-h <remote host>]\n"
183 "[-p <remote port>]\n"
184 "[-i ignore deadlock]\n"
185 "[-n <iterations>]\n"
186 "[-R perform renews]\n"
189 "[-s sleep during writes]\n"
190 "[-t <threads>\n", 1), -1);
197 #if defined (ACE_HAS_PTHREADS)
200 #define SUSPEND THR_SUSPENDED
204 ACE_TMAIN(int argc
, ACE_TCHAR
*argv
[])
206 if (parse_args (argc
, argv
) == -1)
211 ACE_Remote_Mutex::set_server_address (ACE_INET_Addr (server_port
, server_host
));
212 global_rlock
= (ACE_Token_Proxy
*) new
213 ACE_Remote_RLock ("THE_TOKEN", ignore_deadlock
, debug
);
214 global_wlock
= (ACE_Token_Proxy
*) new
215 ACE_Remote_WLock ("THE_TOKEN", ignore_deadlock
, debug
);
219 global_rlock
= (ACE_Token_Proxy
*) new
220 ACE_Local_RLock ("THE_TOKEN", ignore_deadlock
, debug
);
221 global_wlock
= (ACE_Token_Proxy
*) new
222 ACE_Local_WLock ("THE_TOKEN", ignore_deadlock
, debug
);
225 ACE_Thread_Manager mgr
;
227 if (mgr
.spawn_n (threads
, ACE_THR_FUNC (run_thread
),
229 THR_BOUND
| SUSPEND
) == -1)
230 ACE_ERROR_RETURN ((LM_DEBUG
, "%p\n", "spawn failed"), -1);
232 #if ! defined (ACE_HAS_PTHREADS)
233 if (mgr
.resume_all () == -1)
234 ACE_ERROR_RETURN ((LM_DEBUG
, "%p\n", "resume failed"), -1);
244 ACE_TMAIN(int, ACE_TCHAR
*[])
246 ACE_ERROR_RETURN ((LM_ERROR
,
247 "threads not supported on this platform\n"), -1);
249 #endif /* ACE_HAS_THREADS */