Skip to content

Commit 71afe70

Browse files
committed
Optimization
1 parent 53607fb commit 71afe70

File tree

7 files changed

+272
-262
lines changed

7 files changed

+272
-262
lines changed

examples/coroutine/scheduler.php

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
$s = scheduler();
3+
$s->add(function (){
4+
Co::sleep(0.2);
5+
echo "hello world\n";
6+
});
7+
$s->start();

library/core/Coroutine/ObjectPool.php

+12-11
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@
55
use BadMethodCallException;
66
use InvalidArgumentException;
77
use RuntimeException;
8+
use Swoole\Coroutine;
89

910
abstract class ObjectPool
1011
{
1112
protected static $context = [];
1213
protected $object_pool;
1314
protected $busy_pool;
1415
protected $type;
15-
16+
1617
public function __construct($type, $pool_size = 10, $concurrency = 10)
1718
{
1819
if (empty($type)) {
@@ -21,21 +22,21 @@ public function __construct($type, $pool_size = 10, $concurrency = 10)
2122
if (!is_numeric($concurrency) || $concurrency <= 0) {
2223
throw new InvalidArgumentException('ObjectPool misuse: parameter concurrency must larger than 0');
2324
}
24-
25+
2526
$this->object_pool = new Channel($pool_size);
2627
$this->busy_pool = new Channel($concurrency);
2728
$this->type = $type;
2829
}
29-
30+
3031
public function get()
3132
{
32-
$cid = \Swoole\Coroutine::getCid();
33+
$cid = Coroutine::getCid();
3334
if ($cid < 0) {
3435
throw new BadMethodCallException('ObjectPool misuse: get must be used in coroutine');
3536
}
3637
$type = $this->type;
37-
$context = \Swoole\Coroutine::getContext();
38-
\Swoole\Coroutine::defer(function () {
38+
$context = Coroutine::getContext();
39+
Coroutine::defer(function () {
3940
$this->free();
4041
});
4142
if (isset($context[$cid][$type])) {
@@ -53,25 +54,25 @@ public function get()
5354
}
5455
$context[$cid]["new"] = true;
5556
}
56-
57+
5758
$context[$cid][$type] = $object;
5859
return $object;
5960
}
60-
61+
6162
public function free()
6263
{
63-
$cid = \Swoole\Coroutine::getCid();
64+
$cid = Coroutine::getCid();
6465
if ($cid < 0) {
6566
throw new BadMethodCallException('ObjectPool misuse: free must be used in coroutine');
6667
}
6768
$type = $this->type;
68-
$context = \Swoole\Coroutine::getContext();
69+
$context = Coroutine::getContext();
6970
$object = $context[$cid][$type];
7071
$this->object_pool->push($object);
7172
if ($context[$cid]["new"]) {
7273
$this->busy_pool->pop();
7374
}
7475
}
75-
76+
7677
abstract function create();
7778
}

library/functions.php

+8
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@ function _array(array $array = []): Swoole\ArrayObject
1717
{
1818
return new Swoole\ArrayObject($array);
1919
}
20+
21+
/**
22+
* @return \Swoole\Coroutine\Scheduler
23+
*/
24+
function scheduler()
25+
{
26+
return new Swoole\Coroutine\Scheduler();
27+
}
2028
}
2129

2230
/**

php_swoole_library.h

+20-11
Original file line numberDiff line numberDiff line change
@@ -663,14 +663,15 @@ static const char* swoole_library_source_core_coroutine_object_pool =
663663
"use BadMethodCallException;\n"
664664
"use InvalidArgumentException;\n"
665665
"use RuntimeException;\n"
666+
"use Swoole\\Coroutine;\n"
666667
"\n"
667668
"abstract class ObjectPool\n"
668669
"{\n"
669670
" protected static $context = [];\n"
670671
" protected $object_pool;\n"
671672
" protected $busy_pool;\n"
672673
" protected $type;\n"
673-
" \n"
674+
"\n"
674675
" public function __construct($type, $pool_size = 10, $concurrency = 10)\n"
675676
" {\n"
676677
" if (empty($type)) {\n"
@@ -679,21 +680,21 @@ static const char* swoole_library_source_core_coroutine_object_pool =
679680
" if (!is_numeric($concurrency) || $concurrency <= 0) {\n"
680681
" throw new InvalidArgumentException('ObjectPool misuse: parameter concurrency must larger than 0');\n"
681682
" }\n"
682-
" \n"
683+
"\n"
683684
" $this->object_pool = new Channel($pool_size);\n"
684685
" $this->busy_pool = new Channel($concurrency);\n"
685686
" $this->type = $type;\n"
686687
" }\n"
687-
" \n"
688+
"\n"
688689
" public function get()\n"
689690
" {\n"
690-
" $cid = \\Swoole\\Coroutine::getCid();\n"
691+
" $cid = Coroutine::getCid();\n"
691692
" if ($cid < 0) {\n"
692693
" throw new BadMethodCallException('ObjectPool misuse: get must be used in coroutine');\n"
693694
" }\n"
694695
" $type = $this->type;\n"
695-
" $context = \\Swoole\\Coroutine::getContext();\n"
696-
" \\Swoole\\Coroutine::defer(function () {\n"
696+
" $context = Coroutine::getContext();\n"
697+
" Coroutine::defer(function () {\n"
697698
" $this->free();\n"
698699
" });\n"
699700
" if (isset($context[$cid][$type])) {\n"
@@ -711,26 +712,26 @@ static const char* swoole_library_source_core_coroutine_object_pool =
711712
" }\n"
712713
" $context[$cid][\"new\"] = true;\n"
713714
" }\n"
714-
" \n"
715+
"\n"
715716
" $context[$cid][$type] = $object;\n"
716717
" return $object;\n"
717718
" }\n"
718-
" \n"
719+
"\n"
719720
" public function free()\n"
720721
" {\n"
721-
" $cid = \\Swoole\\Coroutine::getCid();\n"
722+
" $cid = Coroutine::getCid();\n"
722723
" if ($cid < 0) {\n"
723724
" throw new BadMethodCallException('ObjectPool misuse: free must be used in coroutine');\n"
724725
" }\n"
725726
" $type = $this->type;\n"
726-
" $context = \\Swoole\\Coroutine::getContext();\n"
727+
" $context = Coroutine::getContext();\n"
727728
" $object = $context[$cid][$type];\n"
728729
" $this->object_pool->push($object);\n"
729730
" if ($context[$cid][\"new\"]) {\n"
730731
" $this->busy_pool->pop();\n"
731732
" }\n"
732733
" }\n"
733-
" \n"
734+
"\n"
734735
" abstract function create();\n"
735736
"}\n";
736737

@@ -1813,6 +1814,14 @@ static const char* swoole_library_source_functions =
18131814
" {\n"
18141815
" return new Swoole\\ArrayObject($array);\n"
18151816
" }\n"
1817+
"\n"
1818+
" /**\n"
1819+
" * @return \\Swoole\\Coroutine\\Scheduler\n"
1820+
" */\n"
1821+
" function scheduler()\n"
1822+
" {\n"
1823+
" return new Swoole\\Coroutine\\Scheduler();\n"
1824+
" }\n"
18161825
"}\n"
18171826
"\n"
18181827
"/**\n"

0 commit comments

Comments
 (0)