Wednesday, July 16, 2008

Coroutines in C

I've just uploaded a functional coroutine library for C, called libconcurrency. It's available under the LGPL. I think it's the most complete, flexible and simplest coroutine implementation I've seen, so hopefully it will find some use.

Next on the todo list are some more rigourous tests of the corner cases, and then extending libconcurrency to scale across CPUs. This will make it the C equivalent of Manticore for ML.

There is a rich opportunity for scalable concurrency in C. Of course, I only built this library to serve as a core component of a virtual machine I'm building, and that's all I'm going to say about that. ;-)

9 comments:

Chris said...

Have you looked at libcoroutine? It's by the guy who created Io. I haven't looked at it personally, but it sounds worth investigating if your looking for libraries for such things.

Sandro Magi said...

I provide a comparison to existing libraries at the libconcurrency project page, including libCoroutine.

kjk said...

Not sure how strongly do you feel about your licence choice. Any chance of changing the licence to MIT or BSD or public domain? LGPL is cumbersome when using in non-open-source applications since you have to put the code in a separate *.dll just to satisfy the license. MIT/BSD don't require that.

Sandro Magi said...

I don't believe that's an accurate assessment of the LGPL requirements. The LGPL requires only that any modifications to libconcurrency code used in a distributed program be available under an LGPL-compatible license. It places no restrictions on how you link or include the code in your program. I think it's a fair compromise between the GPL and the MIT/BSD licenses.

I will probably make a dll available soon as well.

Aaron Denney said...

Sandro Magi: It's a bit stronger than that. Modifications have to be made available, and you have to afford your users the opportunity to use other versions of the LGPL software with the software that's using it. Actually, it even specifies one of two ways that you must use to do this: providing object files so they can relink, or using it as a DLL so they can replace the DLL.

See, e.g. section 4.d in the LGPLv3.

d) Do one of the following:

* 0) Convey the Minimal Corresponding Source under the terms of this License, and the Corresponding Application Code in a form suitable for, and under terms that permit, the user to recombine or relink the Application with a modified version of the Linked Version to produce a modified Combined Work, in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source.
* 1) Use a suitable shared library mechanism for linking with the Library. A suitable mechanism is one that (a) uses at run time a copy of the Library already present on the user's computer system, and (b) will operate properly with a modified version of the Library that is interface-compatible with the Linked Version.

Early versions have substantially similar restrictions, though they may be worded a bit more confusingly

Sandro Magi said...

Thanks for the overview Aaron. The libconcurrency build files are already set up to generate a dll, so I don't see this requirement as all that onerous. Do you or others really think it is?

I will take these additional restrictions under advisement though.

Aaron Denney said...

Oh, no, I personally don't think it's an issue at all.

bodhi said...

Nice post. I'm curious how you would use multiple cores (in your todo list) as you are using stack copying technique.

Sandro Magi said...

If I still had time to work on it, I'd probably implement something like Apple's Grand Central Dispatch.