Skip to content

Implement Orc function lazy initialization correctly via atomic operations

Sebastian Dröge requested to merge slomo/orc:atomic-ops into main

By simply reading the initialized flag with a normal read operation, the compiler is allowed to optimize code in a way that assumes this value to never ever change from another thread. As such, the second read after the mutex is taken is allowed to be optimized away and on concurrent initialization the code would be compiled by Orc twice.

Similarly, if the initialized flag was set it would not be guaranteed that this thread would also see the (non-atomic) write to the code pointer yet because both threads were accessing the same variable with an atomic operation.

By always accessing the initialized flag with atomic operations, both problems should be avoided now at minimal cost, especially if a C11 compatible compiler is used.

Merge request reports