Changeset 433 for pjproject/trunk/pjlib/src/pj/os_core_unix.c
- Timestamp:
- May 10, 2006 7:24:40 PM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
pjproject/trunk/pjlib/src/pj/os_core_unix.c
r338 r433 17 17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 18 */ 19 /* 20 * Contributors: 21 * - Thanks for Zetron, Inc. (Phil Torre, ptorre@zetron.com) for donating 22 * the RTEMS port. 23 */ 19 24 #define _GNU_SOURCE 20 25 #include <pj/os.h> … … 154 159 #endif 155 160 156 PJ_LOG(4,(THIS_FILE, "pjlib %s for Unixinitialized",161 PJ_LOG(4,(THIS_FILE, "pjlib %s for POSIX initialized", 157 162 PJ_VERSION)); 158 163 … … 197 202 //*thread_ptr = (pj_thread_t*)pj_thread_local_get (thread_tls_id); 198 203 //return PJ_SUCCESS; 204 PJ_LOG(4,(THIS_FILE, "Info: possibly re-registering existing " 205 "thread")); 199 206 } 200 207 … … 282 289 /* Done. */ 283 290 PJ_LOG(6,(rec->obj_name, "Thread quitting")); 291 284 292 return result; 285 293 } … … 299 307 #if PJ_HAS_THREADS 300 308 pj_thread_t *rec; 309 pthread_attr_t thread_attr; 310 void *stack_addr; 301 311 int rc; 312 313 PJ_UNUSED_ARG(stack_addr); 302 314 303 315 PJ_CHECK_STACK(); … … 306 318 /* Create thread record and assign name for the thread */ 307 319 rec = (struct pj_thread_t*) pj_pool_zalloc(pool, sizeof(pj_thread_t)); 308 if (!rec) 309 return PJ_ENOMEM; 320 PJ_ASSERT_RETURN(rec, PJ_ENOMEM); 310 321 311 322 /* Set name. */ … … 320 331 } 321 332 333 /* Set default stack size */ 334 if (stack_size == 0) 335 stack_size = PJ_THREAD_DEFAULT_STACK_SIZE; 336 322 337 #if defined(PJ_OS_HAS_CHECK_STACK) && PJ_OS_HAS_CHECK_STACK!=0 323 rec->stk_size = stack_size ? stack_size : 0xFFFFFFFFUL;338 rec->stk_size = stack_size; 324 339 rec->stk_max_usage = 0; 325 340 #endif … … 328 343 if (flags & PJ_THREAD_SUSPENDED) { 329 344 rc = pj_mutex_create_simple(pool, NULL, &rec->suspended_mutex); 330 if (rc != PJ_SUCCESS) 345 if (rc != PJ_SUCCESS) { 331 346 return rc; 347 } 332 348 333 349 pj_mutex_lock(rec->suspended_mutex); … … 336 352 } 337 353 338 PJ_LOG(6, (rec->obj_name, "Thread created")); 354 355 /* Init thread attributes */ 356 pthread_attr_init(&thread_attr); 357 358 #if defined(PJ_THREAD_SET_STACK_SIZE) && PJ_THREAD_SET_STACK_SIZE!=0 359 /* Set thread's stack size */ 360 rc = pthread_attr_setstacksize(&thread_attr, stack_size); 361 if (rc != 0) 362 return PJ_RETURN_OS_ERROR(rc); 363 #endif /* PJ_THREAD_SET_STACK_SIZE */ 364 365 366 #if defined(PJ_THREAD_ALLOCATE_STACK) && PJ_THREAD_ALLOCATE_STACK!=0 367 /* Allocate memory for the stack */ 368 stack_addr = pj_pool_alloc(pool, stack_size); 369 PJ_ASSERT_RETURN(stack_addr, PJ_ENOMEM); 370 371 rc = pthread_attr_setstackaddr(&thread_attr, stack_addr); 372 if (rc != 0) 373 return PJ_RETURN_OS_ERROR(rc); 374 #endif /* PJ_THREAD_ALLOCATE_STACK */ 375 339 376 340 377 /* Create the thread. */ 341 378 rec->proc = proc; 342 379 rec->arg = arg; 343 rc = pthread_create( &rec->thread, NULL,thread_main, rec);344 if (rc != 0) 380 rc = pthread_create( &rec->thread, &thread_attr, &thread_main, rec); 381 if (rc != 0) { 345 382 return PJ_RETURN_OS_ERROR(rc); 383 } 346 384 347 385 *ptr_thread = rec; 386 387 PJ_LOG(6, (rec->obj_name, "Thread created")); 348 388 return PJ_SUCCESS; 349 389 #else … … 424 464 if (result == 0) 425 465 return PJ_SUCCESS; 426 else 427 return PJ_RETURN_OS_ERROR(result); 466 else { 467 /* Calling pthread_join() on a thread that no longer exists and 468 * getting back ESRCH isn't an error (in this context). 469 * Thanks Phil Torre <ptorre@zetron.com>. 470 */ 471 return result==ESRCH ? PJ_SUCCESS : PJ_RETURN_OS_ERROR(result); 472 } 428 473 #else 429 474 PJ_CHECK_STACK(); … … 438 483 PJ_DEF(pj_status_t) pj_thread_destroy(pj_thread_t *p) 439 484 { 440 /* This function is used to destroy thread handle in other platforms. 441 * I suppose there's nothing to do here.. 442 */ 443 PJ_CHECK_STACK(); 485 PJ_CHECK_STACK(); 486 487 /* Destroy mutex used to suspend thread */ 488 if (p->suspended_mutex) { 489 pj_mutex_destroy(p->suspended_mutex); 490 p->suspended_mutex = NULL; 491 } 492 444 493 return PJ_SUCCESS; 445 494 } … … 450 499 PJ_DEF(pj_status_t) pj_thread_sleep(unsigned msec) 451 500 { 452 PJ_CHECK_STACK(); 453 return usleep(msec * 1000); 501 /* TODO: should change this to something like PJ_OS_HAS_NANOSLEEP */ 502 #if defined(PJ_RTEMS) && PJ_RTEMS!=0 503 enum { NANOSEC_PER_MSEC = 1000000 }; 504 struct timespec req; 505 506 PJ_CHECK_STACK(); 507 req.tv_sec = msec / 1000; 508 req.tv_nsec = (msec % 1000) * NANOSEC_PER_MSEC; 509 510 if (nanosleep(&req, NULL) == 0) 511 return PJ_SUCCESS; 512 513 return PJ_RETURN_OS_ERROR(pj_get_native_os_error()); 514 #else 515 PJ_CHECK_STACK(); 516 if (usleep(msec * 1000) == 0) 517 return PJ_SUCCESS; 518 519 return PJ_RETURN_OS_ERROR(pj_get_native_os_error()); 520 #endif /* PJ_RTEMS */ 454 521 } 455 522 … … 514 581 pj_status_t rc; 515 582 pj_atomic_t *atomic_var = pj_pool_calloc(pool, 1, sizeof(pj_atomic_t)); 516 if (!atomic_var) 517 return PJ_ENOMEM; 583 PJ_ASSERT_RETURN(atomic_var, PJ_ENOMEM); 518 584 519 585 #if PJ_HAS_THREADS … … 766 832 PJ_CHECK_STACK(); 767 833 768 pthread_mutexattr_init(&attr); 834 rc = pthread_mutexattr_init(&attr); 835 if (rc != 0) 836 return PJ_RETURN_OS_ERROR(rc); 769 837 770 838 if (type == PJ_MUTEX_SIMPLE) { … … 772 840 extern int pthread_mutexattr_settype(pthread_mutexattr_t*,int); 773 841 rc = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_FAST_NP); 842 #elif defined(PJ_RTEMS) && PJ_RTEMS!=0 843 /* Nothing to do, default is simple */ 774 844 #else 775 845 rc = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL); … … 779 849 extern int pthread_mutexattr_settype(pthread_mutexattr_t*,int); 780 850 rc = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE_NP); 851 #elif defined(PJ_RTEMS) && PJ_RTEMS!=0 852 // Phil Torre <ptorre@zetron.com>: 853 // The RTEMS implementation of POSIX mutexes doesn't include 854 // pthread_mutexattr_settype(), so what follows is a hack 855 // until I get RTEMS patched to support the set/get functions. 856 PJ_TODO(FIX_RTEMS_RECURSIVE_MUTEX_TYPE) 857 attr.recursive = 1; 781 858 #else 782 859 rc = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); … … 832 909 833 910 mutex = pj_pool_alloc(pool, sizeof(*mutex)); 834 if (!mutex) return PJ_ENOMEM;911 PJ_ASSERT_RETURN(mutex, PJ_ENOMEM); 835 912 836 913 if ((rc=init_mutex(mutex, name, type)) != PJ_SUCCESS) … … 1003 1080 1004 1081 /////////////////////////////////////////////////////////////////////////////// 1082 /* 1083 * Include Read/Write mutex emulation for POSIX platforms that lack it (e.g. 1084 * RTEMS). Otherwise use POSIX rwlock. 1085 */ 1086 #if defined(PJ_EMULATE_RWMUTEX) && PJ_EMULATE_RWMUTEX!=0 1087 # include "os_rwmutex.c" 1088 #else 1005 1089 struct pj_rwmutex_t 1006 1090 { … … 1095 1179 return PJ_SUCCESS; 1096 1180 } 1181 1182 #endif /* PJ_EMULATE_RWMUTEX */ 1183 1097 1184 1098 1185 /////////////////////////////////////////////////////////////////////////////// … … 1114 1201 PJ_ASSERT_RETURN(pool != NULL && ptr_sem != NULL, PJ_EINVAL); 1115 1202 1116 sem = pj_pool_alloc(pool, sizeof(*sem)); 1117 if (!sem) return PJ_ENOMEM;1203 sem = pj_pool_alloc(pool, sizeof(*sem)); 1204 PJ_ASSERT_RETURN(sem, PJ_ENOMEM); 1118 1205 1119 1206 if (sem_init( &sem->sem, 0, initial) != 0)
Note: See TracChangeset
for help on using the changeset viewer.