diff options
author | David 'Digit' Turner <digit@google.com> | 2014-07-21 16:32:32 +0000 |
---|---|---|
committer | Android Git Automerger <android-git-automerger@android.com> | 2014-07-21 16:32:32 +0000 |
commit | a268ba5f6bbbc67904124f7368f617dbb3fae6ef (patch) | |
tree | 889f16f76567054ea5fc085f274ceade0a09bd81 /emulator/opengl/shared/emugl/common/thread_unittest.cpp | |
parent | 4af45028b6b9761038a83a445d6d21137e8be2ef (diff) | |
parent | c245592b0d472437ade15a22076c2193441f0eea (diff) | |
download | sdk-a268ba5f6bbbc67904124f7368f617dbb3fae6ef.zip sdk-a268ba5f6bbbc67904124f7368f617dbb3fae6ef.tar.gz sdk-a268ba5f6bbbc67904124f7368f617dbb3fae6ef.tar.bz2 |
am c245592b: Merge changes I551e0f3d,I26d84cf5,I27c993d1 into idea133 automerge: 4e42336
* commit 'c245592b0d472437ade15a22076c2193441f0eea':
emulator/opengl: Remove out-of-process handling code.
emulator/opengl: refactor Thread class.
emulator/opengl: refactor shared library handling.
Diffstat (limited to 'emulator/opengl/shared/emugl/common/thread_unittest.cpp')
-rw-r--r-- | emulator/opengl/shared/emugl/common/thread_unittest.cpp | 109 |
1 files changed, 109 insertions, 0 deletions
diff --git a/emulator/opengl/shared/emugl/common/thread_unittest.cpp b/emulator/opengl/shared/emugl/common/thread_unittest.cpp new file mode 100644 index 0000000..db1931f --- /dev/null +++ b/emulator/opengl/shared/emugl/common/thread_unittest.cpp @@ -0,0 +1,109 @@ +// Copyright (C) 2014 The Android Open Source Project +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "emugl/common/thread.h" + +#include "emugl/common/mutex.h" + +#include <gtest/gtest.h> + +namespace emugl { + +namespace { + +// A simple thread instance that does nothing at all and exits immediately. +class EmptyThread : public ::emugl::Thread { +public: + intptr_t main() { return 42; } +}; + +class CountingThread : public ::emugl::Thread { +public: + class State { + public: + State() : mLock(), mCount(0) {} + ~State() {} + + void increment() { + mLock.lock(); + mCount++; + mLock.unlock(); + } + + int count() const { + int ret; + mLock.lock(); + ret = mCount; + mLock.unlock(); + return ret; + } + + private: + mutable Mutex mLock; + int mCount; + }; + + CountingThread(State* state) : mState(state) {} + + intptr_t main() { + mState->increment(); + return 0; + } + +private: + State* mState; +}; + +} // namespace + +TEST(ThreadTest, SimpleThread) { + Thread* thread = new EmptyThread(); + EXPECT_TRUE(thread); + EXPECT_TRUE(thread->start()); + intptr_t status; + EXPECT_TRUE(thread->wait(&status)); + EXPECT_EQ(42, status); +} + +TEST(ThreadTest, MultipleThreads) { + CountingThread::State state; + const size_t kMaxThreads = 100; + Thread* threads[kMaxThreads]; + + // Create all threads. + for (size_t n = 0; n < kMaxThreads; ++n) { + threads[n] = new CountingThread(&state); + EXPECT_TRUE(threads[n]) << "thread " << n; + } + + // Start them all. + for (size_t n = 0; n < kMaxThreads; ++n) { + EXPECT_TRUE(threads[n]->start()) << "thread " << n; + } + + // Wait for them all. + for (size_t n = 0; n < kMaxThreads; ++n) { + EXPECT_TRUE(threads[n]->wait(NULL)) << "thread " << n; + } + + // Check state. + EXPECT_EQ((int)kMaxThreads, state.count()); + + // Delete them all. + for (size_t n = 0; n < kMaxThreads; ++n) { + delete threads[n]; + } +} + +} // namespace emugl |