Changeset 10790
- Timestamp:
- 11/19/09 18:17:24 (4 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
OpenSceneGraph/trunk/src/OpenThreads/pthreads/PThread.c++
r10219 r10790 40 40 #endif 41 41 #if defined (__FreeBSD__) || defined (__APPLE__) || defined (__MACH__) 42 #include <sys/types.h>43 #include <sys/sysctl.h>42 #include <sys/types.h> 43 #include <sys/sysctl.h> 44 44 #endif 45 45 … … 112 112 static void *StartThread(void *data) { 113 113 114 Thread *thread = static_cast<Thread *>(data);115 116 PThreadPrivateData *pd =117 static_cast<PThreadPrivateData *>(thread->_prvData);114 Thread *thread = static_cast<Thread *>(data); 115 116 PThreadPrivateData *pd = 117 static_cast<PThreadPrivateData *>(thread->_prvData); 118 118 119 119 … … 138 138 139 139 140 ThreadCleanupStruct tcs;141 tcs.thread = thread;142 tcs.runflag = &pd->isRunning;143 144 // Set local storage so that Thread::CurrentThread() can return the right thing145 int status = pthread_setspecific(PThreadPrivateData::s_tls_key, thread);146 if (status)140 ThreadCleanupStruct tcs; 141 tcs.thread = thread; 142 tcs.runflag = &pd->isRunning; 143 144 // Set local storage so that Thread::CurrentThread() can return the right thing 145 int status = pthread_setspecific(PThreadPrivateData::s_tls_key, thread); 146 if (status) 147 147 { 148 148 printf("Error: pthread_setspecific(,) returned error status, status = %d\n",status); 149 149 } 150 150 151 pthread_cleanup_push(thread_cleanup_handler, &tcs);151 pthread_cleanup_push(thread_cleanup_handler, &tcs); 152 152 153 153 #ifdef ALLOW_PRIORITY_SCHEDULING 154 154 155 //---------------------------------------------------------------------156 // Set the proper scheduling priorities157 //158 SetThreadSchedulingParams(thread);155 //--------------------------------------------------------------------- 156 // Set the proper scheduling priorities 157 // 158 SetThreadSchedulingParams(thread); 159 159 160 160 #endif // ] ALLOW_PRIORITY_SCHEDULING … … 169 169 pd->isRunning = false; 170 170 171 pthread_cleanup_pop(0);171 pthread_cleanup_pop(0); 172 172 173 173 return 0; … … 182 182 #ifdef ALLOW_PRIORITY_SCHEDULING // [ 183 183 184 if(sysconf(_POSIX_THREAD_PRIORITY_SCHEDULING)) {185 186 int status, my_policy, min_priority, max_priority;187 struct sched_param my_param;188 189 status = pthread_getschedparam(thread->getProcessId(),190 &my_policy,191 &my_param);192 193 if(status != 0) {194 printf("THREAD INFO (%d) : Get sched: %s\n",195 thread->getProcessId(),196 strerror(status));197 } else {198 printf(199 "THREAD INFO (%d) : Thread running at %s / Priority: %d\n",200 thread->getProcessId(),201 (my_policy == SCHED_FIFO ? "SCHEDULE_FIFO"202 : (my_policy == SCHED_RR ? "SCHEDULE_ROUND_ROBIN"203 : (my_policy == SCHED_OTHER ? "SCHEDULE_OTHER"204 : "UNKNOWN"))),205 my_param.sched_priority);206 207 max_priority = sched_get_priority_max(my_policy);208 min_priority = sched_get_priority_min(my_policy);209 210 printf(211 "THREAD INFO (%d) : Max priority: %d, Min priority: %d\n",212 thread->getProcessId(),213 max_priority, min_priority);214 215 }216 217 } else {218 printf(219 "THREAD INFO (%d) POSIX Priority scheduling not available\n",220 thread->getProcessId());221 }222 223 fflush(stdout);184 if(sysconf(_POSIX_THREAD_PRIORITY_SCHEDULING)) { 185 186 int status, my_policy, min_priority, max_priority; 187 struct sched_param my_param; 188 189 status = pthread_getschedparam(thread->getProcessId(), 190 &my_policy, 191 &my_param); 192 193 if(status != 0) { 194 printf("THREAD INFO (%d) : Get sched: %s\n", 195 thread->getProcessId(), 196 strerror(status)); 197 } else { 198 printf( 199 "THREAD INFO (%d) : Thread running at %s / Priority: %d\n", 200 thread->getProcessId(), 201 (my_policy == SCHED_FIFO ? "SCHEDULE_FIFO" 202 : (my_policy == SCHED_RR ? "SCHEDULE_ROUND_ROBIN" 203 : (my_policy == SCHED_OTHER ? "SCHEDULE_OTHER" 204 : "UNKNOWN"))), 205 my_param.sched_priority); 206 207 max_priority = sched_get_priority_max(my_policy); 208 min_priority = sched_get_priority_min(my_policy); 209 210 printf( 211 "THREAD INFO (%d) : Max priority: %d, Min priority: %d\n", 212 thread->getProcessId(), 213 max_priority, min_priority); 214 215 } 216 217 } else { 218 printf( 219 "THREAD INFO (%d) POSIX Priority scheduling not available\n", 220 thread->getProcessId()); 221 } 222 223 fflush(stdout); 224 224 225 225 #endif // ] ALLOW_PRIORITY_SCHEDULING … … 233 233 static int SetThreadSchedulingParams(Thread *thread) { 234 234 235 int status = 0;235 int status = 0; 236 236 237 237 #ifdef ALLOW_PRIORITY_SCHEDULING // [ 238 238 239 if(sysconf(_POSIX_THREAD_PRIORITY_SCHEDULING)) {240 241 int th_policy;242 int max_priority, nominal_priority, min_priority;243 sched_param th_param;244 pthread_getschedparam(thread->getProcessId(),245 &th_policy, &th_param);239 if(sysconf(_POSIX_THREAD_PRIORITY_SCHEDULING)) { 240 241 int th_policy; 242 int max_priority, nominal_priority, min_priority; 243 sched_param th_param; 244 pthread_getschedparam(thread->getProcessId(), 245 &th_policy, &th_param); 246 246 247 247 #ifndef __linux__ 248 248 249 switch(thread->getSchedulePolicy()) {250 251 case Thread::THREAD_SCHEDULE_FIFO:252 th_policy = SCHED_FIFO;253 break;254 255 case Thread::THREAD_SCHEDULE_ROUND_ROBIN:256 th_policy = SCHED_RR;257 break;258 259 case Thread::THREAD_SCHEDULE_TIME_SHARE:260 th_policy = SCHED_OTHER;261 break;262 263 default:249 switch(thread->getSchedulePolicy()) { 250 251 case Thread::THREAD_SCHEDULE_FIFO: 252 th_policy = SCHED_FIFO; 253 break; 254 255 case Thread::THREAD_SCHEDULE_ROUND_ROBIN: 256 th_policy = SCHED_RR; 257 break; 258 259 case Thread::THREAD_SCHEDULE_TIME_SHARE: 260 th_policy = SCHED_OTHER; 261 break; 262 263 default: 264 264 #ifdef __sgi 265 th_policy = SCHED_RR;266 #else 267 th_policy = SCHED_FIFO;268 #endif 269 break;270 };271 272 #else 273 th_policy = SCHED_OTHER; // Must protect linux from realtime.265 th_policy = SCHED_RR; 266 #else 267 th_policy = SCHED_FIFO; 268 #endif 269 break; 270 }; 271 272 #else 273 th_policy = SCHED_OTHER; // Must protect linux from realtime. 274 274 #endif 275 275 276 276 #ifdef __linux__ 277 277 278 max_priority = 0;279 min_priority = 20;280 nominal_priority = (max_priority + min_priority)/2;281 282 #else 283 284 max_priority = sched_get_priority_max(th_policy);285 min_priority = sched_get_priority_min(th_policy);286 nominal_priority = (max_priority + min_priority)/2;287 288 #endif 289 290 switch(thread->getSchedulePriority()) {291 292 case Thread::THREAD_PRIORITY_MAX:293 th_param.sched_priority = max_priority;294 break;295 296 case Thread::THREAD_PRIORITY_HIGH:297 th_param.sched_priority = (max_priority + nominal_priority)/2;298 break;299 300 case Thread::THREAD_PRIORITY_NOMINAL:301 th_param.sched_priority = nominal_priority;302 break;303 304 case Thread::THREAD_PRIORITY_LOW:305 th_param.sched_priority = (min_priority + nominal_priority)/2;306 break;307 308 case Thread::THREAD_PRIORITY_MIN:309 th_param.sched_priority = min_priority;310 break;311 312 default:313 th_param.sched_priority = max_priority;314 break;315 316 }317 318 status = pthread_setschedparam(thread->getProcessId(),319 th_policy,320 &th_param);321 322 323 if(getenv("OUTPUT_THREADLIB_SCHEDULING_INFO") != 0)324 PrintThreadSchedulingInfo(thread);325 326 }278 max_priority = 0; 279 min_priority = 20; 280 nominal_priority = (max_priority + min_priority)/2; 281 282 #else 283 284 max_priority = sched_get_priority_max(th_policy); 285 min_priority = sched_get_priority_min(th_policy); 286 nominal_priority = (max_priority + min_priority)/2; 287 288 #endif 289 290 switch(thread->getSchedulePriority()) { 291 292 case Thread::THREAD_PRIORITY_MAX: 293 th_param.sched_priority = max_priority; 294 break; 295 296 case Thread::THREAD_PRIORITY_HIGH: 297 th_param.sched_priority = (max_priority + nominal_priority)/2; 298 break; 299 300 case Thread::THREAD_PRIORITY_NOMINAL: 301 th_param.sched_priority = nominal_priority; 302 break; 303 304 case Thread::THREAD_PRIORITY_LOW: 305 th_param.sched_priority = (min_priority + nominal_priority)/2; 306 break; 307 308 case Thread::THREAD_PRIORITY_MIN: 309 th_param.sched_priority = min_priority; 310 break; 311 312 default: 313 th_param.sched_priority = max_priority; 314 break; 315 316 } 317 318 status = pthread_setschedparam(thread->getProcessId(), 319 th_policy, 320 &th_param); 321 322 323 if(getenv("OUTPUT_THREADLIB_SCHEDULING_INFO") != 0) 324 PrintThreadSchedulingInfo(thread); 325 326 } 327 327 328 328 #endif // ] ALLOW_PRIORITY_SCHEDULING 329 329 330 return status;330 return status; 331 331 }; 332 332 }; … … 406 406 std::cout<<"Error: Thread "<<this<<" still running in destructor"<<std::endl; 407 407 408 //---------------------------------------------------------------------409 // Kill the thread when it is destructed410 //411 cancel();408 //--------------------------------------------------------------------- 409 // Kill the thread when it is destructed 410 // 411 cancel(); 412 412 } 413 413 … … 422 422 423 423 Thread *thread = 424 static_cast<Thread *>(pthread_getspecific(PThreadPrivateData::s_tls_key));424 static_cast<Thread *>(pthread_getspecific(PThreadPrivateData::s_tls_key)); 425 425 426 426 return thread; … … 452 452 if(sysconf(_POSIX_THREAD_PRIORITY_SCHEDULING)) { 453 453 454 int max_priority, nominal_priority, min_priority;455 456 int th_policy;457 sched_param th_param;458 pthread_getschedparam(pthread_self(),459 &th_policy, &th_param);460 461 max_priority = sched_get_priority_max(th_policy);462 min_priority = sched_get_priority_min(th_policy);463 nominal_priority = (max_priority + min_priority)/2;464 465 th_param.sched_priority = nominal_priority;466 467 pthread_setschedparam(pthread_self(),468 th_policy,469 &th_param);470 471 s_masterThreadPriority = Thread::THREAD_PRIORITY_NOMINAL;454 int max_priority, nominal_priority, min_priority; 455 456 int th_policy; 457 sched_param th_param; 458 pthread_getschedparam(pthread_self(), 459 &th_policy, &th_param); 460 461 max_priority = sched_get_priority_max(th_policy); 462 min_priority = sched_get_priority_min(th_policy); 463 nominal_priority = (max_priority + min_priority)/2; 464 465 th_param.sched_priority = nominal_priority; 466 467 pthread_setschedparam(pthread_self(), 468 th_policy, 469 &th_param); 470 471 s_masterThreadPriority = Thread::THREAD_PRIORITY_NOMINAL; 472 472 473 473 } else { 474 474 475 s_masterThreadPriority = Thread::THREAD_PRIORITY_DEFAULT;475 s_masterThreadPriority = Thread::THREAD_PRIORITY_DEFAULT; 476 476 477 477 } … … 584 584 status = pthread_attr_init( &thread_attr ); 585 585 if(status != 0) { 586 return status;586 return status; 587 587 } 588 588 … … 592 592 pthread_attr_getstacksize( &thread_attr, &defaultStackSize); 593 593 if(status != 0) { 594 return status;594 return status; 595 595 } 596 596 597 597 if(defaultStackSize < pd->stackSize) { 598 598 599 pthread_attr_setstacksize( &thread_attr, pd->stackSize);600 if(status != 0) {601 return status;602 }599 pthread_attr_setstacksize( &thread_attr, pd->stackSize); 600 if(status != 0) { 601 return status; 602 } 603 603 } 604 604 … … 608 608 pthread_attr_getstacksize( &thread_attr, &defaultStackSize); 609 609 if(status != 0) { 610 return status;610 return status; 611 611 } 612 612 … … 621 621 622 622 status = pthread_attr_setinheritsched( &thread_attr, 623 PTHREAD_EXPLICIT_SCHED );623 PTHREAD_EXPLICIT_SCHED ); 624 624 625 625 pthread_attr_setscope(&thread_attr, PTHREAD_SCOPE_SYSTEM); … … 628 628 629 629 if(status != 0) { 630 return status;630 return status; 631 631 } 632 632 … … 641 641 642 642 if(status != 0) { 643 return status;643 return status; 644 644 } 645 645 … … 700 700 701 701 if(pthread_self() != pd->tid) 702 return -1;702 return -1; 703 703 704 704 pthread_testcancel(); … … 783 783 784 784 if(pd->isRunning) 785 return ThreadPrivateActions::SetThreadSchedulingParams(this);785 return ThreadPrivateActions::SetThreadSchedulingParams(this); 786 786 else 787 return 0;787 return 0; 788 788 789 789 #else … … 822 822 823 823 if(pd->isRunning) 824 return ThreadPrivateActions::SetThreadSchedulingParams(this);824 return ThreadPrivateActions::SetThreadSchedulingParams(this); 825 825 else 826 return 0;826 return 0; 827 827 #else 828 828 return -1; … … 949 949 size_t num_cpus_length = sizeof(num_cpus); 950 950 #if defined(__FreeBSD__) 951 sysctlbyname("hw.ncpu", &num_cpus, &num_cpus_length, NULL, 0); 951 sysctlbyname("hw.ncpu", &num_cpus, &num_cpus_length, NULL, 0); 952 952 #else 953 953 sysctlbyname("hw.activecpu", &num_cpus, &num_cpus_length, NULL, 0);
