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     error = clReleaseContext(localContext);
56     test_error(error, "Unable to release local context");
57 
58     // Note: since we manually released the context, we need to set it to NULL
59     // to prevent a double-release
60     localContext = NULL;
61 
62     // At this point, all three callbacks should have already been called
63     int numErrors = 0;
64     for (int i = 0; i < 3; i++)
65     {
66         // Spin waiting for the release to finish.  If you don't call the
67         // context_destructor_callback, you will not pass the test.
68         log_info("\tWaiting for callback %d...\n", i);
69         int wait = 0;
70         while (0 == callbackOrders[i])
71         {
72             usleep(100000); // 1/10th second
73             if (++wait >= 10 * 10)
74             {
75                 log_error("\tERROR: Callback %d was not called within 10 "
76                           "seconds!  Assuming failure.\n",
77                           i + 1);
78                 numErrors++;
79                 break;
80             }
81         }
82 
83         if (callbackOrders[i] != 3 - i)
84         {
85             log_error("\tERROR: Callback %d was called in the wrong order! "
86                       "(Was called order %d, should have been order %d)\n",
87                       i + 1, callbackOrders[i], 3 - i);
88             numErrors++;
89         }
90     }
91 
92     return (numErrors > 0) ? TEST_FAIL : TEST_PASS;
93 }
94