| 1 | #include <android/log.h>
|
|---|
| 2 | #include <boost/context/all.hpp>
|
|---|
| 3 | #include <boost/context/execution_context_v1.hpp>
|
|---|
| 4 |
|
|---|
| 5 | #define TRACE(...) __android_log_print(ANDROID_LOG_DEBUG, "boostTest", __VA_ARGS__)
|
|---|
| 6 | #define ENSURE(x) if ( !(x) ) ensureFailed()
|
|---|
| 7 |
|
|---|
| 8 | void ensureFailed()
|
|---|
| 9 | {
|
|---|
| 10 | TRACE("%s %d %s",__PRETTY_FUNCTION__, __LINE__,"ENSURE HAS FAILED");
|
|---|
| 11 | *(int*)0=12345;
|
|---|
| 12 | }
|
|---|
| 13 |
|
|---|
| 14 | void checksprintfResult(int res, int size)
|
|---|
| 15 | {
|
|---|
| 16 | if ( !(res>=0 && res<size) ) {
|
|---|
| 17 | ENSURE(false);
|
|---|
| 18 | }
|
|---|
| 19 | }
|
|---|
| 20 |
|
|---|
| 21 | std::string formatBySnprintf(uint64_t src)
|
|---|
| 22 | {
|
|---|
| 23 | const bool hexFormat = true;
|
|---|
| 24 | const char* Format = hexFormat ? "0x%016llX" : "%llu";
|
|---|
| 25 | const int size = 100;
|
|---|
| 26 | char str[size];
|
|---|
| 27 | int res = snprintf ( str, size, Format, src );
|
|---|
| 28 | checksprintfResult(res,size);
|
|---|
| 29 | std::string result(str,res);
|
|---|
| 30 | TRACE("result %d %s",result.size(), result.c_str());
|
|---|
| 31 | return result;
|
|---|
| 32 | }
|
|---|
| 33 |
|
|---|
| 34 | void callsnprintf()
|
|---|
| 35 | {
|
|---|
| 36 | TRACE("%s %d",__PRETTY_FUNCTION__, __LINE__);
|
|---|
| 37 | ENSURE(formatBySnprintf(0xbcdef1234567890)=="0x0BCDEF1234567890");
|
|---|
| 38 | TRACE("%s %d",__PRETTY_FUNCTION__, __LINE__);
|
|---|
| 39 | }
|
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 | void FiberProc2(void* arg)
|
|---|
| 43 | {
|
|---|
| 44 | TRACE("%s %d",__PRETTY_FUNCTION__, __LINE__);
|
|---|
| 45 | boost::context::execution_context& mainCtx = *((boost::context::execution_context*)arg);
|
|---|
| 46 | callsnprintf();
|
|---|
| 47 | TRACE("%s %d",__PRETTY_FUNCTION__, __LINE__);
|
|---|
| 48 | mainCtx();
|
|---|
| 49 | TRACE("%s %d must be unreachable!!!",__PRETTY_FUNCTION__, __LINE__);
|
|---|
| 50 | ENSURE(false); // not reachable
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 | void showBuildType()
|
|---|
| 54 | {
|
|---|
| 55 | #ifdef NDEBUG
|
|---|
| 56 | TRACE("%s %d release build",__PRETTY_FUNCTION__, __LINE__);
|
|---|
| 57 | #else
|
|---|
| 58 | TRACE("%s %d debug build",__PRETTY_FUNCTION__, __LINE__);
|
|---|
| 59 | #endif
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | void boostContextTest2()
|
|---|
| 63 | {
|
|---|
| 64 | showBuildType();
|
|---|
| 65 | TRACE("%s %d",__PRETTY_FUNCTION__, __LINE__);
|
|---|
| 66 | {
|
|---|
| 67 | TRACE("%s %d calling WITHOUT fiber",__PRETTY_FUNCTION__, __LINE__);
|
|---|
| 68 | callsnprintf();
|
|---|
| 69 | }
|
|---|
| 70 | {
|
|---|
| 71 | TRACE("%s %d calling WITH fiber",__PRETTY_FUNCTION__, __LINE__);
|
|---|
| 72 | boost::context::execution_context mainCtx(boost::context::execution_context::current());
|
|---|
| 73 | boost::context::execution_context childCtx(&FiberProc2);
|
|---|
| 74 | childCtx(&mainCtx);
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | TRACE("%s %d",__PRETTY_FUNCTION__, __LINE__);
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 |
|
|---|
| 81 | void boostContextTest()
|
|---|
| 82 | {
|
|---|
| 83 | boostContextTest2();
|
|---|
| 84 | }
|
|---|