2
0
mirror of https://github.com/boostorg/context.git synced 2026-01-19 04:02:17 +00:00

execution_context removed

This commit is contained in:
Oliver Kowalke
2019-08-29 13:42:03 +02:00
parent 227fbd01f2
commit 0c754f90c5
35 changed files with 102 additions and 5758 deletions

View File

@@ -1,68 +0,0 @@
# Boost.Context Library Examples Jamfile
# Copyright Oliver Kowalke 2014.
# Distributed under the Boost Software License, Version 1.0.
# (See accompanying file LICENSE_1_0.txt or copy at
# http://www.boost.org/LICENSE_1_0.txt)
# For more information, see http://www.boost.org/
import common ;
import feature ;
import indirect ;
import modules ;
import os ;
import toolset ;
import architecture ;
project boost/context/example/execution_context_v2
: requirements
<library>/boost/context//boost_context
<toolset>gcc,<segmented-stacks>on:<cxxflags>-fsplit-stack
<toolset>gcc,<segmented-stacks>on:<cxxflags>-DBOOST_USE_SEGMENTED_STACKS
<toolset>clang,<segmented-stacks>on:<cxxflags>-fsplit-stack
<toolset>clang,<segmented-stacks>on:<cxxflags>-DBOOST_USE_SEGMENTED_STACKS
<link>static
<threading>multi
;
exe jump_void
: jump_void.cpp
;
exe jump
: jump.cpp
;
exe fibonacci
: fibonacci.cpp
;
exe parser
: parser.cpp
;
exe parameter
: parameter.cpp
;
exe ontop_void
: ontop_void.cpp
;
exe ontop
: ontop.cpp
;
exe throw
: throw.cpp
;
exe echosse
: echosse.cpp
;
#exe backtrace
# : backtrace.cpp
# : <linkflags>"-lunwind"
# ;

View File

@@ -1,58 +0,0 @@
// Copyright Oliver Kowalke 2014.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#define UNW_LOCAL_ONLY
#include <cstdlib>
#include <iostream>
#include <libunwind.h>
#include <boost/context/execution_context.hpp>
namespace ctx = boost::context;
void backtrace() {
unw_cursor_t cursor;
unw_context_t context;
unw_getcontext( & context);
unw_init_local( & cursor, & context);
while ( 0 < unw_step( & cursor) ) {
unw_word_t offset, pc;
unw_get_reg( & cursor, UNW_REG_IP, & pc);
if ( 0 == pc) {
break;
}
std::cout << "0x" << pc << ":";
char sym[256];
if ( 0 == unw_get_proc_name( & cursor, sym, sizeof( sym), & offset) ) {
std::cout << " (" << sym << "+0x" << offset << ")" << std::endl;
} else {
std::cout << " -- error: unable to obtain symbol name for this frame" << std::endl;
}
}
}
void bar() {
backtrace();
}
void foo() {
bar();
}
ctx::execution_context< void > f1( ctx::execution_context< void > && ctxm) {
foo();
return std::move( ctxm);
}
int main() {
ctx::execution_context< void > ctx1( f1);
ctx1 = ctx1();
std::cout << "main: done" << std::endl;
return EXIT_SUCCESS;
}

View File

@@ -1,45 +0,0 @@
// Copyright Oliver Kowalke 2009.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#include <cstddef>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <emmintrin.h>
#include <boost/context/execution_context.hpp>
namespace ctx = boost::context;
void echoSSE( int i) {
__m128i xmm;
xmm = _mm_set_epi32( i, i + 1, i + 2, i + 3);
uint32_t v32[4];
memcpy( & v32, & xmm, 16);
std::cout << v32[0];
std::cout << v32[1];
std::cout << v32[2];
std::cout << v32[3];
}
ctx::execution_context< int > echo( ctx::execution_context< int > && ctx, int i) {
for (;;) {
std::cout << i;
echoSSE( i);
std::cout << " ";
std::tie( ctx, i) = ctx( 0);
}
return std::move( ctx);
}
int main( int argc, char * argv[]) {
ctx::execution_context< int > ctx( echo);
for ( int i = 0; i < 10; ++i) {
ctx = std::get< 0 >( ctx( i) );
}
std::cout << "\nDone" << std::endl;
return EXIT_SUCCESS;
}

View File

@@ -1,38 +0,0 @@
// Copyright Oliver Kowalke 2014.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#include <cstdlib>
#include <iostream>
#include <memory>
#include <boost/context/execution_context.hpp>
namespace ctx = boost::context;
int main() {
int n=35;
ctx::execution_context< int > source(
[n](ctx::execution_context< int > && sink, int) mutable {
int a=0;
int b=1;
while(n-->0){
auto result=sink(a);
sink=std::move(std::get<0>(result));
auto next=a+b;
a=b;
b=next;
}
return std::move( sink);
});
for(int i=0;i<10;++i){
auto result=source(i);
source=std::move(std::get<0>(result));
std::cout<<std::get<1>(result)<<" ";
}
std::cout<<std::endl;
std::cout << "main: done" << std::endl;
}

