Compile fixes
[ACE_TAO.git] / ACE / tests / Task_Group_Test.cpp
blobd46c32597a1f432d2fb4990d8061959dc648d4a9
2 //=============================================================================
3 /**
4 * @file Task_Group_Test.cpp
6 * This test program illustrates the logic of the grp_id handling
7 * mechanism in <ACE_Task_Base> and the <ACE_Thread_Manager>.
8 * Partially reuses test submitted by Paxton Mason in Bugzilla #2366
10 * @author Martin Corino <mcorino@remedy.nl>
12 //=============================================================================
15 #include "test_config.h"
16 #include "ace/Atomic_Op.h"
17 #include "ace/Task.h"
18 #include "ace/OS_NS_unistd.h"
20 #if defined (ACE_HAS_THREADS)
22 const int MAX_GROUP_THREADS = ACE_MAX_THREADS * 2;
24 static ACE_Atomic_Op<ACE_Thread_Mutex, int> run_count (0);
26 class Waiter_Task : public ACE_Task_Base
28 public:
29 Waiter_Task (ACE_Thread_Manager* tm = 0)
30 : ACE_Task_Base (tm) {}
31 int svc() override;
34 int
35 Waiter_Task::svc()
37 ACE_DEBUG ((LM_DEBUG,
38 ACE_TEXT ("(%t|grp_id=%d) waiting for thread cancellation.\n"), this->grp_id ()));
40 run_count++;
42 // wait until canceled
43 while (!ACE_Task_Base::thr_mgr ()->testcancel (ACE_OS::thr_self ()))
44 ACE_OS::sleep (0);
46 ACE_DEBUG ((LM_DEBUG,
47 ACE_TEXT ("(%t|grp_id=%d) thread cancelled.\n"), this->grp_id ()));
49 run_count--;
51 return 0;
54 #endif /* ACE_HAS_THREADS */
56 int
57 run_main (int, ACE_TCHAR *[])
59 ACE_START_TEST (ACE_TEXT ("Task_Group_Test"));
61 #if defined (ACE_HAS_THREADS)
63 ACE_thread_t thread_list[MAX_GROUP_THREADS] = {0};
64 int group_id = -1;
66 Waiter_Task waiter_task (ACE_Thread_Manager::instance ());
68 // activate a single thread
69 waiter_task.activate (THR_NEW_LWP | THR_DETACHED, 1);
70 // get the assigned grp_id
71 group_id = waiter_task.grp_id ();
72 // attempt to add a thread -> this should fail without force_activate
73 if (waiter_task.activate (THR_NEW_LWP | THR_DETACHED, 1) != 1)
75 ACE_ERROR ((LM_ERROR,
76 ACE_TEXT ("ACE_Task_Base::activate should have failed to add new thread.")));
78 // force addition of new thread
79 if (waiter_task.activate (THR_NEW_LWP | THR_DETACHED, 1, 1) != 0)
81 ACE_ERROR ((LM_ERROR,
82 ACE_TEXT ("ACE_Task_Base::activate should have succeeded to force addition of new thread.")));
84 // force addition of new thread ignoring specific group id
85 if (waiter_task.activate (THR_NEW_LWP | THR_DETACHED, 1, 1, ACE_DEFAULT_THREAD_PRIORITY, group_id+1) != 0)
87 ACE_ERROR ((LM_ERROR,
88 ACE_TEXT ("ACE_Task_Base::activate should have succeeded to force addition of new thread.")));
90 // we should now have 3 threads in group 'group_id'
91 if (waiter_task.thr_mgr ()->thread_grp_list (group_id, &thread_list[0], MAX_GROUP_THREADS) != 3)
93 ACE_ERROR ((LM_ERROR,
94 ACE_TEXT ("Should have found 3 threads in group %d."), group_id));
97 // cancel all threads
98 waiter_task.thr_mgr ()->cancel_all ();
99 // wait for threads to exit cleanly
100 while (waiter_task.thr_mgr ()->num_threads_in_task (&waiter_task) > 0)
101 ACE_OS::sleep (0);
103 // restart 2 threads reusing the group id
104 if (waiter_task.activate (THR_NEW_LWP | THR_DETACHED, 2) != 0)
106 ACE_ERROR ((LM_ERROR,
107 ACE_TEXT ("ACE_Task_Base::activate should have succeeded to add new threads.")));
109 // attempt to add a thread -> this should fail without force_activate
110 if (waiter_task.activate (THR_NEW_LWP | THR_DETACHED, 1) != 1)
112 ACE_ERROR ((LM_ERROR,
113 ACE_TEXT ("ACE_Task_Base::activate should have failed to add new thread.")));
115 // force addition of new thread
116 if (waiter_task.activate (THR_NEW_LWP | THR_DETACHED, 1, 1) != 0)
118 ACE_ERROR ((LM_ERROR,
119 ACE_TEXT ("ACE_Task_Base::activate should have succeeded to force addition of new thread.")));
121 // force addition of new thread ignoring specific group id
122 if (waiter_task.activate (THR_NEW_LWP | THR_DETACHED, 1, 1, ACE_DEFAULT_THREAD_PRIORITY, group_id+1) != 0)
124 ACE_ERROR ((LM_ERROR,
125 ACE_TEXT ("ACE_Task_Base::activate should have succeeded to force addition of new thread.")));
127 // we should now have 4 threads in group 'group_id'
128 if (waiter_task.thr_mgr ()->thread_grp_list (group_id, &thread_list[0], MAX_GROUP_THREADS) != 4)
130 ACE_ERROR ((LM_ERROR,
131 ACE_TEXT ("Should have found 4 threads in group %d."), group_id));
134 // wait till the threads actually started up
135 while (run_count != 4)
136 ACE_OS::sleep (0);
138 // change the group id of all threads of the task
139 group_id += 1;
140 waiter_task.grp_id (group_id);
142 // we should still have 4 threads in group with new 'group_id'
143 if (waiter_task.thr_mgr ()->thread_grp_list (group_id, &thread_list[0], MAX_GROUP_THREADS) != 4)
145 ACE_ERROR ((LM_ERROR,
146 ACE_TEXT ("Should have found 4 threads in group %d."), group_id));
149 // cancel all threads
150 waiter_task.thr_mgr ()->cancel_all ();
151 // wait for threads to exit cleanly
152 while (waiter_task.thr_mgr ()->num_threads_in_task (&waiter_task) > 0)
153 ACE_OS::sleep (0);
155 // restart 3 threads using explicit new group id
156 group_id = 33;
157 if (waiter_task.activate (THR_NEW_LWP | THR_DETACHED, 3, 0, ACE_DEFAULT_THREAD_PRIORITY, group_id) != 0)
159 ACE_ERROR ((LM_ERROR,
160 ACE_TEXT ("ACE_Task_Base::activate should have succeeded to add new threads.")));
162 // attempt to add a thread -> this should fail without force_activate
163 if (waiter_task.activate (THR_NEW_LWP | THR_DETACHED, 1) != 1)
165 ACE_ERROR ((LM_ERROR,
166 ACE_TEXT ("ACE_Task_Base::activate should have failed to add new thread.")));
168 // force addition of new thread
169 if (waiter_task.activate (THR_NEW_LWP | THR_DETACHED, 1, 1) != 0)
171 ACE_ERROR ((LM_ERROR,
172 ACE_TEXT ("ACE_Task_Base::activate should have succeeded to force addition of new thread.")));
174 // force addition of new thread ignoring specific group id
175 if (waiter_task.activate (THR_NEW_LWP | THR_DETACHED, 1, 1, ACE_DEFAULT_THREAD_PRIORITY, group_id+1) != 0)
177 ACE_ERROR ((LM_ERROR,
178 ACE_TEXT ("ACE_Task_Base::activate should have succeeded to force addition of new thread.")));
180 // we should now have 5 threads in group 'group_id'
181 if (waiter_task.thr_mgr ()->thread_grp_list (group_id, &thread_list[0], MAX_GROUP_THREADS) != 5)
183 ACE_ERROR ((LM_ERROR,
184 ACE_TEXT ("Should have found 5 threads in group %d."), group_id));
187 // cancel all threads
188 waiter_task.thr_mgr ()->cancel_all ();
189 // wait for threads to exit cleanly
190 while (waiter_task.thr_mgr ()->num_threads_in_task (&waiter_task) > 0)
191 ACE_OS::sleep (0);
193 #else
194 ACE_ERROR ((LM_INFO,
195 ACE_TEXT ("threads not supported on this platform\n")));
196 #endif /* ACE_HAS_THREADS */
197 ACE_END_TEST;
198 return 0;