Skip to content

Commit 2cfca75

Browse files
committed
Revert ability to reference TimeoutCancellation and SignalCancellation
1 parent 3774a59 commit 2cfca75

File tree

4 files changed

+6
-134
lines changed

4 files changed

+6
-134
lines changed

src/SignalCancellation.php

Lines changed: 3 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,9 @@ final class SignalCancellation implements Cancellation
2020
/**
2121
* @param int|int[] $signals Signal number or array of signal numbers.
2222
* @param string $message Message for SignalException. Default is "Operation cancelled by signal".
23-
* @param bool $reference If false, unreference the underlying event-loop callback.
2423
*/
25-
public function __construct(
26-
int|array $signals,
27-
string $message = "Operation cancelled by signal",
28-
private bool $reference = false,
29-
) {
24+
public function __construct(int|array $signals, string $message = "Operation cancelled by signal")
25+
{
3026
if (\is_int($signals)) {
3127
$signals = [$signals];
3228
}
@@ -53,11 +49,7 @@ public function __construct(
5349
};
5450

5551
foreach ($signals as $signal) {
56-
$callbackIds[] = $callbackId = EventLoop::onSignal($signal, $callback);
57-
58-
if (!$reference) {
59-
EventLoop::unreference($callbackId);
60-
}
52+
$callbackIds[] = EventLoop::unreference(EventLoop::onSignal($signal, $callback));
6153
}
6254

6355
$this->callbackIds = $callbackIds;
@@ -92,48 +84,4 @@ public function throwIfRequested(): void
9284
{
9385
$this->cancellation->throwIfRequested();
9486
}
95-
96-
/**
97-
* @return bool True if the internal event-loop callback is referenced, false if not or if the cancellation has
98-
* occurred.
99-
*/
100-
public function isReferenced(): bool
101-
{
102-
return $this->reference && !$this->cancellation->isRequested();
103-
}
104-
105-
/**
106-
* References the internal event-loop callback, keeping the loop running while the timeout is applicable.
107-
* If the timeout has expired (cancellation has been requested), this method is a no-op.
108-
*
109-
* @return $this
110-
*/
111-
public function reference(): self
112-
{
113-
if (!$this->cancellation->isRequested()) {
114-
foreach ($this->callbackIds as $callbackId) {
115-
EventLoop::reference($callbackId);
116-
}
117-
}
118-
119-
$this->reference = true;
120-
121-
return $this;
122-
}
123-
124-
/**
125-
* Unreferences the internal event-loop callback, allowing the loop to stop while the repeat loop is enabled.
126-
*
127-
* @return $this
128-
*/
129-
public function unreference(): self
130-
{
131-
foreach ($this->callbackIds as $callbackId) {
132-
EventLoop::unreference($callbackId);
133-
}
134-
135-
$this->reference = false;
136-
137-
return $this;
138-
}
13987
}

src/TimeoutCancellation.php

Lines changed: 3 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,9 @@ final class TimeoutCancellation implements Cancellation
1919
/**
2020
* @param float $timeout Seconds until cancellation is requested.
2121
* @param string $message Message for TimeoutException. Default is "Operation timed out".
22-
* @param bool $reference If false, unreference the underlying event-loop callback.
2322
*/
24-
public function __construct(
25-
float $timeout,
26-
string $message = "Operation timed out",
27-
bool $reference = false,
28-
) {
23+
public function __construct(float $timeout, string $message = "Operation timed out")
24+
{
2925
$this->cancellation = $source = new Internal\Cancellable;
3026

3127
$trace = null; // Defined in case assertions are disabled.
@@ -41,9 +37,7 @@ public function __construct(
4137
$source->cancel(new TimeoutException($message));
4238
});
4339

44-
if (!$reference) {
45-
EventLoop::unreference($this->callbackId);
46-
}
40+
EventLoop::unreference($this->callbackId);
4741
}
4842

4943
/**
@@ -73,44 +67,4 @@ public function throwIfRequested(): void
7367
{
7468
$this->cancellation->throwIfRequested();
7569
}
76-
77-
/**
78-
* @return bool True if the internal event-loop callback is referenced, false if not or if the cancellation has
79-
* occurred.
80-
*/
81-
public function isReferenced(): bool
82-
{
83-
if ($this->cancellation->isRequested()) {
84-
return false;
85-
}
86-
87-
return EventLoop::isReferenced($this->callbackId);
88-
}
89-
90-
/**
91-
* References the internal event-loop callback, keeping the loop running while the timeout is applicable.
92-
* If the timeout has expired (cancellation has been requested), this method is a no-op.
93-
*
94-
* @return $this
95-
*/
96-
public function reference(): self
97-
{
98-
if (!$this->cancellation->isRequested()) {
99-
EventLoop::reference($this->callbackId);
100-
}
101-
102-
return $this;
103-
}
104-
105-
/**
106-
* Unreferences the internal event-loop callback, allowing the loop to stop while the repeat loop is enabled.
107-
*
108-
* @return $this
109-
*/
110-
public function unreference(): self
111-
{
112-
EventLoop::unreference($this->callbackId);
113-
114-
return $this;
115-
}
11670
}

test/Cancellation/SignalCancellationTest.php

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace Amp\Cancellation;
44

55
use Amp\CancelledException;
6-
use Amp\DeferredFuture;
76
use Amp\SignalCancellation;
87
use Amp\SignalException;
98
use Amp\TestCase;
@@ -53,20 +52,4 @@ public function testWatcherCancellation(): void
5352
unset($cancellation);
5453
self::assertSame($identifiers, EventLoop::getIdentifiers());
5554
}
56-
57-
public function testWatcherUnreference(): void
58-
{
59-
$this->expectException(CancelledException::class);
60-
61-
$cancellation = new SignalCancellation(\SIGUSR1, reference: true);
62-
63-
self::assertTrue($cancellation->isReferenced());
64-
65-
EventLoop::defer(function (): void {
66-
\posix_kill(\getmypid(), \SIGUSR1);
67-
});
68-
69-
$deferred = new DeferredFuture();
70-
$deferred->getFuture()->await($cancellation);
71-
}
7255
}

test/Cancellation/TimeoutCancellationTest.php

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace Amp\Cancellation;
44

55
use Amp\CancelledException;
6-
use Amp\DeferredFuture;
76
use Amp\TestCase;
87
use Amp\TimeoutCancellation;
98
use Amp\TimeoutException;
@@ -43,16 +42,4 @@ public function testWatcherCancellation(): void
4342
unset($cancellation);
4443
self::assertSame($identifiers, EventLoop::getIdentifiers());
4544
}
46-
47-
public function testWatcherUnreference(): void
48-
{
49-
$this->expectException(CancelledException::class);
50-
51-
$cancellation = new TimeoutCancellation(0.001, reference: true);
52-
53-
self::assertTrue($cancellation->isReferenced());
54-
55-
$deferred = new DeferredFuture();
56-
$deferred->getFuture()->await($cancellation);
57-
}
5845
}

0 commit comments

Comments
 (0)