2 //=============================================================================
4 * @file Manual_Event_Test.cpp
6 * This test verifies the functionality of the <ACE_Manual_Event>
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
31 static ACE_Manual_Event
evt ((unsigned int) 0);
33 // Number of worker threads.
34 static long n_workers
= 10;
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.
42 print_usage_and_die ()
45 ACE_TEXT ("usage: %n [-w n_workers] [-n iteration_count]\n")));
50 parse_args (int argc
, ACE_TCHAR
*argv
[])
52 ACE_Get_Opt
get_opt (argc
, argv
, ACE_TEXT ("w:"));
56 while ((c
= get_opt ()) != -1)
60 n_workers
= ACE_OS::atoi (get_opt
.opt_arg ());
63 print_usage_and_die ();
68 // Worker tries to acquire the semaphore, hold it for a while, and
69 // then manually releases it.
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 ()")),
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")));
98 ACE_TEXT (" (%P|%t) wait failed %p\n"),
99 ACE_TEXT ("but not with ETIME")));
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
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")));
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
),
161 ACE_ERROR_RETURN ((LM_ERROR
,
163 ACE_TEXT ("spawn_n")),
166 // gives all workers chance to start
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
,
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
188 if (evt
.signal () == -1)
189 ACE_ERROR_RETURN ((LM_ERROR
,
191 ACE_TEXT ("signal")),
194 ACE_Thread_Manager::instance ()->wait ();
196 ACE_UNUSED_ARG (argc
);
197 ACE_UNUSED_ARG (argv
);
199 ACE_TEXT ("Threads not supported on this platform\n")));
200 #endif /* ACE_HAS_THREADS */