changed file headers & added AUTHORS file
[PHP-P10.git] / Tools / timer.inc.php
1 <?php
2 /******************************* PHP-P10 v2 *****************************
3  * Copyright (C) 2011  Philipp Kreil (pk910)                            *
4  *                                                                      *
5  * This program is free software: you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation, either version 3 of the License, or    *
8  * (at your option) any later version.                                  *
9  *                                                                      * 
10  * This program is distributed in the hope that it will be useful,      *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
13  * GNU General Public License for more details.                         *
14  *                                                                      *
15  * You should have received a copy of the GNU General Public License    *
16  * along with this program. If not, see <http://www.gnu.org/licenses/>. *
17  *                                                                      *
18  ************************************************************************
19  * 
20  * Tools/timer.inc.php
21  *
22  *  timer functions
23  */
24 $timers['id']=0;
25
26 function timer_loop() {
27  global $timers;
28  $mtime = microtime(true);
29  $ret = false;
30  foreach ($timers as $id => $timer) {
31   if(($timer['expire'] - 0.00019) <= $mtime) { //we expire a timer 0,19 ms before (to reduce timer desyncs)
32    if((is_array($timer['function']) && method_exists($timer['function'][0],$timer['function'][1])) || (!is_array($timer['function']) && function_exists($timer['function']))) {
33     call_user_func_array($timer['function'],$timer['params']);
34    }
35    $ret = true;
36    unset($timers[$id]);
37   }
38  }
39  return $ret;
40 }
41
42 function timer($seconds,$command,$parameter) {
43  global $timers;
44  $new['expire'] = microtime(true) + $seconds;
45  $new['function'] = $command;
46  $new['params'] = $parameter;
47  while(isset($timers[$timers['id']])|| !isset($timers['id'])) {
48   $timers['id']++;
49   if($timers['id'] > 9999999) $timers['id'] = 0;
50  }
51  $timers[$timers['id']] = $new;
52  return $timers['id'];
53 }
54
55 function utimer($seconds,$command,$parameter) {
56  global $timers;
57  $new['expire'] = microtime(true) + ($seconds / 1000);
58  $new['function'] = $command;
59  $new['params'] = $parameter;
60  while($timers[$timers['id']] || !$timers['id']) {
61   $timers['id']++;
62   if($timers['id'] > 9999999) $timers['id'] = 0;
63  }
64  $timers[$timers['id']] = $new;
65  return $timers['id'];
66 }
67
68 function kill_timer($id) {
69  global $timers;
70  unset($timers[$id]);
71 }
72
73 $timers['id']=0;
74
75 ?>