Skip to content

Commit f7f479a

Browse files
committed
Use std::unique_lock instead of custom wrapper
1 parent 29c2bc1 commit f7f479a

19 files changed

+165
-166
lines changed

include/acl_thread.h

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ extern ACL_TLS int acl_global_lock_count;
3434
extern ACL_TLS int acl_inside_sig_flag;
3535
extern ACL_TLS int acl_inside_sig_old_lock_count;
3636

37-
extern acl_mutex_wrapper_t acl_wrapper_mutex;
37+
extern acl_mutex_wrapper_t acl_mutex_wrapper;
3838

3939
// -- signal handler functions --
4040
// When we enter a signal handler, we save "acl_global_lock_count" to
@@ -128,28 +128,6 @@ class acl_mutex_wrapper_t {
128128
void unlock() { acl_unlock(); }
129129
};
130130

131-
// Custom implementation of std::lock_guard that takes in a boolean argument to
132-
// determine if to lock or not. Future improvement: this should be templated to
133-
// take in any mutex class that adheres to the BasicLockable requirement.
134-
class acl_condition_lock_guard {
135-
public:
136-
explicit acl_condition_lock_guard(acl_mutex_wrapper_t &mtex, bool cond)
137-
: mtex(mtex), cond(cond) {
138-
if (cond) {
139-
mtex.lock();
140-
}
141-
}
142-
~acl_condition_lock_guard() {
143-
if (cond) {
144-
mtex.unlock();
145-
}
146-
}
147-
148-
private:
149-
acl_mutex_wrapper_t &mtex;
150-
bool cond;
151-
};
152-
153131
// RAII class warpper for acl specific functionality acl_suspend_lock()
154132
class acl_suspend_lock_guard {
155133
public:

src/acl_command.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
ACL_EXPORT
3939
CL_API_ENTRY cl_int CL_API_CALL
4040
clEnqueueBarrierIntelFPGA(cl_command_queue command_queue) {
41-
std::scoped_lock<acl_mutex_wrapper_t> lock{acl_wrapper_mutex};
41+
std::scoped_lock<acl_mutex_wrapper_t> lock{acl_mutex_wrapper};
4242

4343
if (!acl_command_queue_is_valid(command_queue)) {
4444
return CL_INVALID_COMMAND_QUEUE;
@@ -66,7 +66,7 @@ ACL_EXPORT
6666
CL_API_ENTRY cl_int CL_API_CALL
6767
clEnqueueMarkerIntelFPGA(cl_command_queue command_queue, cl_event *event) {
6868
cl_int result;
69-
std::scoped_lock<acl_mutex_wrapper_t> lock{acl_wrapper_mutex};
69+
std::scoped_lock<acl_mutex_wrapper_t> lock{acl_mutex_wrapper};
7070

7171
if (!acl_command_queue_is_valid(command_queue)) {
7272
return CL_INVALID_COMMAND_QUEUE;
@@ -91,7 +91,7 @@ CL_API_ENTRY cl_int CL_API_CALL clEnqueueWaitForEventsIntelFPGA(
9191
cl_command_queue command_queue, cl_uint num_event, const cl_event *events) {
9292
cl_int result;
9393

94-
std::scoped_lock<acl_mutex_wrapper_t> lock{acl_wrapper_mutex};
94+
std::scoped_lock<acl_mutex_wrapper_t> lock{acl_mutex_wrapper};
9595

9696
if (!acl_command_queue_is_valid(command_queue)) {
9797
return CL_INVALID_COMMAND_QUEUE;
@@ -129,7 +129,7 @@ clWaitForEventsIntelFPGA(cl_uint num_events, const cl_event *event_list) {
129129
cl_context context;
130130
bool first_yield_to_hal = true;
131131

132-
std::scoped_lock<acl_mutex_wrapper_t> lock{acl_wrapper_mutex};
132+
std::scoped_lock<acl_mutex_wrapper_t> lock{acl_mutex_wrapper};
133133

134134
if (num_events == 0 || event_list == 0) {
135135
return CL_INVALID_VALUE;
@@ -214,7 +214,7 @@ CL_API_ENTRY cl_int CL_API_CALL clEnqueueMarkerWithWaitListIntelFPGA(
214214
cl_int result;
215215
cl_event ret_event = NULL;
216216

217-
std::scoped_lock<acl_mutex_wrapper_t> lock{acl_wrapper_mutex};
217+
std::scoped_lock<acl_mutex_wrapper_t> lock{acl_mutex_wrapper};
218218

219219
if (!acl_command_queue_is_valid(command_queue)) {
220220
return CL_INVALID_COMMAND_QUEUE;
@@ -265,7 +265,7 @@ CL_API_ENTRY cl_int CL_API_CALL clEnqueueBarrierWithWaitListIntelFPGA(
265265
const cl_event *event_wait_list, cl_event *event) {
266266
cl_int result;
267267
cl_event local_event;
268-
std::scoped_lock<acl_mutex_wrapper_t> lock{acl_wrapper_mutex};
268+
std::scoped_lock<acl_mutex_wrapper_t> lock{acl_mutex_wrapper};
269269

270270
result = clEnqueueMarkerWithWaitList(command_queue, num_events_in_wait_list,
271271
event_wait_list, &local_event);

src/acl_command_queue.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ clCreateCommandQueueWithPropertiesIntelFPGA(
114114
cl_command_queue result = 0;
115115
cl_command_queue_properties cq_properties = 0;
116116
cl_uint q_size_properties = 0, idx = 0;
117-
std::scoped_lock<acl_mutex_wrapper_t> lock{acl_wrapper_mutex};
117+
std::scoped_lock<acl_mutex_wrapper_t> lock{acl_mutex_wrapper};
118118

119119
if (!acl_context_is_valid(context)) {
120120
BAIL(CL_INVALID_CONTEXT);
@@ -251,7 +251,7 @@ CL_API_ENTRY cl_command_queue CL_API_CALL clCreateCommandQueue(
251251
ACL_EXPORT
252252
CL_API_ENTRY cl_int CL_API_CALL
253253
clRetainCommandQueueIntelFPGA(cl_command_queue command_queue) {
254-
std::scoped_lock<acl_mutex_wrapper_t> lock{acl_wrapper_mutex};
254+
std::scoped_lock<acl_mutex_wrapper_t> lock{acl_mutex_wrapper};
255255

256256
if (!acl_command_queue_is_valid(command_queue)) {
257257
return CL_INVALID_COMMAND_QUEUE;
@@ -269,7 +269,7 @@ clRetainCommandQueue(cl_command_queue command_queue) {
269269
ACL_EXPORT
270270
CL_API_ENTRY cl_int CL_API_CALL
271271
clReleaseCommandQueueIntelFPGA(cl_command_queue command_queue) {
272-
std::scoped_lock<acl_mutex_wrapper_t> lock{acl_wrapper_mutex};
272+
std::scoped_lock<acl_mutex_wrapper_t> lock{acl_mutex_wrapper};
273273

274274
if (!acl_command_queue_is_valid(command_queue)) {
275275
return CL_INVALID_COMMAND_QUEUE;
@@ -298,7 +298,7 @@ CL_API_ENTRY cl_int CL_API_CALL clGetCommandQueueInfoIntelFPGA(
298298
cl_command_queue command_queue, cl_command_queue_info param_name,
299299
size_t param_value_size, void *param_value, size_t *param_value_size_ret) {
300300
acl_result_t result;
301-
std::scoped_lock<acl_mutex_wrapper_t> lock{acl_wrapper_mutex};
301+
std::scoped_lock<acl_mutex_wrapper_t> lock{acl_mutex_wrapper};
302302

303303
if (!acl_command_queue_is_valid(command_queue)) {
304304
return CL_INVALID_COMMAND_QUEUE;
@@ -356,7 +356,7 @@ CL_API_ENTRY cl_int CL_API_CALL clSetCommandQueuePropertyIntelFPGA(
356356
cl_command_queue command_queue, cl_command_queue_properties properties,
357357
cl_bool enable, cl_command_queue_properties *old_properties) {
358358
cl_command_queue_properties bad_properties;
359-
std::scoped_lock<acl_mutex_wrapper_t> lock{acl_wrapper_mutex};
359+
std::scoped_lock<acl_mutex_wrapper_t> lock{acl_mutex_wrapper};
360360

361361
bad_properties =
362362
~((cl_command_queue_properties)CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE |
@@ -411,7 +411,7 @@ CL_API_ENTRY cl_int CL_API_CALL
411411
clFlushIntelFPGA(cl_command_queue command_queue) {
412412
bool any_queued = false;
413413
const acl_hal_t *hal = acl_get_hal();
414-
std::scoped_lock<acl_mutex_wrapper_t> lock{acl_wrapper_mutex};
414+
std::scoped_lock<acl_mutex_wrapper_t> lock{acl_mutex_wrapper};
415415

416416
if (!acl_command_queue_is_valid(command_queue)) {
417417
return CL_INVALID_COMMAND_QUEUE;
@@ -463,7 +463,7 @@ CL_API_ENTRY cl_int CL_API_CALL
463463
clFinishIntelFPGA(cl_command_queue command_queue) {
464464
cl_event event = 0;
465465
cl_int result;
466-
std::scoped_lock<acl_mutex_wrapper_t> lock{acl_wrapper_mutex};
466+
std::scoped_lock<acl_mutex_wrapper_t> lock{acl_mutex_wrapper};
467467

468468
if (!acl_command_queue_is_valid(command_queue)) {
469469
return CL_INVALID_COMMAND_QUEUE;

src/acl_context.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ CL_API_ENTRY cl_context CL_API_CALL clCreateContextIntelFPGA(
8383
cl_int *errcode_ret) {
8484
cl_context context;
8585
cl_int status;
86-
std::scoped_lock<acl_mutex_wrapper_t> lock{acl_wrapper_mutex};
86+
std::scoped_lock<acl_mutex_wrapper_t> lock{acl_mutex_wrapper};
8787

8888
context = l_create_context(properties, pfn_notify, user_data, &status);
8989
if (context == NULL || status != CL_SUCCESS) {
@@ -172,7 +172,7 @@ CL_API_ENTRY cl_context CL_API_CALL clCreateContextFromTypeIntelFPGA(
172172
cl_uint num_devices = 0;
173173
cl_int status;
174174
cl_device_id devices[ACL_MAX_DEVICE];
175-
std::scoped_lock<acl_mutex_wrapper_t> lock{acl_wrapper_mutex};
175+
std::scoped_lock<acl_mutex_wrapper_t> lock{acl_mutex_wrapper};
176176

177177
context = l_create_context(properties, pfn_notify, user_data, &status);
178178
if (context == NULL || status != CL_SUCCESS) {
@@ -256,7 +256,7 @@ CL_API_ENTRY cl_context CL_API_CALL clCreateContextFromType(
256256

257257
ACL_EXPORT
258258
CL_API_ENTRY cl_int CL_API_CALL clRetainContextIntelFPGA(cl_context context) {
259-
std::scoped_lock<acl_mutex_wrapper_t> lock{acl_wrapper_mutex};
259+
std::scoped_lock<acl_mutex_wrapper_t> lock{acl_mutex_wrapper};
260260

261261
// Note: Context creation uses acl_retain<> directly, but users must use
262262
// clRetainContext.
@@ -277,7 +277,7 @@ CL_API_ENTRY cl_int CL_API_CALL clRetainContext(cl_context context) {
277277

278278
ACL_EXPORT
279279
CL_API_ENTRY cl_int CL_API_CALL clReleaseContextIntelFPGA(cl_context context) {
280-
std::scoped_lock<acl_mutex_wrapper_t> lock{acl_wrapper_mutex};
280+
std::scoped_lock<acl_mutex_wrapper_t> lock{acl_mutex_wrapper};
281281

282282
// Error out if the reference count is already 0
283283
if (!acl_context_is_valid(context)) {
@@ -371,7 +371,7 @@ CL_API_ENTRY cl_int CL_API_CALL clGetContextInfoIntelFPGA(
371371
cl_context context, cl_context_info param_name, size_t param_value_size,
372372
void *param_value, size_t *param_value_size_ret) {
373373
acl_result_t result;
374-
std::scoped_lock<acl_mutex_wrapper_t> lock{acl_wrapper_mutex};
374+
std::scoped_lock<acl_mutex_wrapper_t> lock{acl_mutex_wrapper};
375375

376376
if (!acl_context_is_valid(context)) {
377377
return CL_INVALID_CONTEXT;
@@ -464,7 +464,7 @@ static cl_context l_create_context(const cl_context_properties *properties,
464464
cl_context context = 0;
465465
cl_int status;
466466

467-
std::scoped_lock<acl_mutex_wrapper_t> lock{acl_wrapper_mutex};
467+
std::scoped_lock<acl_mutex_wrapper_t> lock{acl_mutex_wrapper};
468468

469469
if (user_data && !pfn_notify) {
470470
BAIL(CL_INVALID_VALUE);
@@ -479,7 +479,7 @@ static cl_context l_create_context(const cl_context_properties *properties,
479479
platform_owner_pid != acl_get_pid()) {
480480
if (pfn_notify) {
481481
{
482-
acl_suspend_lock_guard l(acl_wrapper_mutex);
482+
acl_suspend_lock_guard l(acl_mutex_wrapper);
483483
(pfn_notify)("Cannot create contexts in more than one process", 0, 0,
484484
user_data);
485485
}
@@ -493,7 +493,7 @@ static cl_context l_create_context(const cl_context_properties *properties,
493493
if (context == 0) {
494494
if (pfn_notify) {
495495
{
496-
acl_suspend_lock_guard l(acl_wrapper_mutex);
496+
acl_suspend_lock_guard l(acl_mutex_wrapper);
497497
(pfn_notify)("Could not allocate a context object", 0, 0, user_data);
498498
}
499499
}
@@ -520,7 +520,7 @@ static cl_context l_create_context(const cl_context_properties *properties,
520520
static cl_int l_finalize_context(cl_context context, cl_uint num_devices,
521521
const cl_device_id *devices) {
522522
cl_int status;
523-
std::scoped_lock<acl_mutex_wrapper_t> lock{acl_wrapper_mutex};
523+
std::scoped_lock<acl_mutex_wrapper_t> lock{acl_mutex_wrapper};
524524

525525
status = acl_get_hal()->try_devices(num_devices, devices, &acl_platform);
526526
if (status) {
@@ -1122,7 +1122,7 @@ void acl_update_context(cl_context context) {
11221122
CL_EXCEPTION_TYPE_INTEL exception_type = 1ULL << i;
11231123
if (device->device_exception_status & exception_type) {
11241124
{
1125-
acl_suspend_lock_guard l(acl_wrapper_mutex);
1125+
acl_suspend_lock_guard l(acl_mutex_wrapper);
11261126
notify_fn(exception_type, device->exception_private_info[i],
11271127
device->exception_cb[i], notify_user_data);
11281128
}
@@ -1313,7 +1313,7 @@ void acl_context_callback(cl_context context, const std::string errinfo) {
13131313
acl_notify_fn_t notify_fn = context->notify_fn;
13141314
void *notify_user_data = context->notify_user_data;
13151315
{
1316-
acl_suspend_lock_guard l(acl_wrapper_mutex);
1316+
acl_suspend_lock_guard l(acl_mutex_wrapper);
13171317
notify_fn(errinfo.c_str(), 0, 0, notify_user_data);
13181318
}
13191319
}

src/acl_device.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ CL_API_ENTRY cl_int CL_API_CALL clGetDeviceIDsIntelFPGA(
4141
cl_device_id *devices, cl_uint *num_devices) {
4242
cl_int status = CL_SUCCESS;
4343
cl_uint num_matched = 0;
44-
std::scoped_lock<acl_mutex_wrapper_t> lock{acl_wrapper_mutex};
44+
std::scoped_lock<acl_mutex_wrapper_t> lock{acl_mutex_wrapper};
4545

4646
if (!acl_platform_is_valid(platform)) {
4747
return CL_INVALID_PLATFORM;
@@ -101,7 +101,7 @@ CL_API_ENTRY cl_int CL_API_CALL clGetDeviceInfoIntelFPGA(
101101
char name_buf[MAX_NAME_SIZE];
102102
acl_result_t result;
103103
cl_context context = 0;
104-
std::scoped_lock<acl_mutex_wrapper_t> lock{acl_wrapper_mutex};
104+
std::scoped_lock<acl_mutex_wrapper_t> lock{acl_mutex_wrapper};
105105

106106
#ifndef REMOVE_VALID_CHECKS
107107
if (!acl_device_is_valid_ptr(device)) {
@@ -613,7 +613,7 @@ CL_API_ENTRY cl_int CL_API_CALL clCreateSubDevicesIntelFPGA(
613613
// Since we don't support creating sub devices, we should follow the first
614614
// case if in_device is not valid, and the second case if it is.
615615

616-
std::scoped_lock<acl_mutex_wrapper_t> lock{acl_wrapper_mutex};
616+
std::scoped_lock<acl_mutex_wrapper_t> lock{acl_mutex_wrapper};
617617
// Suppress compiler warnings.
618618
partition_properties = partition_properties;
619619
num_entries = num_entries;
@@ -638,7 +638,7 @@ CL_API_ENTRY cl_int CL_API_CALL clCreateSubDevices(
638638

639639
ACL_EXPORT
640640
CL_API_ENTRY cl_int CL_API_CALL clRetainDeviceIntelFPGA(cl_device_id device) {
641-
std::scoped_lock<acl_mutex_wrapper_t> lock{acl_wrapper_mutex};
641+
std::scoped_lock<acl_mutex_wrapper_t> lock{acl_mutex_wrapper};
642642

643643
// Spec says:
644644
// "increments the device reference count if device is a valid sub-device
@@ -676,7 +676,7 @@ CL_API_ENTRY cl_int CL_API_CALL clRetainDevice(cl_device_id device) {
676676

677677
ACL_EXPORT CL_API_ENTRY cl_int CL_API_CALL
678678
clReleaseDeviceIntelFPGA(cl_device_id device) {
679-
std::scoped_lock<acl_mutex_wrapper_t> lock{acl_wrapper_mutex};
679+
std::scoped_lock<acl_mutex_wrapper_t> lock{acl_mutex_wrapper};
680680

681681
// Spec says:
682682
// "decrements the device reference count if device is a valid sub-device
@@ -712,7 +712,7 @@ clReconfigurePLLIntelFPGA(cl_device_id device, const char *pll_settings_str) {
712712
// comments specified for struct pll_setting_t in include/acl_pll.
713713
const acl_hal_t *hal;
714714
cl_int configure_status;
715-
std::scoped_lock<acl_mutex_wrapper_t> lock{acl_wrapper_mutex};
715+
std::scoped_lock<acl_mutex_wrapper_t> lock{acl_mutex_wrapper};
716716

717717
if (!acl_device_is_valid(device)) {
718718
return CL_INVALID_DEVICE;
@@ -745,7 +745,7 @@ clSetDeviceExceptionCallbackIntelFPGA(
745745
acl_exception_notify_fn_t pfn_exception_notify, void *user_data) {
746746
unsigned i;
747747

748-
std::scoped_lock<acl_mutex_wrapper_t> lock{acl_wrapper_mutex};
748+
std::scoped_lock<acl_mutex_wrapper_t> lock{acl_mutex_wrapper};
749749

750750
if (!pfn_exception_notify)
751751
return CL_INVALID_VALUE;

0 commit comments

Comments
 (0)