Mutexes and Persistence

To make it easier to store persistent variables, there are some new functions to facilitate the safe updating of data in the User2 table.

GetMutex/ReleaseMutex can be used to create any number of named mutexes. GetMutex will fail if another client already holds the mutex.

GetMutex (name)

Attempts to obtain a named mutex from the server. If another user already has the named mutex, returns 0, else 1 if successful. Always successful on a single user system.

Use this when you need to ensure that some operation will only be executed by one client.

ReleaseMutex (name)

Releases the named mutex. If client logs out before releasing a mutex, the mutex is automatically released.

Once you have obtained a mutex, it is safe to get, update, and set your persistent data. GetPersistemt and SetPersistent operate specifically on the User2 table, loading an identified record into an associative list (whose keys are the user2 field names)

GetPersistent (devkey, key)

Loads values for a User2 record into an associative array. the array keys are "int1", "int2", "float1", "float2", "date1", "date2", "text1", "text2", "data".

SetPersistent (devkey, key, array)

Updates a user2 record with values from an associative array using the key values as for GetPersistent().

The Update_Currency_Rates sample script in Acme Widgets uses these to keep track of the last time currency rates were updated.

Posted in MWScript | Comments Off on Mutexes and Persistence