Wednesday, March 31, 2010

A potted guide to stackless coroutines

Keen-eyed Asio users may have noticed that Boost 1.42 includes a new example, HTTP Server 4, that shows how to use stackless coroutines in conjunction with asynchronous operations. This follows on from the coroutines I explored in the previous three posts, but with a few improvements. In particular:

  • the pesky entry pseudo-keyword is gone; and
  • a new fork pseudo-keyword has been added.

The result bears a passing resemblance to C#'s yield and friends. This post aims to document my stackless coroutine API, but before launching into a long and wordy explanation, here's a little picture to liven things up:

[ Click image for full size. This image was generated from this source code using asioviz. ]

class coroutine

Every coroutine needs to store its current state somewhere. For that we have a class called coroutine:

class coroutine
{
public:
coroutine();
bool is_child() const;
bool is_parent() const;
bool is_complete() const;
};

Coroutines are copy-constructible and assignable, and the space overhead is a single int. They can be used as a base class:

class session : coroutine
{
...
};

or as a data member:

class session
{
...
coroutine coro_;
};

or even bound in as a function argument using bind() (see previous post). It doesn't really matter as long as you maintain a copy of the object for as long as you want to keep the coroutine alive.

reenter

The reenter macro is used to define the body of a coroutine. It takes a single argument: a pointer or reference to a coroutine object. For example, if the base class is a coroutine object you may write:

reenter (this)
{
... coroutine body ...
}

and if a data member or other variable you can write:

reenter (coro_)
{
... coroutine body ...
}

When reenter is executed at runtime, control jumps to the location of the last yield or fork.

The coroutine body may also be a single statement. This lets you save a few keystrokes by writing things like:

reenter (this) for (;;)
{
...
}

Limitation: The reenter macro is implemented using a switch. This means that you must take care when using local variables within the coroutine body. The local variable is not allowed in a position where reentering the coroutine could bypass the variable definition.

yield statement

This form of the yield keyword is often used with asynchronous operations:

yield socket_->async_read_some(buffer(*buffer_), *this);

This divides into four logical steps:

  1. yield saves the current state of the coroutine.
  2. The statement initiates the asynchronous operation.
  3. The resume point is defined immediately following the statement.
  4. Control is transferred to the end of the coroutine body.

When the asynchronous operation completes, the function object is invoked and reenter causes control to transfer to the resume point. It is important to remember to carry the coroutine state forward with the asynchronous operation. In the above snippet, the current class is a function object object with a coroutine object as base class or data member.

The statement may also be a compound statement, and this permits us to define local variables with limited scope:

yield
{
mutable_buffers_1 b = buffer(*buffer_);
socket_->async_read_some(b, *this);
}

yield return expression ;

This form of yield is often used in generators or coroutine-based parsers. For example, the function object:

struct interleave : coroutine
{
istream& is1;
istream& is2;
char operator()(char c)
{
reenter (this) for (;;)
{
yield return is1.get();
yield return is2.get();
}
}
};

defines a trivial coroutine that interleaves the characters from two input streams.

This type of yield divides into three logical steps:

  1. yield saves the current state of the coroutine.
  2. The resume point is defined immediately following the semicolon.
  3. The value of the expression is returned from the function.

yield ;

This form of yield is equivalent to the following steps:

  1. yield saves the current state of the coroutine.
  2. The resume point is defined immediately following the semicolon.
  3. Control is transferred to the end of the coroutine body.

This form might be applied when coroutines are used for cooperative threading and scheduling is explicitly managed. For example:

struct task : coroutine
{
...
void operator()()
{
reenter (this)
{
while (... not finished ...)
{
... do something ...
yield;
... do some more ...
yield;
}
}
}
...
};
...
task t1, t2;
for (;;)
{
t1();
t2();
}

yield break ;

The final form of yield is adopted from C# and is used to explicitly terminate the coroutine. This form is comprised of two steps:

  1. yield sets the coroutine state to indicate termination.
  2. Control is transferred to the end of the coroutine body.
Once terminated, calls to is_complete() return true and the coroutine cannot be reentered.

Note that a coroutine may also be implicitly terminated if the coroutine body is exited without a yield, e.g. by return, throw or by running to the end of the body.

fork statement ;

The fork pseudo-keyword is used when "forking" a coroutine, i.e. splitting it into two (or more) copies. One use of fork is in a server, where a new coroutine is created to handle each client connection:

