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

Side by Side Diff: libraries/nacl-mounts/util/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 PACKAGES_LIBRARIES_NACL_MOUNTS_BASE_PTHREAD_HELPERS_H_
7 #define PACKAGES_LIBRARIES_NACL_MOUNTS_BASE_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 private:
51 pthread_mutex_t mutex_;
52 DISALLOW_COPY_AND_ASSIGN(Mutex);
53 };
54
55 class SimpleAutoLock {
56 public:
57 explicit SimpleAutoLock(pthread_mutex_t *lock) {
58 lock_ = lock;
59 pthread_mutex_lock(lock_);
60 }
61 explicit SimpleAutoLock(Mutex& lock) {
62 lock_ = lock.get();
63 pthread_mutex_lock(lock_);
64 }
65 ~SimpleAutoLock() {
66 pthread_mutex_unlock(lock_);
67 }
68 pthread_mutex_t* get() {
69 return lock_;
70 }
71 private:
72 pthread_mutex_t *lock_;
73 };
74
75 class Cond {
76 public:
77 Cond() {
78 pthread_cond_init(&cond_, NULL);
79 }
80
81 ~Cond() {
82 pthread_cond_destroy(&cond_);
83 }
84
85 pthread_cond_t* get() {
86 return &cond_;
87 }
88
89 void broadcast() {
90 pthread_cond_broadcast(&cond_);
91 }
92
93 void signal() {
94 pthread_cond_signal(&cond_);
95 }
96
97 int wait(Mutex& lock) {
98 return pthread_cond_wait(&cond_, lock.get());
99 }
100
101 int timedwait(Mutex& lock, const timespec* abstime) {
102 return pthread_cond_timedwait(&cond_, lock.get(), abstime);
103 }
104
105 private:
106 mutable pthread_cond_t cond_;
107 DISALLOW_COPY_AND_ASSIGN(Cond);
108 };
109
110 class ThreadSafeRefCount {
111 public:
112 ThreadSafeRefCount()
113 : ref_(0) {
114 }
115
116 int32_t AddRef() {
117 __sync_fetch_and_add(&ref_, 1);
118 return ref_;
119 }
120
121 int32_t Release() {
122 __sync_fetch_and_sub(&ref_, 1);
123 return ref_;
124 }
125
126 private:
127 int32_t ref_;
128 DISALLOW_COPY_AND_ASSIGN(ThreadSafeRefCount);
129 };
130
131 #endif // PACKAGES_LIBRARIES_NACL_MOUNTS_BASE_PTHREAD_HELPERS_H_
132
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698