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

Side by Side Diff: libraries/nacl-mounts/base/PthreadHelpers.h

Issue 10392070: Socket subsystem implementation (Closed) Base URL: http://naclports.googlecode.com/svn/trunk/src/
Patch Set: Created 8 years, 6 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 /*
2 * Copyright (c) 2012 The Native Client Authors. All rights reserved.
3 * Use of this source code is governed by a BSD-style license that can be
4 * found in the LICENSE file.
5 */
6 #ifndef PTHREAD_HELPERS_H
7 #define PTHREAD_HELPERS_H
8
9 #include <assert.h>
10 #include <pthread.h>
11 #include <stdint.h>
12
13 // A macro to disallow the evil copy constructor and operator= functions
14 // This should be used in the private: declarations for a class
15 #define DISALLOW_COPY_AND_ASSIGN(TypeName) \
16 TypeName(const TypeName&); \
17 void operator=(const TypeName&)
18
19 // A macro to disallow all the implicit constructors, namely the
20 // default constructor, copy constructor and operator= functions.
21 //
22 // This should be used in the private: declarations for a class
23 // that wants to prevent anyone from instantiating it. This is
24 // especially useful for classes containing only static methods.
25 #define DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName) \
26 TypeName(); \
27 DISALLOW_COPY_AND_ASSIGN(TypeName)
28
29 class Mutex {
30 public:
31 Mutex() {
32 pthread_mutexattr_t attrs;
33 int result = pthread_mutexattr_init(&attrs);
34 assert(result == 0);
35 result = pthread_mutexattr_settype(&attrs, PTHREAD_MUTEX_RECURSIVE);
36 assert(result == 0);
37 result = pthread_mutex_init(&mutex_, &attrs);
38 assert(result == 0);
39 pthread_mutexattr_destroy(&attrs);
40 }
41
42 ~Mutex() {
43 pthread_mutex_destroy(&mutex_);
44 }
45
46 pthread_mutex_t* get() {
47 return &mutex_;
48 }
49
50 class Lock {
51 public:
52 explicit Lock(Mutex& mutex) : mutex_(mutex) {
53 pthread_mutex_lock(mutex_.get());
54 }
55
56 ~Lock() {
57 pthread_mutex_unlock(mutex_.get());
58 }
59
60 private:
61 Mutex& mutex_;
62 DISALLOW_COPY_AND_ASSIGN(Lock);
63 };
64
65 private:
66 pthread_mutex_t mutex_;
67 DISALLOW_COPY_AND_ASSIGN(Mutex);
68 };
69
70 class Cond {
71 public:
72 Cond() {
73 pthread_cond_init(&cond_, NULL);
74 }
75
76 ~Cond() {
77 pthread_cond_destroy(&cond_);
78 }
79
80 pthread_cond_t* get() {
81 return &cond_;
82 }
83
84 void broadcast() {
85 pthread_cond_broadcast(&cond_);
86 }
87
88 void signal() {
89 pthread_cond_signal(&cond_);
90 }
91
92 int wait(Mutex& mutex) {
93 return pthread_cond_wait(&cond_, mutex.get());
94 }
95
96 int timedwait(Mutex& mutex, const timespec* abstime) {
97 return pthread_cond_timedwait(&cond_, mutex.get(), abstime);
98 }
99
100 private:
101 mutable pthread_cond_t cond_;
102 DISALLOW_COPY_AND_ASSIGN(Cond);
103 };
104
105 class ThreadSafeRefCount {
106 public:
107 ThreadSafeRefCount()
108 : ref_(0) {
109 }
110
111 int32_t AddRef() {
112 __sync_fetch_and_add(&ref_, 1);
113 return ref_;
114 }
115
116 int32_t Release() {
117 __sync_fetch_and_sub(&ref_, 1);
118 return ref_;
119 }
120
121 private:
122 int32_t ref_;
123 DISALLOW_COPY_AND_ASSIGN(ThreadSafeRefCount);
124 };
125
126 #ifndef NDEBUG
127 #define LOG(format, args...) \
128 dbgprintf(format , ## args)
129 #else
130 #define LOG(format, args...)
131 #endif
132
133 #endif // PTHREAD_HELPERS_H
134
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698