Home | History | Annotate | Download | only in ports
      1 #include "SkThread.h"
      2 
      3 #include <pthread.h>
      4 #include <errno.h>
      5 
      6 SkMutex gAtomicMutex;
      7 
      8 int32_t sk_atomic_inc(int32_t* addr)
      9 {
     10     SkAutoMutexAcquire ac(gAtomicMutex);
     11 
     12     int32_t value = *addr;
     13     *addr = value + 1;
     14     return value;
     15 }
     16 
     17 int32_t sk_atomic_dec(int32_t* addr)
     18 {
     19     SkAutoMutexAcquire ac(gAtomicMutex);
     20 
     21     int32_t value = *addr;
     22     *addr = value - 1;
     23     return value;
     24 }
     25 
     26 //////////////////////////////////////////////////////////////////////////////
     27 
     28 static void print_pthread_error(int status)
     29 {
     30     switch (status) {
     31     case 0: // success
     32         break;
     33     case EINVAL:
     34         printf("pthread error [%d] EINVAL\n", status);
     35         break;
     36     case EBUSY:
     37         printf("pthread error [%d] EBUSY\n", status);
     38         break;
     39     default:
     40         printf("pthread error [%d] unknown\n", status);
     41         break;
     42     }
     43 }
     44 
     45 SkMutex::SkMutex(bool isGlobal) : fIsGlobal(isGlobal)
     46 {
     47     if (sizeof(pthread_mutex_t) > sizeof(fStorage))
     48     {
     49         SkDEBUGF(("pthread mutex size = %d\n", sizeof(pthread_mutex_t)));
     50         SkASSERT(!"mutex storage is too small");
     51     }
     52 
     53     int status;
     54     pthread_mutexattr_t attr;
     55 
     56     status = pthread_mutexattr_init(&attr);
     57     print_pthread_error(status);
     58     SkASSERT(0 == status);
     59 
     60     status = pthread_mutex_init((pthread_mutex_t*)fStorage, &attr);
     61     print_pthread_error(status);
     62     SkASSERT(0 == status);
     63 }
     64 
     65 SkMutex::~SkMutex()
     66 {
     67     int status = pthread_mutex_destroy((pthread_mutex_t*)fStorage);
     68 
     69     // only report errors on non-global mutexes
     70     if (!fIsGlobal)
     71     {
     72         print_pthread_error(status);
     73         SkASSERT(0 == status);
     74     }
     75 }
     76 
     77 void SkMutex::acquire()
     78 {
     79     int status = pthread_mutex_lock((pthread_mutex_t*)fStorage);
     80     print_pthread_error(status);
     81     SkASSERT(0 == status);
     82 }
     83 
     84 void SkMutex::release()
     85 {
     86     int status = pthread_mutex_unlock((pthread_mutex_t*)fStorage);
     87     print_pthread_error(status);
     88     SkASSERT(0 == status);
     89 }
     90 
     91