Skip to content

Commit 04c421e

Browse files
committed
fix thread atomic
1 parent 9fd4829 commit 04c421e

File tree

3 files changed

+148
-0
lines changed

3 files changed

+148
-0
lines changed

ext-src/swoole_thread_atomic.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ PHP_METHOD(swoole_thread_atomic, __construct) {
196196
o->res = new AtomicResource();
197197
auto resource_id = php_swoole_thread_resource_insert(o->res);
198198
zend_update_property_long(swoole_thread_atomic_ce, SW_Z8_OBJ_P(ZEND_THIS), ZEND_STRL("id"), resource_id);
199+
o->res->value = value;
199200
}
200201

201202
PHP_METHOD(swoole_thread_atomic, add) {
@@ -300,6 +301,7 @@ PHP_METHOD(swoole_thread_atomic_long, __construct) {
300301
o->res = new AtomicLongResource();
301302
auto resource_id = php_swoole_thread_resource_insert(o->res);
302303
zend_update_property_long(swoole_thread_atomic_ce, SW_Z8_OBJ_P(ZEND_THIS), ZEND_STRL("id"), resource_id);
304+
o->res->value = value;
303305
}
304306

305307
PHP_METHOD(swoole_thread_atomic_long, add) {

tests/swoole_thread/atomic_ctor.phpt

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
--TEST--
2+
swoole_thread: atomic ctor
3+
--SKIPIF--
4+
<?php
5+
require __DIR__ . '/../include/skipif.inc';
6+
skip_if_nts();
7+
?>
8+
--FILE--
9+
<?php
10+
require __DIR__ . '/../include/bootstrap.php';
11+
12+
use Swoole\Thread;
13+
use Swoole\Thread\Lock;
14+
15+
16+
$tm = new \SwooleTest\ThreadManager();
17+
18+
$tm->parentFunc = function () {
19+
$lock = new Lock;
20+
$lock->lock();
21+
$num1 = random_int(1, 1 << 31);
22+
$num2 = random_int(1 << 31, PHP_INT_MAX);
23+
$atomic1 = new Swoole\Thread\Atomic($num1);
24+
$atomic2 = new Swoole\Thread\Atomic\Long($num2);
25+
$thread = Thread::exec(__FILE__, $lock, $atomic1, $atomic2, $num1, $num2);
26+
$lock->lock();
27+
echo "main thread\n";
28+
$thread->join();
29+
};
30+
31+
$tm->childFunc = function ($lock, $atomic1, $atomic2, $num1, $num2) {
32+
echo "child thread\n";
33+
usleep(200_000);
34+
$lock->unlock();
35+
Assert::eq($atomic1->get(), $num1);
36+
Assert::eq($atomic2->get(), $num2);
37+
exit(0);
38+
};
39+
40+
$tm->run();
41+
?>
42+
--EXPECTF--
43+
child thread
44+
main thread
45+
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
--TEST--
2+
swoole_thread/server: send large packet
3+
--SKIPIF--
4+
<?php
5+
require __DIR__ . '/../../include/skipif.inc';
6+
skip_if_nts();
7+
?>
8+
--FILE--
9+
<?php
10+
require __DIR__ . '/../../include/bootstrap.php';
11+
12+
use Swoole\Thread;
13+
use Swoole\Thread\Lock;
14+
15+
const SIZE = 2 * 1024 * 1024;
16+
17+
$tm = new \SwooleTest\ThreadManager();
18+
$tm->initFreePorts();
19+
20+
$tm->parentFunc = function () use ($tm) {
21+
$queue = new Swoole\Thread\Queue();
22+
$atomic = new Swoole\Thread\Atomic(1);
23+
$thread = Thread::exec(__FILE__, $queue, $atomic);
24+
echo $queue->pop(-1);
25+
26+
$c = MAX_CONCURRENCY_LOW;
27+
$n = MAX_REQUESTS_LOW;
28+
29+
for ($i = 0; $i < $c; $i++) {
30+
go(function () use ($tm, $i, $n, $atomic) {
31+
$cli = new Co\Client(SWOOLE_SOCK_TCP);
32+
$cli->set([
33+
'open_length_check' => true,
34+
'package_max_length' => 4 * 1024 * 1024,
35+
'package_length_type' => 'N',
36+
'package_length_offset' => 0,
37+
'package_body_offset' => 4,
38+
]);
39+
if ($cli->connect('127.0.0.1', $tm->getFreePort(), 2) == false) {
40+
echo "ERROR\n";
41+
return;
42+
}
43+
for ($i = 0; $i < $n; $i++) {
44+
$sid = strval(rand(10000000, 99999999));
45+
$send_data = str_repeat('A', 1000) . $sid;
46+
$cli->send(pack('N', strlen($send_data)) . $send_data);
47+
$data = $cli->recv();
48+
Assert::same(strlen($data), SIZE);
49+
Assert::same($sid, substr($data, -8, 8));
50+
}
51+
});
52+
}
53+
Swoole\Event::wait();
54+
$atomic->set(0);
55+
echo "done\n";
56+
echo $queue->pop(-1);
57+
};
58+
59+
$tm->childFunc = function ($queue, $atomic) use ($tm) {
60+
$serv = new Swoole\Server('127.0.0.1', $tm->getFreePort(), SWOOLE_THREAD);
61+
$serv->set(array(
62+
'worker_num' => 2,
63+
'log_level' => SWOOLE_LOG_ERROR,
64+
'open_length_check' => true,
65+
'package_max_length' => 4 * 1024 * 1024,
66+
'package_length_type' => 'N',
67+
'package_length_offset' => 0,
68+
'package_body_offset' => 4,
69+
'init_arguments' => function () use ($queue, $atomic) {
70+
return [$queue, $atomic];
71+
}
72+
));
73+
$serv->on("WorkerStart", function (Swoole\Server $serv, $workerId) use ($queue, $atomic) {
74+
if ($workerId == 0) {
75+
$queue->push("begin\n", Thread\Queue::NOTIFY_ALL);
76+
\Swoole\Timer::tick(200, function ($timerId) use ($atomic, $serv) {
77+
if ($atomic->get() == 0) {
78+
$serv->shutdown();
79+
\Swoole\Timer::clear($timerId);
80+
}
81+
});
82+
}
83+
});
84+
$serv->on("WorkerStop", function (Swoole\Server $serv, $workerId) use ($queue, $atomic) {
85+
});
86+
$serv->on('receive', function (Swoole\Server $serv, $fd, $rid, $data) use ($queue, $atomic) {
87+
$send_data = str_repeat('A', SIZE - 12) . substr($data, -8, 8);
88+
$serv->send($fd, pack('N', strlen($send_data)) . $send_data);
89+
});
90+
$serv->on('shutdown', function () use ($queue, $atomic) {
91+
$queue->push("shutdown\n", Thread\Queue::NOTIFY_ALL);
92+
});
93+
$serv->start();
94+
};
95+
96+
$tm->run();
97+
?>
98+
--EXPECT--
99+
begin
100+
done
101+
shutdown

0 commit comments

Comments
 (0)