reenter (this)
{
do
{
socket_.reset(new tcp::socket(io_service_));
yield acceptor->async_accept(*socket_, *this);
fork server(*this)();
} while (is_parent());
... client-specific handling follows ...
}

The logical steps involved in a fork are:

  1. fork saves the current state of the coroutine.
  2. The statement creates a copy of the coroutine and either executes it immediately or schedules it for later execution.
  3. The resume point is defined immediately following the semicolon.
  4. For the "parent", control immediately continues from the next line.

The functions is_parent() and is_child() can be used to differentiate between parent and child. You would use these functions to alter subsequent control flow.

Note that fork doesn't do the actual forking by itself. It is your responsibility to write the statement so that it creates a clone of the coroutine and calls it. The clone can be called immediately, as above, or scheduled for delayed execution using something like io_service::post().

96 comments:

Unknown said...

Thought you might be interested in real application using light-weight thread approach in C++. - http://code.google.com/p/altalena/

Unknown said...

I just started working with your HTTP sample. It looks awesome, but the first hurdle I've encountered is with the macros. They don't work with the Visual C++ (9.0) compiler if "Debug Information Format" is set to "Program Database for Edit and Continue (/ZI)". It fails with a C2051: case expression not constant error (at the "case __LINE__:" part). I'm sure it's a compiler bug because frustratingly it works if you get the compiler to output the preprocessed file.

OvermindDL1 said...

That is because with /ZI the __LINE__ macro will change every time that file is edited and recompiled on the fly, so it does make sense. Edit and Continue has a *LOT* of issues though, I never compile with it enabled as it causes code generation to change and bugs to disappear and appear, not good.

Anonymous said...

Hi,

what do you think of provisionally accepted Boost.Context?

http://ok73.ok.funpic.de/boost/libs/context/doc/html/

It even has ASIO example :)

Dave Abrahams said...

Thoroughly awesome

Susan said...

Find out who was born on any day in any month in history via our calendar of famous birthdays. Includes famous, historical, noteworthy and Celebrity birthdays.

oncasinosite said...

Wow, what a great knowledge you have shared admin this is one of the best blogs I have ever read.

카지노사이트
먹튀검증
스포츠토토
토토사이트

totosafeguide said...

You are doing amazing work keep it up, and thank you so much for sharing this important and useful information with us and all the best for your upcoming post.

사설토토
온라인카지노
파워볼게임

betmantoto said...

This is a great inspiring article. I am pretty much pleased with your good work. You put really very helpful information. Keep it up. Keep blogging Looking to reading your next post.

토토
바카라사이트
파워볼사이트

안전놀이터 said...

Thank you for the good writeup. It in fact was
a amusement account it. Look advanced to more added agreeable from
you! By the way, how could we communicate? 안전놀이터

sportstoto top said...

Great website and thanks for offering good quality info. nice thanks for share great post here 스포츠토토

sportstoto top said...

먹튀검증 This is a very good article. I see the greatest contents on your blog and I extremely love reading them.


dfun90340 said...

Thank you so much for ding the impressive job here, everyone will surely like your post.
I really enjoy reading and also appreciate your work. 온라인카지노

dfun90340 said...

온라인카지노 Great Information sharing .. I am very happy to read this article .. thanks for giving us go through info.Fantastic nice. I appreciate this post.

dfun90340 said...

일본야동 Admiring the time and effort you put into your blog and detailed information you offer!..

안전놀이터 said...

We are looking for a lot of data on this item. In the meantime, this is the perfect article I was looking for . Please post a lot about items related to 안전놀이터!!! I am waiting for your article. And when you are having difficulty writing articles, I think you can get a lot of help by visiting my .

SEO CANADA said...

Great webpage brother I am gona inform this to all my friends and contacts 먹튀검증

카지노사이트 바카라사이트 said...

I will definitely digg it and personally suggest to my friends. I am sure they’ll be benefited from this site.온라인바둑이

카지노사이트 바카라사이트 said...

We are really grateful for your blog post온라인카지노

카지노사이트 바카라사이트 said...

Great blog article.Really looking forward to read more.?Will read on온라인카지노

카지노사이트 바카라사이트 said...

thank you for letting me see this information,if you could post more good contents in the future.온라인카지노

'/lk said...

