Giter Site home page Giter Site logo

libcopp's Introduction

libcopp

cross-platform coroutine library of c++

in developing ...

Build & Run Unit Test in Linux Windows
Status Build Status Build status

LICENSE

License under the MIT license

Document

Generate document with doxygen.

Doxygen file located at doc/libcopp.doxyfile .

INSTALL

libcopp use cmake to generate makefile and switch build tools.

Prerequisites

  • [required] GCC or Clang or VC support ISO C++ 98 and upper
  • [required] cmake 2.8.9 and upper
  • [optional] gtest 1.6.0 and upper (better test supported)
  • [optional] Boost.Test (Boost.Test supported)

Unix

  • [required] ar, as, ld (binutils)
  • [optional] if using gtest, pthread is required.

Windows

  • [required] masm (in vc)
  • [optional] if using gtest, pthread is required.

Build

1. make a build directory

mkdir build

2. run cmake command

cmake <libcopp dir> [options]

options can be cmake options. such as set compile toolchains, source directory or options of libcopp that control build actions. libcopp options are listed below:

-DBUILD_SHARED_LIBS=YES|NO [default=NO] enable build dynamic library.

-DLIBCOPP_ENABLE_SEGMENTED_STACKS=YES|NO [default=NO] enable split stack supported context.(it's only availabe in linux and gcc 4.7.0 or upper)

-DLIBCOPP_ENABLE_VALGRIND=YES|NO [default=NO] enable valgrind supported context.

-DGTEST_ROOT=[path] set gtest library install prefix path

3. make libcopp

make [options]

4. run test [optional]

test/coroutine_test

5. install [optional]

make install

Or you can just copy include directory and libcopp.a in lib or lib64 into your project to use it.

USAGE

Just include headers and linking library file of your platform to use libcopp

Example

coroutine_context example

There is a simple example of using coroutine context below:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <inttypes.h>
#include <stdint.h>

// include context header file
#include <libcopp/coroutine/coroutine_context_container.h>

// define a coroutine runner
class my_runner : public copp::detail::coroutine_runnable_base
{
public:
    int operator()() {
        // ... your code here ...printf("cortoutine %" PRIxPTR " exit and return %d.\n", (intptr_t)&co_obj, co_obj.get_ret_code());
        copp::coroutine_context_default* addr = get_coroutine_context<copp::coroutine_context_default>();
        std::cout<< "cortoutine "<< addr<< " is running."<< std::endl;

        addr->yield();
        std::cout<< "cortoutine "<< addr<< " is resumed."<< std::endl;

        return 1;
    }
};

int main() {
    // create a coroutine
    copp::coroutine_context_default co_obj;
    std::cout<< "cortoutine "<< &co_obj<< " is created."<< std::endl;

    // create a runner
    my_runner runner;

    // bind runner to coroutine object
    co_obj.create(&runner);

    // start a coroutine
    co_obj.start();

    // yield from runner
    std::cout<< "cortoutine "<< &co_obj<< " is yield."<< std::endl;
    co_obj.resume();

    std::cout<< "cortoutine "<< &co_obj<< " exit and return "<< co_obj.get_ret_code()<< "."<< std::endl;
    return 0;
}

And then, you can custom many function such as set your stack allocator, coroutine type and etc. by set your template parameters of coroutine context. Notice: One coroutine runner can only below to one coroutine context

coroutine task example

There is a simple example of using coroutine task below:

#include <iostream>

// include task header file
#include <libcotask/task.h>

typedef cotask::task<> my_task_t;

int main(int argc, char* argv[]) {
    // create a task using factory function [with lambda expression]
	my_task_t::prt_t task = my_task_t::create([](){
	    std::cout<< "task "<< cotask::this_task::get_task()->get_id()<< " started"<< std::endl;
        cotask::this_task::get_task()->yield();
		std::cout<< "task "<< cotask::this_task::get_task()->get_id()<< " resumed"<< std::endl;
		return 0;
    });
	
	std::cout<< "task "<< task->get_id()<< " created"<< std::endl;
    // start a task
    task->start();

	std::cout<< "task "<< task->get_id()<< " yield"<< std::endl;
	task->resume();
	std::cout<< "task "<< task->get_id()<< " stoped, ready to be destroyed."<< std::endl;

    return 0;
}

And then, you can custom many functions by set your macro type of coroutine and task to do some other function.

NOTICE

split stack support: if in Linux and user gcc 4.7.0 or upper, add -DLIBCOPP_ENABLE_SEGMENTED_STACKS=YES to use split stack supported context.

DEVELOPER

basic coroutine object summary

safe coroutine object summary

DEVELOP PLAN

THANKS TO

mutouyun

libcopp's People

Contributors

owent avatar

Watchers

 avatar

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.