PREV UP NEXT StackThreads/MP: version 0.77 User's Guide

3.4.3: Try Lock and Lock Any

@        int st_read_and_lock_any_int(il, els, n, ires);
@        int st_read_and_lock_any_long(ll, els, n, lres);
@        int st_read_and_lock_any_ptr(pl, els, n, pres);
@
@        int st_try_read_and_lock_int(il, ires);
@        int st_try_read_and_lock_long(ll, lres);
@        int st_try_read_and_lock_ptr(pl, pres);
@
@                int els; int n; 
@                st_int_loc_t * il;  int * ires;
@                st_long_loc_t * ll; long * lres;
@                st_ptr_loc_t * pl;  void ** pres;

Suppose il points to address a, st_read_and_lock_any_int(il, els, n, ires) locks any of locations { a, a + els, a + 2 els, ... , a + (n - 1) els }. It returns the index of the locked location. That is, if address a + k els, it returns k. It also writes the value previously written at a + k els to *ires. The locked location can then be freed by applying st_write_and_unlock_int on the address a + k els. st_read_and_lock_any_long and st_read_and_lock_any_ptr are similar functions for st_long_loc_t * and st_ptr_loc_t *, respectively.

These primitives are useful when multiple resources are available and you want to lock whatever resource is available.

st_try_read_and_lock_int(il, ires) tries to lock the location il. If it successfully obtains the lock, it writes the value previously written at il to *ires and returns zero. It otherwise returns -1. st_try_read_and_lock_long and st_try_read_and_lock_ptr are similar functions for st_long_loc_t * and st_ptr_loc_t *, respectively.