OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef GTEST_PPAPI_THREAD_CONDITION_H_ | 5 #ifndef GTEST_PPAPI_THREAD_CONDITION_H_ |
6 #define GTEST_PPAPI_THREAD_CONDITION_H_ | 6 #define GTEST_PPAPI_THREAD_CONDITION_H_ |
7 | 7 |
8 #include <pthread.h> | 8 #include <pthread.h> |
9 | 9 |
10 struct timespec; | 10 struct timespec; |
(...skipping 23 matching lines...) Expand all Loading... |
34 void Unlock() { | 34 void Unlock() { |
35 pthread_mutex_unlock(&cond_mutex_); | 35 pthread_mutex_unlock(&cond_mutex_); |
36 } | 36 } |
37 | 37 |
38 // Signal the condition. This will cause Wait() to return. | 38 // Signal the condition. This will cause Wait() to return. |
39 void Signal() { | 39 void Signal() { |
40 pthread_cond_broadcast(&condition_); | 40 pthread_cond_broadcast(&condition_); |
41 } | 41 } |
42 | 42 |
43 // Wait for a Signal(). Note that this can spuriously return, so you should | 43 // Wait for a Signal(). Note that this can spuriously return, so you should |
44 // have a guard bool to see if the condtion is really true. E.g., in the | 44 // have a guard bool to see if the condition is really true. E.g., in the |
45 // calling thread: | 45 // calling thread: |
46 // cond_lock->Lock(); | 46 // cond_lock->Lock(); |
47 // cond_true = true; | 47 // cond_true = true; |
48 // cond_lock->Signal(); | 48 // cond_lock->Signal(); |
49 // cond_lock->Unlock(); | 49 // cond_lock->Unlock(); |
50 // In the worker thread: | 50 // In the worker thread: |
51 // cond_lock->Lock(); | 51 // cond_lock->Lock(); |
52 // while (!cond_true) { | 52 // while (!cond_true) { |
53 // cond_lock->Wait(); | 53 // cond_lock->Wait(); |
54 // } | 54 // } |
55 // cond_lock->Unlock(); | 55 // cond_lock->Unlock(); |
56 void Wait() { | 56 void Wait() { |
57 pthread_cond_wait(&condition_, &cond_mutex_); | 57 pthread_cond_wait(&condition_, &cond_mutex_); |
58 } | 58 } |
59 | 59 |
60 private: | 60 private: |
61 pthread_mutex_t cond_mutex_; | 61 pthread_mutex_t cond_mutex_; |
62 pthread_cond_t condition_; | 62 pthread_cond_t condition_; |
63 }; | 63 }; |
64 | 64 |
65 #endif // GTEST_PPAPI_THREAD_CONDITION_H_ | 65 #endif // GTEST_PPAPI_THREAD_CONDITION_H_ |
OLD | NEW |