I have read your article, it is very informative and helpful for me. I admire the valuable information you offer in your articles. Thanks for posting it 먹튀검증

Robert said...

I have browsed most of your posts. This post is probably where I got the most 메이저놀이터

Roman said...

I was truly thankful for the useful info on this fantastic subject along with similarly prepare for much more terrific messages. Many thanks a lot for valuing this refinement write-up with me. I am valuing it significantly! Preparing for an additional excellent article 사설토토

lionerrr said...

This is a wonderful article, Given so much info in it, These type of articles keeps the users interest in the website, and keep on sharing more ... good luck 토토사이트가입

먹튀사이트 said...

This is actually the kind of information I have been trying to find. Thank you for writing this information. 먹튀사이트

Unknown said...

You delivered such an impressive piece to read, giving every subject enlightenment for us to gain information. Thanks for sharing such information with us due to which my several concepts have been cleared. 꽁머니홍보

gjhh said...

Glad to chat your blog, I seem to be forward to more reliable articles and I think we all wish to thank so many good articles, blog to share with us 먹튀검증목록

gjhh said...

There is definately a great deal to know about this subject. I like all of the points you've made 토토사이트

'/lk said...

Thanks for every other informative site. The place else may just I get that kind of information written in such an ideal means? I have a venture that I’m just now operating on, and I have been on the look out for such information 안전놀이터

Robert said...

Glad to read. Do you want help with essay? Get it right from here without burning your pocket. Know more about essay help 먹튀검증

lionerrr said...

Hi! This is my first visit to your blog! We are a team of volunteers and new initiatives in the same niche. Blog gave us useful information to work. You have done an amazing job 먹튀폴리스

Roman said...

useful information on topics that plenty are interested on for this wonderful post.Admiring the time and effort you put into your b!.. 먹튀폴리스

'/lk said...

This is actually the kind of information I have been trying to find. Thank you for writing this information 먹튀검증사이트

gjhh said...

This is a great article, Given such a great amount of information in it, These kind of articles keeps the clients enthusiasm for the site, and continue sharing more ... 온라인카지노

바카라사이트 said...

What a fantabulous post this has been. Never seen this kind of useful post. I am grateful to you and expect more number of posts like these. Thank you very much 바카라사이트

lionerrr said...

Nice to be visiting your blog again, it has been months for me. Well this article that i've been waited for so long. I need this article to complete my assignment in the college, and it has same topic with your article. Thanks, great share 토토사이트

gjhh said...

Thanks for a very interesting blog. What else may I get that kind of info written in such a perfect approach? I’ve a undertaking that I am simply now operating on, and I have been at the look out for such info. 메이저사이트

Unknown said...

Excellent information on your blog, thank you for taking the time to share with us. Amazing insight you have on this, it's nice to find a website that details so much information about different artists. 토토핫

asjh said...

"nice post

thanks for sharing" 토토사이트

Unknown said...

This is important, though it's necessary to help you head over to it weblink: 먹튀검증

Unknown said...

I wanted to thank you for this excellent read!! I definitely loved every little bit of it. I have you bookmarked your site to check out the new stuff you post. 토토사이트

jack chein aaaaa said...

It is a really very nice blog 먹튀폴리스

jacky staryyy said...

 definitely enjoying every little bit of it. It is a great website and nice share. I want to thank you. Good job! You guys do a great blog, and have some great contents. Keep up the good work 먹튀폴리스

Unknown said...

Wow, cool post. I'd like to write like this too - taking time and real hard work to make a great article... but I put things off too much and never seem to get started. Thanks though. 메이저사이트

Robert said...

Everything has its own value, but this is really precious information shared by Author 안전놀이터

Robert said...

Wonderful article man! The way you have explained everything step by step is phenomenal. I really appreciate your work. Keep it up!!! In case, if you are looking 토토사이트

miojmnu9 said...

Trying to say thanks won't simply be adequate, for the fantasti c clarity in your written work. I will immediately snatch your rss channel to remain educated of any updates 바카라사이트

Robert said...

I'm really enjoying the design and layout of your site. It's a very easy on the eyes which makes it much more pleasant for me to come here and visit more often. Did you hire out a developer to create your theme? Superb work! 토토사이트

'/lk said...

I read this article. I think You put a lot of effort to create this article. I appreciate your work. 메이저놀이터

gjhh said...

