1 //
2 // Copyright (c) 2017 The Khronos Group Inc.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //    http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 #include "genericThread.h"
17 
18 #if defined(_WIN32)
19 #include <windows.h>
20 #else // !_WIN32
21 #include <pthread.h>
22 #endif
23 
IStaticReflector(void * data)24 void *genericThread::IStaticReflector(void *data)
25 {
26     genericThread *t = (genericThread *)data;
27     return t->IRun();
28 }
29 
Start(void)30 bool genericThread::Start(void)
31 {
32 #if defined(_WIN32)
33     mHandle = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)IStaticReflector,
34                            this, 0, NULL);
35     return (mHandle != NULL);
36 #else // !_WIN32
37     int error = pthread_create((pthread_t *)&mHandle, NULL, IStaticReflector,
38                                (void *)this);
39     return (error == 0);
40 #endif // !_WIN32
41 }
42 
Join(void)43 void *genericThread::Join(void)
44 {
45 #if defined(_WIN32)
46     WaitForSingleObject((HANDLE)mHandle, INFINITE);
47     return NULL;
48 #else // !_WIN32
49     void *retVal;
50     int error = pthread_join((pthread_t)mHandle, &retVal);
51     if (error != 0) retVal = NULL;
52     return retVal;
53 #endif // !_WIN32
54 }
55