1 // Exercise more tests for the ACE Tasks. This test can spawn off
2 // zillions of tasks and then wait for them using both polling and the
5 #include "ace/OS_main.h"
8 #include "ace/Service_Config.h"
9 #include "ace/Atomic_Op.h"
12 #if defined (ACE_HAS_THREADS)
14 typedef ACE_Atomic_Op
<ACE_Thread_Mutex
, int> ATOMIC_INT
;
17 static ATOMIC_INT
task_count (zero
);
18 static ATOMIC_INT
max_count (zero
);
19 static ATOMIC_INT
wait_count (zero
);
21 static int n_threads
= 0;
23 // Default number of tasks.
24 static const int default_threads
= ACE_DEFAULT_THREADS
;
26 // Default number of times to run the test.
27 static const int default_iterations
= 1000;
29 class Task_Test
: public ACE_Task
<ACE_MT_SYNCH
>
32 //FUZZ: disable check_for_lack_ACE_OS
33 virtual int open (void *args
= 0);
34 virtual int close (u_long flags
= 0);
35 //FUZZ: enable check_for_lack_ACE_OS
40 static ACE_Thread_Mutex lock_
;
43 ACE_Thread_Mutex
Task_Test::lock_
;
46 Task_Test::open (void *)
48 ACE_GUARD_RETURN (ACE_Thread_Mutex
, ace_mon
, Task_Test::lock_
, -1);
51 ACE_DEBUG ((LM_DEBUG
, "(%t) creating Task_Test, task count = %d\n",
52 task_count
.value ()));
54 return this->activate (THR_BOUND
);
58 Task_Test::close (u_long
)
60 ACE_GUARD_RETURN (ACE_Thread_Mutex
, ace_mon
, Task_Test::lock_
, -1);
63 ACE_DEBUG ((LM_DEBUG
, "(%t) destroying Task_Test, task count = %d\n",
64 task_count
.value ()));
75 ACE_DEBUG ((LM_DEBUG
, "(%t) svc: waiting\n"));
78 if (max_count
>= n_threads
)
83 ACE_DEBUG ((LM_DEBUG
, "(%t) svc: finished waiting\n"));
88 ACE_TMAIN (int argc
, ACE_TCHAR
*argv
[])
90 n_threads
= argc
> 1 ? ACE_OS::atoi (argv
[1]) : default_threads
;
91 int n_iterations
= argc
> 2 ? ACE_OS::atoi (argv
[2]) : default_iterations
;
93 Task_Test
**task_array
= new Task_Test
*[n_threads
];
95 for (int i
= 1; i
<= n_iterations
; i
++)
97 ACE_DEBUG ((LM_DEBUG
, "(%t) iteration = %d, max_count %d\n",
98 i
, max_count
.value ()));
101 ACE_DEBUG ((LM_DEBUG
, "(%t) starting %d task%s\n",
102 n_threads
, n_threads
== 1 ? "" : "s"));
104 // Launch the new tasks.
105 for (int j
= 0; j
< n_threads
; j
++)
107 task_array
[j
] = new Task_Test
;
108 // Activate the task, i.e., make it an active object.
109 task_array
[j
]->open ();
112 // Wait for initialization to kick in.
113 while (max_count
== 0)
114 ACE_Thread::yield ();
116 ACE_DEBUG ((LM_DEBUG
, "(%t) waiting for threads to finish\n"));
118 // Wait for the threads to finish this iteration.
119 while (max_count
!= n_threads
&& wait_count
!= 0)
120 ACE_Thread::yield ();
122 ACE_DEBUG ((LM_DEBUG
,
123 "(%t) iteration %d finished, max_count %d, wait_count %d, waiting for tasks to exit\n",
124 i
, max_count
.value (), wait_count
.value ()));
126 // Wait for all the tasks to exit.
127 ACE_Thread_Manager::instance ()->wait ();
129 // Delete the existing tasks.
130 for (int k
= 0; k
< n_threads
; k
++)
131 delete task_array
[k
];
134 delete [] task_array
;
136 ACE_DEBUG ((LM_DEBUG
, "(%t) shutting down the test\n"));
142 ACE_TMAIN (int, ACE_TCHAR
*[])
144 ACE_ERROR ((LM_ERROR
, "threads not supported on this platform\n"));
147 #endif /* ACE_HAS_THREADS */