Ignore:
Timestamp:
May 10, 2006 7:24:40 PM (18 years ago)
Author:
bennylp
Message:

Merge-in RTEMS port patch by Phil Torre <ptorre@…>, alpha release.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • pjproject/trunk/pjlib/src/pj/os_core_unix.c

    r338 r433  
    1717 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA  
    1818 */ 
     19/* 
     20 * Contributors: 
     21 * - Thanks for Zetron, Inc. (Phil Torre, ptorre@zetron.com) for donating 
     22 *   the RTEMS port. 
     23 */ 
    1924#define _GNU_SOURCE 
    2025#include <pj/os.h> 
     
    154159#endif    
    155160 
    156     PJ_LOG(4,(THIS_FILE, "pjlib %s for Unix initialized", 
     161    PJ_LOG(4,(THIS_FILE, "pjlib %s for POSIX initialized", 
    157162              PJ_VERSION)); 
    158163 
     
    197202        //*thread_ptr = (pj_thread_t*)pj_thread_local_get (thread_tls_id); 
    198203        //return PJ_SUCCESS; 
     204        PJ_LOG(4,(THIS_FILE, "Info: possibly re-registering existing " 
     205                             "thread")); 
    199206    } 
    200207 
     
    282289    /* Done. */ 
    283290    PJ_LOG(6,(rec->obj_name, "Thread quitting")); 
     291 
    284292    return result; 
    285293} 
     
    299307#if PJ_HAS_THREADS 
    300308    pj_thread_t *rec; 
     309    pthread_attr_t thread_attr; 
     310    void *stack_addr; 
    301311    int rc; 
     312 
     313    PJ_UNUSED_ARG(stack_addr); 
    302314 
    303315    PJ_CHECK_STACK(); 
     
    306318    /* Create thread record and assign name for the thread */ 
    307319    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); 
    310321     
    311322    /* Set name. */ 
     
    320331    } 
    321332 
     333    /* Set default stack size */ 
     334    if (stack_size == 0) 
     335        stack_size = PJ_THREAD_DEFAULT_STACK_SIZE; 
     336 
    322337#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; 
    324339    rec->stk_max_usage = 0; 
    325340#endif 
     
    328343    if (flags & PJ_THREAD_SUSPENDED) { 
    329344        rc = pj_mutex_create_simple(pool, NULL, &rec->suspended_mutex); 
    330         if (rc != PJ_SUCCESS) 
     345        if (rc != PJ_SUCCESS) { 
    331346            return rc; 
     347        } 
    332348 
    333349        pj_mutex_lock(rec->suspended_mutex); 
     
    336352    } 
    337353     
    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 
    339376 
    340377    /* Create the thread. */ 
    341378    rec->proc = proc; 
    342379    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) { 
    345382        return PJ_RETURN_OS_ERROR(rc); 
     383    } 
    346384 
    347385    *ptr_thread = rec; 
     386 
     387    PJ_LOG(6, (rec->obj_name, "Thread created")); 
    348388    return PJ_SUCCESS; 
    349389#else 
     
    424464    if (result == 0) 
    425465        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    } 
    428473#else 
    429474    PJ_CHECK_STACK(); 
     
    438483PJ_DEF(pj_status_t) pj_thread_destroy(pj_thread_t *p) 
    439484{ 
    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 
    444493    return PJ_SUCCESS; 
    445494} 
     
    450499PJ_DEF(pj_status_t) pj_thread_sleep(unsigned msec) 
    451500{ 
    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 */ 
    454521} 
    455522 
     
    514581    pj_status_t rc; 
    515582    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); 
    518584     
    519585#if PJ_HAS_THREADS 
     
    766832    PJ_CHECK_STACK(); 
    767833 
    768     pthread_mutexattr_init(&attr); 
     834    rc = pthread_mutexattr_init(&attr); 
     835    if (rc != 0) 
     836        return PJ_RETURN_OS_ERROR(rc); 
    769837 
    770838    if (type == PJ_MUTEX_SIMPLE) { 
     
    772840        extern int pthread_mutexattr_settype(pthread_mutexattr_t*,int); 
    773841        rc = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_FAST_NP); 
     842#elif defined(PJ_RTEMS) && PJ_RTEMS!=0 
     843        /* Nothing to do, default is simple */ 
    774844#else 
    775845        rc = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL); 
     
    779849        extern int pthread_mutexattr_settype(pthread_mutexattr_t*,int); 
    780850        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; 
    781858#else 
    782859        rc = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); 
     
    832909 
    833910    mutex = pj_pool_alloc(pool, sizeof(*mutex)); 
    834     if (!mutex) return PJ_ENOMEM; 
     911    PJ_ASSERT_RETURN(mutex, PJ_ENOMEM); 
    835912 
    836913    if ((rc=init_mutex(mutex, name, type)) != PJ_SUCCESS) 
     
    10031080 
    10041081/////////////////////////////////////////////////////////////////////////////// 
     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 
    10051089struct pj_rwmutex_t 
    10061090{ 
     
    10951179    return PJ_SUCCESS; 
    10961180} 
     1181 
     1182#endif  /* PJ_EMULATE_RWMUTEX */ 
     1183 
    10971184 
    10981185/////////////////////////////////////////////////////////////////////////////// 
     
    11141201    PJ_ASSERT_RETURN(pool != NULL && ptr_sem != NULL, PJ_EINVAL); 
    11151202 
    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); 
    11181205 
    11191206    if (sem_init( &sem->sem, 0, initial) != 0)  
Note: See TracChangeset for help on using the changeset viewer.