SHMEM_SET_LOCK(3)

πŸ’– SHMEM_SET_LOCK(3) β€” Open MPI, but make it make sense πŸ’–

okay bestie, sit down, hydrate, because today we're talking about shmem_set_lock(3), shmem_clear_lock(3) and shmem_test_lock(3) β€” the trio from the Open MPI manual that literally just releases, locks, and tests a mutual exclusion memory lock. that's it. that's the whole vibe. three little functions guarding your critical section like bouncers at the club 🫢


✨ SYNOPSIS ✨

C or C++ besties, come get your headers:

#include <mpp/shmem.h>

void shmem_clear_lock(volatile long *lock)

void shmem_set_lock(volatile long *lock)

int shmem_test_lock(volatile long *lock)

Fortran girlies, this one's for you:

INCLUDE "mpp/shmem.fh"

INTEGER lock, SHMEM_TEST_LOCK

CALL SHMEM_CLEAR_LOCK(lock)

CALL SHMEM_SET_LOCK(lock)

I = SHMEM_TEST_LOCK(lock)

🌸 DESCRIPTION 🌸

So. shmem_set_lock</p> sets a mutual exclusion lock β€” but it's polite about it πŸ’… it waits until the lock has been freed by any other PE that's currently holding it before it goes ahead and sets it. and the waiting PEs? they're not fighting for it, no chaos, no drama: they're assured of getting the lock in a first-come, first-served manner. line etiquette. queen behavior. πŸ‘‘

Then shmem_clear_lock</p> releases a lock that was previously set by shmem_set_lock β€” but only after making sure that all local and remote stores initiated in the critical region are complete. it doesn't just dip, it closes the tab properly first πŸŽ€

And shmem_test_lock</p> is the low-commitment one: it sets a mutual exclusion lock only if it is currently cleared. by using this function, a PE can avoid blocking on a set lock. if the lock is currently set, the function just... returns. no waiting, no pining, it's giving unbothered ✨

These routines are appropriate for protecting a critical region from simultaneous update by multiple PEs. love that for them. they accept the following arguments:

  • lock β€” a symmetric data object that is a scalar variable or an array of length 1. this data object must be set to 0 on all processing elements (PEs) prior to the first use. lock must be of type integer. and if you're using Fortran, it must be of default kind. no cap, set it to zero first or you're gonna have a bad time 😭

πŸ“ NOTES πŸ“

the term symmetric data object is defined over on intro_shmem(3). go read it, it's the lore, iykyk πŸ¦‹


πŸŽ€ RETURN VALUES πŸŽ€

shmem_test_lock returns 0 if the lock was originally cleared and this call was able to set the lock. and it returns a value of 1 if the lock had been set and the call returned without waiting to set the lock. two outcomes, both valid, both iconic ⭐

See also:


fr fr, don't forget: lock goes to 0 on every PE before first use. that's the whole assignment. πŸ“

and that's the tea, bestie β€” this page is copyright 2003-2026, The Open MPI Community, and it was last touched on Sep 10, 2026. take care of your critical sections, they're the main characters πŸ’œπŸŒˆ

πŸ“¦ where this page comes from