View File

@@ -1,32 +0,0 @@
// Copyright Oliver Kowalke 2014.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#include <cstdlib>
#include <iostream>
#include <boost/context/execution_context.hpp>
namespace ctx = boost::context;
ctx::execution_context< int > f1( ctx::execution_context< int > && ctxm, int data) {
std::cout << "f1: entered first time: " << data << std::endl;
std::tie( ctxm, data) = ctxm( data + 2);
std::cout << "f1: entered second time: " << data << std::endl;
return std::move( ctxm);
}
int main() {
int data = 1;
ctx::execution_context< int > ctx1( f1);
std::tie( ctx1, data) = ctx1( data + 2);
std::cout << "f1: returned first time: " << data << std::endl;
std::tie( ctx1, data) = ctx1( data + 2);
std::cout << "f1: returned second time: " << data << std::endl;
std::cout << "main: done" << std::endl;
return EXIT_SUCCESS;
}

View File

@@ -1,31 +0,0 @@
// Copyright Oliver Kowalke 2014.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#include <cstdlib>
#include <iostream>
#include <boost/context/execution_context.hpp>
namespace ctx = boost::context;
ctx::execution_context< void > f1( ctx::execution_context< void > && ctxm) {
std::cout << "f1: entered first time" << std::endl;
ctxm = ctxm();
std::cout << "f1: entered second time" << std::endl;
return std::move( ctxm);
}
int main() {
ctx::execution_context< void > ctx1( f1);
ctx1 = ctx1();
std::cout << "f1: returned first time" << std::endl;
ctx1 = ctx1();
std::cout << "f1: returned second time" << std::endl;
std::cout << "main: done" << std::endl;
return EXIT_SUCCESS;
}

View File

@@ -1,42 +0,0 @@
// Copyright Oliver Kowalke 2014.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#include <cstdlib>
#include <iostream>
#include <tuple>
#include <boost/context/execution_context.hpp>
namespace ctx = boost::context;
ctx::execution_context< int > f1( ctx::execution_context< int > && ctx, int data) {
std::cout << "f1: entered first time: " << data << std::endl;
std::tie( ctx, data) = ctx( data + 1);
std::cout << "f1: entered second time: " << data << std::endl;
std::tie( ctx, data) = ctx( data + 1);
std::cout << "f1: entered third time: " << data << std::endl;
return std::move( ctx);
}
int f2( int data) {
std::cout << "f2: entered: " << data << std::endl;
return -1;
}
int main() {
int data = 0;
ctx::execution_context< int > ctx( f1);
std::tie( ctx, data) = ctx( data + 1);
std::cout << "f1: returned first time: " << data << std::endl;
std::tie( ctx, data) = ctx( data + 1);
std::cout << "f1: returned second time: " << data << std::endl;
std::tie( ctx, data) = ctx( ctx::exec_ontop_arg, f2, data + 1);
std::cout << "f1: returned third time" << std::endl;
std::cout << "main: done" << std::endl;
return EXIT_SUCCESS;
}

View File

@@ -1,40 +0,0 @@
// Copyright Oliver Kowalke 2014.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#include <cstdlib>
#include <iostream>
#include <tuple>
#include <boost/context/execution_context.hpp>
namespace ctx = boost::context;
ctx::execution_context< void > f1( ctx::execution_context< void > && ctx) {
std::cout << "f1: entered first time" << std::endl;
ctx = ctx();
std::cout << "f1: entered second time" << std::endl;
ctx = ctx();
std::cout << "f1: entered third time" << std::endl;
return std::move( ctx);
}
void f2() {
std::cout << "f2: entered" << std::endl;
}
int main() {
ctx::execution_context< void > ctx( f1);
ctx = ctx();
std::cout << "f1: returned first time" << std::endl;
ctx = ctx();
std::cout << "f1: returned second time" << std::endl;
ctx = ctx( ctx::exec_ontop_arg, f2);
std::cout << "f1: returned third time" << std::endl;
std::cout << "main: done" << std::endl;
return EXIT_SUCCESS;
}

View File

