Merge pull request #2316 from jwillemsen/jwi-taskcommenttypo
[ACE_TAO.git] / ACE / netsvcs / clients / Tokens / rw_lock / rw_locks.cpp
blobfd1b2797d1232df2b832763b648e4b5b87e76ba4
2 //=============================================================================
3 /**
4 * @file rw_locks.cpp
6 * test_rw_locks shows how to use ACE_Local_RLock, ACE_Local_WLock,
7 * ACE_Remote_RLock, and ACE_Remote_WLock.
9 * @author Tim Harrison
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;
33 static int debug = 0;
34 static int remote = 0;
35 static int reads = 4;
36 static int write_sleep = 0;
37 static int renew = 0;
39 static void *
40 run_thread (void *)
42 for (int x = 0; x < iterations; x++)
44 int y = 0;
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"));
52 goto READ_DEADLOCK;
54 else
55 return 0;
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"));
64 if (renew)
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"));
73 goto READ_DEADLOCK;
75 else
76 return 0;
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);
85 READ_DEADLOCK:
87 for (; y > 0; y--)
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"));
96 else
98 if (write_sleep)
99 ACE_OS::sleep (1);
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);
105 if (renew)
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"));
115 else
116 return 0;
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"));
133 return 0;
136 static int
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; )
145 switch (c)
147 case 'h': // specify the host machine on which the server is running
148 server_host = get_opt.opt_arg ();
149 remote = 1;
150 break;
151 case 'p': // specify the port on which the server is running
152 server_port = ACE_OS::atoi (get_opt.opt_arg ());
153 remote = 1;
154 break;
155 case 't':
156 threads = ACE_OS::atoi (get_opt.opt_arg ());
157 break;
158 case 'R':
159 renew = 1;
160 break;
161 case 'r':
162 reads = ACE_OS::atoi (get_opt.opt_arg ());
163 break;
164 case 'd':
165 debug = 1;
166 break;
167 case 's':
168 write_sleep = 1;
169 break;
170 case 'n':
171 iterations = ACE_OS::atoi (get_opt.opt_arg ());
172 break;
173 case 'i':
174 ignore_deadlock = 1;
175 break;
176 case 'u':
177 // usage same as unknown.
178 ACE_FALLTHROUGH;
179 default:
180 ACE_ERROR_RETURN ((LM_ERROR,
181 "%n:\n"
182 "[-h <remote host>]\n"
183 "[-p <remote port>]\n"
184 "[-i ignore deadlock]\n"
185 "[-n <iterations>]\n"
186 "[-R perform renews]\n"
187 "[-r <reads>]\n"
188 "[-d debug]\n"
189 "[-s sleep during writes]\n"
190 "[-t <threads>\n", 1), -1);
194 return 0;
197 #if defined (ACE_HAS_PTHREADS)
198 #define SUSPEND 0
199 #else
200 #define SUSPEND THR_SUSPENDED
201 #endif
204 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
206 if (parse_args (argc, argv) == -1)
207 return -1;
209 if (remote)
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);
217 else
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),
228 (void *) 0,
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);
235 #endif
237 mgr.wait ();
239 return 0;
242 #else
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 */