Changeset 1744


Ignore:
Timestamp:
08/11/11 11:45:43 (7 months ago)
Author:
Freddie Akeroyd
Message:

Add pthread locking for Linux. Refs #300

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/napi.c

    r1739 r1744  
    3434#include <stdarg.h> 
    3535#include <stdint.h> 
     36 
     37#include "nxconfig.h" 
    3638#include "napi.h" 
    3739#include "nxstack.h" 
     
    5860#include "nx_stptok.h" 
    5961 
    60 #ifdef _WIN32 
     62#if defined(_WIN32) 
    6163/* 
    6264 *  HDF5 on windows does not do locking for multiple threads conveniently so we will implement it ourselves. 
     
    8890    ( nxilock() , nxiunlock(__call) ) 
    8991 
     92#elif HAVE_LIBPTHREAD 
     93 
     94#include <pthread.h> 
     95 
     96static pthread_mutex_t nx_mutex; 
     97 
     98static void nx_pthread_init() 
     99{ 
     100    pthread_mutexattr_t attr; 
     101    pthread_mutexattr_init(&attr); 
     102    pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); 
     103    pthread_mutex_init(&nx_mutex, &attr); 
     104} 
     105 
     106static int nxilock() 
     107{ 
     108    static pthread_once_t once_control = PTHREAD_ONCE_INIT; 
     109    if (pthread_once(&once_control, nx_pthread_init) != 0) 
     110    { 
     111        NXReportError("pthread_once failed"); 
     112        return NX_ERROR; 
     113    } 
     114    if (pthread_mutex_lock(&nx_mutex) != 0) 
     115    { 
     116        NXReportError("pthread_mutex_lock failed"); 
     117        return NX_ERROR; 
     118    } 
     119    return NX_OK; 
     120} 
     121 
     122static int nxiunlock(int ret) 
     123{ 
     124    if (pthread_mutex_unlock(&nx_mutex) != 0) 
     125    { 
     126        NXReportError("pthread_mutex_unlock failed"); 
     127        return NX_ERROR; 
     128    } 
     129    return ret; 
     130} 
     131 
     132#define LOCKED_CALL(__call) \ 
     133    ( nxilock() , nxiunlock(__call) ) 
     134 
    90135#else 
    91136 
    92137#define LOCKED_CALL(__call) \ 
    93         __call 
     138           __call 
    94139 
    95140#endif /* _WIN32 */ 
Note: See TracChangeset for help on using the changeset viewer.