summaryrefslogtreecommitdiffstats
path: root/gpu/command_buffer/client/atomicops.cc
blob: 318c337fb643ce168f47a4a3a7b2e218561178a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "../client/atomicops.h"
#include "../common/logging.h"

#if !defined(__native_client__)
#include "base/atomicops.h"
#include "base/synchronization/lock.h"
#else
#include <pthread.h>
#endif

namespace gpu {

void MemoryBarrier() {
#if defined(__native_client__)
  __sync_synchronize();
#else
  base::subtle::MemoryBarrier();
#endif
}

#if defined(__native_client__)

class LockImpl {
 public:
  LockImpl()
       : acquired_(false) {
    pthread_mutex_init(&mutex_, NULL);
  }

  ~LockImpl() {
    pthread_mutex_destroy(&mutex_);
  }

  void Acquire() {
    pthread_mutex_lock(&mutex_);
    acquired_ = true;
  }

  void Release() {
    GPU_DCHECK(acquired_);
    acquired_ = false;
    pthread_mutex_unlock(&mutex_);
  }

  bool Try() {
    bool acquired = pthread_mutex_trylock(&mutex_) == 0;
    if (acquired) {
      acquired_ = true;
    }
    return acquired;
  }

  void AssertAcquired() const {
    GPU_DCHECK(acquired_);
  }

 private:
  bool acquired_;
  pthread_mutex_t mutex_;

  DISALLOW_COPY_AND_ASSIGN(LockImpl);
};

#else  // !__native_client__

class LockImpl : public base::Lock {
};

#endif  // !__native_client__

Lock::Lock()
    : lock_(new LockImpl()) {
}

Lock::~Lock() {
}

void Lock::Acquire() {
  lock_->Acquire();
}

void Lock::Release() {
  lock_->Release();
}

bool Lock::Try() {
  return lock_->Try();
}

void Lock::AssertAcquired() const {
  return lock_->AssertAcquired();
}

}  // namespace gpu