2 //=============================================================================
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"
16 #if defined (ACE_HAS_THREADS)
18 class Cancel_Check
: public ACE_Task
<ACE_MT_SYNCH
>
21 // Create a checker with the state, type requested.
22 Cancel_Check (bool enable
, bool async
);
24 //FUZZ: disable check_for_lack_ACE_OS
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.
32 /// Returns true iff settings match what was requested.
42 Cancel_Check::operator!()
47 Cancel_Check::Cancel_Check (bool enable
, bool async
)
48 : enable_req_ (enable
), async_req_(async
), failed_ (false)
55 #if defined (ACE_HAS_PTHREADS) && !defined (ACE_LACKS_PTHREAD_CANCEL)
57 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE
, &state
);
58 if (state
== PTHREAD_CANCEL_ENABLE
&& !this->enable_req_
)
61 ACE_TEXT ("Cancel found enabled, should not be\n")));
64 else if (state
== PTHREAD_CANCEL_DISABLE
&& this->enable_req_
)
67 ACE_TEXT ("Cancel found disabled, should not be\n")));
73 ACE_TEXT ("Cancel found %s; ok\n"),
74 state
== PTHREAD_CANCEL_ENABLE
? ACE_TEXT ("enabled") :
75 ACE_TEXT ("disabled")));
79 pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED
, &type
);
80 if (type
== PTHREAD_CANCEL_ASYNCHRONOUS
&& !this->async_req_
)
83 ACE_TEXT ("Cancel type async, should not be\n")));
86 else if (type
== PTHREAD_CANCEL_DEFERRED
&& this->async_req_
)
89 ACE_TEXT ("Cancel type deferred, should not be\n")));
95 ACE_TEXT ("Cancel type %s; ok\n"),
96 type
== PTHREAD_CANCEL_DEFERRED
? ACE_TEXT ("deferred") :
97 ACE_TEXT ("asynchronous")));
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
,
115 ACE_TEXT ("Cancel_Check activate failed")),
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
>
129 /// Create the thread with specified stack size
130 Stack_Size_Check (size_t stack_size
);
132 //FUZZ: disable check_for_lack_ACE_OS
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.
140 /// Returns true iff failed_ == false.
146 /// Flag indicating the test failed.
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 ()
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
);
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;
188 ACE_DEBUG ((LM_DEBUG
,
189 ACE_TEXT ("%t: My stack size attr %B; correct.\n"),
195 Stack_Size_Check::open (void *)
197 if (this->activate (THR_NEW_LWP
| THR_JOINABLE
,
200 ACE_DEFAULT_THREAD_PRIORITY
,
206 ACE_ERROR_RETURN ((LM_ERROR
,
208 ACE_TEXT ("Stack_Size_Check activate failed")),
214 #endif /* ACE_HAS_THREADS */
218 run_main (int, ACE_TCHAR
*[])
220 ACE_START_TEST (ACE_TEXT ("Thread_Attrs_Test"));
223 #if defined (ACE_HAS_THREADS)
224 Stack_Size_Check
size_checker (40*1024);
225 status
= size_checker
.open(0);
228 if (size_checker
.wait () == -1)
229 ACE_ERROR_RETURN ((LM_ERROR
,
230 ACE_TEXT ("size_checker %p\n"),
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"),
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)
274 ACE_ERROR ((LM_ERROR
, ACE_TEXT ("%p\n"), ACE_TEXT ("Cancel_Check open")));
279 ACE_TEXT ("threads not supported on this platform\n")));
280 #endif /* ACE_HAS_THREADS */