Library Routines for Manipulating Locks

Locks are commonly used in shared-memory programming for synchronization. Each lock may be in one of three states: uninitialized, unlocked, or locked. If a lock is in an unlocked state, a thread can set it, which changes its state to locked. Once a lock is set, it is owned by the thread that set it, and may be unset only by that thread.

OpenMP has two kinds of locks, simple locks and nestable locks. The locks should be accessed only through the routines that are described in this section. The two types of locks are distinct, and should be accessed only through the routines that are appropriate to their type. These routines always access the current values of the lock variables, so it is unnecessary to use explicit flush directives before or after calling them. All of the lock routines bind to all threads, so the team membership of the thread that calls one is immaterial.

There are five routines for manipulating each kind of lock. Their syntax differs only by having nest_ preceding lock for the nestable locks, and of course the arguments to the routines must be locks of the appropriate kind. Each function takes a single argument. In C, the argument must be a pointer to a lock of the appropriate type. In Fortran, the kind of the argument must be appropriate. All routines except the test ones have no return value, but if they are successful, the lock will change state appropriately.

omp_init_[nest_]lock(lock)
omp_init_[nest_]lock initializes a lock. The referenced lock must be in the uninitialized state when this routine is called. The result of a successful call is a lock in the unlocked state. If the lock is nestable, the nesting count is set to 0.
omp_destroy_[nest_]lock(lock)
omp_destroy_[nest_]lock uninitializes a lock. The referenced lock must be in the unlocked state when this routine is called. The result of a successful call is a lock in the uninitialized state.
omp_set_[nest_]lock(lock)
omp_set_[nest_]lock blocks the thread that calls it until the referenced lock is set. The referenced lock must not be in the uninitialized state. If the lock is unlocked, it will be set to the locked state and the thread will return immediately. If the lock is owned by another thread, the calling routine will block until the lock is unlocked by its owner. The lock that is referenced by omp_set_lock must not be owned by the calling thread. If the lock that is referenced by omp_set_nest_lock is owned by the calling thread, its nest count is incremented.
omp_unset_[nest_]lock(lock)
omp_unset_[nest_]lock provides a means for unlocking a lock. The referenced lock must be owned by the thread that calls this routine. For a simple lock, the result is an unlocked lock. For a nestable lock, the result is a decremented nest count, and if that value is 0, the lock enters the unlocked state. For either type of lock, if one or more other threads have been waiting for the lock, one gets the lock and resumes execution.
int omp_test_[nest_]lock(lock)
omp_test_[nest_]lock attempts to set a lock but does not block the calling routine. Aside from not blocking, their behavior is the same as the corresponding set routines. For simple locks, omp_test_lock returns true if the lock has been acquired, false otherwise. For nestable locks, omp_test_nest_lock returns the new nest count if the lock is successfully set, otherwise zero.
 
©   Cornell University  |  Center for Advanced Computing  |  Copyright Statement  |  Inclusivity Statement