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
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 operation here
25 *userPtr = ++sDestructorIndex;
26 }
27
28 #ifndef ABS
29 #define ABS( x ) ( ( x < 0 ) ? -x : x )
30 #endif
31
test_mem_object_destructor_callback_single(clMemWrapper & memObject)32 int test_mem_object_destructor_callback_single( clMemWrapper &memObject )
33 {
34 cl_int error;
35 int i;
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 = clSetMemObjectDestructorCallback( memObject, mem_destructor_callback, (void*) &callbackOrders[ 0 ] );
43 test_error( error, "Unable to set destructor callback" );
44
45 error = clSetMemObjectDestructorCallback( memObject, mem_destructor_callback, (void*) &callbackOrders[ 1 ] );
46 test_error( error, "Unable to set destructor callback" );
47
48 error = clSetMemObjectDestructorCallback( memObject, mem_destructor_callback, (void*) &callbackOrders[ 2 ] );
49 test_error( error, "Unable to set destructor callback" );
50
51 // Now release the buffer, which SHOULD call the callbacks
52 error = clReleaseMemObject( memObject );
53 test_error( error, "Unable to release test buffer" );
54
55 // Note: since we manually released the mem wrapper, we need to set it to 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( i = 0; i < 3; i++ )
61 {
62 // Spin waiting for the release to finish. If you don't call the mem_destructor_callback, you will not
63 // pass the test. bugzilla 6316
64 while( 0 == callbackOrders[i] )
65 {}
66
67 if( ABS( callbackOrders[ i ] ) != 3-i )
68 {
69 log_error( "\tERROR: Callback %d was called in the wrong order! (Was called order %d, should have been order %d)\n",
70 i+1, ABS( callbackOrders[ i ] ), i );
71 numErrors++;
72 }
73 }
74
75 return ( numErrors > 0 ) ? -1 : 0;
76 }
77
test_mem_object_destructor_callback(cl_device_id deviceID,cl_context context,cl_command_queue queue,int num_elements)78 int test_mem_object_destructor_callback(cl_device_id deviceID, cl_context context, cl_command_queue queue, int num_elements)
79 {
80 clMemWrapper testBuffer, testImage;
81 cl_int error;
82
83
84 // Create a buffer and an image to test callbacks against
85 testBuffer = clCreateBuffer( context, CL_MEM_READ_WRITE, 1024, NULL, &error );
86 test_error( error, "Unable to create testing buffer" );
87
88 if( test_mem_object_destructor_callback_single( testBuffer ) != 0 )
89 {
90 log_error( "ERROR: Destructor callbacks for buffer object FAILED\n" );
91 return -1;
92 }
93
94 if( checkForImageSupport( deviceID ) == 0 )
95 {
96 cl_image_format imageFormat = { CL_RGBA, CL_SIGNED_INT8 };
97 testImage = create_image_2d( context, CL_MEM_READ_ONLY, &imageFormat, 16, 16, 0, NULL, &error );
98 test_error( error, "Unable to create testing image" );
99
100 if( test_mem_object_destructor_callback_single( testImage ) != 0 )
101 {
102 log_error( "ERROR: Destructor callbacks for image object FAILED\n" );
103 return -1;
104 }
105 }
106
107 return 0;
108 }
109