/adei/trunk

To get this branch, use:
bzr branch http://darksoft.org/webbzr/adei/trunk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php

class LOCK {
 var $name;                     // Lock Name

 var $lockf;                    // SETUP independent lock (i.e. all setups could hold the lock in parallel)
 var $rlock;                    // Global lock (but uses shared mode unless specified with LOCK_ALL flag)

 var $lock_file;                // File associated with the lock
 var $rlock_file;               // File associated with the rlock
 
 const BLOCK = 1;               // Blocking wait or report error
 const ALL = 2;                 // Lock for all setups or just for the current
 const EXCLUSIVE = 2;           // Exclusive lock
 
 const FLOCK_TIMEOUT = 60;      // In seconds (flock timeouts after specified number of seconds)
 const FLOCK_SLEEP = 0.1;       // In seconds (sleep in busy loop)
 
 function __construct($name) {
    global $TMP_PATH;
    global $ADEI_SETUP;
    
    $dir = $TMP_PATH . "/locks";

    if (!is_dir($dir)) {
    	if (!@mkdir($dir, 0777 /*0755*/, true))
	    throw new ADEIException(translate("It is not possible to create lock directory \"$dir\""));

# When creating from apache, the 0777 mode is ignored for unknown reason	
	@chmod($dir, 0777);
    }

    $this->name = $name;

    if ($ADEI_SETUP) $this->lock_file = $dir . "/${ADEI_SETUP}__${name}.lock";
    else $this->lock_file = $dir . "/ADEI__${name}.lock";

    $umask = @umask(0);

    $this->lockf = @fopen($this->lock_file, "a+");
    if (!$this->lockf) {
	@umask($umask);
	throw new ADEIException(translate("It is not possible to create lock file \"%s\"", $this->lock_file));
    }

    $this->rlock_file = $dir . "/${name}.lock";
    $this->rlock = @fopen($this->rlock_file, "a+");
    if (!$this->rlock) {
	fclose($this->lockf);
        @umask($umask);
	throw new ADEIException(translate("It is not possible to create lock file \"%s\"", $this->rlock_file));
    }

    @umask($umask);
 }

 function __destruct() {
    fclose($this->lockf);
    fclose($this->rlock);
 }

 static function timed_flock($handle, $flags = 0, $timeout = LOCK::FLOCK_TIMEOUT, &$would_block = NULL) {
    if (0) { // we have pcntl
/*        pcntl_signal(SIGALRM, function() {});
        pcntl_alarm($timeout);

        try {
            $res = flock($handle, $flags);
        } finally {
            pcntl_alarm(0);
            pcntl_signal_dispatch();
            pcntl_signal(SIGALRM, SIG_DFL);    
        }*/
    } else {
        $retries = $timeout / LOCK::FLOCK_SLEEP;
        
        $r = 0; $wb = 0; 
        $res = flock($handle, $flags | LOCK_NB, $wb);
        
        while ((!$res)&&($wb)&&(++$r < $retries)) {
            usleep(ceil(LOCK::FLOCK_SLEEP * 1000000));
            
            $wb = 0; 
            $res = flock($handle, $flags | LOCK_NB, $wb);
        }
    }

    if ($would_block !== NULL) {
        $would_block = $wb;
    }

    return $res;
 }

 function TimedLock($flag = 0, $timeout = LOCK::FLOCK_TIMEOUT, $errmsg = false) {
    if ($flag&LOCK::BLOCK) {
	$wb = 0;
	
	if ($flag&LOCK::ALL)
	    $res = LOCK::timed_flock($this->rlock, LOCK_EX, $timeout, $wb);
	else
	    $res = LOCK::timed_flock($this->rlock, LOCK_SH, $timeout, $wb);

	if (!$res) {
	    if ($errmsg) throw new ADEIException($errmsg);
	    else if ($wb) throw new ADEIException(translate("Timeout locking file: %s", $this->rlock_file));
	    else throw new ADEIException(translate("Locking is failed (file: %s)", $this->rlock_file));
	}
	
	$res = LOCK::timed_flock($this->lockf, LOCK_EX, $timeout, $wb);
        if (!$res) {
	    flock($this->rlock,  LOCK_UN);
	    
	    if ($errmsg) throw new ADEIException($errmsg);
	    else if ($wb) ADEIException(translate("Timeout locking file: %s", $this->lock_file));
	    else throw new ADEIException(translate("Locking is failed (file: %s)", $this->lock_file));
	}
    } else {
	if ($flag&LOCK::ALL)
	    $res = flock($this->rlock, LOCK_EX|LOCK_NB);
	else
	    $res = flock($this->rlock, LOCK_SH|LOCK_NB);

        if ((!$res)&&($errmsg)) throw new ADEIException($errmsg);

	$res = flock($this->lockf, LOCK_EX|LOCK_NB);
        if (!$res) {
	    flock($this->rlock, LOCK_UN);
	    if ($errmsg) throw new ADEIException($errmsg);
	}
    }

    return $res;
 }

 function Lock($flag = 0, $errmsg = false) {
    return $this->TimedLock($flag, LOCK::FLOCK_TIMEOUT, $errmsg);
 }
 
 function UnLock() {
    flock($this->lockf, LOCK_UN);
    flock($this->rlock, LOCK_UN);
 }
}


?>