Update NEWS
[ACE_TAO.git] / ACE / netsvcs / clients / Tokens / collection / rw_locks.cpp
blob7215381fd8504490ef153e35f9ad3793414eccd0
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)
10 static ACE_Token_Proxy *global_rlock;
11 static ACE_Token_Proxy *global_wlock;
13 static char *server_host = ACE_DEFAULT_SERVER_HOST;
14 static int server_port = ACE_DEFAULT_SERVER_PORT;
15 static int ignore_deadlock = 0;
16 static int threads = 2;
17 static int iterations = 50;
18 static int debug = 0;
19 static int remote = 0;
20 static int reads = 4;
21 static int write_sleep = 0;
23 static void *
24 run_thread (void *vp)
26 for (int x = 0; x < iterations; x++)
28 int y = 0;
29 for (; y < reads; y++)
31 if (global_rlock->acquire () == -1)
33 if (ACE_Log_Msg::instance ()->errnum () == EDEADLK)
35 ACE_DEBUG ((LM_DEBUG, "rlock deadlock detected\n"));
36 goto READ_DEADLOCK;
38 else
39 return 0;
42 ACE_DEBUG ((LM_DEBUG, "(%t) rlock acquired.\n"));
45 READ_DEADLOCK:
46 for (; y > 0; y--)
48 if (global_rlock->release () == 0)
49 ACE_DEBUG ((LM_DEBUG, "(%t) r-released.\n"));
52 if (global_wlock->acquire () == -1)
54 ACE_DEBUG ((LM_DEBUG, "wlock deadlock detected\n"));
56 else
58 if (write_sleep)
59 ACE_OS::sleep (1);
60 ACE_DEBUG ((LM_DEBUG, "\t\t(%t) wlock acquired.\n"));
61 if (global_wlock->release () == 0)
62 ACE_DEBUG ((LM_DEBUG, "\t\t(%t) w-released.\n"));
66 ACE_DEBUG ((LM_DEBUG, "(%t) thread exiting.\n"));
67 return 0;
70 static int
71 parse_args (int argc, ACE_TCHAR *argv[])
73 ACE_LOG_MSG->open (argv[0], ACE_Log_Msg::STDERR); // | ACE_Log_Msg::VERBOSE);
75 ACE_Get_Opt get_opt (argc, argv, ACE_TEXT("t:iun:drR:sp:h:"), 1);
77 for (int c; (c = get_opt ()) != -1; )
79 switch (c)
81 case 'h': // specify the host machine on which the server is running
82 server_host = get_opt.opt_arg ();
83 break;
84 case 'p': // specify the port on which the server is running
85 server_port = ACE_OS::atoi (get_opt.opt_arg ());
86 break;
87 case 't':
88 threads = ACE_OS::atoi (get_opt.opt_arg ());
89 break;
90 case 'R':
91 reads = ACE_OS::atoi (get_opt.opt_arg ());
92 break;
93 case 'd':
94 debug = 1;
95 break;
96 case 'r':
97 remote = 1;
98 break;
99 case 's':
100 write_sleep = 1;
101 break;
102 case 'n':
103 iterations = ACE_OS::atoi (get_opt.opt_arg ());
104 break;
105 case 'i':
106 ignore_deadlock = 1;
107 break;
108 case 'u':
109 // usage: 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 */