]> git.mxchange.org Git - friendica.git/blob - include/lock.php
Merge remote-tracking branch 'refs/remotes/friendica/develop' into develop
[friendica.git] / include / lock.php
1 <?php
2
3 // Provide some ability to lock a PHP function so that multiple processes
4 // can't run the function concurrently
5 if(! function_exists('lock_function')) {
6 function lock_function($fn_name, $block = true, $wait_sec = 2, $timeout = 30) {
7         if( $wait_sec == 0 )
8                 $wait_sec = 2;  // don't let the user pick a value that's likely to crash the system
9
10         $got_lock = false;
11         $start = time();
12
13         do {
14                 q("LOCK TABLE `locks` WRITE");
15                 $r = q("SELECT `locked`, `created` FROM `locks` WHERE `name` = '%s' LIMIT 1",
16                         dbesc($fn_name)
17                 );
18
19                 if((dbm::is_result($r)) AND (!$r[0]['locked'] OR (strtotime($r[0]['created']) < time() - 3600))) {
20                         q("UPDATE `locks` SET `locked` = 1, `created` = '%s' WHERE `name` = '%s'",
21                                 dbesc(datetime_convert()),
22                                 dbesc($fn_name)
23                         );
24                         $got_lock = true;
25                 }
26                 elseif (! dbm::is_result($r)) {
27                         /// @TODO the Boolean value for count($r) should be equivalent to the Boolean value of $r
28                         q("INSERT INTO `locks` (`name`, `created`, `locked`) VALUES ('%s', '%s', 1)",
29                                 dbesc($fn_name),
30                                 dbesc(datetime_convert())
31                         );
32                         $got_lock = true;
33                 }
34
35                 q("UNLOCK TABLES");
36
37                 if(($block) && (! $got_lock))
38                         sleep($wait_sec);
39
40         } while(($block) && (! $got_lock) && ((time() - $start) < $timeout));
41
42         logger('lock_function: function ' . $fn_name . ' with blocking = ' . $block . ' got_lock = ' . $got_lock . ' time = ' . (time() - $start), LOGGER_DEBUG);
43
44         return $got_lock;
45 }}
46
47
48 if(! function_exists('block_on_function_lock')) {
49 function block_on_function_lock($fn_name, $wait_sec = 2, $timeout = 30) {
50         if( $wait_sec == 0 )
51                 $wait_sec = 2;  // don't let the user pick a value that's likely to crash the system
52
53         $start = time();
54
55         do {
56                 $r = q("SELECT locked FROM locks WHERE name = '%s' LIMIT 1",
57                                 dbesc($fn_name)
58                      );
59
60                 if (dbm::is_result($r) && $r[0]['locked'])
61                         sleep($wait_sec);
62
63         } while(dbm::is_result($r) && $r[0]['locked'] && ((time() - $start) < $timeout));
64
65         return;
66 }}
67
68
69 if(! function_exists('unlock_function')) {
70 function unlock_function($fn_name) {
71         $r = q("UPDATE `locks` SET `locked` = 0, `created` = '%s' WHERE `name` = '%s'",
72                         dbesc(NULL_DATE),
73                         dbesc($fn_name)
74              );
75
76         logger('unlock_function: released lock for function ' . $fn_name, LOGGER_DEBUG);
77
78         return;
79 }}