Fix a race condition in device initialization.
@@ -370,7 +370,6 @@ pocl_reinit_devices ()
if (pocl_num_devices == 0)
return CL_DEVICE_NOT_FOUND;
- POCL_LOCK (pocl_init_lock);
POCL_MSG_WARN ("REINIT all devices\n");
unsigned i, j, dev_index;
@@ -402,8 +401,6 @@ pocl_reinit_devices ()
FINISH:
devices_active = 1;
- POCL_UNLOCK (pocl_init_lock);
-
return retval;
}
@@ -416,23 +413,25 @@ pocl_init_devices ()
In case libhwloc has the OpenCL plugin installed, it initializes
it and it leads to initializing pocl again which leads to an
infinite loop. */
-
+ POCL_LOCK (pocl_init_lock);
if (!first_init_done)
{
if (init_in_progress)
+ {
+ POCL_UNLOCK (pocl_init_lock);
return CL_SUCCESS; /* debatable, but what else can we do ? */
+ }
init_in_progress = 1;
- POCL_INIT_LOCK (pocl_init_lock);
}
- POCL_LOCK(pocl_init_lock);
-
if (first_init_done)
{
- POCL_UNLOCK(pocl_init_lock);
- POCL_MSG_PRINT_GENERAL ("FIRST INIT done; REINIT all devices\n");
if (!devices_active)
+ {
+ POCL_MSG_PRINT_GENERAL ("FIRST INIT done; REINIT all devices\n");
pocl_reinit_devices (); // TODO err check
+ }
+ POCL_UNLOCK(pocl_init_lock);
return pocl_num_devices ? CL_SUCCESS : CL_DEVICE_NOT_FOUND;
}
@@ -506,6 +505,7 @@ pocl_init_devices ()
{
const char *dev_env = pocl_get_string_option ("POCL_DEVICES", NULL);
POCL_MSG_WARN ("no devices found. POCL_DEVICES=%s\n", dev_env);
+ POCL_UNLOCK(pocl_init_lock);
return CL_DEVICE_NOT_FOUND;
}
@@ -546,6 +546,7 @@ pocl_init_devices ()
switch (ret)
{
case CL_OUT_OF_HOST_MEMORY:
+ POCL_UNLOCK (pocl_init_lock);
return ret;
case CL_SUCCESS:
break;