I am always searching online for articles that can help me. There is obviously a lot to know about this. I think you made some good points in Features also. Keep working, great job! 먹튀폴리스

dsfgasdfgjsdbn said...

Nice post! This is a very nice blog that I will definitively come back to more times this year! Thanks for informative post. 안전놀이터

Anonymous said...

I am hoping the same best effort from you in the future as well. In fact your creative writing skills has inspired me. 사설토토

'/lk said...

It's always exciting to read articles from other writers and practice something from their web sites. 토토사이트

officelaptop029@gmail.com said...

Thank you again for all the knowledge you distribute,Good post. I was very interested in the article, it's quite inspiring I should admit. I like visiting you site since I always come across interesting articles like this one.Great Job, I greatly appreciate that.Do Keep sharing! Regards 사설토토

kh said...

Wow, What an Outstanding post. I found this too much informatics. It is what I was seeking for 슈어맨

UFABET1688 said...

I am really enjoying reading your well written articles. It looks like you spend a lot of effort and time on your blog. I have bookmarked it and I am looking forward to reading new articles. Keep up the good work. ufabet1688

Unknown said...

Thank you for helping people get the information they need. Great stuff as usual. Keep up the great work!!! 토토

Henry Jacob said...

I found your this post while searching for some related information on blog search...Its a good post..keep posting and update the information. buy website traffic

asjh said...

wow, awesome blog.Thanks Again. Much obliged 먹튀검증

asjh said...

hay you there you must hire my dating service which you can book there. 토토사이트

Unknown said...

I want you to thank for your time of this wonderful read!! 승부벳

Unknown said...

I like your post and also like your website because your website is very fast and everything in this website is good. Keep writing such informative posts. I have bookmark your website. Thanks for sharing 먹튀검증사이트

chein said...

Very interesting blog. A lot of blogs I see these days don't really provide anything that I'm interested in, but I'm most definitely interested in this one. 메이저사이트 추천

Unknown said...

Its a great pleasure reading your post.Its full of information I am looking for and I love to post a comment that "The content of your post is awesome" Great work 안전놀이터

Unknown said...

Thank you for some other informative blog. 이기자벳

Unknown said...

I have bookmarked it and I am anticipating perusing new articles. Keep doing awesome 메이저사이트

Unknown said...

You have done a great job on this article. It’s very readable and highly intelligent. You have even managed to make it understandable and easy to read. You have some real writing talent. Thank you. 토토사이트

Thomas Hardy said...

HONDA Motorcycle may also be a two- or three-wheeled automobile. There are three major varieties of motorcycles: street, off-road, and twin purposes. Within these types, there are many sub-types of motorcycles for a number of purposes. There may be frequently a racing counterpart to every type.
HONDA Motorcycle Blessings or Disadvantages
Honda is amongst the most important seasoned makers of Motorcycles and that they serve a global market turning in alongside side Honda Motorcycle Price in Bangladesh a suitable assortment of contraptions going from satisfactory models. Some of the professionals have included the accompanying

Thomas Hardy said...

Bajaj Bike is and a few-wheeled engine vehicle. There are 3 significant kinds of bikes: road, rough terrain, and double reason. Inside these sports, there are many sub-kinds of bicycles for various purposes. There is some of the time a hustling partner to each kind. Every setup offers either a specific favorable position or wide fitness.
Bajaj Bike Favorable Circumstances or Weaknesses
Bajaj is among the most prepared creators of bicycles and they serve an overall market conveying alongside Bajaj bike Price in BD a wide variety of contraptions going from quality models.

Thomas Hardy said...

The usage of mobile phones is beggar description. Mobile phones have both merits and demerits, so you have to be aware enough about it. Everyone should research on mobile phones before buying the mobile phones. You may get all the information from the Mobile Bazar of Mobile Price in BD. The basic usage of mobile phone is to allow you to contact with another person while you are almost anywhere. You can easily send the messages and email by the help of the mobile phones. You can also chart with the people by using it and that make the conversation process easier. Along with these:

Unknown said...

That is the excellent mindset, nonetheless is just not help to make every sence whatsoever preaching about that mather. Virtually any method many thanks in addition to i had endeavor to promote your own article in to delicius nevertheless it is apparently a dilemma using your information sites can you please recheck the idea. thanks once more 메이저사이트 추천

Thomas Hardy said...

