Debian Codegraph @

netsvcs/clients/Tokens/rw_lock/rw_locks.cpp

raw symbols SCIP
1
2 //=============================================================================
3 /**
4 * @file rw_locks.cpp
5 *
6 * test_rw_locks shows how to use ACE_Local_RLock, ACE_Local_WLock,
7 * ACE_Remote_RLock, and ACE_Remote_WLock.
8 *
9 * @author Tim Harrison
10 */
11 //=============================================================================
12
13
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"
19
20 #if defined (ACE_HAS_THREADS) && defined (ACE_HAS_THREADS_LIBRARY)
21
22
23 typedef ACE_Token_Invariant_Manager ACE_TOKEN_INVARIANTS;
24
25 static ACE_Token_Proxy *global_rlock;
26 static ACE_Token_Proxy *global_wlock;
27
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;
38
39 static void *
40 run_thread (void *)
41 {
42 for (int x = 0; x < iterations; x++)
43 {
44 int y = 0;
45 for (; y < reads; y++)
46 {
47 if (global_rlock->acquire () == -1)
48 {
49 if (ACE_Log_Msg::instance ()->errnum () == EDEADLK)
50 {
51 ACE_DEBUG ((LM_DEBUG, "rlock deadlock detected\n"));
52 goto READ_DEADLOCK;
53 }
54 else
55 return 0;
56 }
57
58 if (ACE_TOKEN_INVARIANTS::instance ()->acquired (global_rlock) == 0)
59 ACE_ERROR_RETURN ((LM_ERROR, "reader acquire violated invariant.\n"), 0);
60
61 ACE_DEBUG ((LM_DEBUG, "(%t) rlock acquired.\n"));
62 }
63
64 if (renew)
65 {
66 ACE_TOKEN_INVARIANTS::instance ()->releasing (global_rlock);
67
68 if (global_rlock->renew () == -1)
69 {
70 if (ACE_Log_Msg::instance ()->errnum () == EDEADLK)
71 {
72 ACE_DEBUG ((LM_DEBUG, "rlock deadlock detected during renew\n"));
73 goto READ_DEADLOCK;
74 }
75 else
76 return 0;
77 }
78
79 ACE_DEBUG ((LM_DEBUG, "(%t) rlock renewed.\n"));
80
81 if (ACE_TOKEN_INVARIANTS::instance ()->acquired (global_rlock) == 0)
82 ACE_ERROR_RETURN ((LM_ERROR, "reader renew violated invariant.\n"), 0);
83 }
84
85 READ_DEADLOCK:
86
87 for (; y > 0; y--)
88 {
89 ACE_TOKEN_INVARIANTS::instance ()->releasing (global_rlock);
90 if (global_rlock->release () == 0)
91 ACE_DEBUG ((LM_DEBUG, "(%t) r-released.\n"));
92 }
93
94 if (global_wlock->acquire () == -1)
95 ACE_DEBUG ((LM_DEBUG, "wlock deadlock detected\n"));
96 else
97 {
98 if (write_sleep)
99 ACE_OS::sleep (1);
100 ACE_DEBUG ((LM_DEBUG, "\t\t(%t) wlock acquired.\n"));
101
102 if (ACE_TOKEN_INVARIANTS::instance ()->acquired (global_wlock) == 0)
103 ACE_ERROR_RETURN ((LM_ERROR, "writer acquire violated invariant.\n"), 0);
104
105 if (renew)
106 {
107 ACE_TOKEN_INVARIANTS::instance ()->releasing (global_wlock);
108
109 if (global_wlock->renew () == -1)
110 {
111 if (ACE_Log_Msg::instance ()->errnum () == EDEADLK)
112 {
113 ACE_DEBUG ((LM_DEBUG, "wlock deadlock detected during renew\n"));
114 }
115 else
116 return 0;
117 }
118
119 ACE_DEBUG ((LM_DEBUG, "(%t) rlock renewed.\n"));
120
121 if (ACE_TOKEN_INVARIANTS::instance ()->acquired (global_wlock) == 0)
122 ACE_ERROR_RETURN ((LM_ERROR, "writer renew violated invariant.\n"), 0);
123 }
124
125 ACE_TOKEN_INVARIANTS::instance ()->releasing (global_wlock);
126
127 if (global_wlock->release () == 0)
128 ACE_DEBUG ((LM_DEBUG, "\t\t(%t) w-released.\n"));
129 }
130 }
131
132 ACE_DEBUG ((LM_DEBUG, "(%t) thread exiting.\n"));
133 return 0;
134 }
135
136 static int
137 parse_args (int argc, ACE_TCHAR *argv[])
138 {
139 ACE_LOG_MSG->open (argv[0], ACE_Log_Msg::STDERR); // | ACE_Log_Msg::VERBOSE);
140
141 ACE_Get_Opt get_opt (argc, argv, ACE_TEXT("t:iun:dr:sp:h:R"), 1);
142
143 for (int c; (c = get_opt ()) != -1; )
144 {
145 switch (c)
146 {
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);
191 }
192 }
193
194 return 0;
195 }
196
197 #if defined (ACE_HAS_PTHREADS)
198 #define SUSPEND 0
199 #else
200 #define SUSPEND THR_SUSPENDED
201 #endif
202
203 int
204 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
205 {
206 if (parse_args (argc, argv) == -1)
207 return -1;
208
209 if (remote)
210 {
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);
216 }
217 else
218 {
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);
223 }
224
225 ACE_Thread_Manager mgr;
226
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);
231
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
236
237 mgr.wait ();
238
239 return 0;
240 }
241
242 #else
243 int
244 ACE_TMAIN(int, ACE_TCHAR *[])
245 {
246 ACE_ERROR_RETURN ((LM_ERROR,
247 "threads not supported on this platform\n"), -1);
248 }
249 #endif /* ACE_HAS_THREADS */