Git Product home page Git Product logo

brewlin / goos Goto Github PK

View Code? Open in Web Editor NEW
9.0 1.0 2.0 155 KB

GPM 多线程协程调度器 for PHP Extension,multi-core concurrency library

Home Page: https://wiki.brewlin.com/wiki/blog/goos/Goos-%E5%A4%9A%E7%BA%BF%E7%A8%8B%E5%8D%8F%E7%A8%8B%E5%AE%9E%E7%8E%B0%E7%AE%80%E8%A6%81/

License: Apache License 2.0

Shell 0.54% M4 2.72% C++ 94.59% C 0.81% Assembly 0.61% PHP 0.73%
schedule gpm php thread coroutine extension currency multi-core

goos's Introduction

goos

GitHub issues GitHub forks GitHub license

GPM 多线程协程调度器 for PHP Extension

NOTICE:TSRM 使变量共享变得复杂和低效,目前仅用于学习研究

process

  • php环境线程隔离,协程隔离
  • 实现G-M调度,任意协程G创建后,自动绑定到线程M上去执行
  • 实现多线程协程G调度,切出与恢复
  • 优化php内存相关
  • 引入P, 实现G-P-M 任务窃取调度
  • 协程栈自动收缩,防止 stack overflow
  • c&php 协程栈复用
  • 实现抢占调度,可以对任意在执行的协程发起抢占
  • 优化抢占调度,检查任意超过10ms持有G的线程,发起抢占调度

config & install

# 编译php必须加上 ZTS支持
> wget https://www.php.net/distributions/php-7.3.5.tar.gz
> tar zxvf php-7.3.5.tar.gz & cd php-7.3.5
> ./configure --prefix=/path/to/  --enable-cli --with-config-file-path=/path/to/etc 
--sysconfdir=/path/to/etc --enable-maintainer-zts

# 编译扩展
> cd /path/to/goos
> php7.3.5-ize
> ./configure --with-php-config=php7.3.5-config
> make install
> echo 'extension=go.so' >> phpetcpath/php.ini 

@G-M 多线程调度

<?php
Runtime::GOMAXPROCS(10);
$ref = ["ref"];
for($i = 0;$i <100; $i++)
{
    //support reference params
    go(function()use($i,&$ref){
       go(function(){
           var_dump($i,$ref);
       });
    });
}
Runtime::wait();

@G-P-M 任务窃取调度

@G-P-M 信号抢占调度

<?php
//设置只有一个工作线程,在不抢占的情况下,永远无法触发 go 2
Runtime::GOMAXPROCS(1);
go(function(){
    for(;;) echo "go 1\n"; 
});
go(function(){
    for(;;) echo "go 2\n";
});
Runtime::wait();

docs

goos's People

Contributors

brewlin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.