Revert "Use a variable on the stack to not have a temporary in the call"
[ACE_TAO.git] / ACE / tests / Thread_Attrs_Test.cpp
bloba8edb0327b839887880b6bc3c1405b56a3c9d04c
2 //=============================================================================
3 /**
4 * @file Thread_Attrs_Test.cpp
6 * This test program ensures that attributes set on a thread via the
7 * ACE_Task/ACE_Thread_Manager are honored.
9 * @author Steve Huston <shuston@riverace.com>
11 //=============================================================================
13 #include "test_config.h"
14 #include "ace/Task.h"
16 #if defined (ACE_HAS_THREADS)
18 class Cancel_Check : public ACE_Task<ACE_MT_SYNCH>
20 public:
21 // Create a checker with the state, type requested.
22 Cancel_Check (bool enable, bool async);
24 //FUZZ: disable check_for_lack_ACE_OS
25 // Spawn the thread
26 int open (void * = 0) override;
27 //FUZZ: enable check_for_lack_ACE_OS
29 // Check the cancel settings against what is expected then exit.
30 int svc () override;
32 /// Returns true iff settings match what was requested.
33 bool operator! ();
35 private:
36 bool enable_req_;
37 bool async_req_;
38 bool failed_;
41 bool
42 Cancel_Check::operator!()
44 return this->failed_;
47 Cancel_Check::Cancel_Check (bool enable, bool async)
48 : enable_req_ (enable), async_req_(async), failed_ (false)
52 int
53 Cancel_Check::svc ()
55 #if defined (ACE_HAS_PTHREADS) && !defined (ACE_LACKS_PTHREAD_CANCEL)
56 int state;
57 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &state);
58 if (state == PTHREAD_CANCEL_ENABLE && !this->enable_req_)
60 ACE_ERROR ((LM_ERROR,
61 ACE_TEXT ("Cancel found enabled, should not be\n")));
62 this->failed_ = true;
64 else if (state == PTHREAD_CANCEL_DISABLE && this->enable_req_)
66 ACE_ERROR ((LM_ERROR,
67 ACE_TEXT ("Cancel found disabled, should not be\n")));
68 this->failed_ = true;
70 else
72 ACE_DEBUG ((LM_DEBUG,
73 ACE_TEXT ("Cancel found %s; ok\n"),
74 state == PTHREAD_CANCEL_ENABLE ? ACE_TEXT ("enabled") :
75 ACE_TEXT ("disabled")));
78 int type;
79 pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &type);
80 if (type == PTHREAD_CANCEL_ASYNCHRONOUS && !this->async_req_)
82 ACE_ERROR ((LM_ERROR,
83 ACE_TEXT ("Cancel type async, should not be\n")));
84 this->failed_ = true;
86 else if (type == PTHREAD_CANCEL_DEFERRED && this->async_req_)
88 ACE_ERROR ((LM_ERROR,
89 ACE_TEXT ("Cancel type deferred, should not be\n")));
90 this->failed_ = true;
92 else
94 ACE_DEBUG ((LM_DEBUG,
95 ACE_TEXT ("Cancel type %s; ok\n"),
96 type == PTHREAD_CANCEL_DEFERRED ? ACE_TEXT ("deferred") :
97 ACE_TEXT ("asynchronous")));
100 #endif
101 return 0;
105 Cancel_Check::open (void *)
107 long flags = THR_NEW_LWP | THR_JOINABLE;
108 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Flags before cancels: 0x%x\n"), flags));
109 flags |= (this->enable_req_ ? THR_CANCEL_ENABLE : THR_CANCEL_DISABLE);
110 flags |= (this->async_req_ ? THR_CANCEL_ASYNCHRONOUS : THR_CANCEL_DEFERRED);
111 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Flags after cancels: 0x%x\n"), flags));
112 if (this->activate (flags) == -1)
113 ACE_ERROR_RETURN ((LM_ERROR,
114 ACE_TEXT ("%p\n"),
115 ACE_TEXT ("Cancel_Check activate failed")),
116 -1);
117 return 0;
122 * @class Stack_Size_Check
124 * @brief Defines a task that verifies its stack size.
126 class Stack_Size_Check : public ACE_Task<ACE_MT_SYNCH>
128 public:
129 /// Create the thread with specified stack size
130 Stack_Size_Check (size_t stack_size);
132 //FUZZ: disable check_for_lack_ACE_OS
133 /// Spawn the thread
134 int open (void * = 0) override;
135 //FUZZ: enable check_for_lack_ACE_OS
137 /// Check the stack size against what is expected then exit.
138 int svc () override;
140 /// Returns true iff failed_ == false.
141 bool operator! ();
143 private:
144 size_t stack_size_;
146 /// Flag indicating the test failed.
147 bool failed_;
150 bool
151 Stack_Size_Check::operator!()
153 return this->failed_;
156 Stack_Size_Check::Stack_Size_Check (size_t stack_size)
157 : stack_size_ (stack_size), failed_ (false)
162 Stack_Size_Check::svc ()
164 size_t my_size = 0;
166 #ifdef __USE_GNU
167 pthread_attr_t my_attrs;
168 pthread_getattr_np (pthread_self (), &my_attrs);
169 pthread_attr_getstacksize (&my_attrs, &my_size);
170 pthread_attr_destroy (&my_attrs);
171 #else
172 // No known way to do this yet... feel free to fill this in.
173 my_size = this->stack_size_;
174 #endif /* __USE_GNU */
176 // The Posix docs say that the size set for the threads stack will be the
177 // *minimum* size allocated (the actual size may be bigger because of
178 // a) pagesize rounding, b) guardsize addition) so we can really only
179 // check if we have gotten *at least* what we asked for.
180 if (my_size < this->stack_size_)
182 ACE_ERROR ((LM_ERROR,
183 ACE_TEXT ("%t: My stack size attr %B; expected %B\n"),
184 my_size, this->stack_size_));
185 this->failed_ = true;
187 else
188 ACE_DEBUG ((LM_DEBUG,
189 ACE_TEXT ("%t: My stack size attr %B; correct.\n"),
190 my_size));
191 return 0;
195 Stack_Size_Check::open (void *)
197 if (this->activate (THR_NEW_LWP | THR_JOINABLE,
200 ACE_DEFAULT_THREAD_PRIORITY,
205 &stack_size_) == -1)
206 ACE_ERROR_RETURN ((LM_ERROR,
207 ACE_TEXT ("%p\n"),
208 ACE_TEXT ("Stack_Size_Check activate failed")),
209 -1);
211 return 0;
214 #endif /* ACE_HAS_THREADS */
218 run_main (int, ACE_TCHAR *[])
220 ACE_START_TEST (ACE_TEXT ("Thread_Attrs_Test"));
222 int status = 0;
223 #if defined (ACE_HAS_THREADS)
224 Stack_Size_Check size_checker (40*1024);
225 status = size_checker.open(0);
226 if (status == 0)
228 if (size_checker.wait () == -1)
229 ACE_ERROR_RETURN ((LM_ERROR,
230 ACE_TEXT ("size_checker %p\n"),
231 ACE_TEXT ("wait")),
233 if (!size_checker)
234 status = 1;
237 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Cancel flags sanity check:\n")
238 ACE_TEXT (" THR_CANCEL_ENABLE: 0x%x\n")
239 ACE_TEXT (" THR_CANCEL_DISABLE: 0x%x\n")
240 ACE_TEXT (" THR_CANCEL_DEFERRED: 0x%x\n")
241 ACE_TEXT (" THR_CANCEL_ASYNCHRONOUS: 0x%x\n"),
242 THR_CANCEL_ENABLE,
243 THR_CANCEL_DISABLE,
244 THR_CANCEL_DEFERRED,
245 THR_CANCEL_ASYNCHRONOUS));
246 // Cancel check args: enable (yes/no), async/deferred
247 Cancel_Check check1 (true, true);
248 Cancel_Check check2 (true, false);
249 Cancel_Check check3 (false, true);
250 Cancel_Check check4 (false, false);
251 if (check1.open(0) == 0)
253 check1.wait ();
254 if (!check1)
255 status = 1;
257 check2.open (0);
258 check2.wait ();
259 if (!check2)
260 status = 1;
262 check3.open (0);
263 check3.wait ();
264 if (!check3)
265 status = 1;
267 check4.open (0);
268 check4.wait ();
269 if (!check4)
270 status = 1;
272 else
274 ACE_ERROR ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("Cancel_Check open")));
275 status = 1;
277 #else
278 ACE_ERROR ((LM_INFO,
279 ACE_TEXT ("threads not supported on this platform\n")));
280 #endif /* ACE_HAS_THREADS */
281 ACE_END_TEST;
282 return status;