/adei/ui

To get this branch, use:
bzr branch http://darksoft.org/webbzr/adei/ui

« back to all changes in this revision

Viewing changes to classes/lock.php

  • Committer: Suren A. Chilingaryan
  • Date: 2008-04-02 10:23:22 UTC
  • Revision ID: csa@dside.dyndns.org-20080402102322-okib92sicg2dx3o3
Initial import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
 
 
3
class LOCK {
 
4
 var $lockf;
 
5
 
 
6
 const BLOCK = 1;
 
7
 
 
8
 function __construct($name) {
 
9
    global $TMP_PATH;
 
10
    global $SETUP;
 
11
    
 
12
    $dir = $TMP_PATH . "/locks";
 
13
 
 
14
    if (!is_dir($dir)) {
 
15
        if (!@mkdir($dir, 0755, true)) 
 
16
            throw new ADEIException(translate("It is not possible to create lock directory \"$dir\""));
 
17
    }
 
18
    
 
19
    if ($SETUP) $fname = $dir . "/${SETUP}__${name}.lock";
 
20
    else $fname = $dir . "/ADEI__${name}.lock";
 
21
 
 
22
    $this->lockf = @fopen($fname, "a+");
 
23
    if (!$this->lockf)
 
24
        throw new ADEIException(translate("It is not possible to create lock file \"$fname\""));
 
25
 }
 
26
 
 
27
 function __destruct() {
 
28
    fclose($this->lockf);
 
29
 }
 
30
 
 
31
 function Lock($flag = 0, $errmsg = false) {
 
32
    if ($flag&LOCK::BLOCK) {
 
33
        $res = flock($this->lockf, LOCK_EX);
 
34
        if (!$res) {
 
35
            if ($errmsg) throw new ADEIException($errmsg);
 
36
            else throw new ADEIException(translate("Locking is failed"));
 
37
        }
 
38
    } else {
 
39
        $res = flock($this->lockf, LOCK_EX|LOCK_NB);
 
40
        if ((!$res)&&($errmsg)) throw new ADEIException($errmsg);
 
41
    }
 
42
    
 
43
    return $res;
 
44
 }
 
45
 
 
46
 function UnLock() {
 
47
    flock($this->lockf, LOCK_UN);
 
48
 }
 
49
}
 
50
 
 
51
 
 
52
?>
 
 
b'\\ No newline at end of file'