/adei/trunk

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

« back to all changes in this revision

Viewing changes to classes/lock.php

  • Committer: Suren A. Chilingaryan
  • Date: 2019-01-06 06:03:10 UTC
  • Revision ID: csa@suren.me-20190106060310-40z2rwmb62v17ynu
Provide TimedLock call and restrict amount of time we are waiting for the LOGGER lock

Show diffs side-by-side

added added

removed removed

Lines of Context:
59
59
    fclose($this->rlock);
60
60
 }
61
61
 
62
 
 static function timed_flock($handle, $flags = 0, $timeout = LOCK::FLOCK_TIMEOUT) {
 
62
 static function timed_flock($handle, $flags = 0, $timeout = LOCK::FLOCK_TIMEOUT, &$would_block = NULL) {
63
63
    if (0) { // we have pcntl
64
64
/*        pcntl_signal(SIGALRM, function() {});
65
65
        pcntl_alarm($timeout);
85
85
        }
86
86
    }
87
87
 
 
88
    if ($would_block !== NULL) {
 
89
        $would_block = $wb;
 
90
    }
 
91
 
88
92
    return $res;
89
93
 }
90
94
 
91
 
 function Lock($flag = 0, $errmsg = false) {
 
95
 function TimedLock($flag = 0, $timeout = LOCK::FLOCK_TIMEOUT, $errmsg = false) {
92
96
    if ($flag&LOCK::BLOCK) {
 
97
        $wb = 0;
93
98
        
94
99
        if ($flag&LOCK::ALL)
95
 
            $res = LOCK::timed_flock($this->rlock, LOCK_EX);
 
100
            $res = LOCK::timed_flock($this->rlock, LOCK_EX, $timeout, $wb);
96
101
        else
97
 
            $res = LOCK::timed_flock($this->rlock, LOCK_SH);
 
102
            $res = LOCK::timed_flock($this->rlock, LOCK_SH, $timeout, $wb);
98
103
 
99
104
        if (!$res) {
100
105
            if ($errmsg) throw new ADEIException($errmsg);
 
106
            else if ($wb) throw new ADEIException(translate("Timeout locking file: %s", $this->rlock_file));
101
107
            else throw new ADEIException(translate("Locking is failed (file: %s)", $this->rlock_file));
102
108
        }
103
109
        
104
 
        $res = LOCK::timed_flock($this->lockf, LOCK_EX);
 
110
        $res = LOCK::timed_flock($this->lockf, LOCK_EX, $timeout, $wb);
105
111
        if (!$res) {
106
112
            flock($this->rlock,  LOCK_UN);
107
113
            
108
114
            if ($errmsg) throw new ADEIException($errmsg);
 
115
            else if ($wb) ADEIException(translate("Timeout locking file: %s", $this->lock_file));
109
116
            else throw new ADEIException(translate("Locking is failed (file: %s)", $this->lock_file));
110
117
        }
111
118
    } else {
125
132
 
126
133
    return $res;
127
134
 }
 
135
 
 
136
 function Lock($flag = 0, $errmsg = false) {
 
137
    return $this->TimedLock($flag, LOCK::FLOCK_TIMEOUT, $errmsg);
 
138
 }
128
139
 
129
140
 function UnLock() {
130
141
    flock($this->lockf, LOCK_UN);