1 : /*
2 : * Copyright 2010 The Native Client Authors. All rights reserved.
3 : * Use of this source code is governed by a BSD-style license that can
4 : * be found in the LICENSE file.
5 : */
6 :
7 : #include <pthread.h>
8 :
9 : #include "native_client/src/trusted/port/mutex.h"
10 :
11 : /*
12 : * Unfortunately NaClSync does not have the correct recursive
13 : * property so we need to use our own version.
14 : */
15 :
16 : namespace port {
17 :
18 : class Mutex : public IMutex {
19 : public:
20 0 : Mutex() {
21 : pthread_mutexattr_t attr;
22 :
23 : // Create a recursive mutex, so we can reenter on the same thread
24 0 : pthread_mutexattr_init(&attr);
25 0 : pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
26 0 : pthread_mutex_init(&mutex_, &attr);
27 0 : pthread_mutexattr_destroy(&attr);
28 0 : }
29 :
30 0 : ~Mutex() {
31 0 : pthread_mutex_destroy(&mutex_);
32 0 : }
33 :
34 0 : virtual void Lock() {
35 0 : pthread_mutex_lock(&mutex_);
36 0 : }
37 :
38 0 : virtual bool Try() {
39 0 : return pthread_mutex_trylock(&mutex_) == 0;
40 : }
41 :
42 0 : virtual void Unlock() {
43 0 : pthread_mutex_unlock(&mutex_);
44 0 : }
45 :
46 : public:
47 : pthread_mutex_t mutex_;
48 : };
49 :
50 0 : IMutex* IMutex::Allocate() {
51 0 : return new Mutex;
52 : }
53 :
54 0 : void IMutex::Free(IMutex *mutex) {
55 0 : delete static_cast<Mutex*>(mutex);
56 0 : }
57 :
58 : } // namespace port
|