Debian Codegraph @

netsvcs/clients/Tokens/collection/rw_locks.cpp

raw symbols SCIP
1 #include "ace/Get_Opt.h"
2 #include "ace/Local_Tokens.h"
3 #include "ace/Remote_Tokens.h"
4 #include "ace/Thread_Manager.h"
5
6 #if defined (ACE_HAS_THREADS) && defined (ACE_HAS_THREADS_LIBRARY)
7
8
9 static ACE_Token_Proxy *global_rlock;
10 static ACE_Token_Proxy *global_wlock;
11
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;
17 static int debug = 0;
18 static int remote = 0;
19 static int reads = 4;
20 static int write_sleep = 0;
21
22 static void *
23 run_thread (void *vp)
24 {
25 for (int x = 0; x < iterations; x++)
26 {
27 int y = 0;
28 for (; y < reads; y++)
29 {
30 if (global_rlock->acquire () == -1)
31 {
32 if (ACE_Log_Msg::instance ()->errnum () == EDEADLK)
33 {
34 ACE_DEBUG ((LM_DEBUG, "rlock deadlock detected\n"));
35 goto READ_DEADLOCK;
36 }
37 else
38 return 0;
39 }
40
41 ACE_DEBUG ((LM_DEBUG, "(%t) rlock acquired.\n"));
42 }
43
44 READ_DEADLOCK:
45 for (; y > 0; y--)
46 {
47 if (global_rlock->release () == 0)
48 ACE_DEBUG ((LM_DEBUG, "(%t) r-released.\n"));
49 }
50
51 if (global_wlock->acquire () == -1)
52 {
53 ACE_DEBUG ((LM_DEBUG, "wlock deadlock detected\n"));
54 }
55 else
56 {
57 if (write_sleep)
58 ACE_OS::sleep (1);
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"));
62 }
63 }
64
65 ACE_DEBUG ((LM_DEBUG, "(%t) thread exiting.\n"));
66 return 0;
67 }
68
69 static int
70 parse_args (int argc, ACE_TCHAR *argv[])
71 {
72 ACE_LOG_MSG->open (argv[0], ACE_Log_Msg::STDERR); // | ACE_Log_Msg::VERBOSE);
73
74 ACE_Get_Opt get_opt (argc, argv, ACE_TEXT("t:iun:drR:sp:h:"), 1);
75
76 for (int c; (c = get_opt ()) != -1; )
77 {
78 switch (c)
79 {
80 case 'h': // specify the host machine on which the server is running
81 server_host = get_opt.opt_arg ();
82 break;
83 case 'p': // specify the port on which the server is running
84 server_port = ACE_OS::atoi (get_opt.opt_arg ());
85 break;
86 case 't':
87 threads = ACE_OS::atoi (get_opt.opt_arg ());
88 break;
89 case 'R':
90 reads = ACE_OS::atoi (get_opt.opt_arg ());
91 break;
92 case 'd':
93 debug = 1;
94 break;
95 case 'r':
96 remote = 1;
97 break;
98 case 's':
99 write_sleep = 1;
100 break;
101 case 'n':
102 iterations = ACE_OS::atoi (get_opt.opt_arg ());
103 break;
104 case 'i':
105 ignore_deadlock = 1;
106 break;
107 case 'u':
108 // usage same as unknown.
109 ACE_FALLTHROUGH;
110 default:
111 ACE_ERROR_RETURN ((LM_ERROR,
112 "%n:\n"
113 "[-i ignore deadlock]\n"
114 "[-n <iterations>]\n"
115 "[-R <reads>]\n"
116 "[-r use remote locks]\n"
117 "[-d debug]\n"
118 "[-s sleep during writes]\n"
119 "[-t <threads>\n", 1), -1);
120 break;
121 }
122 }
123
124 return 0;
125 }
126
127 int
128 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
129 {
130 if (parse_args (argc, argv) == -1)
131 return -1;
132
133 if (remote)
134 {
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);
140 }
141 else
142 {
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);
147 }
148
149 ACE_Thread_Manager mgr;
150
151 if (mgr.spawn_n (threads, ACE_THR_FUNC (run_thread),
152 (void *) 0,
153 THR_BOUND | THR_SUSPENDED) == -1)
154 ACE_ERROR_RETURN ((LM_DEBUG, "%p\n", "spawn failed"), -1);
155
156 if (mgr.resume_all () == -1)
157 ACE_ERROR_RETURN ((LM_DEBUG, "%p\n", "resume failed"), -1);
158
159 mgr.wait ();
160
161 return 0;
162 }
163
164 #else
165 int
166 ACE_TMAIN(int, ACE_TCHAR *[])
167 {
168 ACE_ERROR_RETURN ((LM_ERROR,
169 "threads not supported on this platform\n"), -1);
170 }
171 #endif /* ACE_HAS_THREADS */