1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
|
//=============================================================================
/**
* @file Thread_Attrs_Test.cpp
*
* $Id: Thread_Attrs_Test.cpp 93638 2011-03-24 13:16:05Z johnnyw $
*
* This test program ensures that attributes set on a thread via the
* ACE_Task/ACE_Thread_Manager are honored.
*
*
* @author Steve Huston <shuston@riverace.com>
*/
//=============================================================================
#include "test_config.h"
#include "ace/Task.h"
#if defined (ACE_HAS_THREADS)
namespace
{
// Change this to 'true' if you want lots of debugging messages in the log
const bool PRINT_DEBUG_MSGS = true;
}
class Cancel_Check : public ACE_Task<ACE_MT_SYNCH>
{
public:
// Create a checker with the state, type requested.
Cancel_Check (bool enable, bool async);
//FUZZ: disable check_for_lack_ACE_OS
// Spawn the thread
virtual int open (void * = 0);
//FUZZ: enable check_for_lack_ACE_OS
// Check the cancel settings against what is expected then exit.
virtual int svc (void);
/// Returns true iff settings match what was requested.
bool operator! ();
private:
bool enable_req_;
bool async_req_;
bool failed_;
};
bool
Cancel_Check::operator!()
{
return this->failed_;
}
Cancel_Check::Cancel_Check (bool enable, bool async)
: enable_req_ (enable), async_req_(async), failed_ (false)
{
}
int
Cancel_Check::svc (void)
{
#if defined (ACE_HAS_PTHREADS)
int state;
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &state);
if (state == PTHREAD_CANCEL_ENABLE && !this->enable_req_)
{
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("Cancel found enabled, should not be\n")));
this->failed_ = true;
}
else if (state == PTHREAD_CANCEL_DISABLE && this->enable_req_)
{
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("Cancel found disabled, should not be\n")));
this->failed_ = true;
}
else
{
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("Cancel found %s; ok\n"),
state == PTHREAD_CANCEL_ENABLE ? ACE_TEXT ("enabled") :
ACE_TEXT ("disabled")));
}
int type;
pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &type);
if (type == PTHREAD_CANCEL_ASYNCHRONOUS && !this->async_req_)
{
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("Cancel type async, should not be\n")));
this->failed_ = true;
}
else if (type == PTHREAD_CANCEL_DEFERRED && this->async_req_)
{
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("Cancel type deferred, should not be\n")));
this->failed_ = true;
}
else
{
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("Cancel type %s; ok\n"),
type == PTHREAD_CANCEL_DEFERRED ? ACE_TEXT ("deferred") :
ACE_TEXT ("asynchronous")));
}
#endif
return 0;
}
int
Cancel_Check::open (void *)
{
long flags = THR_NEW_LWP | THR_JOINABLE;
ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Flags before cancels: 0x%x\n"), flags));
flags |= (this->enable_req_ ? THR_CANCEL_ENABLE : THR_CANCEL_DISABLE);
flags |= (this->async_req_ ? THR_CANCEL_ASYNCHRONOUS : THR_CANCEL_DEFERRED);
ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Flags after cancels: 0x%x\n"), flags));
if (this->activate (flags) == -1)
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("%p\n"),
ACE_TEXT ("Cancel_Check activate failed")),
-1);
return 0;
}
/**
* @class Stack_Size_Check
*
* @brief Defines a task that verifies its stack size.
*/
class Stack_Size_Check : public ACE_Task<ACE_MT_SYNCH>
{
public:
/// Create the thread with specified stack size
Stack_Size_Check (size_t stack_size);
//FUZZ: disable check_for_lack_ACE_OS
/// Spawn the thread
virtual int open (void * = 0);
//FUZZ: enable check_for_lack_ACE_OS
/// Check the stack size against what is expected then exit.
virtual int svc (void);
/// Returns true iff failed_ == false.
bool operator! ();
private:
size_t stack_size_;
/// Flag indicating the test failed.
bool failed_;
};
bool
Stack_Size_Check::operator!()
{
return this->failed_;
}
Stack_Size_Check::Stack_Size_Check (size_t stack_size)
: stack_size_ (stack_size), failed_ (false)
{
}
int
Stack_Size_Check::svc (void)
{
size_t my_size = 0;
#ifdef __USE_GNU
pthread_attr_t my_attrs;
pthread_getattr_np (pthread_self (), &my_attrs);
pthread_attr_getstacksize (&my_attrs, &my_size);
pthread_attr_destroy (&my_attrs);
#else
// No known way to do this yet... feel free to fill this in.
my_size = this->stack_size_;
#endif /* __USE_GNU */
// The Posix docs say that the size set for the threads stack will be the
// *minimum* size allocated (the actual size may be bigger because of
// a) pagesize rounding, b) guardsize addition) so we can really only
// check if we have gotten *at least* what we asked for.
if (my_size < this->stack_size_)
{
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("%t: My stack size attr %B; expected %B\n"),
my_size, this->stack_size_));
this->failed_ = true;
}
else
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("%t: My stack size attr %B; correct.\n"),
my_size));
return 0;
}
int
Stack_Size_Check::open (void *)
{
if (this->activate (THR_NEW_LWP | THR_JOINABLE,
1,
0,
ACE_DEFAULT_THREAD_PRIORITY,
-1,
0,
0,
0,
&stack_size_) == -1)
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("%p\n"),
ACE_TEXT ("Stack_Size_Check activate failed")),
-1);
return 0;
}
#endif /* ACE_HAS_THREADS */
int
run_main (int, ACE_TCHAR *[])
{
ACE_START_TEST (ACE_TEXT ("Thread_Attrs_Test"));
int status = 0;
#if defined (ACE_HAS_THREADS)
Stack_Size_Check size_checker (40*1024);
status = size_checker.open(0);
if (status == 0)
{
if (size_checker.wait () == -1)
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("size_checker %p\n"),
ACE_TEXT ("wait")),
1);
if (!size_checker)
status = 1;
}
ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Cancel flags sanity check:\n")
ACE_TEXT (" THR_CANCEL_ENABLE: 0x%x\n")
ACE_TEXT (" THR_CANCEL_DISABLE: 0x%x\n")
ACE_TEXT (" THR_CANCEL_DEFERRED: 0x%x\n")
ACE_TEXT (" THR_CANCEL_ASYNCHRONOUS: 0x%x\n"),
THR_CANCEL_ENABLE,
THR_CANCEL_DISABLE,
THR_CANCEL_DEFERRED,
THR_CANCEL_ASYNCHRONOUS));
// Cancel check args: enable (yes/no), async/deferred
Cancel_Check check1 (true, true);
Cancel_Check check2 (true, false);
Cancel_Check check3 (false, true);
Cancel_Check check4 (false, false);
if (check1.open(0) == 0)
{
check1.wait ();
if (!check1)
status = 1;
check2.open (0);
check2.wait ();
if (!check2)
status = 1;
check3.open (0);
check3.wait ();
if (!check3)
status = 1;
check4.open (0);
check4.wait ();
if (!check4)
status = 1;
}
else
{
ACE_ERROR ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("Cancel_Check open")));
status = 1;
}
#else
ACE_ERROR ((LM_INFO,
ACE_TEXT ("threads not supported on this platform\n")));
#endif /* ACE_HAS_THREADS */
ACE_END_TEST;
return status;
}
|