1 // Copyright (C) 2014 The Android Open Source Project 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #ifndef EMUGL_COMMON_TESTING_TEST_THREAD_H 16 #define EMUGL_COMMON_TESTING_TEST_THREAD_H 17 18 #ifdef _WIN32 19 # define WIN32_LEAN_AND_MEAN 1 20 # include <windows.h> 21 #else 22 # include <pthread.h> 23 #endif 24 25 namespace emugl { 26 27 // Very basic platform thread wrapper that only uses a tiny stack. 28 // This shall only be used during unit testing, and allows test code 29 // to not depend on the implementation of emugl::Thread. 30 class TestThread { 31 public: 32 // Main thread function type. 33 typedef void* (ThreadFunction)(void* param); 34 35 // Constructor actually launches a new platform thread. TestThread(ThreadFunction * func,void * funcParam)36 TestThread(ThreadFunction* func, void* funcParam) { 37 #ifdef _WIN32 38 mThread = CreateThread(NULL, 39 16384, 40 (DWORD WINAPI (*)(void*))func, 41 funcParam, 42 NULL, 43 NULL); 44 #else 45 pthread_attr_t attr; 46 pthread_attr_init(&attr); 47 pthread_attr_setstacksize(&attr, 16384); 48 pthread_create(&mThread, &attr, func, funcParam); 49 pthread_attr_destroy(&attr); 50 #endif 51 } 52 ~TestThread()53 ~TestThread() { 54 #ifdef _WIN32 55 CloseHandle(mThread); 56 #endif 57 } 58 join()59 void join() { 60 #ifdef _WIN32 61 WaitForSingleObject(mThread, INFINITE); 62 #else 63 void* ret = NULL; 64 pthread_join(mThread, &ret); 65 #endif 66 } 67 68 private: 69 #ifdef _WIN32 70 HANDLE mThread; 71 #else 72 pthread_t mThread; 73 #endif 74 }; 75 76 } // namespace emugl 77 78 #endif // EMUGL_COMMON_TESTING_TEST_THREAD_H 79