Merge pull request #2301 from sonndinh/remove-dup-reactor-functions
[ACE_TAO.git] / ACE / netsvcs / clients / Tokens / collection / rw_locks.cpp
blobf84d15b7f191ad87b63f39fa21e1f31dae0df72f
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;
17 static int debug = 0;
18 static int remote = 0;
19 static int reads = 4;
20 static int write_sleep = 0;
22 static void *
23 run_thread (void *vp)
25 for (int x = 0; x < iterations; x++)
27 int y = 0;
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"));
35 goto READ_DEADLOCK;
37 else
38 return 0;
41 ACE_DEBUG ((LM_DEBUG, "(%t) rlock acquired.\n"));
44 READ_DEADLOCK:
45 for (; y > 0; y--)
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"));
55 else
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"));
65 ACE_DEBUG ((LM_DEBUG, "(%t) thread exiting.\n"));
66 return 0;
69 static int
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; )
78 switch (c)
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;
124 return 0;
128 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
130 if (parse_args (argc, argv) == -1)
131 return -1;
133 if (remote)
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);
141 else
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),
152 (void *) 0,
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);
159 mgr.wait ();
161 return 0;
164 #else
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 */