xref: /aosp_15_r20/external/OpenCL-CTS/test_conformance/api/test_context_destructor_callback.cpp (revision 6467f958c7de8070b317fc65bcb0f6472e388d82)
1 //
2 // Copyright (c) 2020 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 
18 static volatile cl_int sDestructorIndex;
19 
context_destructor_callback(cl_context context,void * userData)20 void CL_CALLBACK context_destructor_callback(cl_context context, void *userData)
21 {
22     int *userPtr = (int *)userData;
23 
24     // ordering of callbacks is guaranteed, meaning we don't need to do atomic
25     // operation here
26     *userPtr = ++sDestructorIndex;
27 }
28 
test_context_destructor_callback(cl_device_id deviceID,cl_context context,cl_command_queue queue,int num_elements)29 int test_context_destructor_callback(cl_device_id deviceID, cl_context context,
30                                      cl_command_queue queue, int num_elements)
31 {
32     cl_int error;
33     clContextWrapper localContext =
34         clCreateContext(NULL, 1, &deviceID, NULL, NULL, &error);
35     test_error(error, "Unable to create local context");
36 
37     // Set up some variables to catch the order in which callbacks are called
38     volatile int callbackOrders[3] = { 0, 0, 0 };
39     sDestructorIndex = 0;
40 
41     // Set up the callbacks
42     error = clSetContextDestructorCallback(
43         localContext, context_destructor_callback, (void *)&callbackOrders[0]);
44     test_error(error, "Unable to set destructor callback");
45 
46     error = clSetContextDestructorCallback(
47         localContext, context_destructor_callback, (void *)&callbackOrders[1]);
48     test_error(error, "Unable to set destructor callback");
49 
50     error = clSetContextDestructorCallback(
51         localContext, context_destructor_callback, (void *)&callbackOrders[2]);
52     test_error(error, "Unable to set destructor callback");
53 
54     // Now release the context, which SHOULD call the callbacks
55     localContext.reset();
56 
57     // At this point, all three callbacks should have already been called
58     int numErrors = 0;
59     for (int i = 0; i < 3; i++)
60     {
61         // Spin waiting for the release to finish.  If you don't call the
62         // context_destructor_callback, you will not pass the test.
63         log_info("\tWaiting for callback %d...\n", i);
64         int wait = 0;
65         while (0 == callbackOrders[i])
66         {
67             usleep(100000); // 1/10th second
68             if (++wait >= 10 * 10)
69             {
70                 log_error("\tERROR: Callback %d was not called within 10 "
71                           "seconds!  Assuming failure.\n",
72                           i + 1);
73                 numErrors++;
74                 break;
75             }
76         }
77 
78         if (callbackOrders[i] != 3 - i)
79         {
80             log_error("\tERROR: Callback %d was called in the wrong order! "
81                       "(Was called order %d, should have been order %d)\n",
82                       i + 1, callbackOrders[i], 3 - i);
83             numErrors++;
84         }
85     }
86 
87     return (numErrors > 0) ? TEST_FAIL : TEST_PASS;
88 }
89