1
2 // Copyright Oliver Kowalke 2009.
3 // Distributed under the Boost Software License, Version 1.0.
4 // (See accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
6
7 #include <cstdlib>
8 #include <iostream>
9
10 #include <boost/context/all.hpp>
11 #include <boost/exception_ptr.hpp>
12
13 #include "simple_stack_allocator.hpp"
14
15 namespace ctx = boost::context;
16
17 typedef ctx::simple_stack_allocator<
18 8 * 1024 * 1024, // 8MB
19 64 * 1024, // 64kB
20 8 * 1024 // 8kB
21 > stack_allocator;
22
23 ctx::fcontext_t fcm = 0;
24 ctx::fcontext_t fc = 0;
25 boost::exception_ptr except;
26
f(intptr_t arg)27 void f( intptr_t arg)
28 {
29 std::cout << "calling caller..." << std::endl;
30 ctx::jump_fcontext( & fc, fcm, arg);
31 try {
32 try {
33 throw std::runtime_error("mycoro exception");
34 } catch(const std::exception& e) {
35 std::cout << "calling caller in the catch block..." << std::endl;
36 ctx::jump_fcontext( & fc, fcm, arg);
37 std::cout << "rethrowing mycoro exception..." << std::endl;
38 throw;
39 }
40 } catch (const std::exception& e) {
41 std::cout << "mycoro caught: " << e.what() << std::endl;
42 }
43 std::cout << "exiting mycoro..." << std::endl;
44 }
45
main(int argc,char * argv[])46 int main( int argc, char * argv[])
47 {
48 stack_allocator alloc;
49
50 void * base = alloc.allocate( stack_allocator::default_stacksize());
51 fc = ctx::make_fcontext( base, stack_allocator::default_stacksize(), f);
52
53 ctx::jump_fcontext( & fcm, fc, ( intptr_t) 0);
54 try {
55 try {
56 throw std::runtime_error("main exception");
57 } catch( std::exception const& e) {
58 std::cout << "calling callee in the catch block..." << std::endl;
59 ctx::jump_fcontext( & fcm, fc, ( intptr_t) 0);
60 std::cout << "rethrowing main exception..." << std::endl;
61 throw;
62 }
63 } catch ( std::exception const& e) {
64 std::cout << "main caught: " << e.what() << std::endl;
65 }
66 std::cout << "calling callee one last time..." << std::endl;
67 ctx::jump_fcontext( & fcm, fc, ( intptr_t) 0);
68 std::cout << "exiting main..." << std::endl;
69
70 return EXIT_SUCCESS;
71 }
72