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,
34 cl_command_queue queue, int num_elements)
35 {
36 cl_int error;
37
38
39 // Set up a user event to act as a gate
40 clEventWrapper gateEvent = clCreateUserEvent(context, &error);
41 test_error(error, "Unable to create user gate event");
42
43 // Set up a few actions gated on the user event
44 NDRangeKernelAction action1;
45 ReadBufferAction action2;
46 WriteBufferAction action3;
47
48 clEventWrapper actionEvents[3];
49 Action *actions[] = { &action1, &action2, &action3, NULL };
50
51 for (int i = 0; actions[i] != NULL; i++)
52 {
53 error = actions[i]->Setup(deviceID, context, queue);
54 test_error(error, "Unable to set up test action");
55
56 error = actions[i]->Execute(queue, 1, &gateEvent, &actionEvents[i]);
57 test_error(error, "Unable to execute test action");
58 }
59
60 // Now, instead of releasing the gate, we spawn a separate thread to do so
61 log_info("\tStarting trigger thread...\n");
62 std::thread thread(trigger_user_event, &gateEvent);
63
64 log_info("\tWaiting for actions...\n");
65 error = clWaitForEvents(3, &actionEvents[0]);
66 test_error(error, "Unable to wait for action events");
67
68 thread.join();
69 log_info("\tActions completed.\n");
70
71 // If we got here without error, we're good
72 return 0;
73 }
74