Repository: mesos Updated Branches: refs/heads/master 19d0a89f2 -> 1b9ce37f3
Used std::thread instead of pthread for stout proc tests. Review: https://reviews.apache.org/r/36828 Project: http://git-wip-us.apache.org/repos/asf/mesos/repo Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/1b9ce37f Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/1b9ce37f Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/1b9ce37f Branch: refs/heads/master Commit: 1b9ce37f37592125487d8a0979364cf6bbce6b6f Parents: 19d0a89 Author: Joris Van Remoortere <[email protected]> Authored: Thu Jul 30 15:04:21 2015 -0700 Committer: Benjamin Hindman <[email protected]> Committed: Thu Jul 30 15:07:36 2015 -0700 ---------------------------------------------------------------------- .../3rdparty/stout/tests/proc_tests.cpp | 55 ++++++++++---------- 1 file changed, 28 insertions(+), 27 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/mesos/blob/1b9ce37f/3rdparty/libprocess/3rdparty/stout/tests/proc_tests.cpp ---------------------------------------------------------------------- diff --git a/3rdparty/libprocess/3rdparty/stout/tests/proc_tests.cpp b/3rdparty/libprocess/3rdparty/stout/tests/proc_tests.cpp index 5d24f21..bdb8a43 100644 --- a/3rdparty/libprocess/3rdparty/stout/tests/proc_tests.cpp +++ b/3rdparty/libprocess/3rdparty/stout/tests/proc_tests.cpp @@ -12,13 +12,15 @@ * limitations under the License */ -#include <pthread.h> #include <unistd.h> // For getpid, getppid. +#include <condition_variable> #include <iostream> #include <list> +#include <mutex> #include <set> #include <string> +#include <thread> #include <gmock/gmock.h> @@ -27,6 +29,7 @@ #include <stout/numify.hpp> #include <stout/os.hpp> #include <stout/proc.hpp> +#include <stout/synchronized.hpp> #include <stout/try.hpp> using proc::CPU; @@ -88,33 +91,27 @@ TEST(ProcTest, SingleThread) } -void* cancelFunction(void*) -{ - // Newly created threads have PTHREAD_CANCEL_ENABLE and - // PTHREAD_CANCEL_DEFERRED so they can be cancelled from the main - // thread. - while (true) { - // Use pthread_testcancel() as opposed to sleep() because we've - // seen sleep() hang on certain linux machines even though sleep - // should be a cancellation point. - pthread_testcancel(); - } - - return NULL; -} - - // NOTE: This test assumes there is only a single thread running for the test. TEST(ProcTest, MultipleThreads) { - size_t numThreads = 5; + const size_t numThreads = 5; + + std::thread* runningThreads[numThreads]; - pthread_t pthreads[numThreads]; + std::mutex mutex; + std::condition_variable cond; + bool stop = false; // Create additional threads. - for (size_t i = 0; i < numThreads; i++) - { - EXPECT_EQ(0, pthread_create(&pthreads[i], NULL, cancelFunction, NULL)); + for (size_t i = 0; i < numThreads; i++) { + runningThreads[i] = new std::thread([&mutex, &cond, &stop]() { + // Wait until the main thread tells us to exit. + synchronized (mutex) { + while (!stop) { + synchronized_wait(&cond, &mutex); + } + } + }); } // Check we have the expected number of threads. @@ -124,11 +121,15 @@ TEST(ProcTest, MultipleThreads) EXPECT_EQ(1u + numThreads, threads.get().size()); EXPECT_EQ(1u, threads.get().count(::getpid())); - // Terminate the threads. - for (size_t i = 0; i < numThreads; i++) - { - EXPECT_EQ(0, pthread_cancel(pthreads[i])); - EXPECT_EQ(0, pthread_join(pthreads[i], NULL)); + // Terminate the additional threads. + synchronized (mutex) { + stop = true; + cond.notify_all(); + } + + for (size_t i = 0; i < numThreads; i++) { + runningThreads[i]->join(); + delete runningThreads[i]; } // There is some delay before /proc updates after the threads have
