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

Side by Side Diff: experimental/conways_life/threading/condition_lock.h

Issue 10928195: First round of dead file removal (Closed) Base URL: https://github.com/samclegg/nativeclient-sdk.git@master
Patch Set: Created 8 years, 3 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 | « experimental/conways_life/stamp.cc ('k') | experimental/conways_life/threading/pthread_ext.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 (c) 2011 The Native Client 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 CONDITION_LOCK_H_
6 #define CONDITION_LOCK_H_
7
8 #include <pthread.h>
9
10 namespace threading {
11 // Class to manage a lock associated with a specific value. The calling thread
12 // can ask to acquire the lock only when the lock is in a certain condition.
13 class ConditionLock {
14 public:
15 ConditionLock() : condition_value_(0) {
16 InitLock();
17 }
18 explicit ConditionLock(int32_t condition_value)
19 : condition_value_(condition_value) {
20 InitLock();
21 }
22
23 virtual ~ConditionLock() {
24 pthread_cond_destroy(&condition_condition_);
25 pthread_mutex_destroy(&condition_lock_);
26 }
27
28 // Acquire the mutex without regard to the condition.
29 void Lock() {
30 pthread_mutex_lock(&condition_lock_);
31 }
32
33 // Acquire the mutex lock when the lock values are equal. Blocks the
34 // calling thread until the lock can be acquired and the condition is met.
35 void LockWhenCondition(int32_t condition_value) {
36 Lock();
37 while (condition_value != condition_value_) {
38 pthread_cond_wait(&condition_condition_, &condition_lock_);
39 }
40 // When this method returns, |contition_lock_| will be acquired. The
41 // calling thread must unlock it.
42 }
43
44 // Acquire the mutex lock when the lock values are _NOT_ equal. Blocks the
45 // calling thread until the lock can be acquired and the condition is met.
46 void LockWhenNotCondition(int32_t condition_value) {
47 Lock();
48 while (condition_value == condition_value_) {
49 pthread_cond_wait(&condition_condition_, &condition_lock_);
50 }
51 // When this method returns, |contition_lock_| will be acquired. The
52 // calling thread must unlock it.
53 }
54
55 // Release the lock without changing the condition. Signal the condition
56 // so that threads waiting in LockWhenCondtion() will wake up. If there are
57 // no threads waiting for the signal, this has the same effect as a simple
58 // mutex unlock.
59 void Unlock() {
60 pthread_cond_broadcast(&condition_condition_);
61 pthread_mutex_unlock(&condition_lock_);
62 }
63
64 // Release the lock, setting the condition's value. This assumes that
65 // |condition_lock_| has been acquired.
66 void UnlockWithCondition(unsigned int condition_value) {
67 condition_value_ = condition_value;
68 Unlock();
69 }
70
71 // Return the current condition value without any mutex protection.
72 int32_t condition_value() const {
73 return condition_value_;
74 }
75
76 private:
77 void InitLock() {
78 pthread_mutex_init(&condition_lock_, NULL);
79 pthread_cond_init(&condition_condition_, NULL);
80 }
81
82 pthread_mutex_t condition_lock_;
83 pthread_cond_t condition_condition_;
84 int32_t condition_value_;
85 };
86 } // namespace threading
87
88 #endif // CONDITION_LOCK_H_
OLDNEW
« no previous file with comments | « experimental/conways_life/stamp.cc ('k') | experimental/conways_life/threading/pthread_ext.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698