source: subversion/sites/namefinder/php/lock.php @ 4135

Last change on this file since 4135 was 4134, checked in by david, 12 years ago

Main application files

File size: 768 bytes
Line 
1<?php
2
3class lock {
4
5  /* manual control over a central lock in the database: wait for the
6     lock to become free before proceeding, except that there is a timeout
7
8     Maps directly toa database table of the same name
9  */
10
11  var id; // actually there's only one record, id=0
12
13  /* static */ function getlock() {
14    static $tries = 20;
15    $lock = new lock();
16    $lock->id = 0;
17    for ($i = 0; $i < $tries; $i++) {
18      if ($db->delete($lock, 'id') == 1) { return $lock; }
19      usleep(50000); //20 times a second for up to a second
20    }
21    return NULL; // we failed to get a lock despite trying several times
22  }
23
24  // --------------------------------------------------
25  function unlock() {
26    $db->insert($this);
27  }
28
29}
30
31?>
Note: See TracBrowser for help on using the repository browser.