-
Notifications
You must be signed in to change notification settings - Fork 42
/
TimerManager.php
168 lines (140 loc) · 3.99 KB
/
TimerManager.php
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php
/*
* This file is part of Icicle, a library for writing asynchronous code in PHP using promises and coroutines.
*
* @copyright 2014-2015 Aaron Piotrowski. All rights reserved.
* @license Apache-2.0 See the LICENSE file that was distributed with this source code for more information.
*/
namespace Icicle\Loop\Manager\Libevent;
use Icicle\Loop\Events\EventFactoryInterface;
use Icicle\Loop\Events\TimerInterface;
use Icicle\Loop\LibeventLoop;
use Icicle\Loop\Structures\ObjectStorage;
use Icicle\Loop\Manager\TimerManagerInterface;
class TimerManager implements TimerManagerInterface
{
const MICROSEC_PER_SEC = 1e6;
/**
* @var \Icicle\Loop\LibeventLoop
*/
private $loop;
/**
* @var resource
*/
private $base;
/**
* @var \Icicle\Loop\Events\EventFactoryInterface
*/
private $factory;
/**
* ObjectStorage mapping Timer objects to event resources.
*
* @var \Icicle\Loop\Structures\ObjectStorage
*/
private $timers;
/**
* @var callable
*/
private $callback;
/**
* @param \Icicle\Loop\LibeventLoop $loop
* @param \Icicle\Loop\Events\EventFactoryInterface $factory
*/
public function __construct(LibeventLoop $loop, EventFactoryInterface $factory)
{
$this->loop = $loop;
$this->factory = $factory;
$this->base = $this->loop->getEventBase();
$this->timers = new ObjectStorage();
$this->callback = function ($resource, $what, TimerInterface $timer) {
if ($timer->isPeriodic()) {
event_add($this->timers[$timer], $timer->getInterval() * self::MICROSEC_PER_SEC);
} else {
event_free($this->timers[$timer]);
unset($this->timers[$timer]);
}
$timer->call();
};
}
/**
* @codeCoverageIgnore
*/
public function __destruct()
{
for ($this->timers->rewind(); $this->timers->valid(); $this->timers->next()) {
event_free($this->timers->getInfo());
}
// Need to completely destroy timer events before freeing base or an error is generated.
$this->timers = null;
}
/**
* {@inheritdoc}
*/
public function isEmpty()
{
return !$this->timers->count();
}
/**
* {@inheritdoc}
*/
public function create($interval, $periodic, callable $callback, array $args = [])
{
$timer = $this->factory->timer($this, $interval, $periodic, $callback, $args);
$this->start($timer);
return $timer;
}
/**
* {@inheritdoc}
*/
public function start(TimerInterface $timer)
{
if (!isset($this->timers[$timer])) {
$event = event_new();
event_timer_set($event, $this->callback, $timer);
event_base_set($event, $this->base);
$this->timers[$timer] = $event;
event_add($event, $timer->getInterval() * self::MICROSEC_PER_SEC);
}
}
/**
* {@inheritdoc}
*/
public function stop(TimerInterface $timer)
{
if (isset($this->timers[$timer])) {
event_free($this->timers[$timer]);
unset($this->timers[$timer]);
}
}
/**
* {@inheritdoc}
*/
public function isPending(TimerInterface $timer)
{
return isset($this->timers[$timer]);
}
/**
* {@inheritdoc}
*/
public function unreference(TimerInterface $timer)
{
$this->timers->unreference($timer);
}
/**
* {@inheritdoc}
*/
public function reference(TimerInterface $timer)
{
$this->timers->reference($timer);
}
/**
* {@inheritdoc}
*/
public function clear()
{
for ($this->timers->rewind(); $this->timers->valid(); $this->timers->next()) {
event_free($this->timers->getInfo());
}
$this->timers = new ObjectStorage();
}
}