[libomptarget] Fix a race condition in checkDeviceAndCtors
authorLechen Yu <lechenyu21@gmail.com>
Mon, 31 Oct 2022 14:36:58 +0000 (15:36 +0100)
committerJoachim Protze <protze@itc.rwth-aachen.de>
Mon, 31 Oct 2022 14:38:22 +0000 (15:38 +0100)
commitb923c15d3c00c78a7b9258e79755df9587e1f0b1
treec7c85e20a185010fae7e91636e53ff865de54357
parentaf91b19338dde956ce7ebd6890d1a8e4295b091b
[libomptarget] Fix a race condition in checkDeviceAndCtors

When multiple threads invoke checkDeviceAndCtors, both of them may read true
from the shared variable Device.HasPendingGlobals, and then invoke initLibrary
redundantly. Therefore only protecting the access to Device.HasPendingGlobals
is not sufficient to guarantee that initLibrary is invoked just once.

To fix this race condition, we move the invocation of initLibrary into the
critical section, and remove the same lock inside initLibrary.

Differential Revision: https://reviews.llvm.org/D136952
openmp/libomptarget/src/omptarget.cpp