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 
mem_destructor_callback(cl_mem memObject,void * userData)20 void CL_CALLBACK mem_destructor_callback(cl_mem memObject, 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_mem_object_destructor_callback_single(clMemWrapper & memObject)29 int test_mem_object_destructor_callback_single(clMemWrapper &memObject)
30 {
31     cl_int error;
32 
33     // Set up some variables to catch the order in which callbacks are called
34     volatile int callbackOrders[3] = { 0, 0, 0 };
35     sDestructorIndex = 0;
36 
37     // Set up the callbacks
38     error = clSetMemObjectDestructorCallback(memObject, mem_destructor_callback,
39                                              (void *)&callbackOrders[0]);
40     test_error(error, "Unable to set destructor callback");
41 
42     error = clSetMemObjectDestructorCallback(memObject, mem_destructor_callback,
43                                              (void *)&callbackOrders[1]);
44     test_error(error, "Unable to set destructor callback");
45 
46     error = clSetMemObjectDestructorCallback(memObject, mem_destructor_callback,
47                                              (void *)&callbackOrders[2]);
48     test_error(error, "Unable to set destructor callback");
49 
50     // Now release the buffer, which SHOULD call the callbacks
51     error = clReleaseMemObject(memObject);
52     test_error(error, "Unable to release test buffer");
53 
54     // Note: since we manually released the mem wrapper, we need to set it to
55     // NULL to prevent a double-release
56     memObject = NULL;
57 
58     // At this point, all three callbacks should have already been called
59     int numErrors = 0;
60     for (int i = 0; i < 3; i++)
61     {
62         // Spin waiting for the release to finish.  If you don't call the
63         // mem_destructor_callback, you will not pass the test.  bugzilla 6316
64         log_info("\tWaiting for callback %d...\n", i);
65         int wait = 0;
66         while (0 == callbackOrders[i])
67         {
68             usleep(100000); // 1/10th second
69             if (++wait >= 10 * 10)
70             {
71                 log_error("\tERROR: Callback %d was not called within 10 "
72                           "seconds!  Assuming failure.\n",
73                           i + 1);
74                 numErrors++;
75                 break;
76             }
77         }
78 
79         if (callbackOrders[i] != 3 - i)
80         {
81             log_error("\tERROR: Callback %d was called in the wrong order! "
82                       "(Was called order %d, should have been order %d)\n",
83                       i + 1, callbackOrders[i], 3 - i);
84             numErrors++;
85         }
86     }
87 
88     return (numErrors > 0) ? TEST_FAIL : TEST_PASS;
89 }
90 
test_mem_object_destructor_callback(cl_device_id deviceID,cl_context context,cl_command_queue queue,int num_elements)91 int test_mem_object_destructor_callback(cl_device_id deviceID,
92                                         cl_context context,
93                                         cl_command_queue queue,
94                                         int num_elements)
95 {
96     clMemWrapper testBuffer, testImage;
97     cl_int error;
98 
99 
100     // Create a buffer and an image to test callbacks against
101     testBuffer = clCreateBuffer(context, CL_MEM_READ_WRITE, 1024, NULL, &error);
102     test_error(error, "Unable to create testing buffer");
103 
104     if (test_mem_object_destructor_callback_single(testBuffer) != TEST_PASS)
105     {
106         log_error("ERROR: Destructor callbacks for buffer object FAILED\n");
107         return TEST_FAIL;
108     }
109 
110     if (checkForImageSupport(deviceID) == 0)
111     {
112         cl_image_format imageFormat = { CL_RGBA, CL_SIGNED_INT8 };
113         testImage = create_image_2d(context, CL_MEM_READ_ONLY, &imageFormat, 16,
114                                     16, 0, NULL, &error);
115         test_error(error, "Unable to create testing image");
116 
117         if (test_mem_object_destructor_callback_single(testImage) != TEST_PASS)
118         {
119             log_error("ERROR: Destructor callbacks for image object FAILED\n");
120             return TEST_FAIL;
121         }
122     }
123 
124     return TEST_PASS;
125 }
126