aboutsummaryrefslogtreecommitdiffstats
path: root/include
diff options
context:
space:
mode:
authorzhanyong.wan <zhanyong.wan@861a406c-534a-0410-8894-cb66d6ee9925>2010-02-26 05:42:53 +0000
committerzhanyong.wan <zhanyong.wan@861a406c-534a-0410-8894-cb66d6ee9925>2010-02-26 05:42:53 +0000
commitc85a77a6ab0ef05c4a9a8554bf8c5e1c8687cc75 (patch)
tree45ff15530d24e3b41939398d3afaffe98e11b3ba /include
parent4f874c187beb3d185f7892887c8b13f356bf1fd6 (diff)
downloadgoogletest-c85a77a6ab0ef05c4a9a8554bf8c5e1c8687cc75.tar.gz
googletest-c85a77a6ab0ef05c4a9a8554bf8c5e1c8687cc75.tar.bz2
googletest-c85a77a6ab0ef05c4a9a8554bf8c5e1c8687cc75.zip
Simplifies ThreadStartSemaphore's implementation.
Diffstat (limited to 'include')
-rw-r--r--include/gtest/internal/gtest-port.h38
1 files changed, 26 insertions, 12 deletions
diff --git a/include/gtest/internal/gtest-port.h b/include/gtest/internal/gtest-port.h
index bdf75e2f..6910c7b7 100644
--- a/include/gtest/internal/gtest-port.h
+++ b/include/gtest/internal/gtest-port.h
@@ -220,6 +220,7 @@
#include <regex.h> // NOLINT
#include <strings.h> // NOLINT
#include <sys/types.h> // NOLINT
+#include <time.h> // NOLINT
#include <unistd.h> // NOLINT
#define GTEST_USES_POSIX_RE 1
@@ -935,28 +936,41 @@ class ThreadLocal {
GTEST_DISALLOW_COPY_AND_ASSIGN_(ThreadLocal);
};
+// Sleeps for (roughly) n milli-seconds. This function is only for
+// testing Google Test's own constructs. Don't use it in user tests,
+// either directly or indirectly.
+inline void SleepMilliseconds(int n) {
+ const timespec time = {
+ 0, // 0 seconds.
+ n * 1000L * 1000L, // And n ms.
+ };
+ nanosleep(&time, NULL);
+}
+
// Allows a controller thread to pause execution of newly created
// threads until signalled. Instances of this class must be created
// and destroyed in the controller thread.
//
-// This class is supplied only for testing Google Test's own
-// constructs. Do not use it in user tests, either directly or indirectly.
+// This class is only for testing Google Test's own constructs. Do not
+// use it in user tests, either directly or indirectly.
class ThreadStartSemaphore {
public:
- ThreadStartSemaphore();
- ~ThreadStartSemaphore();
+ ThreadStartSemaphore() : signalled_(false) {}
+
// Signals to all threads created with this semaphore to start. Must
// be called from the controller thread.
- void Signal();
+ void Signal() { signalled_ = true; }
+
// Blocks until the controller thread signals. Must be called from a test
// thread.
- void Wait();
+ void Wait() {
+ while(!signalled_) {
+ SleepMilliseconds(10);
+ }
+ }
private:
- // We cannot use Mutex here as this class is intended for testing it.
- pthread_mutex_t mutex_;
- pthread_cond_t cond_;
- bool signalled_;
+ volatile bool signalled_;
GTEST_DISALLOW_COPY_AND_ASSIGN_(ThreadStartSemaphore);
};
@@ -971,8 +985,8 @@ class ThreadStartSemaphore {
// ThreadWithParam<int> thread(&ThreadFunc, 5, &semaphore);
// semaphore.Signal(); // Allows the thread to start.
//
-// This class is supplied only for testing Google Test's own
-// constructs. Do not use it in user tests, either directly or indirectly.
+// This class is only for testing Google Test's own constructs. Do not
+// use it in user tests, either directly or indirectly.
template <typename T>
class ThreadWithParam {
public: