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 "compat.h" 17 #include "threadTesting.h" 18 #include "errorHelpers.h" 19 #include <stdio.h> 20 #include <string.h> 21 22 #if !defined(_WIN32) 23 #include <pthread.h> 24 #endif 25 26 #if 0 // Disabed for now 27 28 typedef struct 29 { 30 basefn mFunction; 31 cl_device_id mDevice; 32 cl_context mContext; 33 int mNumElements; 34 } TestFnArgs; 35 36 //////////////////////////////////////////////////////////////////////////////// 37 // Thread-based testing. Spawns a new thread to run the given test function, 38 // then waits for it to complete. The entire idea is that, if the thread crashes, 39 // we can catch it and report it as a failure instead of crashing the entire suite 40 //////////////////////////////////////////////////////////////////////////////// 41 42 void *test_thread_wrapper( void *data ) 43 { 44 TestFnArgs *args; 45 int retVal; 46 cl_context context; 47 48 args = (TestFnArgs *)data; 49 50 /* Create a new context to use (contexts can't cross threads) */ 51 context = clCreateContext(NULL, args->mDeviceGroup); 52 if( context == NULL ) 53 { 54 log_error("clCreateContext failed for new thread\n"); 55 return (void *)(-1); 56 } 57 58 /* Call function */ 59 retVal = args->mFunction( args->mDeviceGroup, args->mDevice, context, args->mNumElements ); 60 61 clReleaseContext( context ); 62 63 return (void *)retVal; 64 } 65 66 int test_threaded_function( basefn fnToTest, cl_device_id device, cl_context context, cl_command_queue queue, int numElements ) 67 { 68 int error; 69 pthread_t threadHdl; 70 void *retVal; 71 TestFnArgs args; 72 73 74 args.mFunction = fnToTest; 75 args.mDeviceGroup = deviceGroup; 76 args.mDevice = device; 77 args.mContext = context; 78 args.mNumElements = numElements; 79 80 81 error = pthread_create( &threadHdl, NULL, test_thread_wrapper, (void *)&args ); 82 if( error != 0 ) 83 { 84 log_error( "ERROR: Unable to create thread for testing!\n" ); 85 return -1; 86 } 87 88 /* Thread has been started, now just wait for it to complete (or crash) */ 89 error = pthread_join( threadHdl, &retVal ); 90 if( error != 0 ) 91 { 92 log_error( "ERROR: Unable to join testing thread!\n" ); 93 return -1; 94 } 95 96 return (int)((intptr_t)retVal); 97 } 98 #endif 99