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, this, 0, NULL );
34 return ( mHandle != NULL );
35 #else // !_WIN32
36 int error = pthread_create( (pthread_t*)&mHandle, NULL, IStaticReflector, (void *)this );
37 return ( error == 0 );
38 #endif // !_WIN32
39 }
40
Join(void)41 void * genericThread::Join( void )
42 {
43 #if defined(_WIN32)
44 WaitForSingleObject( (HANDLE)mHandle, INFINITE );
45 return NULL;
46 #else // !_WIN32
47 void * retVal;
48 int error = pthread_join( (pthread_t)mHandle, &retVal );
49 if( error != 0 )
50 retVal = NULL;
51 return retVal;
52 #endif // !_WIN32
53 }
54