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

Side by Side Diff: mojo/public/cpp/utility/mutex.h

Issue 1784643002: Mojo C++ bindings: remove the utility/ folder. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@13_1_remove_thread_dep
Patch Set: Created 4 years, 9 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
« no previous file with comments | « mojo/public/cpp/utility/lib/thread_local_win.cc ('k') | mojo/public/cpp/utility/run_loop.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2014 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
5 #ifndef MOJO_PUBLIC_CPP_UTILITY_MUTEX_H_
6 #define MOJO_PUBLIC_CPP_UTILITY_MUTEX_H_
7
8 #ifdef _WIN32
9 #error "Not implemented: See crbug.com/342893."
10 #endif
11
12 #include <pthread.h>
13
14 #include "base/macros.h"
15 #include "mojo/public/cpp/system/macros.h"
16
17 namespace mojo {
18
19 #ifdef NDEBUG
20 // Note: Make a C++ constant for |PTHREAD_MUTEX_INITIALIZER|. (We can't directly
21 // use the C macro in an initializer list, since it might expand to |{ ... }|.)
22 namespace internal {
23 const pthread_mutex_t kPthreadMutexInitializer = PTHREAD_MUTEX_INITIALIZER;
24 }
25 #endif
26
27 class Mutex {
28 public:
29 #ifdef NDEBUG
30 Mutex() : mutex_(internal::kPthreadMutexInitializer) {}
31 ~Mutex() { pthread_mutex_destroy(&mutex_); }
32
33 void Lock() { pthread_mutex_lock(&mutex_); }
34 void Unlock() { pthread_mutex_unlock(&mutex_); }
35 bool TryLock() { return pthread_mutex_trylock(&mutex_) == 0; }
36
37 void AssertHeld() {}
38 #else
39 Mutex();
40 ~Mutex();
41
42 void Lock();
43 void Unlock();
44 bool TryLock();
45
46 void AssertHeld();
47 #endif
48
49 private:
50 pthread_mutex_t mutex_;
51
52 DISALLOW_COPY_AND_ASSIGN(Mutex);
53 };
54
55 class MutexLock {
56 public:
57 explicit MutexLock(Mutex* mutex) : mutex_(mutex) { mutex_->Lock(); }
58 ~MutexLock() { mutex_->Unlock(); }
59
60 private:
61 Mutex* const mutex_;
62
63 DISALLOW_COPY_AND_ASSIGN(MutexLock);
64 };
65
66 // Catch bug where variable name is omitted (e.g., |MutexLock (&mu)|).
67 #define MutexLock(x) static_assert(0, "MutexLock() missing variable name");
68
69 } // namespace mojo
70
71 #endif // MOJO_PUBLIC_CPP_UTILITY_MUTEX_H_
OLDNEW
« no previous file with comments | « mojo/public/cpp/utility/lib/thread_local_win.cc ('k') | mojo/public/cpp/utility/run_loop.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698