gfx/skia/trunk/src/ports/SkMutex_pthread.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/skia/trunk/src/ports/SkMutex_pthread.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,87 @@
     1.4 +/*
     1.5 + * Copyright 2013 Google Inc.
     1.6 + *
     1.7 + * Use of this source code is governed by a BSD-style license that can be
     1.8 + * found in the LICENSE file.
     1.9 + */
    1.10 +
    1.11 +#ifndef SkMutex_pthread_DEFINED
    1.12 +#define SkMutex_pthread_DEFINED
    1.13 +
    1.14 +/** Posix pthread_mutex based mutex. */
    1.15 +
    1.16 +#ifdef SK_DEBUG_PTHREAD_MUTEX
    1.17 +#include "SkTypes.h"
    1.18 +#define SkDEBUGCODE_PTHREAD_MUTEX(code) code
    1.19 +#else
    1.20 +#define SkDEBUGCODE_PTHREAD_MUTEX(code)
    1.21 +#ifndef SkDebugf
    1.22 +    void SkDebugf(const char format[], ...);
    1.23 +#endif
    1.24 +#endif
    1.25 +
    1.26 +#include <errno.h>
    1.27 +#include <pthread.h>
    1.28 +
    1.29 +// A SkBaseMutex is a POD structure that can be directly initialized
    1.30 +// at declaration time with SK_DECLARE_STATIC/GLOBAL_MUTEX. This avoids the
    1.31 +// generation of a static initializer in the final machine code (and
    1.32 +// a corresponding static finalizer).
    1.33 +struct SkBaseMutex {
    1.34 +    void acquire() { pthread_mutex_lock(&fMutex); }
    1.35 +    void release() { pthread_mutex_unlock(&fMutex); }
    1.36 +    pthread_mutex_t fMutex;
    1.37 +};
    1.38 +
    1.39 +// A normal mutex that requires to be initialized through normal C++ construction,
    1.40 +// i.e. when it's a member of another class, or allocated on the heap.
    1.41 +class SkMutex : public SkBaseMutex {
    1.42 +public:
    1.43 +    SkMutex() {
    1.44 +        SkDEBUGCODE_PTHREAD_MUTEX(int status = )pthread_mutex_init(&fMutex, NULL);
    1.45 +        SkDEBUGCODE_PTHREAD_MUTEX(
    1.46 +            if (status != 0) {
    1.47 +                print_pthread_error(status);
    1.48 +                SkASSERT(0 == status);
    1.49 +            }
    1.50 +        )
    1.51 +    }
    1.52 +
    1.53 +    ~SkMutex() {
    1.54 +        SkDEBUGCODE_PTHREAD_MUTEX(int status = )pthread_mutex_destroy(&fMutex);
    1.55 +        SkDEBUGCODE_PTHREAD_MUTEX(
    1.56 +            if (status != 0) {
    1.57 +                print_pthread_error(status);
    1.58 +                SkASSERT(0 == status);
    1.59 +            }
    1.60 +        )
    1.61 +    }
    1.62 +
    1.63 +private:
    1.64 +    SkMutex(const SkMutex&);
    1.65 +    SkMutex& operator=(const SkMutex&);
    1.66 +
    1.67 +    static void print_pthread_error(int status) {
    1.68 +        switch (status) {
    1.69 +        case 0: // success
    1.70 +            break;
    1.71 +        case EINVAL:
    1.72 +            SkDebugf("pthread error [%d] EINVAL\n", status);
    1.73 +            break;
    1.74 +        case EBUSY:
    1.75 +            SkDebugf("pthread error [%d] EBUSY\n", status);
    1.76 +            break;
    1.77 +        default:
    1.78 +            SkDebugf("pthread error [%d] unknown\n", status);
    1.79 +            break;
    1.80 +        }
    1.81 +    }
    1.82 +};
    1.83 +
    1.84 +// Using POD-style initialization prevents the generation of a static initializer.
    1.85 +#define SK_DECLARE_STATIC_MUTEX(name) static SkBaseMutex name = { PTHREAD_MUTEX_INITIALIZER }
    1.86 +
    1.87 +// Special case used when the static mutex must be available globally.
    1.88 +#define SK_DECLARE_GLOBAL_MUTEX(name) SkBaseMutex name = { PTHREAD_MUTEX_INITIALIZER }
    1.89 +
    1.90 +#endif

mercurial