This site will give all of you the most recent updates, Mobile Price in BD that is identified with mobiles.
We structured each model of various brands in such a manner that will assist you in comparing one model with another.
You can likewise get the survey of each model from our site which will push you to everything of that model.
We likewise give the most recent cost of each model which is fixed from well-known web-based business destinations.

website said...

thank you for some other informative blog.

check out said...

wow, awesome blog.Thanks Again. Much obliged.

powerball365.info said...

Thanks for sharing a nice article really such a wonderful site you have done a great job once more thanks a lot 파워볼게임

racesitepro said...

Fantastic site. Lots of helpful information here. 경마

reelgamesite said...

Super interesting discussion. Thank you for the great comments. 슬롯머신

totosite365info said...

This is what i call , great article website that i have read . hope i can do same as this 스포츠토토

badugisitenet said...

Wonderful website. Lots of helpful info here. I’m sending
it to some pals ans also sharing in delicious. And naturally, thanks for your effort! 온라인바둑이
‮‬

https://www.pharaohcasino.net said...

you’re in reality a just right webmaster. The website loading pace is amazing.카지노사이트

bamgosoocomq said...

Great post. I was checking continuously this blog and I am impressed!
Extremely helpful info particularly the last part :) I care for such info
a lot. I was seeking this particular information for
a long time. Thank you and good luck.강남오피

oncasino.site said...

바카라사이트 Oh well, I came across your blog site and I found this stunning and interesting to read. Please keep me posted.

oncasino.site said...

온라인카지노 Interesting topic for a blog. I have been searching the Internet for fun and came upon your website. Fabulous post. Thanks a ton for sharing your knowledge! It is great to see that some people still put in an effort into managing their websites. I’ll be sure to check back again real soon.

oncasino.site said...

스포츠토토 I havent any word to appreciate this post…..Really i am impressed from this post….the person who create this post it was a great human..thanks for shared this with us.

oncasino.site said...

토토사이트 I truly desired the articles it’s truly wonderful best author

bamgosoocomq said...

Just desire tto say your article is as surprising.
The clarity in your post is simply excellent and i could
assume you’re an expert on this subject.
Well with your permission allow me to grab your RSS feed to keep updated with forthcoming
post. Thanks a million and please carry on the enjoyable work.오피


coincasinotop said...

Great post, beautiful weblog with great informational content. This is a really interesting and informative content. 카지노사이트

슬롯머신777사이트 said...

Hi! this is often nice article you shared with great information. Thanks for giving such an exquisite informative information. 슬롯머신

블랙잭사이트 said...

I am curious to find out what blog system you happen to be working with? I’m experiencing some small security problems with my latest site and I would like to find something more secure.

바카라사이트

assignment help said...

A student has to learn multitasking because education nowadays has become a race. You will have to perform your level best in a lot of online assignment help, assessments and extra-curricular activities as well, if you want to be an all-rounder. Even if you do not aspire to be an all-rounder and just want a cool and considerate academic life you will have to do the same things. Because, literally every student is trying their level best to achieve heights. So, missing deadlines and not finishing homework is not an option today. If due to extra work pressure, or sudden illness or any other genuine reason you think that “I need to take my online class”, go ahead and hire an expert. There is nothing bad din doing so.

yasul.top said...

I'm satisfied, I must claim. Truly seldom do I run into a blog that?s both educative as well as enjoyable, and let me tell you, you have struck the nail on the head. Your concept is impressive; the issue is something that inadequate individuals are speaking smartly about. I am very happy that I came across this in my search for something relating to this. 야한소설

ophunter.net said...

Hi there! I just want to offer you a huge thumbs up for the great information you have here on this post. I’ll be coming back to your website for more soon. 대딸방

massage.blue said...

You’re so cool! I do not believe I’ve truly read through a single thing like this before. So good to find somebody with a few original thoughts on this issue. Really. 스포츠마사지 . many thanks for starting this up. This site is one thing that is required on the internet, someone with a little originality.

gunma.top said...

There is visibly a bundle to know about this 출장마사지. I think you made certain good factors in features also.

assignment help said...

You will soon a lot of genuine options to try. Share your assignment help prerequisites with them and they will provide you with solutions as soon as you want. Do not fall prey to anti-social elements in the digital programming assignment help world while trying to gain assistance. Be wise and choose your service provider patiently.