diff --git a/doc/fcontext.qbk b/doc/fcontext.qbk index ce20def..d4a6f3b 100644 --- a/doc/fcontext.qbk +++ b/doc/fcontext.qbk @@ -28,7 +28,7 @@ __fls_alloc__, __fls_free__.] [heading Executing a context] A new context supposed to execute a __context_fn__ (returning void and accepting -intptr_t as argument) will be created on top of the stack (at 16 byte boundary) +void * as argument) will be created on top of the stack (at 16 byte boundary) by function __make_fcontext__. // context-function @@ -53,7 +53,7 @@ new state of the original context. boost::context::fcontext_t fcm,fc1,fc2; - void f1(intptr_t) + void f1(void *) { std::cout<<"f1: entered"< pair_t; - void f(intptr_t param) + void f(void * param) { pair_t* p=(pair_t*)param; - p=(pair_t*)boost::context::jump_fcontext(&fc,fcm,(intptr_t)(p->first+p->second)); - boost::context::jump_fcontext(&fc,fcm,(intptr_t)(p->first+p->second)); + p=(pair_t*)boost::context::jump_fcontext(&fc,fcm,(void *)(p->first+p->second)); + boost::context::jump_fcontext(&fc,fcm,(void *)(p->first+p->second)); } std::size_t size(8192); @@ -130,11 +130,11 @@ other context. pair_t p(std::make_pair(2,7)); fc=boost::context::make_fcontext(sp,size,f); - int res=(int)boost::context::jump_fcontext(&fcm,fc,(intptr_t)&p); + int res=(int)boost::context::jump_fcontext(&fcm,fc,(void *)&p); std::cout< fcontext_t; - intptr_t jump_fcontext(fcontext_t* ofc,fcontext_t nfc,intptr_t vp); - fcontext_t make_fcontext(void* sp,std::size_t size,void(*fn)(intptr_t)); + void * jump_fcontext(fcontext_t* ofc,fcontext_t nfc,void * vp); + fcontext_t make_fcontext(void* sp,std::size_t size,void(*fn)(void *)); [heading `sp`] [variablelist @@ -187,18 +187,18 @@ downwards or upwards).]] [[Member:] [Tracks the memory for the context's stack.]] ] -[heading `intptr_t jump_fcontext(fcontext_t* ofc,fcontext_t nfc,intptr_t p) +[heading `void * jump_fcontext(fcontext_t* ofc,fcontext_t nfc,void * p) [variablelist [[Effects:] [Stores the current context data (stack pointer, instruction pointer, and CPU registers) to `*ofc` and restores the context data from `nfc`, -which implies jumping to `nfc`'s execution context. The intptr_t argument, `p`, +which implies jumping to `nfc`'s execution context. The void * argument, `p`, is passed to the current context to be returned by the most recent call to `jump_fcontext()` in the same thread.]] [[Returns:] [The third pointer argument passed to the most recent call to `jump_fcontext()`, if any.]] ] -[heading `fcontext_t make_fcontext(void* sp,std::size_t size,void(*fn)(intptr_t))`] +[heading `fcontext_t make_fcontext(void* sp,std::size_t size,void(*fn)(void *))`] [variablelist [[Precondition:] [Stack `sp` and function pointer `fn` are valid (depending on the architecture `sp` points to the top or bottom of the stack) and `size` > 0.]] diff --git a/example/echosse.cpp b/example/echosse.cpp index 2a48b08..2dcf9e0 100644 --- a/example/echosse.cpp +++ b/example/echosse.cpp @@ -26,8 +26,7 @@ typedef ctx::simple_stack_allocator< ctx::fcontext_t fcm = 0; ctx::fcontext_t fc = 0; -void echoSSE( int i) -{ +void echoSSE( int i) { __m128i xmm; xmm = _mm_set_epi32(i, i+1, i+2, i+3); uint32_t v32[4]; @@ -40,28 +39,24 @@ void echoSSE( int i) std::cout << v32[3]; } -void echo( intptr_t param) -{ - int i = ( int) ctx::jump_fcontext( & fc, fcm, 0); - for (;;) - { +void echo( void * param) { + int i = *( int *) ctx::jump_fcontext( & fc, fcm, 0); + for (;;) { std::cout << i; echoSSE( i); std::cout << " "; - i = ( int) ctx::jump_fcontext( & fc, fcm, 0); + i = * ( int *) ctx::jump_fcontext( & fc, fcm, 0); } } -int main( int argc, char * argv[]) -{ +int main( int argc, char * argv[]) { stack_allocator alloc; void * sp = alloc.allocate( stack_allocator::default_stacksize() ); fc = ctx::make_fcontext( sp, stack_allocator::default_stacksize(), echo); ctx::jump_fcontext( & fcm, fc, 0); - for ( int i = 0; i < 10; ++i) - ctx::jump_fcontext( & fcm, fc, ( intptr_t) i); - + for ( int i = 0; i < 10; ++i) { + ctx::jump_fcontext( & fcm, fc, & i); + } std::cout << "\nDone" << std::endl; - return EXIT_SUCCESS; } diff --git a/example/exception.cpp b/example/exception.cpp index 1733786..e08e3bf 100644 --- a/example/exception.cpp +++ b/example/exception.cpp @@ -24,12 +24,12 @@ ctx::fcontext_t fcm = 0; ctx::fcontext_t fc = 0; boost::exception_ptr except; -void f( intptr_t arg) -{ - try - { throw std::runtime_error( ( char *) arg); } - catch ( std::runtime_error const& e) - { except = boost::current_exception(); } +void f( void * arg) { + try { + throw std::runtime_error( ( char *) arg); + } catch ( std::runtime_error const& e) { + except = boost::current_exception(); + } ctx::jump_fcontext( & fc, fcm, arg); } @@ -42,13 +42,14 @@ int main( int argc, char * argv[]) std::cout << "main: call start_fcontext( & fcm, fc, 0)" << std::endl; const char * what = "hello world"; - ctx::jump_fcontext( & fcm, fc, ( intptr_t) what); - try - { if ( except) boost::rethrow_exception( except); } - catch ( std::exception const& ex) - { std::cerr << "exception: " << ex.what() << std::endl; } - + ctx::jump_fcontext( & fcm, fc, ( void *) what); + try { + if ( except) { + boost::rethrow_exception( except); + } + } catch ( std::exception const& ex) { + std::cerr << "exception: " << ex.what() << std::endl; + } std::cout << "main: done" << std::endl; - return EXIT_SUCCESS; } diff --git a/example/exit.cpp b/example/exit.cpp index a03d6c6..b22c1a3 100644 --- a/example/exit.cpp +++ b/example/exit.cpp @@ -24,39 +24,30 @@ ctx::fcontext_t fcm = 0; ctx::fcontext_t fc1 = 0; ctx::fcontext_t fc2 = 0; -void f1( intptr_t) -{ +void f1( void *) { std::cout << "f1: entered" << std::endl; std::cout << "f1: call jump_fcontext( & fc1, fc2, 0)" << std::endl; ctx::jump_fcontext( & fc1, fc2, 0); std::cout << "f1: return" << std::endl; } -void f2( intptr_t) -{ +void f2( void *) { std::cout << "f2: entered" << std::endl; std::cout << "f2: call jump_fcontext( & fc2, fc1, 0)" << std::endl; ctx::jump_fcontext( & fc2, fc1, 0); BOOST_ASSERT( false && ! "f2: never returns"); } -int main( int argc, char * argv[]) -{ +int main( int argc, char * argv[]) { std::cout << "size: 0x" << std::hex << sizeof( ctx::fcontext_t) << std::endl; - stack_allocator alloc; - void * sp1 = alloc.allocate( stack_allocator::default_stacksize()); fc1 = ctx::make_fcontext( sp1, stack_allocator::default_stacksize(), f1); - void * sp2 = alloc.allocate( stack_allocator::default_stacksize()); fc2 = ctx::make_fcontext( sp2, stack_allocator::default_stacksize(), f2); - std::cout << "main: call start_fcontext( & fcm, fc1, 0)" << std::endl; ctx::jump_fcontext( & fcm, fc1, 0); - std::cout << "main: done" << std::endl; BOOST_ASSERT( false && ! "main: never returns"); - return EXIT_SUCCESS; } diff --git a/example/jump.cpp b/example/jump.cpp index 8f60187..ddc5118 100644 --- a/example/jump.cpp +++ b/example/jump.cpp @@ -24,23 +24,17 @@ typedef ctx::simple_stack_allocator< ctx::fcontext_t fcm = 0; ctx::fcontext_t fc1 = 0; -void f1( intptr_t) -{ +void f1( void *) { std::cout << "f1: entered" << std::endl; ctx::jump_fcontext( & fc1, fcm, 0); } -int main( int argc, char * argv[]) -{ +int main( int argc, char * argv[]) { stack_allocator alloc; - void * base1 = alloc.allocate( stack_allocator::default_stacksize()); fc1 = ctx::make_fcontext( base1, stack_allocator::default_stacksize(), f1); - std::cout << "main: call start_fcontext( & fcm, fc1, 0)" << std::endl; ctx::jump_fcontext( & fcm, fc1, 0); - std::cout << "main: done" << std::endl; - return EXIT_SUCCESS; } diff --git a/example/rethrow.cpp b/example/rethrow.cpp index fc6ac84..312a14a 100644 --- a/example/rethrow.cpp +++ b/example/rethrow.cpp @@ -24,8 +24,7 @@ ctx::fcontext_t fcm = 0; ctx::fcontext_t fc = 0; boost::exception_ptr except; -void f( intptr_t arg) -{ +void f( void * arg) { std::cout << "calling caller..." << std::endl; ctx::jump_fcontext( & fc, fcm, arg); try { @@ -43,20 +42,17 @@ void f( intptr_t arg) std::cout << "exiting mycoro..." << std::endl; } -int main( int argc, char * argv[]) -{ +int main( int argc, char * argv[]) { stack_allocator alloc; - void * base = alloc.allocate( stack_allocator::default_stacksize()); fc = ctx::make_fcontext( base, stack_allocator::default_stacksize(), f); - - ctx::jump_fcontext( & fcm, fc, ( intptr_t) 0); + ctx::jump_fcontext( & fcm, fc, 0); try { try { throw std::runtime_error("main exception"); } catch( std::exception const& e) { std::cout << "calling callee in the catch block..." << std::endl; - ctx::jump_fcontext( & fcm, fc, ( intptr_t) 0); + ctx::jump_fcontext( & fcm, fc, 0); std::cout << "rethrowing main exception..." << std::endl; throw; } @@ -64,8 +60,8 @@ int main( int argc, char * argv[]) std::cout << "main caught: " << e.what() << std::endl; } std::cout << "calling callee one last time..." << std::endl; - ctx::jump_fcontext( & fcm, fc, ( intptr_t) 0); + ctx::jump_fcontext( & fcm, fc, 0); std::cout << "exiting main..." << std::endl; - - return EXIT_SUCCESS; + return E + XIT_SUCCESS; } diff --git a/example/stacked.cpp b/example/stacked.cpp index efe2e07..4bde06f 100644 --- a/example/stacked.cpp +++ b/example/stacked.cpp @@ -25,34 +25,26 @@ ctx::fcontext_t fcm = 0; ctx::fcontext_t fc1 = 0; ctx::fcontext_t fc2 = 0; -void f2( intptr_t) -{ +void f2( void *) { std::cout << "f2: entered" << std::endl; ctx::jump_fcontext( & fc2, fc1, 0); } -void f1( intptr_t) -{ +void f1( void *) { std::cout << "f1: entered" << std::endl; - stack_allocator alloc; void * sp = alloc.allocate( stack_allocator::default_stacksize()); fc2 = ctx::make_fcontext( sp, stack_allocator::default_stacksize(), f2); ctx::jump_fcontext( & fc1, fc2, 0); - ctx::jump_fcontext( & fc1, fcm, 0); } -int main( int argc, char * argv[]) -{ +int main( int argc, char * argv[]) { stack_allocator alloc; void * sp = alloc.allocate( stack_allocator::default_stacksize()); fc1 = ctx::make_fcontext( sp, stack_allocator::default_stacksize(), f1); - std::cout << "main: call start_fcontext( & fcm, fc1, 0)" << std::endl; ctx::jump_fcontext( & fcm, fc1, 0); - std::cout << "main: done" << std::endl; - return EXIT_SUCCESS; } diff --git a/example/transfer.cpp b/example/transfer.cpp index b9fb448..8bbb4b3 100644 --- a/example/transfer.cpp +++ b/example/transfer.cpp @@ -26,31 +26,24 @@ ctx::fcontext_t fc1 = 0; typedef std::pair< int, int > pair_t; -void f1( intptr_t param) -{ +void f1( void * param) { pair_t * p = ( pair_t *) param; - - p = ( pair_t *) ctx::jump_fcontext( & fc1, fcm, ( intptr_t) ( p->first + p->second) ); - - ctx::jump_fcontext( & fc1, fcm, ( intptr_t) ( p->first + p->second) ); + int res = p->first + p->second; + p = ( pair_t *) ctx::jump_fcontext( & fc1, fcm, & res); + res = p->first + p->second; + ctx::jump_fcontext( & fc1, fcm, & res); } -int main( int argc, char * argv[]) -{ +int main( int argc, char * argv[]) { stack_allocator alloc; - void * sp = alloc.allocate( stack_allocator::default_stacksize() ); fc1 = ctx::make_fcontext( sp, stack_allocator::default_stacksize(), f1); - pair_t p( std::make_pair( 2, 7) ); - int res = ( int) ctx::jump_fcontext( & fcm, fc1, ( intptr_t) & p); + int res = * ( int *) ctx::jump_fcontext( & fcm, fc1, & p); std::cout << p.first << " + " << p.second << " == " << res << std::endl; - p = std::make_pair( 5, 6); - res = ( int) ctx::jump_fcontext( & fcm, fc1, ( intptr_t) & p); + res = * ( int *) ctx::jump_fcontext( & fcm, fc1, & p); std::cout << p.first << " + " << p.second << " == " << res << std::endl; - std::cout << "main: done" << std::endl; - return EXIT_SUCCESS; } diff --git a/include/boost/context/execution_context.ipp b/include/boost/context/execution_context.ipp index ec54432..1f8f603 100644 --- a/include/boost/context/execution_context.ipp +++ b/include/boost/context/execution_context.ipp @@ -53,17 +53,12 @@ namespace detail { struct activation_record { typedef boost::intrusive_ptr< activation_record > ptr_t; - enum flag_t { - flag_main_ctx = 1 << 1, - flag_preserve_fpu = 1 << 2 - }; - thread_local static ptr_t current_rec; std::atomic< std::size_t > use_count; fcontext_t fctx; stack_context sctx; - int flags; + bool main_ctx; // used for toplevel-context // (e.g. main context, thread-entry context) @@ -71,42 +66,32 @@ struct activation_record { use_count( 0), fctx( nullptr), sctx(), - flags( flag_main_ctx) { + main_ctx( true) { } activation_record( fcontext_t fctx_, stack_context sctx_) noexcept : use_count( 0), fctx( fctx_), sctx( sctx_), - flags( 0) { + main_ctx( false) { } virtual ~activation_record() noexcept = default; - void * resume( void * vp, bool fpu) noexcept { + void * resume( void * vp) noexcept { // store current activation record in local variable activation_record * from = current_rec.get(); // store `this` in static, thread local pointer // `this` will become the active (running) context // returned by execution_context::current() current_rec = this; - // set FPU flag - if (fpu) { - from->flags |= flag_preserve_fpu; - this->flags |= flag_preserve_fpu; - } else { - from->flags &= ~flag_preserve_fpu; - this->flags &= ~flag_preserve_fpu; - } # if defined(BOOST_USE_SEGMENTED_STACKS) // adjust segmented stack properties __splitstack_getcontext( from->sctx.segments_ctx); __splitstack_setcontext( sctx.segments_ctx); # endif // context switch from parent context to `this`-context - intptr_t ret = jump_fcontext( & from->fctx, fctx, reinterpret_cast< intptr_t >( vp), fpu); - // parent context resumed - return reinterpret_cast< void * >( ret); + return jump_fcontext( & from->fctx, fctx, vp); } virtual void deallocate() { @@ -134,9 +119,9 @@ struct activation_record_initializer { template< typename Fn, typename Tpl, typename StackAlloc > class capture_record : public activation_record { private: - StackAlloc salloc_; - Fn fn_; - Tpl tpl_; + StackAlloc salloc_; + Fn fn_; + Tpl tpl_; activation_record * caller_; static void destroy( capture_record * p) { @@ -167,12 +152,12 @@ public: void run() noexcept { try { - void * vp = caller_->resume( caller_, true); + void * vp = caller_->resume( caller_); do_invoke( fn_, std::tuple_cat( tpl_, std::tie( vp) ) ); } catch (...) { std::terminate(); } - BOOST_ASSERT( 0 == (flags & flag_main_ctx) ); + BOOST_ASSERT( ! main_ctx); } }; @@ -194,10 +179,8 @@ private: // entered if the execution context // is resumed for the first time template< typename AR > - static void entry_func( intptr_t p) noexcept { - BOOST_ASSERT( 0 != p); - - AR * ar( reinterpret_cast< AR * >( p) ); + static void entry_func( void * vp) noexcept { + AR * ar( reinterpret_cast< AR * >( vp) ); BOOST_ASSERT( nullptr != ar); // start execution of toplevel context-function @@ -292,7 +275,7 @@ public: ptr_( create_context( segmented_stack(), std::forward< Fn >( fn), std::make_tuple( std::forward< Args >( args) ...) ) ) { - ptr_->resume( ptr_.get(), true); + ptr_->resume( ptr_.get() ); } template< typename Fn, typename ... Args > @@ -305,7 +288,7 @@ public: ptr_( create_context( salloc, std::forward< Fn >( fn), std::make_tuple( std::forward< Args >( args) ...) ) ) { - ptr_->resume( ptr_.get(), true); + ptr_->resume( ptr_.get() ); } template< typename Fn, typename ... Args > @@ -318,7 +301,7 @@ public: ptr_( create_context( palloc, salloc, std::forward< Fn >( fn), std::make_tuple( std::forward< Args >( args) ...) ) ) { - ptr_->resume( ptr_.get(), true); + ptr_->resume( ptr_.get() ); } # else template< typename Fn, typename ... Args > @@ -331,7 +314,7 @@ public: ptr_( create_context( fixedsize_stack(), std::forward< Fn >( fn), std::make_tuple( std::forward< Args >( args) ...) ) ) { - ptr_->resume( ptr_.get(), true); + ptr_->resume( ptr_.get() ); } template< typename StackAlloc, typename Fn, typename ... Args > @@ -344,7 +327,7 @@ public: ptr_( create_context( salloc, std::forward< Fn >( fn), std::make_tuple( std::forward< Args >( args) ...) ) ) { - ptr_->resume( ptr_.get(), true); + ptr_->resume( ptr_.get() ); } template< typename StackAlloc, typename Fn, typename ... Args > @@ -357,7 +340,7 @@ public: ptr_( create_context( palloc, salloc, std::forward< Fn >( fn), std::make_tuple( std::forward< Args >( args) ...) ) ) { - ptr_->resume( ptr_.get(), true); + ptr_->resume( ptr_.get() ); } # endif @@ -385,8 +368,8 @@ public: return * this; } - void * operator()( void * vp = nullptr, bool preserve_fpu = false) noexcept { - return ptr_->resume( vp, preserve_fpu); + void * operator()( void * vp = nullptr) noexcept { + return ptr_->resume( vp); } explicit operator bool() const noexcept { diff --git a/include/boost/context/fcontext.hpp b/include/boost/context/fcontext.hpp index fa457c6..a0b6057 100644 --- a/include/boost/context/fcontext.hpp +++ b/include/boost/context/fcontext.hpp @@ -7,14 +7,6 @@ #ifndef BOOST_CONTEXT_FCONTEXT_H #define BOOST_CONTEXT_FCONTEXT_H -#if defined(__PGI) -#include -#endif - -#if defined(_WIN32_WCE) -typedef int intptr_t; -#endif - #include #include @@ -30,9 +22,9 @@ namespace context { typedef void* fcontext_t; extern "C" BOOST_CONTEXT_DECL -intptr_t BOOST_CONTEXT_CALLDECL jump_fcontext( fcontext_t * ofc, fcontext_t nfc, intptr_t vp); +void * BOOST_CONTEXT_CALLDECL jump_fcontext( fcontext_t * from, const fcontext_t to, void * vp); extern "C" BOOST_CONTEXT_DECL -fcontext_t BOOST_CONTEXT_CALLDECL make_fcontext( void * sp, std::size_t size, void (* fn)( intptr_t) ); +fcontext_t BOOST_CONTEXT_CALLDECL make_fcontext( void * sp, std::size_t size, void (* fn)( void *) ); }} diff --git a/performance/fcontext/performance_fcontext.cpp b/performance/fcontext/performance_fcontext.cpp index d92c538..b37230c 100644 --- a/performance/fcontext/performance_fcontext.cpp +++ b/performance/fcontext/performance_fcontext.cpp @@ -29,12 +29,12 @@ struct transfer_t { boost::context::fcontext_t fctx; }; -static void foo( intptr_t data) { - transfer_t * t_ = reinterpret_cast< transfer_t * >( data); +static void foo( void * data) { + transfer_t * t_ = static_cast< transfer_t * >( data); while ( true) { transfer_t t = { 0, 0 }; - t_ = reinterpret_cast< transfer_t * >( - boost::context::jump_fcontext( & t.fctx, t_->fctx, reinterpret_cast< intptr_t >( & t) ) ); + t_ = static_cast< transfer_t * >( + boost::context::jump_fcontext( & t.fctx, t_->fctx, & t) ); } } @@ -44,23 +44,20 @@ duration_type measure_time_fc() { stack_alloc.allocate( stack_allocator::default_stacksize() ), stack_allocator::default_stacksize(), foo); - transfer_t t = { 0, 0 }; // cache warum-up - transfer_t * t_ = reinterpret_cast< transfer_t * >( - boost::context::jump_fcontext( & t.fctx, fctx, reinterpret_cast< intptr_t >( & t) ) ); - + transfer_t * t_ = static_cast< transfer_t * >( + boost::context::jump_fcontext( & t.fctx, fctx, & t) ); time_point_type start( clock_type::now() ); for ( std::size_t i = 0; i < jobs; ++i) { transfer_t t = { 0, 0 }; - t_ = reinterpret_cast< transfer_t * >( - boost::context::jump_fcontext( & t.fctx, t_->fctx, reinterpret_cast< intptr_t >( & t) ) ); + t_ = static_cast< transfer_t * >( + boost::context::jump_fcontext( & t.fctx, t_->fctx, & t) ); } duration_type total = clock_type::now() - start; total -= overhead_clock(); // overhead of measurement total /= jobs; // loops total /= 2; // 2x jump_fcontext - return total; } @@ -71,17 +68,15 @@ cycle_type measure_cycles_fc() { stack_alloc.allocate( stack_allocator::default_stacksize() ), stack_allocator::default_stacksize(), foo); - transfer_t t = { 0, 0 }; // cache warum-up - transfer_t * t_ = reinterpret_cast< transfer_t * >( - boost::context::jump_fcontext( & t.fctx, fctx, reinterpret_cast< intptr_t >( & t) ) ); - + transfer_t * t_ = static_cast< transfer_t * >( + boost::context::jump_fcontext( & t.fctx, fctx, & t) ); cycle_type start( cycles() ); for ( std::size_t i = 0; i < jobs; ++i) { transfer_t t = { 0, 0 }; - t_ = reinterpret_cast< transfer_t * >( - boost::context::jump_fcontext( & t.fctx, t_->fctx, reinterpret_cast< intptr_t >( & t) ) ); + t_ = static_cast< transfer_t * >( + boost::context::jump_fcontext( & t.fctx, t_->fctx, & t) ); } cycle_type total = cycles() - start; total -= overhead_cycle(); // overhead of measurement @@ -92,17 +87,13 @@ cycle_type measure_cycles_fc() { } #endif -int main( int argc, char * argv[]) -{ - try - { +int main( int argc, char * argv[]) { + try { bind_to_processor( 0); - boost::program_options::options_description desc("allowed options"); desc.add_options() ("help", "help message") ("jobs,j", boost::program_options::value< boost::uint64_t >( & jobs), "jobs to run"); - boost::program_options::variables_map vm; boost::program_options::store( boost::program_options::parse_command_line( @@ -111,24 +102,21 @@ int main( int argc, char * argv[]) desc), vm); boost::program_options::notify( vm); - if ( vm.count("help") ) { std::cout << desc << std::endl; return EXIT_SUCCESS; } - boost::uint64_t res = measure_time_fc().count(); std::cout << "fcontext_t: average of " << res << " nano seconds" << std::endl; #ifdef BOOST_CONTEXT_CYCLE res = measure_cycles_fc(); std::cout << "fcontext_t: average of " << res << " cpu cycles" << std::endl; #endif - return EXIT_SUCCESS; + } catch ( std::exception const& e) { + std::cerr << "exception: " << e.what() << std::endl; + } catch (...) { + std::cerr << "unhandled exception" << std::endl; } - catch ( std::exception const& e) - { std::cerr << "exception: " << e.what() << std::endl; } - catch (...) - { std::cerr << "unhandled exception" << std::endl; } return EXIT_FAILURE; } diff --git a/test/test_context.cpp b/test/test_context.cpp index 95b2350..96f0c6c 100644 --- a/test/test_context.cpp +++ b/test/test_context.cpp @@ -36,67 +36,59 @@ int value1 = 0; std::string value2; double value3 = 0.; -void f1( intptr_t) -{ +void f1( void *) { ++value1; ctx::jump_fcontext( & fc, fcm, 0); } -void f3( intptr_t) -{ +void f3( void *) { ++value1; ctx::jump_fcontext( & fc, fcm, 0); ++value1; ctx::jump_fcontext( & fc, fcm, 0); } -void f4( intptr_t) -{ - ctx::jump_fcontext( & fc, fcm, 7); +void f4( void *) { + int i = 7; + ctx::jump_fcontext( & fc, fcm, & i); } -void f5( intptr_t arg) -{ +void f5( void * arg) { ctx::jump_fcontext( & fc, fcm, arg); } -void f6( intptr_t arg) -{ +void f6( void * arg) { std::pair< int, int > data = * ( std::pair< int, int > * ) arg; int res = data.first + data.second; data = * ( std::pair< int, int > *) - ctx::jump_fcontext( & fc, fcm, ( intptr_t) res); + ctx::jump_fcontext( & fc, fcm, & res); res = data.first + data.second; - ctx::jump_fcontext( & fc, fcm, ( intptr_t) res); + ctx::jump_fcontext( & fc, fcm, & res); } -void f7( intptr_t arg) -{ - try - { throw std::runtime_error( ( char *) arg); } - catch ( std::runtime_error const& e) - { value2 = e.what(); } +void f7( void * arg) { + try { + throw std::runtime_error( ( char *) arg); + } catch ( std::runtime_error const& e) { + value2 = e.what(); + } ctx::jump_fcontext( & fc, fcm, arg); } -void f8( intptr_t arg) -{ +void f8( void * arg) { double d = * ( double *) arg; d += 3.45; value3 = d; ctx::jump_fcontext( & fc, fcm, 0); } -void f10( intptr_t) -{ +void f10( void *) { value1 = 3; ctx::jump_fcontext( & fc2, fc1, 0); } -void f9( intptr_t) -{ +void f9( void *) { std::cout << "f1: entered" << std::endl; - stack_allocator alloc; void * sp = alloc.allocate( stack_allocator::default_stacksize()); fc2 = ctx::make_fcontext( sp, stack_allocator::default_stacksize(), f10); @@ -104,40 +96,30 @@ void f9( intptr_t) ctx::jump_fcontext( & fc1, fcm, 0); } -void test_setup() -{ +void test_setup() { stack_allocator alloc; - void * sp = alloc.allocate( stack_allocator::minimum_stacksize() ); fc = ctx::make_fcontext( sp, stack_allocator::minimum_stacksize(), f1); BOOST_CHECK( fc); } -void test_start() -{ +void test_start() { value1 = 0; - stack_allocator alloc; - void * sp = alloc.allocate( stack_allocator::minimum_stacksize() ); fc = ctx::make_fcontext( sp, stack_allocator::minimum_stacksize(), f1); BOOST_CHECK( fc); - BOOST_CHECK_EQUAL( 0, value1); ctx::jump_fcontext( & fcm, fc, 0); BOOST_CHECK_EQUAL( 1, value1); } -void test_jump() -{ +void test_jump() { value1 = 0; - stack_allocator alloc; - void * sp = alloc.allocate( stack_allocator::minimum_stacksize() ); fc = ctx::make_fcontext( sp, stack_allocator::minimum_stacksize(), f3); BOOST_CHECK( fc); - BOOST_CHECK_EQUAL( 0, value1); ctx::jump_fcontext( & fcm, fc, 0); BOOST_CHECK_EQUAL( 1, value1); @@ -145,75 +127,59 @@ void test_jump() BOOST_CHECK_EQUAL( 2, value1); } -void test_result() -{ +void test_result() { stack_allocator alloc; - void * sp = alloc.allocate( stack_allocator::minimum_stacksize() ); fc = ctx::make_fcontext( sp, stack_allocator::minimum_stacksize(), f4); BOOST_CHECK( fc); - - int result = ( int) ctx::jump_fcontext( & fcm, fc, 0); + int result = * ( int *) ctx::jump_fcontext( & fcm, fc, 0); BOOST_CHECK_EQUAL( 7, result); } -void test_arg() -{ +void test_arg() { stack_allocator alloc; - int i = 7; void * sp = alloc.allocate( stack_allocator::minimum_stacksize() ); fc = ctx::make_fcontext( sp, stack_allocator::minimum_stacksize(), f5); BOOST_CHECK( fc); - - int result = ( int) ctx::jump_fcontext( & fcm, fc, i); + int result = * ( int *) ctx::jump_fcontext( & fcm, fc, & i); BOOST_CHECK_EQUAL( i, result); } -void test_transfer() -{ +void test_transfer() { stack_allocator alloc; - std::pair< int, int > data = std::make_pair( 3, 7); void * sp = alloc.allocate( stack_allocator::minimum_stacksize() ); fc = ctx::make_fcontext( sp, stack_allocator::minimum_stacksize(), f6); BOOST_CHECK( fc); - - int result = ( int) ctx::jump_fcontext( & fcm, fc, ( intptr_t) & data); + int result = * ( int *) ctx::jump_fcontext( & fcm, fc, & data); BOOST_CHECK_EQUAL( 10, result); data = std::make_pair( 7, 7); - result = ( int) ctx::jump_fcontext( & fcm, fc, ( intptr_t) & data); + result = * ( int *) ctx::jump_fcontext( & fcm, fc, & data); BOOST_CHECK_EQUAL( 14, result); } -void test_exception() -{ +void test_exception() { stack_allocator alloc; - const char * what = "hello world"; void * sp = alloc.allocate( stack_allocator::default_stacksize() ); fc = ctx::make_fcontext( sp, stack_allocator::minimum_stacksize(), f7); BOOST_CHECK( fc); - - ctx::jump_fcontext( & fcm, fc, ( intptr_t) what); + ctx::jump_fcontext( & fcm, fc, ( void *) what); BOOST_CHECK_EQUAL( std::string( what), value2); } -void test_fp() -{ +void test_fp() { stack_allocator alloc; - double d = 7.13; void * sp = alloc.allocate( stack_allocator::minimum_stacksize() ); fc = ctx::make_fcontext( sp, stack_allocator::minimum_stacksize(), f8); BOOST_CHECK( fc); - - ctx::jump_fcontext( & fcm, fc, (intptr_t) & d); + ctx::jump_fcontext( & fcm, fc, & d); BOOST_CHECK_EQUAL( 10.58, value3); } -void test_stacked() -{ +void test_stacked() { value1 = 0; stack_allocator alloc; void * sp = alloc.allocate( stack_allocator::default_stacksize()); @@ -222,11 +188,9 @@ void test_stacked() BOOST_CHECK_EQUAL( 3, value1); } -boost::unit_test::test_suite * init_unit_test_suite( int, char* []) -{ +boost::unit_test::test_suite * init_unit_test_suite( int, char* []) { boost::unit_test::test_suite * test = BOOST_TEST_SUITE("Boost.Context: context test suite"); - test->add( BOOST_TEST_CASE( & test_setup) ); test->add( BOOST_TEST_CASE( & test_start) ); test->add( BOOST_TEST_CASE( & test_jump) ); @@ -236,6 +200,5 @@ boost::unit_test::test_suite * init_unit_test_suite( int, char* []) test->add( BOOST_TEST_CASE( & test_exception) ); test->add( BOOST_TEST_CASE( & test_fp) ); test->add( BOOST_TEST_CASE( & test_stacked) ); - return test; }