Changeset 4728 for pjproject/trunk/pjlib/src/pj/os_core_unix.c
- Timestamp:
- Feb 4, 2014 10:13:56 AM (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
pjproject/trunk/pjlib/src/pj/os_core_unix.c
r4359 r4728 1 1 /* $Id$ */ 2 /* 2 /* 3 3 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com) 4 4 * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org> … … 16 16 * You should have received a copy of the GNU General Public License 17 17 * along with this program; if not, write to the Free Software 18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 19 19 */ 20 20 /* … … 165 165 pj_log_init(); 166 166 167 /* Initialize exception ID for the pool. 167 /* Initialize exception ID for the pool. 168 168 * Must do so after critical section is configured. 169 169 */ … … 171 171 if (rc != PJ_SUCCESS) 172 172 return rc; 173 173 174 174 /* Init random seed. */ 175 175 /* Or probably not. Let application in charge of this */ … … 188 188 } 189 189 } 190 #endif 190 #endif 191 191 192 192 /* Flag PJLIB as initialized */ … … 383 383 * Get native thread handle 384 384 */ 385 PJ_DEF(void*) pj_thread_get_os_handle(pj_thread_t *thread) 385 PJ_DEF(void*) pj_thread_get_os_handle(pj_thread_t *thread) 386 386 { 387 387 PJ_ASSERT_RETURN(thread, NULL); … … 441 441 442 442 if(cstr_thread_name && pj_strlen(&thread_name) < sizeof(thread->obj_name)-1) 443 pj_ansi_snprintf(thread->obj_name, sizeof(thread->obj_name), 443 pj_ansi_snprintf(thread->obj_name, sizeof(thread->obj_name), 444 444 cstr_thread_name, thread->thread); 445 445 else 446 pj_ansi_snprintf(thread->obj_name, sizeof(thread->obj_name), 446 pj_ansi_snprintf(thread->obj_name, sizeof(thread->obj_name), 447 447 "thr%p", (void*)thread->thread); 448 448 449 449 rc = pj_thread_local_set(thread_tls_id, thread); 450 450 if (rc != PJ_SUCCESS) { … … 458 458 thread->stk_max_usage = 0; 459 459 #else 460 stack_ptr = '\0';460 PJ_UNUSED_ARG(stack_ptr); 461 461 #endif 462 462 … … 533 533 * pj_thread_create(...) 534 534 */ 535 PJ_DEF(pj_status_t) pj_thread_create( pj_pool_t *pool, 535 PJ_DEF(pj_status_t) pj_thread_create( pj_pool_t *pool, 536 536 const char *thread_name, 537 pj_thread_proc *proc, 537 pj_thread_proc *proc, 538 538 void *arg, 539 pj_size_t stack_size, 539 pj_size_t stack_size, 540 540 unsigned flags, 541 541 pj_thread_t **ptr_thread) … … 555 555 rec = (struct pj_thread_t*) pj_pool_zalloc(pool, sizeof(pj_thread_t)); 556 556 PJ_ASSERT_RETURN(rec, PJ_ENOMEM); 557 557 558 558 /* Set name. */ 559 if (!thread_name) 559 if (!thread_name) 560 560 thread_name = "thr%p"; 561 561 562 562 if (strchr(thread_name, '%')) { 563 563 pj_ansi_snprintf(rec->obj_name, PJ_MAX_OBJ_NAME, thread_name, rec); … … 587 587 pj_assert(rec->suspended_mutex == NULL); 588 588 } 589 589 590 590 591 591 /* Init thread attributes */ … … 668 668 #if PJ_HAS_THREADS 669 669 pj_thread_t *rec = (pj_thread_t*)pj_thread_local_get(thread_tls_id); 670 670 671 671 if (rec == NULL) { 672 672 pj_assert(!"Calling pjlib from unknown/external thread. You must " … … 706 706 return PJ_SUCCESS; 707 707 else { 708 /* Calling pthread_join() on a thread that no longer exists and 709 * getting back ESRCH isn't an error (in this context). 708 /* Calling pthread_join() on a thread that no longer exists and 709 * getting back ESRCH isn't an error (in this context). 710 710 * Thanks Phil Torre <ptorre@zetron.com>. 711 711 */ … … 762 762 /* MacOS X (reported on 10.5) seems to always set errno to ETIMEDOUT. 763 763 * It does so because usleep() is declared to return int, and we're 764 * supposed to check for errno only when usleep() returns non-zero. 764 * supposed to check for errno only when usleep() returns non-zero. 765 765 * Unfortunately, usleep() is declared to return void in other platforms 766 * so it's not possible to always check for the return value (unless 766 * so it's not possible to always check for the return value (unless 767 767 * we add a detection routine in autoconf). 768 768 * … … 832 832 * pj_atomic_create() 833 833 */ 834 PJ_DEF(pj_status_t) pj_atomic_create( pj_pool_t *pool, 834 PJ_DEF(pj_status_t) pj_atomic_create( pj_pool_t *pool, 835 835 pj_atomic_value_t initial, 836 836 pj_atomic_t **ptr_atomic) … … 842 842 843 843 PJ_ASSERT_RETURN(atomic_var, PJ_ENOMEM); 844 844 845 845 #if PJ_HAS_THREADS 846 846 rc = pj_mutex_create(pool, "atm%p", PJ_MUTEX_SIMPLE, &atomic_var->mutex); … … 880 880 #if PJ_HAS_THREADS 881 881 pj_mutex_unlock( atomic_var->mutex); 882 #endif 882 #endif 883 883 } 884 884 … … 889 889 { 890 890 pj_atomic_value_t oldval; 891 891 892 892 PJ_CHECK_STACK(); 893 893 … … 959 959 /* 960 960 * pj_atomic_add_and_get() 961 */ 962 PJ_DEF(pj_atomic_value_t) pj_atomic_add_and_get( pj_atomic_t *atomic_var, 961 */ 962 PJ_DEF(pj_atomic_value_t) pj_atomic_add_and_get( pj_atomic_t *atomic_var, 963 963 pj_atomic_value_t value ) 964 964 { … … 968 968 pj_mutex_lock(atomic_var->mutex); 969 969 #endif 970 970 971 971 atomic_var->value += value; 972 972 new_value = atomic_var->value; … … 981 981 /* 982 982 * pj_atomic_add() 983 */ 984 PJ_DEF(void) pj_atomic_add( pj_atomic_t *atomic_var, 983 */ 984 PJ_DEF(void) pj_atomic_add( pj_atomic_t *atomic_var, 985 985 pj_atomic_value_t value ) 986 986 { … … 1012 1012 break; 1013 1013 } 1014 if (i == MAX_THREADS) 1014 if (i == MAX_THREADS) 1015 1015 return PJ_ETOOMANY; 1016 1016 1017 1017 tls_flag[i] = 1; 1018 1018 tls[i] = NULL; … … 1119 1119 defined(PJ_PTHREAD_MUTEXATTR_T_HAS_RECURSIVE) 1120 1120 // Phil Torre <ptorre@zetron.com>: 1121 // The RTEMS implementation of POSIX mutexes doesn't include 1121 // The RTEMS implementation of POSIX mutexes doesn't include 1122 1122 // pthread_mutexattr_settype(), so what follows is a hack 1123 1123 // until I get RTEMS patched to support the set/get functions. … … 1132 1132 #endif 1133 1133 } 1134 1134 1135 1135 if (rc != 0) { 1136 1136 return PJ_RETURN_OS_ERROR(rc); … … 1141 1141 return PJ_RETURN_OS_ERROR(rc); 1142 1142 } 1143 1143 1144 1144 rc = pthread_mutexattr_destroy(&attr); 1145 1145 if (rc != 0) { … … 1176 1176 * pj_mutex_create() 1177 1177 */ 1178 PJ_DEF(pj_status_t) pj_mutex_create(pj_pool_t *pool, 1179 const char *name, 1178 PJ_DEF(pj_status_t) pj_mutex_create(pj_pool_t *pool, 1179 const char *name, 1180 1180 int type, 1181 1181 pj_mutex_t **ptr_mutex) … … 1192 1192 if ((rc=init_mutex(mutex, name, type)) != PJ_SUCCESS) 1193 1193 return rc; 1194 1194 1195 1195 *ptr_mutex = mutex; 1196 1196 return PJ_SUCCESS; … … 1204 1204 * pj_mutex_create_simple() 1205 1205 */ 1206 PJ_DEF(pj_status_t) pj_mutex_create_simple( pj_pool_t *pool, 1206 PJ_DEF(pj_status_t) pj_mutex_create_simple( pj_pool_t *pool, 1207 1207 const char *name, 1208 1208 pj_mutex_t **mutex ) … … 1233 1233 1234 1234 #if PJ_DEBUG 1235 PJ_LOG(6,(mutex->obj_name, "Mutex: thread %s is waiting (mutex owner=%s)", 1235 PJ_LOG(6,(mutex->obj_name, "Mutex: thread %s is waiting (mutex owner=%s)", 1236 1236 pj_thread_this()->obj_name, 1237 1237 mutex->owner_name)); 1238 1238 #else 1239 PJ_LOG(6,(mutex->obj_name, "Mutex: thread %s is waiting", 1239 PJ_LOG(6,(mutex->obj_name, "Mutex: thread %s is waiting", 1240 1240 pj_thread_this()->obj_name)); 1241 1241 #endif … … 1251 1251 } 1252 1252 1253 PJ_LOG(6,(mutex->obj_name, 1254 (status==0 ? 1255 "Mutex acquired by thread %s (level=%d)" : 1253 PJ_LOG(6,(mutex->obj_name, 1254 (status==0 ? 1255 "Mutex acquired by thread %s (level=%d)" : 1256 1256 "Mutex acquisition FAILED by %s (level=%d)"), 1257 1257 pj_thread_this()->obj_name, 1258 1258 mutex->nesting_level)); 1259 1259 #else 1260 PJ_LOG(6,(mutex->obj_name, 1260 PJ_LOG(6,(mutex->obj_name, 1261 1261 (status==0 ? "Mutex acquired by thread %s" : "FAILED by %s"), 1262 1262 pj_thread_this()->obj_name)); … … 1291 1291 } 1292 1292 1293 PJ_LOG(6,(mutex->obj_name, "Mutex released by thread %s (level=%d)", 1294 pj_thread_this()->obj_name, 1293 PJ_LOG(6,(mutex->obj_name, "Mutex released by thread %s (level=%d)", 1294 pj_thread_this()->obj_name, 1295 1295 mutex->nesting_level)); 1296 1296 #else 1297 PJ_LOG(6,(mutex->obj_name, "Mutex released by thread %s", 1297 PJ_LOG(6,(mutex->obj_name, "Mutex released by thread %s", 1298 1298 pj_thread_this()->obj_name)); 1299 1299 #endif … … 1322 1322 PJ_ASSERT_RETURN(mutex, PJ_EINVAL); 1323 1323 1324 PJ_LOG(6,(mutex->obj_name, "Mutex: thread %s is trying", 1324 PJ_LOG(6,(mutex->obj_name, "Mutex: thread %s is trying", 1325 1325 pj_thread_this()->obj_name)); 1326 1326 … … 1333 1333 ++mutex->nesting_level; 1334 1334 1335 PJ_LOG(6,(mutex->obj_name, "Mutex acquired by thread %s (level=%d)", 1335 PJ_LOG(6,(mutex->obj_name, "Mutex acquired by thread %s (level=%d)", 1336 1336 pj_thread_this()->obj_name, 1337 1337 mutex->nesting_level)); 1338 1338 #else 1339 PJ_LOG(6,(mutex->obj_name, "Mutex acquired by thread %s", 1339 PJ_LOG(6,(mutex->obj_name, "Mutex acquired by thread %s", 1340 1340 pj_thread_this()->obj_name)); 1341 1341 #endif 1342 1342 } else { 1343 PJ_LOG(6,(mutex->obj_name, "Mutex: thread %s's trylock() failed", 1343 PJ_LOG(6,(mutex->obj_name, "Mutex: thread %s's trylock() failed", 1344 1344 pj_thread_this()->obj_name)); 1345 1345 } 1346 1346 1347 1347 if (status==0) 1348 1348 return PJ_SUCCESS; … … 1426 1426 1427 1427 PJ_UNUSED_ARG(name); 1428 1428 1429 1429 rwm = PJ_POOL_ALLOC_T(pool, pj_rwmutex_t); 1430 1430 PJ_ASSERT_RETURN(rwm, PJ_ENOMEM); … … 1516 1516 * pj_sem_create() 1517 1517 */ 1518 PJ_DEF(pj_status_t) pj_sem_create( pj_pool_t *pool, 1518 PJ_DEF(pj_status_t) pj_sem_create( pj_pool_t *pool, 1519 1519 const char *name, 1520 unsigned initial, 1520 unsigned initial, 1521 1521 unsigned max, 1522 1522 pj_sem_t **ptr_sem) … … 1537 1537 pj_str_t nam; 1538 1538 1539 /* We should use SEM_NAME_LEN, but this doesn't seem to be 1539 /* We should use SEM_NAME_LEN, but this doesn't seem to be 1540 1540 * declared anywhere? The value here is just from trial and error 1541 1541 * to get the longest name supported. … … 1554 1554 1555 1555 /* Create semaphore */ 1556 sem->sem = sem_open(sem_name, O_CREAT|O_EXCL, S_IRUSR|S_IWUSR, 1556 sem->sem = sem_open(sem_name, O_CREAT|O_EXCL, S_IRUSR|S_IWUSR, 1557 1557 initial); 1558 1558 if (sem->sem == SEM_FAILED) … … 1564 1564 #else 1565 1565 sem->sem = PJ_POOL_ALLOC_T(pool, sem_t); 1566 if (sem_init( sem->sem, 0, initial) != 0) 1566 if (sem_init( sem->sem, 0, initial) != 0) 1567 1567 return PJ_RETURN_OS_ERROR(pj_get_native_os_error()); 1568 1568 #endif 1569 1569 1570 1570 /* Set name. */ 1571 1571 if (!name) { … … 1600 1600 PJ_ASSERT_RETURN(sem, PJ_EINVAL); 1601 1601 1602 PJ_LOG(6, (sem->obj_name, "Semaphore: thread %s is waiting", 1602 PJ_LOG(6, (sem->obj_name, "Semaphore: thread %s is waiting", 1603 1603 pj_thread_this()->obj_name)); 1604 1604 1605 1605 result = sem_wait( sem->sem ); 1606 1606 1607 1607 if (result == 0) { 1608 PJ_LOG(6, (sem->obj_name, "Semaphore acquired by thread %s", 1608 PJ_LOG(6, (sem->obj_name, "Semaphore acquired by thread %s", 1609 1609 pj_thread_this()->obj_name)); 1610 1610 } else { 1611 PJ_LOG(6, (sem->obj_name, "Semaphore: thread %s FAILED to acquire", 1611 PJ_LOG(6, (sem->obj_name, "Semaphore: thread %s FAILED to acquire", 1612 1612 pj_thread_this()->obj_name)); 1613 1613 } … … 1635 1635 1636 1636 result = sem_trywait( sem->sem ); 1637 1637 1638 1638 if (result == 0) { 1639 PJ_LOG(6, (sem->obj_name, "Semaphore acquired by thread %s", 1639 PJ_LOG(6, (sem->obj_name, "Semaphore acquired by thread %s", 1640 1640 pj_thread_this()->obj_name)); 1641 } 1641 } 1642 1642 if (result == 0) 1643 1643 return PJ_SUCCESS;
Note: See TracChangeset
for help on using the changeset viewer.