Code Monkey home page Code Monkey logo

funk's Introduction

FUNK

A C continuation implementation inspired by Adam Dunkels' ProtoThread.

Only 7 C macros and 1 struct definition; less than 100 lines of code.

Simple yet POWERFUL.

Now you can write a coroutine (generator, to be exact) in the following way, quick, simple, and in a natural way.

Enjoy the power of YIELD();

Continuation* add(Continuation* co,
    int a, int b, int * ret)
{
  struct timespec now;
  VAR_BEGIN
    struct timespec start;
  VAR_END

  if (!this || a < 0 || b < 0) { *ret = -1; EXIT(); }
  if (KILL_SIGNALLE()) EXIT();

  clock_gettime(CLOCK_REALTIME, &this->start);
  while(1)
  {
    clock_gettime(CLOCK_REALTIME, &now);
    if ((now.tv_sec - this->start.tv_sec) > 1)
      break;
    YIELD();
  }
  *ret = a + b;
  EXIT();
}

funk's People

Contributors

matianfu avatar

Stargazers

 avatar  avatar  avatar BQ.soong avatar Lin_n avatar Jim avatar Clwyd Y. avatar  avatar  avatar  avatar  avatar FreeBirdLjj avatar Thomas Yang avatar Dave Clayton-Wagner avatar Yangxun avatar  avatar Khaled Diab avatar Gianni Gambetti avatar  avatar Weiyi Li avatar  avatar jiangplus avatar  avatar  avatar Hua avatar shiweifu avatar gcoder avatar Oops avatar Chong Lu avatar Jesse Peng avatar Yuzhong Wen avatar  avatar Yi Ye avatar  avatar  avatar cnxzcxy avatar Colin Song avatar  avatar  avatar Blue Padge avatar Jichao Wu avatar leee avatar Isaac Zhi avatar pepsin avatar

Watchers

 avatar James Cloos avatar  avatar 运筹 avatar Chong Lu avatar  avatar  avatar  avatar

funk's Issues

Event

为什么需要实现Event可以写一本书;

简单的说Event x State就是程序行为的完备数学定义;两者都是程序的Nature;简化的Event是轮循外部变量的语句;简化的State就在if/while等分支语句里;他们可以简化但不会没有;

Event对于FUNK意味着什么呢?

FUNK并非真正的Blocking;再次调用的时候,如果在恢复运行之前做一点小小的检查工作再决定是否继续,Well,就完全等价于Event模式下的EventLoop入口的GetEvent动作;

真正的代码可能写在两个不同的地方,一个是FUNK的goto之前;另一个是goto之后、再次调用sub-FUNK之前,两者用途和形式都会有区别;很可能Event会引起这一层状态机的路径变化,但也可能只有某个subFunk阻塞时关注某个Event;

设计上也有两个原则,一个是在全局的FUNK设计里引入Event机制,但这对那些不需要Event的FUNK来说可能会带来书写代码的负担;

这里还需要仔细考虑再做定夺;

Design Issue: FUNK Cancel/Interrupt

在上层状态机翻转时,上层逻辑很有可能要求取消FUNK操作,需要一种CANCEL机制,或者采用类似Thread的命名习惯,叫做INTERRUPT。

目前考虑的一种办法是:

在Continuation结构体中的ep地址,目前的设计里0值在FUNK占用CPU运行时是可能的和被允许的;事实上任何无效值都被允许,只有在YIELD之后会被赋予有效的代码地址;

那么有可能让上层逻辑通过Continuation*指针把ep值赋值为0,再调用一次,在FUNK内部如果发现this有效但ep为0,认为是interrupt操作,并执行相应的析构操作,最后回收内存,返回0;

如果该FUNK也在调用sub-FUNK,则它应该先用同样的操作去销毁sub-FUNK,构成一个链式逻辑,销毁所有的FUNK对象;

形式上如何实现需要考虑一下;

目前的宏定义不修改和不增加的话,可以在VAR_END之后,先写判断this指针的语句,然后写判断this->ep == 0的语句;这样形式上仍然简洁不过给写代码带来了一点负担(当然,也增加了一个Interruptible的Feature)。

有什么建议或看法?

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.