@@ -1,62 +0,0 @@
// Copyright Oliver Kowalke 2014.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#include <cstdlib>
#include <exception>
#include <iostream>
#include <memory>
#include <string>
#include <boost/variant.hpp>
#include <boost/context/execution_context.hpp>
#include <boost/lexical_cast.hpp>
typedef boost::variant<int,std::string> variant_t;
namespace ctx = boost::context;
class X{
private:
std::exception_ptr excptr_;
ctx::execution_context<variant_t> ctx_;
public:
X():
excptr_(),
ctx_(
[this](ctx::execution_context<variant_t> && ctx, variant_t data){
try {
for (;;) {
int i = boost::get<int>(data);
data = boost::lexical_cast<std::string>(i);
auto result = ctx( data);
ctx = std::move( std::get<0>( result) );
data = std::get<1>( result);
}
} catch ( std::bad_cast const&) {
excptr_=std::current_exception();
}
return std::move( ctx);
})
{}
std::string operator()(int i){
variant_t data = i;
auto result = ctx_( data);
ctx_ = std::move( std::get<0>( result) );
data = std::get<1>( result);
if(excptr_){
std::rethrow_exception(excptr_);
}
return boost::get<std::string>(data);
}
};
int main() {
X x;
std::cout<<x(7)<<std::endl;
std::cout << "done" << std::endl;
}

View File

@@ -1,144 +0,0 @@
// Copyright Oliver Kowalke 2014.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#include <cstdlib>
#include <exception>
#include <functional>
#include <iostream>
#include <memory>
#include <sstream>
#include <boost/context/execution_context.hpp>
namespace ctx = boost::context;
/*
* grammar:
* P ---> E '\0'
* E ---> T {('+'|'-') T}
* T ---> S {('*'|'/') S}
* S ---> digit | '(' E ')'
*/
class Parser{
char next;
std::istream& is;
std::function<void(char)> cb;
char pull(){
return std::char_traits<char>::to_char_type(is.get());
}
void scan(){
do{
next=pull();
}
while(isspace(next));
}
public:
Parser(std::istream& is_,std::function<void(char)> cb_) :
next(), is(is_), cb(cb_)
{}
void run() {
scan();
E();
}
private:
void E(){
T();
while (next=='+'||next=='-'){
cb(next);
scan();
T();
}
}
void T(){
S();
while (next=='*'||next=='/'){
cb(next);
scan();
S();
}
}
void S(){
if (isdigit(next)){
cb(next);
scan();
}
else if(next=='('){
cb(next);
scan();
E();
if (next==')'){
cb(next);
scan();
}else{
throw std::runtime_error("parsing failed");
}
}
else{
throw std::runtime_error("parsing failed");
}
}
};
int main() {
try {
std::istringstream is("1+1");
bool done=false;
std::exception_ptr except;
// execute parser in new execution context
ctx::execution_context<char> source(
[&is,&done,&except](ctx::execution_context<char> && sink,char){
// create parser with callback function
Parser p( is,
[&sink](char ch){
// resume main execution context
auto result = sink(ch);
sink = std::move(std::get<0>(result));
});
try {
// start recursive parsing
p.run();
} catch (...) {
// store other exceptions in exception-pointer
except = std::current_exception();
}
// set termination flag
done=true;
// resume main execution context
return std::move( sink);
});
// user-code pulls parsed data from parser
// invert control flow
auto result = source('\0');
source = std::move(std::get<0>(result));
char c = std::get<1>(result);
if ( except) {
std::rethrow_exception(except);
}
while( ! done) {
printf("Parsed: %c\n",c);
std::tie(source,c) = source('\0');
if (except) {
std::rethrow_exception(except);
}
}
std::cout << "main: done" << std::endl;
return EXIT_SUCCESS;
} catch ( std::exception const& e) {
std::cerr << "exception: " << e.what() << std::endl;
}
return EXIT_FAILURE;
}

View File

@@ -1,47 +0,0 @@
// Copyright Oliver Kowalke 2014.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#include <cstdlib>
#include <exception>
#include <iostream>
#include <stdexcept>
#include <string>
#include <boost/context/execution_context.hpp>
namespace ctx = boost::context;
struct my_exception : public std::runtime_error {
my_exception( std::string const& what) :
std::runtime_error{ what } {
}
};
int main() {
ctx::execution_context< void > ctx([](ctx::execution_context<void> && ctx) {
for (;;) {
try {
std::cout << "entered" << std::endl;
ctx = ctx();
} catch ( ctx::ontop_error const& e) {
try {
std::rethrow_if_nested( e);
} catch ( my_exception const& ex) {
std::cerr << "my_exception: " << ex.what() << std::endl;
}
return e.get_context< void >();
}
}
return std::move( ctx);
});
ctx = ctx();
ctx = ctx();
ctx = ctx( ctx::exec_ontop_arg, []() { throw my_exception("abc"); });
std::cout << "main: done" << std::endl;
return EXIT_SUCCESS;
}