QoL extension to the OpenMP run-time library.
Code like:
omp_lock_t locks[N] {};
for ( int i=0; i<N; ++i ) {
omp_init_lock( &locks[i] );
}
#pragma omp parallel
{
// ...
omp_set_lock( &locks[x] ); // blocking wait
// ...
omp_unset_lock( &locks[x] );
}
#pragma omp parallel
{
// ...
if ( omp_test_lock( &locks[x] ) ) {
// ...
omp_unset_lock( &locks[x] );
}
else {
// ...
}
// ...
}
for ( int i=0; i<N; ++i ) {
omp_destroy_lock( &locks[i] );
}
becomes:
omp_raii_lock_t locks[N] {};
#pragma omp parallel
{
// ...
auto pass = locks[x].await_pass(); // blocking wait
// ...
}
#pragma omp parallel
{
// ...
if ( auto maybe_pass = locks[x].request_pass() ) {
// ...
}
else {
// ...
}
// ...
}
Since the lifetimes of the classes added by this header are scoped, it can sometimes be wise to wrap the passes inside a {...}
block with just the logic that relies on the lock, which might reduce the lock occupancy time.
Add similar RAII wrappers for OpenMP's nested locks.