| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | #include <stdlib.h> |
|---|
| 20 | #include <stdio.h> |
|---|
| 21 | #include <sched.h> |
|---|
| 22 | #include <sys/types.h> |
|---|
| 23 | #include <sys/prctl.h> |
|---|
| 24 | #include <sys/wait.h> |
|---|
| 25 | #include <sys/sysmp.h> |
|---|
| 26 | #include <signal.h> |
|---|
| 27 | #include <unistd.h> |
|---|
| 28 | #include <list> |
|---|
| 29 | #include <OpenThreads/Thread> |
|---|
| 30 | #include "SprocMutexPrivateData.h" |
|---|
| 31 | #include "SprocThreadPrivateData.h" |
|---|
| 32 | #include "SprocThreadPrivateActions.h" |
|---|
| 33 | |
|---|
| 34 | using namespace OpenThreads; |
|---|
| 35 | |
|---|
| 36 | extern int errno; |
|---|
| 37 | const char *OPENTHREAD_VERSION_STRING = "Sproc Thread Model, v1.1 ("__DATE__" "__TIME__")"; |
|---|
| 38 | |
|---|
| 39 | #ifdef DEBUG |
|---|
| 40 | #define DPRINTF(arg) printf arg; fflush(stdout); |
|---|
| 41 | #else |
|---|
| 42 | #define DPRINTF(ARG) |
|---|
| 43 | #endif |
|---|
| 44 | |
|---|
| 45 | static void sproc_dead_child_sig_handler(int sigid); |
|---|
| 46 | |
|---|
| 47 | |
|---|
| 48 | |
|---|
| 49 | |
|---|
| 50 | int SprocThreadPrivateData::nextId = 0; |
|---|
| 51 | |
|---|
| 52 | |
|---|
| 53 | |
|---|
| 54 | |
|---|
| 55 | Thread::ThreadPriority Thread::s_masterThreadPriority = |
|---|
| 56 | Thread::THREAD_PRIORITY_MAX; |
|---|
| 57 | |
|---|
| 58 | bool Thread::s_isInitialized = false; |
|---|
| 59 | |
|---|
| 60 | std::list<Thread *> ThreadPrivateActions::s_threadList; |
|---|
| 61 | |
|---|
| 62 | void ThreadPrivateActions::ThreadCancelTest() { |
|---|
| 63 | |
|---|
| 64 | OpenThreads::Thread *t = GetThread(getpid()); |
|---|
| 65 | |
|---|
| 66 | if(t != 0L) { |
|---|
| 67 | |
|---|
| 68 | SprocThreadPrivateData *pd = |
|---|
| 69 | static_cast<SprocThreadPrivateData *>(t->_prvData); |
|---|
| 70 | |
|---|
| 71 | bool *dieflag = GetDeathFlag(t); |
|---|
| 72 | |
|---|
| 73 | if(*dieflag==false) return; |
|---|
| 74 | |
|---|
| 75 | DPRINTF(("(SPROC THREAD) Thread Cancel Test Passed for %d\n", |
|---|
| 76 | getpid())); |
|---|
| 77 | |
|---|
| 78 | if(!pd->cancelFuncStack.empty()) |
|---|
| 79 | pd->cancelFuncStack.top().routine(pd->cancelFuncStack.top().arg); |
|---|
| 80 | |
|---|
| 81 | t->cancelCleanup(); |
|---|
| 82 | pd->isRunning = false; |
|---|
| 83 | |
|---|
| 84 | exit(1); |
|---|
| 85 | } |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | bool *ThreadPrivateActions::GetDeathFlag(Thread *thread) { |
|---|
| 89 | |
|---|
| 90 | SprocThreadPrivateData *pd = |
|---|
| 91 | static_cast<SprocThreadPrivateData *>(thread->_prvData); |
|---|
| 92 | |
|---|
| 93 | return (bool *)(&(pd->dieFlag)); |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | Thread *ThreadPrivateActions::GetThread(pid_t thread_id) { |
|---|
| 97 | |
|---|
| 98 | std::list<Thread *>::iterator iter; |
|---|
| 99 | for(iter = s_threadList.begin(); |
|---|
| 100 | iter != s_threadList.end(); |
|---|
| 101 | ++iter) { |
|---|
| 102 | |
|---|
| 103 | Thread *t = *iter; |
|---|
| 104 | if(t->getProcessId() == thread_id) return t; |
|---|
| 105 | |
|---|
| 106 | } |
|---|
| 107 | |
|---|
| 108 | return 0L; |
|---|
| 109 | |
|---|
| 110 | }; |
|---|
| 111 | |
|---|
| 112 | void ThreadPrivateActions::ThreadCancelHandler(int sigid) { |
|---|
| 113 | |
|---|
| 114 | Thread *t = GetThread(getpid()); |
|---|
| 115 | |
|---|
| 116 | if(t != 0L) { |
|---|
| 117 | |
|---|
| 118 | bool * dieflag = GetDeathFlag(t); |
|---|
| 119 | |
|---|
| 120 | *dieflag = true; |
|---|
| 121 | |
|---|
| 122 | sigset(SIGINT, SIG_DFL); |
|---|
| 123 | unblockproc(getpid()); |
|---|
| 124 | } |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| 127 | |
|---|
| 128 | |
|---|
| 129 | |
|---|
| 130 | void ThreadPrivateActions::StartThread(void *data) |
|---|
| 131 | { |
|---|
| 132 | |
|---|
| 133 | Thread *thread = static_cast<Thread *>(data); |
|---|
| 134 | |
|---|
| 135 | if (thread->_prvData==0) return; |
|---|
| 136 | |
|---|
| 137 | AddThread(thread); |
|---|
| 138 | |
|---|
| 139 | *((Thread **)&PRDA->usr_prda) = (Thread *)thread; |
|---|
| 140 | |
|---|
| 141 | SetThreadSchedulingParams(thread); |
|---|
| 142 | |
|---|
| 143 | SprocThreadPrivateData *pd = |
|---|
| 144 | static_cast<SprocThreadPrivateData *>(thread->_prvData); |
|---|
| 145 | |
|---|
| 146 | sigset(SIGINT, ThreadCancelHandler); |
|---|
| 147 | |
|---|
| 148 | size_t defaultStackSize; |
|---|
| 149 | prctl(PR_GETSTACKSIZE, &defaultStackSize); |
|---|
| 150 | |
|---|
| 151 | if(defaultStackSize < pd->stackSize) { |
|---|
| 152 | prctl(PR_SETSTACKSIZE, pd->stackSize); |
|---|
| 153 | } |
|---|
| 154 | |
|---|
| 155 | prctl(PR_GETSTACKSIZE, &pd->stackSize); |
|---|
| 156 | |
|---|
| 157 | pd->stackSizeLocked = true; |
|---|
| 158 | |
|---|
| 159 | pd->isRunning = true; |
|---|
| 160 | |
|---|
| 161 | |
|---|
| 162 | pd->threadStartedBlock.release(); |
|---|
| 163 | |
|---|
| 164 | thread->run(); |
|---|
| 165 | |
|---|
| 166 | pd->isRunning = false; |
|---|
| 167 | |
|---|
| 168 | RemoveThread(thread); |
|---|
| 169 | |
|---|
| 170 | if(pd->detached == true ) { |
|---|
| 171 | exit(0); |
|---|
| 172 | } |
|---|
| 173 | |
|---|
| 174 | return; |
|---|
| 175 | |
|---|
| 176 | }; |
|---|
| 177 | |
|---|
| 178 | void ThreadPrivateActions::AddThread(Thread *thread) { |
|---|
| 179 | |
|---|
| 180 | s_threadList.push_front(thread); |
|---|
| 181 | |
|---|
| 182 | }; |
|---|
| 183 | |
|---|
| 184 | void ThreadPrivateActions::RemoveThread(Thread *thread) { |
|---|
| 185 | s_threadList.remove(thread); |
|---|
| 186 | }; |
|---|
| 187 | |
|---|
| 188 | void ThreadPrivateActions::PrintThreadSchedulingInfo(Thread *thread) { |
|---|
| 189 | |
|---|
| 190 | int status, my_policy, min_priority, max_priority; |
|---|
| 191 | struct sched_param my_param; |
|---|
| 192 | |
|---|
| 193 | status = sched_getparam(thread->getProcessId(), |
|---|
| 194 | &my_param); |
|---|
| 195 | |
|---|
| 196 | my_policy = sched_getscheduler(thread->getProcessId()); |
|---|
| 197 | |
|---|
| 198 | if(status != 0 || my_policy == -1) { |
|---|
| 199 | |
|---|
| 200 | printf("THREAD INFO (%d) : Get sched param: %s/%s\n", |
|---|
| 201 | unsigned int(thread->getProcessId()), |
|---|
| 202 | strerror(status), |
|---|
| 203 | strerror(errno)); |
|---|
| 204 | } else { |
|---|
| 205 | printf( |
|---|
| 206 | "THREAD INFO (%d) : Thread running at %s / Priority: %d\n", |
|---|
| 207 | unsigned int(thread->getProcessId()), |
|---|
| 208 | (my_policy == SCHED_FIFO ? "SCHEDULE_FIFO" |
|---|
| 209 | : (my_policy == SCHED_RR ? "SCHEDULE_ROUND_ROBIN" |
|---|
| 210 | : (my_policy == SCHED_TS ? "SCHEDULE_TIME_SHARE" |
|---|
| 211 | : (my_policy == SCHED_OTHER ? "SCHEDULE_OTHER" |
|---|
| 212 | : "UNKNOWN")))), |
|---|
| 213 | my_param.sched_priority); |
|---|
| 214 | |
|---|
| 215 | max_priority = sched_get_priority_max(my_policy); |
|---|
| 216 | min_priority = sched_get_priority_min(my_policy); |
|---|
| 217 | |
|---|
| 218 | printf( |
|---|
| 219 | "THREAD INFO (%d) : Max priority: %d, Min priority: %d\n", |
|---|
| 220 | unsigned int(thread->getProcessId()), |
|---|
| 221 | max_priority, min_priority); |
|---|
| 222 | |
|---|
| 223 | } |
|---|
| 224 | |
|---|
| 225 | } |
|---|
| 226 | |
|---|
| 227 | int ThreadPrivateActions::SetThreadSchedulingParams(Thread *thread) { |
|---|
| 228 | |
|---|
| 229 | int status; |
|---|
| 230 | |
|---|
| 231 | int th_priority; |
|---|
| 232 | int max_priority, nominal_priority, min_priority; |
|---|
| 233 | |
|---|
| 234 | max_priority = 0; |
|---|
| 235 | min_priority = 20; |
|---|
| 236 | nominal_priority = (max_priority + min_priority)/2; |
|---|
| 237 | |
|---|
| 238 | switch(thread->getSchedulePriority()) { |
|---|
| 239 | |
|---|
| 240 | case Thread::THREAD_PRIORITY_MAX: |
|---|
| 241 | th_priority = max_priority; |
|---|
| 242 | break; |
|---|
| 243 | |
|---|
| 244 | case Thread::THREAD_PRIORITY_HIGH: |
|---|
| 245 | th_priority = (max_priority + nominal_priority)/2; |
|---|
| 246 | break; |
|---|
| 247 | |
|---|
| 248 | case Thread::THREAD_PRIORITY_NOMINAL: |
|---|
| 249 | th_priority = nominal_priority; |
|---|
| 250 | break; |
|---|
| 251 | |
|---|
| 252 | case Thread::THREAD_PRIORITY_LOW: |
|---|
| 253 | th_priority = (min_priority + nominal_priority)/2; |
|---|
| 254 | break; |
|---|
| 255 | |
|---|
| 256 | case Thread::THREAD_PRIORITY_MIN: |
|---|
| 257 | th_priority = min_priority; |
|---|
| 258 | break; |
|---|
| 259 | |
|---|
| 260 | default: |
|---|
| 261 | th_priority = max_priority; |
|---|
| 262 | break; |
|---|
| 263 | |
|---|
| 264 | } |
|---|
| 265 | |
|---|
| 266 | status = setpriority(PRIO_PROCESS, thread->getProcessId(), |
|---|
| 267 | th_priority); |
|---|
| 268 | |
|---|
| 269 | if(getenv("OUTPUT_THREADLIB_SCHEDULING_INFO") != 0) |
|---|
| 270 | PrintThreadSchedulingInfo(thread); |
|---|
| 271 | |
|---|
| 272 | return status; |
|---|
| 273 | }; |
|---|
| 274 | |
|---|
| 275 | void ThreadPrivateActions::PushCancelFunction(void (*routine)(void *), void *arg) { |
|---|
| 276 | |
|---|
| 277 | Thread *thread = GetThread(getpid()); |
|---|
| 278 | |
|---|
| 279 | if(thread != 0L) { |
|---|
| 280 | SprocThreadPrivateData *pd = |
|---|
| 281 | static_cast<SprocThreadPrivateData *>(thread->_prvData); |
|---|
| 282 | |
|---|
| 283 | SprocThreadPrivateData::CancelFuncStruct c; |
|---|
| 284 | |
|---|
| 285 | pd->cancelFuncStack.push(c); |
|---|
| 286 | |
|---|
| 287 | SprocThreadPrivateData::CancelFuncStruct *cft = &(pd->cancelFuncStack.top()); |
|---|
| 288 | |
|---|
| 289 | cft->routine = routine; |
|---|
| 290 | cft->arg = arg; |
|---|
| 291 | } |
|---|
| 292 | } |
|---|
| 293 | |
|---|
| 294 | void ThreadPrivateActions::PopCancelFunction() { |
|---|
| 295 | |
|---|
| 296 | Thread *thread = GetThread(getpid()); |
|---|
| 297 | |
|---|
| 298 | if(thread != 0L) { |
|---|
| 299 | |
|---|
| 300 | SprocThreadPrivateData *pd = |
|---|
| 301 | static_cast<SprocThreadPrivateData *>(thread->_prvData); |
|---|
| 302 | |
|---|
| 303 | if(!pd->cancelFuncStack.empty()) |
|---|
| 304 | pd->cancelFuncStack.pop(); |
|---|
| 305 | } |
|---|
| 306 | } |
|---|
| 307 | |
|---|
| 308 | |
|---|
| 309 | |
|---|
| 310 | |
|---|
| 311 | |
|---|
| 312 | |
|---|
| 313 | |
|---|
| 314 | int Thread::SetConcurrency(int concurrencyLevel) { |
|---|
| 315 | |
|---|
| 316 | return -1; |
|---|
| 317 | |
|---|
| 318 | }; |
|---|
| 319 | |
|---|
| 320 | |
|---|
| 321 | |
|---|
| 322 | |
|---|
| 323 | |
|---|
| 324 | |
|---|
| 325 | |
|---|
| 326 | int Thread::GetConcurrency() { |
|---|
| 327 | |
|---|
| 328 | return -1; |
|---|
| 329 | |
|---|
| 330 | }; |
|---|
| 331 | |
|---|
| 332 | |
|---|
| 333 | |
|---|
| 334 | |
|---|
| 335 | |
|---|
| 336 | |
|---|
| 337 | |
|---|
| 338 | Thread::Thread() { |
|---|
| 339 | |
|---|
| 340 | if(!s_isInitialized) Init(); |
|---|
| 341 | |
|---|
| 342 | SprocThreadPrivateData *pd = new SprocThreadPrivateData(); |
|---|
| 343 | pd->stackSize = 128*1024; |
|---|
| 344 | pd->stackSizeLocked = false; |
|---|
| 345 | pd->isRunning = false; |
|---|
| 346 | pd->isCanceled = false; |
|---|
| 347 | pd->idSet = false; |
|---|
| 348 | pd->cancelActive = true; |
|---|
| 349 | pd->detached = false; |
|---|
| 350 | pd->uniqueId = pd->nextId; |
|---|
| 351 | pd->nextId++; |
|---|
| 352 | pd->threadPriority = Thread::THREAD_PRIORITY_DEFAULT; |
|---|
| 353 | pd->threadPolicy = Thread::THREAD_SCHEDULE_DEFAULT; |
|---|
| 354 | |
|---|
| 355 | _prvData = static_cast<void *>(pd); |
|---|
| 356 | } |
|---|
| 357 | |
|---|
| 358 | |
|---|
| 359 | |
|---|
| 360 | |
|---|
| 361 | |
|---|
| 362 | |
|---|
| 363 | |
|---|
| 364 | Thread::~Thread() |
|---|
| 365 | { |
|---|
| 366 | DPRINTF(("(SPROC THREAD) %s:%d, In OpenThreads::Thread destructor\n", |
|---|
| 367 | __FILE__, __LINE__)); |
|---|
| 368 | |
|---|
| 369 | SprocThreadPrivateData *pd = |
|---|
| 370 | static_cast<SprocThreadPrivateData *>(_prvData); |
|---|
| 371 | |
|---|
| 372 | if(pd->isRunning) |
|---|
| 373 | { |
|---|
| 374 | |
|---|
| 375 | DPRINTF(("(SPROC THREAD) %s:%d, about to kill OpenThreads::Thread\n", |
|---|
| 376 | __FILE__, __LINE__)); |
|---|
| 377 | |
|---|
| 378 | |
|---|
| 379 | |
|---|
| 380 | |
|---|
| 381 | |
|---|
| 382 | cancel(); |
|---|
| 383 | |
|---|
| 384 | while (pd->isRunning == true) { |
|---|
| 385 | ::usleep(1); |
|---|
| 386 | } |
|---|
| 387 | |
|---|
| 388 | } |
|---|
| 389 | |
|---|
| 390 | |
|---|
| 391 | DPRINTF(("(SPROC THREAD) %s:%d, Thread destroying private data.\n", |
|---|
| 392 | __FILE__, __LINE__)); |
|---|
| 393 | |
|---|
| 394 | |
|---|
| 395 | delete pd; |
|---|
| 396 | |
|---|
| 397 | _prvData = 0; |
|---|
| 398 | } |
|---|
| 399 | |
|---|
| 400 | |
|---|
| 401 | |
|---|
| 402 | |
|---|
| 403 | |
|---|
| 404 | |
|---|
| 405 | |
|---|
| 406 | void Thread::Init() { |
|---|
| 407 | |
|---|
| 408 | if(s_isInitialized) return; |
|---|
| 409 | |
|---|
| 410 | #ifdef GP_DEBUG |
|---|
| 411 | fprintf(stderr, "%s\n", OPENTHREAD_VERSION_STRING); |
|---|
| 412 | #endif |
|---|
| 413 | |
|---|
| 414 | s_masterThreadPriority = Thread::THREAD_PRIORITY_MAX; |
|---|
| 415 | |
|---|
| 416 | s_isInitialized = true; |
|---|
| 417 | |
|---|
| 418 | } |
|---|
| 419 | |
|---|
| 420 | |
|---|
| 421 | |
|---|
| 422 | |
|---|
| 423 | |
|---|
| 424 | |
|---|
| 425 | |
|---|
| 426 | Thread *Thread::CurrentThread() { |
|---|
| 427 | |
|---|
| 428 | return (*(Thread **)&PRDA->usr_prda); |
|---|
| 429 | |
|---|
| 430 | } |
|---|
| 431 | |
|---|
| 432 | |
|---|
| 433 | |
|---|
| 434 | |
|---|
| 435 | |
|---|
| 436 | |
|---|
| 437 | |
|---|
| 438 | int Thread::getThreadId() { |
|---|
| 439 | |
|---|
| 440 | SprocThreadPrivateData *pd = |
|---|
| 441 | static_cast<SprocThreadPrivateData *> (_prvData); |
|---|
| 442 | return pd->uniqueId; |
|---|
| 443 | } |
|---|
| 444 | |
|---|
| 445 | |
|---|
| 446 | |
|---|
| 447 | |
|---|
| 448 | |
|---|
| 449 | |
|---|
| 450 | |
|---|
| 451 | size_t Thread::getProcessId() { |
|---|
| 452 | |
|---|
| 453 | SprocThreadPrivateData *pd = |
|---|
| 454 | static_cast<SprocThreadPrivateData *> (_prvData); |
|---|
| 455 | |
|---|
| 456 | if(pd->idSet == false) return getpid(); |
|---|
| 457 | |
|---|
| 458 | return (size_t)(pd->pid); |
|---|
| 459 | |
|---|
| 460 | } |
|---|
| 461 | |
|---|
| 462 | |
|---|
| 463 | |
|---|
| 464 | |
|---|
| 465 | |
|---|
| 466 | |
|---|
| 467 | |
|---|
| 468 | bool Thread::isRunning() { |
|---|
| 469 | |
|---|
| 470 | SprocThreadPrivateData *pd = |
|---|
| 471 | static_cast<SprocThreadPrivateData *> (_prvData); |
|---|
| 472 | |
|---|
| 473 | return pd->isRunning; |
|---|
| 474 | |
|---|
| 475 | } |
|---|
| 476 | |
|---|
| 477 | |
|---|
| 478 | |
|---|
| 479 | |
|---|
| 480 | |
|---|
| 481 | |
|---|
| 482 | |
|---|
| 483 | int Thread::start() { |
|---|
| 484 | |
|---|
| 485 | SprocThreadPrivateData *pd = |
|---|
| 486 | static_cast<SprocThreadPrivateData *> (_prvData); |
|---|
| 487 | |
|---|
| 488 | pd->threadStartedBlock.reset(); |
|---|
| 489 | |
|---|
| 490 | int pid = sproc(ThreadPrivateActions::StartThread, |
|---|
| 491 | PR_SALL, |
|---|
| 492 | static_cast<void *>(this)); |
|---|
| 493 | |
|---|
| 494 | |
|---|
| 495 | |
|---|
| 496 | if(pid < 0) { |
|---|
| 497 | perror("sproc encountered an error"); |
|---|
| 498 | return -1; |
|---|
| 499 | } |
|---|
| 500 | |
|---|
| 501 | |
|---|
| 502 | |
|---|
| 503 | |
|---|
| 504 | sysmp(MP_RUNANYWHERE_PID, pid); |
|---|
| 505 | |
|---|
| 506 | pd->pid = pid; |
|---|
| 507 | pd->idSet = true; |
|---|
| 508 | |
|---|
| 509 | |
|---|
| 510 | pd->threadStartedBlock.block(); |
|---|
| 511 | |
|---|
| 512 | return 0; |
|---|
| 513 | |
|---|
| 514 | } |
|---|
| 515 | |
|---|
| 516 | |
|---|
| 517 | |
|---|
| 518 | |
|---|
| 519 | |
|---|
| 520 | |
|---|
| 521 | |
|---|
| 522 | int Thread::startThread() |
|---|
| 523 | { |
|---|
| 524 | if (_prvData) return start(); |
|---|
| 525 | else return 0; |
|---|
| 526 | } |
|---|
| 527 | |
|---|
| 528 | |
|---|
| 529 | |
|---|
| 530 | |
|---|
| 531 | |
|---|
| 532 | |
|---|
| 533 | |
|---|
| 534 | int Thread::detach() { |
|---|
| 535 | |
|---|
| 536 | int status = 0; |
|---|
| 537 | |
|---|
| 538 | SprocThreadPrivateData *pd = |
|---|
| 539 | static_cast<SprocThreadPrivateData *> (_prvData); |
|---|
| 540 | |
|---|
| 541 | pd->detached=true; |
|---|
| 542 | sigset(SIGCLD, sproc_dead_child_sig_handler); |
|---|
| 543 | |
|---|
| 544 | return status; |
|---|
| 545 | |
|---|
| 546 | } |
|---|
| 547 | |
|---|
| 548 | |
|---|
| 549 | |
|---|
| 550 | |
|---|
| 551 | |
|---|
| 552 | |
|---|
| 553 | |
|---|
| 554 | int Thread::join() { |
|---|
| 555 | |
|---|
| 556 | int status; |
|---|
| 557 | |
|---|
| 558 | return waitpid((pid_t)getProcessId(), &status, 0); |
|---|
| 559 | |
|---|
| 560 | |
|---|
| 561 | } |
|---|
| 562 | |
|---|
| 563 | |
|---|
| 564 | |
|---|
| 565 | |
|---|
| 566 | |
|---|
| 567 | |
|---|
| 568 | |
|---|
| 569 | int Thread::testCancel() { |
|---|
| 570 | |
|---|
| 571 | if(getpid() != getProcessId()) return -1; |
|---|
| 572 | |
|---|
| 573 | ThreadPrivateActions::ThreadCancelTest(); |
|---|
| 574 | |
|---|
| 575 | return 0; |
|---|
| 576 | } |
|---|
| 577 | |
|---|
| 578 | |
|---|
| 579 | |
|---|
| 580 | |
|---|
| 581 | |
|---|
| 582 | |
|---|
| 583 | |
|---|
| 584 | int Thread::cancel() { |
|---|
| 585 | |
|---|
| 586 | int status = 0; |
|---|
| 587 | |
|---|
| 588 | SprocThreadPrivateData *pd = |
|---|
| 589 | static_cast<SprocThreadPrivateData *> (_prvData); |
|---|
| 590 | |
|---|
| 591 | if(pd->cancelActive) { |
|---|
| 592 | |
|---|
| 593 | status = kill((pid_t)getProcessId(), SIGINT); |
|---|
| 594 | }; |
|---|
| 595 | |
|---|
| 596 | return status; |
|---|
| 597 | |
|---|
| 598 | } |
|---|
| 599 | |
|---|
| 600 | |
|---|
| 601 | |
|---|
| 602 | |
|---|
| 603 | |
|---|
| 604 | |
|---|
| 605 | |
|---|
| 606 | int Thread::setCancelModeDisable() { |
|---|
| 607 | |
|---|
| 608 | SprocThreadPrivateData *pd = |
|---|
| 609 | static_cast<SprocThreadPrivateData *> (_prvData); |
|---|
| 610 | |
|---|
| 611 | pd->cancelActive = false; |
|---|
| 612 | |
|---|
| 613 | return 0; |
|---|
| 614 | |
|---|
| 615 | } |
|---|
| 616 | |
|---|
| 617 | |
|---|
| 618 | |
|---|
| 619 | |
|---|
| 620 | |
|---|
| 621 | |
|---|
| 622 | |
|---|
| 623 | int Thread::setCancelModeAsynchronous() { |
|---|
| 624 | |
|---|
| 625 | SprocThreadPrivateData *pd = |
|---|
| 626 | static_cast<SprocThreadPrivateData *> (_prvData); |
|---|
| 627 | |
|---|
| 628 | pd->cancelActive = true; |
|---|
| 629 | |
|---|
| 630 | return 0; |
|---|
| 631 | } |
|---|
| 632 | |
|---|
| 633 | |
|---|
| 634 | |
|---|
| 635 | |
|---|
| 636 | |
|---|
| 637 | |
|---|
| 638 | |
|---|
| 639 | int Thread::setCancelModeDeferred() { |
|---|
| 640 | |
|---|
| 641 | SprocThreadPrivateData *pd = |
|---|
| 642 | static_cast<SprocThreadPrivateData *> (_prvData); |
|---|
| 643 | |
|---|
| 644 | pd->cancelActive = true; |
|---|
| 645 | |
|---|
| 646 | return 0; |
|---|
| 647 | |
|---|
| 648 | } |
|---|
| 649 | |
|---|
| 650 | |
|---|
| 651 | |
|---|
| 652 | |
|---|
| 653 | |
|---|
| 654 | |
|---|
| 655 | |
|---|
| 656 | int Thread::setSchedulePriority(ThreadPriority priority) { |
|---|
| 657 | |
|---|
| 658 | SprocThreadPrivateData *pd = |
|---|
| 659 | static_cast<SprocThreadPrivateData *> (_prvData); |
|---|
| 660 | |
|---|
| 661 | pd->threadPriority = priority; |
|---|
| 662 | |
|---|
| 663 | if(pd->isRunning) |
|---|
| 664 | return ThreadPrivateActions::SetThreadSchedulingParams(this); |
|---|
| 665 | else |
|---|
| 666 | return 0; |
|---|
| 667 | |
|---|
| 668 | } |
|---|
| 669 | |
|---|
| 670 | |
|---|
| 671 | |
|---|
| 672 | |
|---|
| 673 | |
|---|
| 674 | |
|---|
| 675 | |
|---|
| 676 | int Thread::getSchedulePriority() { |
|---|
| 677 | |
|---|
| 678 | SprocThreadPrivateData *pd = |
|---|
| 679 | static_cast<SprocThreadPrivateData *> (_prvData); |
|---|
| 680 | |
|---|
| 681 | return pd->threadPriority; |
|---|
| 682 | |
|---|
| 683 | } |
|---|
| 684 | |
|---|
| 685 | |
|---|
| 686 | |
|---|
| 687 | |
|---|
| 688 | |
|---|
| 689 | |
|---|
| 690 | |
|---|
| 691 | int Thread::setSchedulePolicy(ThreadPolicy policy) { |
|---|
| 692 | |
|---|
| 693 | return 0; |
|---|
| 694 | |
|---|
| 695 | } |
|---|
| 696 | |
|---|
| 697 | |
|---|
| 698 | |
|---|
| 699 | |
|---|
| 700 | |
|---|
| 701 | |
|---|
| 702 | |
|---|
| 703 | int Thread::getSchedulePolicy() { |
|---|
| 704 | |
|---|
| 705 | SprocThreadPrivateData *pd = |
|---|
| 706 | static_cast<SprocThreadPrivateData *> (_prvData); |
|---|
| 707 | |
|---|
| 708 | return pd->threadPolicy; |
|---|
| 709 | |
|---|
| 710 | } |
|---|
| 711 | |
|---|
| 712 | |
|---|
| 713 | |
|---|
| 714 | |
|---|
| 715 | |
|---|
| 716 | |
|---|
| 717 | |
|---|
| 718 | int Thread::setStackSize(size_t stackSize) { |
|---|
| 719 | |
|---|
| 720 | SprocThreadPrivateData *pd = |
|---|
| 721 | static_cast<SprocThreadPrivateData *> (_prvData); |
|---|
| 722 | |
|---|
| 723 | if(pd->stackSizeLocked == true) return 13; |
|---|
| 724 | |
|---|
| 725 | pd->stackSize = stackSize; |
|---|
| 726 | |
|---|
| 727 | return 0; |
|---|
| 728 | |
|---|
| 729 | } |
|---|
| 730 | |
|---|
| 731 | |
|---|
| 732 | |
|---|
| 733 | |
|---|
| 734 | |
|---|
| 735 | |
|---|
| 736 | |
|---|
| 737 | size_t Thread::getStackSize() { |
|---|
| 738 | |
|---|
| 739 | SprocThreadPrivateData *pd = |
|---|
| 740 | static_cast<SprocThreadPrivateData *> (_prvData); |
|---|
| 741 | |
|---|
| 742 | return pd->stackSize; |
|---|
| 743 | |
|---|
| 744 | } |
|---|
| 745 | |
|---|
| 746 | |
|---|
| 747 | |
|---|
| 748 | |
|---|
| 749 | |
|---|
| 750 | |
|---|
| 751 | |
|---|
| 752 | void Thread::printSchedulingInfo() { |
|---|
| 753 | |
|---|
| 754 | ThreadPrivateActions::PrintThreadSchedulingInfo(this); |
|---|
| 755 | |
|---|
| 756 | } |
|---|
| 757 | |
|---|
| 758 | |
|---|
| 759 | |
|---|
| 760 | |
|---|
| 761 | |
|---|
| 762 | |
|---|
| 763 | |
|---|
| 764 | int Thread::YieldCurrentThread() { |
|---|
| 765 | |
|---|
| 766 | return sched_yield(); |
|---|
| 767 | |
|---|
| 768 | } |
|---|
| 769 | |
|---|
| 770 | |
|---|
| 771 | |
|---|
| 772 | |
|---|
| 773 | |
|---|
| 774 | |
|---|
| 775 | int Thread::microSleep(unsigned int microsec) |
|---|
| 776 | { |
|---|
| 777 | return ::usleep(microsec); |
|---|
| 778 | } |
|---|
| 779 | |
|---|
| 780 | static void sproc_dead_child_sig_handler(int sigid) { |
|---|
| 781 | |
|---|
| 782 | #ifdef DEBUG |
|---|
| 783 | int pid, status; |
|---|
| 784 | pid = wait(&status); |
|---|
| 785 | DPRINTF(("(SPROC THREAD) Dead Child Handler Caught Signal, Reaped %d\n", |
|---|
| 786 | pid)); |
|---|
| 787 | #endif |
|---|
| 788 | |
|---|
| 789 | sigset(SIGCLD, sproc_dead_child_sig_handler); |
|---|
| 790 | |
|---|
| 791 | } |
|---|
| 792 | |
|---|
| 793 | int Thread::setProcessorAffinity( unsigned int cpunum ) |
|---|
| 794 | { |
|---|
| 795 | return -1; |
|---|
| 796 | } |
|---|
| 797 | |
|---|
| 798 | |
|---|
| 799 | |
|---|
| 800 | |
|---|
| 801 | |
|---|
| 802 | int OpenThreads::GetNumberOfProcessors() |
|---|
| 803 | { |
|---|
| 804 | return 1; |
|---|
| 805 | } |
|---|
| 806 | |
|---|
| 807 | int OpenThreads::SetProcessorAffinityOfCurrentThread(unsigned int cpunum) |
|---|
| 808 | { |
|---|
| 809 | Thread::Init(); |
|---|
| 810 | |
|---|
| 811 | Thread* thread = Thread::CurrentThread(); |
|---|
| 812 | if (thread) |
|---|
| 813 | { |
|---|
| 814 | return thread->setProcessorAffinity(cpunum); |
|---|
| 815 | } |
|---|
| 816 | else |
|---|
| 817 | { |
|---|
| 818 | |
|---|
| 819 | return -1; |
|---|
| 820 | } |
|---|
| 821 | } |
|---|