Compile fixes
[ACE_TAO.git] / ACE / tests / Manual_Event_Test.cpp
blobd8469469dfcb994444a09140181cd85f04760033
2 //=============================================================================
3 /**
4 * @file Manual_Event_Test.cpp
6 * This test verifies the functionality of the <ACE_Manual_Event>
7 * implementation.
9 * @author Martin Corino <mcorino@remedy.nl>
11 //=============================================================================
14 #include "test_config.h"
15 #include "ace/Manual_Event.h"
16 #include "ace/Thread.h"
17 #include "ace/Thread_Manager.h"
18 #include "ace/Get_Opt.h"
19 #include "ace/OS_NS_sys_time.h"
20 #include "ace/OS_NS_time.h"
21 #include "ace/OS_NS_unistd.h"
22 #include "ace/Atomic_Op.h"
24 // Test results, 'success' is 0
25 static int test_result = 0;
27 #if defined (ACE_HAS_THREADS)
29 // Event used in the tests. Start it "unsignalled" (i.e., its initial
30 // state is 0).
31 static ACE_Manual_Event evt ((unsigned int) 0);
33 // Number of worker threads.
34 static long n_workers = 10;
36 // Number of wakeups.
37 static ACE_Atomic_Op<ACE_SYNCH_MUTEX, long> n_awoken;
38 static ACE_Atomic_Op<ACE_SYNCH_MUTEX, long> n_awoken2;
40 // Explain usage and exit.
41 static void
42 print_usage_and_die ()
44 ACE_DEBUG ((LM_DEBUG,
45 ACE_TEXT ("usage: %n [-w n_workers] [-n iteration_count]\n")));
46 ACE_OS::exit (1);
49 static void
50 parse_args (int argc, ACE_TCHAR *argv[])
52 ACE_Get_Opt get_opt (argc, argv, ACE_TEXT ("w:"));
54 int c;
56 while ((c = get_opt ()) != -1)
57 switch (c)
59 case 'w':
60 n_workers = ACE_OS::atoi (get_opt.opt_arg ());
61 break;
62 default:
63 print_usage_and_die ();
64 break;
68 // Worker tries to acquire the semaphore, hold it for a while, and
69 // then manually releases it.
71 static void *
72 worker (void *)
74 if (evt.wait () == -1)
75 ACE_ERROR_RETURN ((LM_ERROR,
76 ACE_TEXT (" (%P|%t) %p\n"),
77 ACE_TEXT ("Failed waiting for pulse ()")),
78 0);
80 ACE_DEBUG ((LM_DEBUG,
81 ACE_TEXT (" (%P|%t) awake\n")));
83 if (++n_awoken < n_workers)
85 //FUZZ: disable check_for_lack_ACE_OS
86 ACE_Time_Value wait (1, 0); // Wait 10 sec
87 //FUZZ: enable check_for_lack_ACE_OS
89 ACE_Time_Value tv = ACE_OS::gettimeofday () + wait;
91 if (evt.wait (&tv) == -1)
93 // verify that we have ETIME
94 if (ACE_OS::last_error () == ETIME)
95 ACE_DEBUG ((LM_DEBUG, ACE_TEXT (" (%P|%t) timeout\n")));
96 else
97 ACE_ERROR ((LM_ERROR,
98 ACE_TEXT (" (%P|%t) wait failed %p\n"),
99 ACE_TEXT ("but not with ETIME")));
101 else
103 ACE_DEBUG ((LM_DEBUG,
104 ACE_TEXT (" (%P|%t) awake in time\n")));
106 if (++n_awoken2 >= (n_workers/2))
107 evt.reset (); // reset signal (rest times out)
110 // Check to ensure that an "infinite timeout" will work.
111 if (evt.wait (0) == -1)
113 //FUZZ: disable check_for_lack_ACE_OS
114 ACE_ERROR ((LM_ERROR,
115 ACE_TEXT (" (%P|%t) %p\n"),
116 ACE_TEXT ("Failed waiting for signal ()\n")));
117 //FUZZ: enable check_for_lack_ACE_OS
120 else
122 ACE_DEBUG ((LM_DEBUG,
123 ACE_TEXT (" (%P|%t) last awake; send signal\n")));
124 // last one wakes others
125 if (evt.signal () == -1)
126 ACE_ERROR ((LM_ERROR, ACE_TEXT (" (%P|%t) %p\n"), ACE_TEXT ("signal")));
128 ACE_OS::sleep (ACE_Time_Value (0, 200 * 1000)); // 200 msec
131 if (evt.wait () == -1)
133 //FUZZ: disable check_for_lack_ACE_OS
134 ACE_ERROR ((LM_ERROR,
135 ACE_TEXT (" (%P|%t) %p\n"),
136 ACE_TEXT ("Failed waiting for signal ()\n")));
137 //FUZZ: enable check_for_lack_ACE_OS
140 ACE_DEBUG ((LM_DEBUG, ACE_TEXT (" (%P|%t) worker finished\n")));
142 return 0;
145 #endif /* ACE_HAS_THREADS */
147 // Test event functionality.
149 int run_main (int argc, ACE_TCHAR *argv[])
151 ACE_START_TEST (ACE_TEXT ("Manual_Event_Test"));
153 #if defined (ACE_HAS_THREADS)
154 parse_args (argc, argv);
156 if (ACE_Thread_Manager::instance ()->spawn_n
157 (static_cast<size_t> (n_workers),
158 ACE_THR_FUNC (worker),
160 THR_NEW_LWP) == -1)
161 ACE_ERROR_RETURN ((LM_ERROR,
162 ACE_TEXT ("%p\n"),
163 ACE_TEXT ("spawn_n")),
166 // gives all workers chance to start
167 ACE_OS::sleep (5);
169 ACE_DEBUG ((LM_DEBUG,
170 ACE_TEXT ("sending pulse ()\n")));
172 // Release the all workers.
173 if (evt.pulse () == -1)
174 ACE_ERROR_RETURN ((LM_ERROR,
175 ACE_TEXT ("%p\n"),
176 ACE_TEXT ("pulse")),
179 // Wait 2 sec
180 ACE_OS::sleep (2);
182 //FUZZ: disable check_for_lack_ACE_OS
183 ACE_DEBUG ((LM_DEBUG,
184 ACE_TEXT ("sending signal ()\n")));
185 //FUZZ: enable check_for_lack_ACE_OS
187 // Signal
188 if (evt.signal () == -1)
189 ACE_ERROR_RETURN ((LM_ERROR,
190 ACE_TEXT ("%p\n"),
191 ACE_TEXT ("signal")),
194 ACE_Thread_Manager::instance ()->wait ();
195 #else
196 ACE_UNUSED_ARG (argc);
197 ACE_UNUSED_ARG (argv);
198 ACE_ERROR ((LM_INFO,
199 ACE_TEXT ("Threads not supported on this platform\n")));
200 #endif /* ACE_HAS_THREADS */
201 ACE_END_TEST;
202 return test_result;