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 "testBase.h"
17 #include "action_classes.h"
18 #include "harness/conversions.h"
19
20 #include <thread>
21
22 #if !defined (_MSC_VER)
23 #include <unistd.h>
24 #endif // !_MSC_VER
25
trigger_user_event(cl_event * event)26 void trigger_user_event(cl_event *event)
27 {
28 usleep(1000000);
29 log_info("\tTriggering gate from separate thread...\n");
30 clSetUserEventStatus(*event, CL_COMPLETE);
31 }
32
test_userevents_multithreaded(cl_device_id deviceID,cl_context context,cl_command_queue queue,int num_elements)33 int test_userevents_multithreaded( cl_device_id deviceID, cl_context context, cl_command_queue queue, int num_elements )
34 {
35 cl_int error;
36
37
38 // Set up a user event to act as a gate
39 clEventWrapper gateEvent = clCreateUserEvent( context, &error );
40 test_error( error, "Unable to create user gate event" );
41
42 // Set up a few actions gated on the user event
43 NDRangeKernelAction action1;
44 ReadBufferAction action2;
45 WriteBufferAction action3;
46
47 clEventWrapper actionEvents[ 3 ];
48 Action * actions[] = { &action1, &action2, &action3, NULL };
49
50 for( int i = 0; actions[ i ] != NULL; i++ )
51 {
52 error = actions[ i ]->Setup( deviceID, context, queue );
53 test_error( error, "Unable to set up test action" );
54
55 error = actions[ i ]->Execute( queue, 1, &gateEvent, &actionEvents[ i ] );
56 test_error( error, "Unable to execute test action" );
57 }
58
59 // Now, instead of releasing the gate, we spawn a separate thread to do so
60 log_info( "\tStarting trigger thread...\n" );
61 std::thread thread(trigger_user_event, &gateEvent);
62
63 log_info( "\tWaiting for actions...\n" );
64 error = clWaitForEvents( 3, &actionEvents[ 0 ] );
65 test_error( error, "Unable to wait for action events" );
66
67 thread.join();
68 log_info( "\tActions completed.\n" );
69
70 // If we got here without error, we're good
71 return 0;
72 }
73
74