Update NEWS
[ACE_TAO.git] / ACE / netsvcs / clients / Tokens / mutex / test_mutex.cpp
bloba78d6e0ac6d89a1cd6ff572858a564bbdf56a4e8
2 //=============================================================================
3 /**
4 * @file test_mutex.cpp
6 * @author Tim Harrison
7 */
8 //=============================================================================
10 #include "ace/Get_Opt.h"
11 #include "ace/Local_Tokens.h"
12 #include "ace/Remote_Tokens.h"
13 #include "ace/Thread.h"
14 #include "ace/Thread_Manager.h"
16 #if defined (ACE_HAS_THREADS) && defined (ACE_HAS_THREADS_LIBRARY)
18 static ACE_Token_Proxy *mutex;
19 static int remote_mutexes = 0;
20 static const char *server_host = ACE_DEFAULT_SERVER_HOST;
21 static int server_port = ACE_DEFAULT_SERVER_PORT;
22 static int iterations = 100;
23 static int spawn_count = 2;
25 static void *
26 run_test (void *)
28 int count = iterations;
29 // test recursive acquisition of a global proxy
30 while (count--)
32 if (mutex->acquire () == -1)
34 ACE_ERROR ((LM_ERROR, "(%t) %p acquire failed\n","test_mutex"));
35 return (void *) -1;
38 // mutex->acquire ();
39 if (mutex->renew () == -1)
41 ACE_ERROR ((LM_ERROR, "(%t) %p renew failed\n","test_mutex"));
42 return (void *) -1;
45 if (mutex->release () == -1)
47 ACE_ERROR ((LM_ERROR, "(%t) %p release failed\n","test_mutex"));
48 return (void *) -1;
51 // mutex->release ();
54 return 0;
57 static int
58 parse_args (int argc, ACE_TCHAR *argv[])
60 ACE_LOG_MSG->open (argv[0]);
62 ACE_Get_Opt get_opt (argc, argv, ACE_TEXT("t:uh:p:n:"), 1);
64 for (int c; (c = get_opt ()) != -1; )
66 switch (c)
68 case 't':
69 spawn_count = ACE_OS::atoi (get_opt.opt_arg ());
70 break;
71 case 'h': // specify the host machine on which the server is running
72 server_host = get_opt.opt_arg ();
73 remote_mutexes = 1;
74 break;
75 case 'p': // specify the port on which the server is running
76 server_port = ACE_OS::atoi (get_opt.opt_arg ());
77 remote_mutexes = 1;
78 break;
79 case 'n': // specify the port on which the server is running
80 iterations = ACE_OS::atoi (get_opt.opt_arg ());
81 break;
82 case 'u':
83 default:
84 ACE_ERROR_RETURN ((LM_ERROR,
85 "%n:\n"
86 "[-h <remote host>]\n"
87 "[-p <remote port>]\n"
88 "[-n <iterations>]\n"
89 "[-t <threads>]\n"
90 "[-h <remote host>]\n"
91 "[-p <remote port>]\n", 1), -1);
92 /* NOTREACHED */
96 return 0;
99 int
100 ACE_TMAIN (int argc, ACE_TCHAR *argv[])
102 ACE_Thread_Manager thread_mgr;
104 if (parse_args (argc, argv) == -1)
105 return -1;
107 if (remote_mutexes)
109 ACE_Remote_Mutex::set_server_address (ACE_INET_Addr (server_port, server_host));
110 mutex = new ACE_Remote_Mutex ("Remote TOKEN", 0, 1);
112 else
114 mutex = new ACE_Local_Mutex ("Local TOKEN", 0, 1);
117 if (thread_mgr.spawn_n (spawn_count,
118 ACE_THR_FUNC (run_test),
120 THR_BOUND) == -1)
121 ACE_ERROR_RETURN ((LM_DEBUG, "%p\n", "spawn"), -1);
123 thread_mgr.wait ();
125 return 0;
127 #else
128 int ACE_TMAIN (int, ACE_TCHAR *[])
130 ACE_ERROR_RETURN ((LM_ERROR, "you must have threads to run this test program\n"), -1);
132 #endif /* ACE_HAS_THREADS */