mutex for synchronizing requests from different users

dnessett

New member
I am trying to figure out how to limit the number of concurrent processes that can call a particular function. This function is called in a plugin I am developing for Moodle, written in PHP. The plugin takes data provided when an activity instance (a general construct, which in my case is associated with a set of problems) is created and populates several plugin specific database tables with expanded information more suitable for display. When an activity instance is first used, the function performs this expansion. All subsequent uses consult the data in the database tables, rather than expand the data provided by the activity instance.

If more than one process is executing the function on the first use of the activity instance, it is possible that the database tables will be corrupted. The expansion is used by multiple users and the first use may occur when multiple users concurrently traverse the expansion code. Normally, this kind of synchronization is provided by placing a lock around the function call.

That is:

lock(lock_instance);

<call function>

unlock(lock_instance);

Any process that tries to execute the function call will queue at the lock until it is at the top of the queue. It will then allow other processes to make the function call only after it executes the unlock. This is a well understood problem in programming and is normally solved using a mutex, but I am having difficulty understanding how PHP might support the solution when processes are associated with different users.

There is a mutex implementation for PHP (https://www.php.net/manual/en/class.syncmutex.php), but it requires the creation of a mutex class instance and it isn't clear how this instance might be accessed by processes running on behalf of different users. PHP has a $Session facility, but as far as I can make out, sessions are restricted to the executions associated with a single user. Processes running on behalf of different users do not share data stored in the $Session array.

Perhaps I am completely confused about the ability of two processes running on behalf of different users gaining access to a shared class instance and if so, I would welcome any help understanding how this might be accomplished. I first posed this problem on the Moodle forum, but have received no help.
 
Back
Top