[1] http://dunkels.com/adam/pt/ [2] https://en.wikipedia.org/wiki/Protothread
Implementing simple cooperative threads in C
31–40 of 88 posts
Re: Implementing simple cooperative threads in C
#32Earlier quoted context omitted.
Are there exceptions in C? If I’m interviewing someone for a programming job and I see goto in there C code... they better have an amazing reason or they won’t be getting the job. Harsh, but it’s reality of how few people are suited for embedded programming.
I spend a lot of time dicking with embedded C code. I think using goto is reasonable some cases. The classic case is to short circuit a block of code when there is an error of some sort. function(){ // early returns here. // stuff // more stuff if(oops_err){ goto some_error; } // yet more stuff // normal exit return 0; // bad exit some_error: // clean up return oops_err; } The above is fine, you avoid a convoluted ex…
https://www.kernel.org/doc/html/v4.10/process/coding-style.h...
Re: Implementing simple cooperative threads in C
#33Re: Implementing simple cooperative threads in C
#34Re: Implementing simple cooperative threads in C
#35Earlier quoted context omitted.
> FWIW, the original implementation of Java (when it was known as Oak) used this trick to manage user level threading. There was also a some code to do the stack swap since you were in a virtual machine anyway, that stuff was all there to be adjusted. I never knew Java did this. I figured most "interpreted" languages (via direct interpreters or bytecode) would be trivial to implement any kind of threads (even pre-emp…
I think he was referring to this https://en.wikipedia.org/wiki/Green_threads BTW mid-grey text on yellow makes your great article difficult to read. I've had to open it in eww to read it. Worth it though, thanks!
J2ME only had geen threads iirc.
Re: Implementing simple cooperative threads in C
#36FWIW, the original implementation of Java (when it was known as Oak) used this trick to manage user level threading. There was also a some code to do the stack swap since you were in a virtual machine anyway, that stuff was all there to be adjusted. Random meta note brenns10, the idiom for giving up the processor is 'yield' (rather than 'relinquish') it is common in cooperative systems. That said, the code in Java th…
Is this why java.lang.Thread still has the yield() method? All Java SE implementations I've ever seen use (preemptive) OS threads, so I assumed it's been this way from the beginning, and this yield() never made much sense to me.
Re: Implementing simple cooperative threads in C
#37A more common method of implementing this that avoids setjmp and longjmp is to code a little assembly to do the stack switch. This is typically faster and easier to port, see: https://probablydance.com/2013/02/20/handmade-coroutines-for...
Re: Implementing simple cooperative threads in C
#38FWIW, the original implementation of Java (when it was known as Oak) used this trick to manage user level threading. There was also a some code to do the stack swap since you were in a virtual machine anyway, that stuff was all there to be adjusted. Random meta note brenns10, the idiom for giving up the processor is 'yield' (rather than 'relinquish') it is common in cooperative systems. That said, the code in Java th…
Is this why java.lang.Thread still has the yield() method? All Java SE implementations I've ever seen use (preemptive) OS threads, so I assumed it's been this way from the beginning, and this yield() never made much sense to me.
Re: Implementing simple cooperative threads in C
#391. You need to decide how much stack space to allocate to a fiber beforehand. If you go over this limit, you will segfault. 2. Thread local storage doesnt exist anymore since you're potentially swapping between different kernel threads (and different fibers are using the same thread and will share storage). 3. Certain types of locks can now fail because the lock isnt transferred to the new thread upon being woken up. Other locks run into rentrancy problems here.
Does anyone know of good solutions to these problems? Is there a library that takes all these things into account?
Re: Implementing simple cooperative threads in C
#40I find the contrast between the text color and background color to be too low for comfortable reading.
In any case, if I added a small JS powered button to switch into a higher contrast style, would that make the site more usable to you?