项目作者: sharow

项目描述 :
:copyright: Concurrent Programming Library (Coroutine) for C11
高级语言: C
项目地址: git://github.com/sharow/libconcurrent.git
创建时间: 2013-11-04T15:50:39Z
项目社区:https://github.com/sharow/libconcurrent

开源协议:zlib License

下载


libconcurrent

zlib License

tiny asymmetric-coroutine library.

Description

  • asymmetric-coroutine
  • bidirectional communication by yield_value/resume_value
  • native context switch
  • C11

Supported Platforms

x86_64 i686 ARM(v6/v7) AArch64 (contributor)
Linux :heavy_check_mark: :heavy_check_mark: :heavy_check_mark: :heavy_check_mark:
FreeBSD :heavy_check_mark: :heavy_check_mark: :question: :question: @t6
OSX :heavy_check_mark: :heavy_multiplication_x: :heavy_multiplication_x: :heavy_check_mark: @kpamnany
(contributor) @mitghi

Code Example

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdint.h>
  4. #include <stdnoreturn.h>
  5. #include <concurrent/concurrent.h>
  6. #include <concurrent/shortname.h>
  7. #define STACK_SIZE (1024 * 2)
  8. noreturn void accumulator(struct concurrent_ctx *ctx)
  9. {
  10. int *v = ctx_get_resume_value(ctx);
  11. int total = *v;
  12. for (;;) {
  13. v = yield_value(ctx, &total); // send total / receive next value
  14. total += *v;
  15. }
  16. }
  17. int main(void)
  18. {
  19. struct concurrent_ctx *ctx;
  20. uint8_t stack[STACK_SIZE];
  21. uint8_t ctx_alloc[ctx_sizeof()];
  22. concurrent_init();
  23. ctx = (struct concurrent_ctx *)ctx_alloc;
  24. ctx_construct(ctx, stack, STACK_SIZE, accumulator, NULL);
  25. for (int i = 1; i <= 10; i++) {
  26. int *total = resume_value(ctx, &i); // send value / receive total
  27. printf("total = %d\n", *total);
  28. }
  29. ctx_destruct(ctx);
  30. concurrent_fin();
  31. return EXIT_SUCCESS;
  32. }
  33. /*
  34. $ gcc -o sample sample.c -lconcurrent.a
  35. $ ./sample
  36. total = 1
  37. total = 3
  38. total = 6
  39. total = 10
  40. total = 15
  41. total = 21
  42. total = 28
  43. total = 36
  44. total = 45
  45. total = 55
  46. */

Requirements for build

  • for x86_64/i686: nasm

Installation

  1. $ git clone https://github.com/sharow/libconcurrent.git libconcurrent
  2. $ cd libconcurrent
  3. $ make
  4. $ sudo make install

for FreeBSD

Available in ports collection as devel/libconcurrent

for OSX

  1. $ brew install nasm

Tests

  1. $ make test

Benchmark

examples/many_context1.c:

  1. -- output: (Xeon E3 2.5Ghz)
  2. 3000000 context switch in 373.5 ms
  3. one context switch in 125 ns
  4. 8031333 resume/yield pair per second
  5. -- output: (RaspberryPi2 ARMv7 900MHz)
  6. 3000000 context switch in 2861.8 ms
  7. one context switch in 954 ns
  8. 1048287 resume/yield pair per second

VS.