: Path and basename to the lockfile to use. * $csId : Id of the critical section, will be part of filename. * $msg : The message to be written to the lockfile. * * If $msg is NULL, a default message is created from session_id and time * * Returns: * Array of lock information. * [0] Filehandle to opened lockfile. * [1] Resource id for semaphor * [2] the old setting of ignore_user_abort. */ function acquireLock ($lockFile, $csId, $msg = NULL) { $fName = $lockFile . $csId . '.lck'; $lock = NULL; setErrExceptionMapping (); $lockHandle = fopen ($fName, 'w'); // if available use semaphores for mutual exclusion because flock // doesn't work reliably in threaded environments. if (function_exists ('sem_get')) { $lock = sem_get (ftok ($fName, $csId), 1, 0644, TRUE); $state = sem_acquire ($lock); while ($state === FALSE) $state = sem_acquire ($lock); } else { $state = flock ($lock, LOCK_EX); while ($state === FALSE) $state = flock ($lock, LOCK_EX); } // Here one could write informations in the lockfile...time, pid, etc. if ($msg === NULL) $msg = session_id () . '::' . time (); fwrite ($lockHandle, $msg . "\n"); fflush ($lockHandle); resetErrExceptionMapping (); $userAbort = ignore_user_abort(TRUE); return array ($lockHandle, $lock, $userAbort); } /** * Release a lock previously acquired by acquireLock. * * Sideeffects: * Sets ignore_user_abort to the value before acquireLock succeds. * * Arguments: * $lock : The array returned by a successfull acquireLock call. */ function releaseLock ($lock) { setErrExceptionMapping (); if ($lock[1] !== NULL) sem_release ($lock[1]); ftruncate ($lock[0], 0); fclose ($lock[0]); resetErrExceptionMapping (); ignore_user_abort($lock[2]); } ?>