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

Side by Side Diff: native_client_sdk/src/libraries/c_salt/threading/thread_condition.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
5 #ifndef THREAD_CONDITION_H_
6 #define THREAD_CONDITION_H_
7
8 #include "c_salt/threading/pthread_ext.h"
9
10 struct timespec;
11
12 namespace c_salt {
13 namespace threading {
14 // A wrapper class for condition signaling. Contains a mutex and condition
15 // pair.
16 class ThreadCondition {
17 public:
18 // Initialize the mutex and the condition.
19 ThreadCondition() {
20 pthread_mutex_init(&cond_mutex_, NULL);
21 pthread_cond_init(&condition_, NULL);
22 }
23
24 virtual ~ThreadCondition() {
25 pthread_cond_destroy(&condition_);
26 pthread_mutex_destroy(&cond_mutex_);
27 }
28
29 // Lock the mutex, do this before signalling the condition.
30 void Lock() {
31 pthread_mutex_lock(&cond_mutex_);
32 }
33
34 // Unlock the mutex, do this after raising a signal or after returning from
35 // Wait().
36 void Unlock() {
37 pthread_mutex_unlock(&cond_mutex_);
38 }
39
40 // Signal the condition. This will cause Wait() to return.
41 void Signal() {
42 pthread_cond_broadcast(&condition_);
43 }
44
45 // Wait for a Signal(). Note that this can spuriously return, so you should
46 // have a guard bool to see if the condtion is really true. E.g., in the
47 // calling thread:
48 // cond_lock->Lock();
49 // cond_true = true;
50 // cond_lock->Signal();
51 // cond_lock->Unlock();
52 // In the worker thread:
53 // cond_lock->Lock();
54 // while (!cond_true) {
55 // cond_lock->Wait();
56 // }
57 // cond_lock->Unlock();
58 void Wait() {
59 pthread_cond_wait(&condition_, &cond_mutex_);
60 }
61
62 // Same as Wait, but wait at most until abs_time. Returns false if the system
63 // time exceeds abs_time before the condition is signaled.
64 bool TimedWait(struct timespec *abs_time) {
65 return (pthread_cond_timedwait(&condition_, &cond_mutex_, abs_time) == 0);
66 }
67
68 private:
69 pthread_mutex_t cond_mutex_;
70 pthread_cond_t condition_;
71 };
72 } // namespace threading
73 } // namespace c_salt
74
75 #endif // THREAD_CONDITION_H_
76
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698