Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(251)

Side by Side Diff: native_client_sdk/src/libraries/c_salt/threading/ref_count.h

Issue 10854137: [NaCl SDK] gtest_ppapi library. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: remove hacks Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 #ifndef REF_COUNT_H_
5 #define REF_COUNT_H_
6
7 #include "c_salt/threading/pthread_ext.h"
8
9 namespace threading {
10
11 // A thread-safe reference counter for class CompletionCallbackFactory.
12 class RefCount {
13 public:
14 RefCount() : ref_(0) {
15 pthread_mutex_init(&mutex_, NULL);
16 }
17 ~RefCount() {
18 pthread_mutex_destroy(&mutex_);
19 }
20
21 int32_t AddRef() {
22 int32_t ret_val = 0;
23 if (pthread_mutex_lock(&mutex_) == PTHREAD_MUTEX_SUCCESS) {
24 ret_val = ++ref_;
25 pthread_mutex_unlock(&mutex_);
26 }
27 return ret_val;
28 }
29
30 int32_t Release() {
31 int32_t ret_val = -1;
32 if (pthread_mutex_lock(&mutex_) == PTHREAD_MUTEX_SUCCESS) {
33 ret_val = --ref_;
34 pthread_mutex_unlock(&mutex_);
35 }
36 return ret_val;
37 }
38
39 private:
40 int32_t ref_;
41 pthread_mutex_t mutex_;
42 };
43
44 } // namespace threading
45 #endif // REF_COUNT_H_
46
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698