| 1 |
|
| #include "function.h" |
| 2 |
|
| #include "catch.h" |
| 3 |
|
| |
| 4 |
|
| namespace xs { namespace func { |
| 5 |
|
| |
| 6 |
|
| Sv::payload_marker_t out_marker; |
| 7 |
|
| Sv::payload_marker_t in_marker; |
| 8 |
|
| |
| 9 |
37 |
| static bool init () { |
| 10 |
37 |
| out_marker.svt_free = [](pTHX_ SV*, MAGIC* mg) -> int { |
| 11 |
53 |
| auto fc = reinterpret_cast<IFunctionCaller*>(mg->mg_ptr); |
| 12 |
53 |
▶ | delete fc; |
Branch: 1 branch
| Branch | True | False | Total |
| delete fc; |
53 |
0 |
53 |
|
| 13 |
53 |
| return 0; |
| 14 |
|
| }; |
| 15 |
|
| |
| 16 |
|
| #ifdef USE_ITHREADS |
| 17 |
|
| out_marker.svt_dup = [](pTHX_ MAGIC* mg, CLONE_PARAMS*) -> int { |
| 18 |
|
| mg->mg_ptr = (char*)reinterpret_cast<IFunctionCaller*>(mg->mg_ptr)->clone(); |
| 19 |
|
| return 0; |
| 20 |
|
| }; |
| 21 |
|
| |
| 22 |
|
| in_marker.svt_free = [](pTHX_ SV*, MAGIC* mg) -> int { |
| 23 |
|
| //printf("SVT FREE IN CALLED %p %p\n", sv, aTHX); |
| 24 |
|
| auto pad = reinterpret_cast<SubPad*>(mg->mg_ptr); |
| 25 |
|
| pad->remove_sub(); |
| 26 |
|
| pad->release(); |
| 27 |
|
| return 0; |
| 28 |
|
| }; |
| 29 |
|
| |
| 30 |
|
| in_marker.svt_dup = [](pTHX_ MAGIC* mg, CLONE_PARAMS*) -> int { |
| 31 |
|
| //printf("SVT DUP IN CALLED %p %p\n", mg->mg_obj, aTHX); |
| 32 |
|
| auto pad = reinterpret_cast<SubPad*>(mg->mg_ptr); |
| 33 |
|
| pad->add_sub(mg->mg_obj); |
| 34 |
|
| pad->retain(); |
| 35 |
|
| return 0; |
| 36 |
|
| }; |
| 37 |
|
| #endif |
| 38 |
|
| |
| 39 |
37 |
| return true; |
| 40 |
|
| } |
| 41 |
|
| static const bool _init = init(); |
| 42 |
|
| |
| 43 |
110 |
▶ | static void XS_function_call (pTHX_ CV* cv) { xs::throw_guard(cv, [=](){ |
Branch: 1 branch
| Branch | True | False | Total |
| static void XS_function_call (pTHX_ CV* cv) { xs::throw_guard(cv, [=](){ |
53 |
0 |
53 |
|
| 44 |
55 |
| dVAR; dXSARGS; |
| 45 |
55 |
| SP -= items; |
| 46 |
55 |
▶ | Sub sub(cv); |
Branch: 1 branch
| Branch | True | False | Total |
| Sub sub(cv); |
55 |
0 |
55 |
|
| 47 |
55 |
| auto fc = reinterpret_cast<IFunctionCaller*>(sub.payload(&out_marker).ptr); |
| 48 |
55 |
▶ | if (!fc) throw "invalid function->sub subroutine"; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!fc) throw "invalid function->sub subroutine"; |
0 |
55 |
55 |
|
| 49 |
55 |
▶ | auto ret = fc->call(&ST(0), items); |
Branch: 1 branch
| Branch | True | False | Total |
| auto ret = fc->call(&ST(0), items); |
53 |
2 |
55 |
|
| 50 |
53 |
▶ | if (!ret) XSRETURN_EMPTY; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ret) XSRETURN_EMPTY; |
44 |
9 |
53 |
|
| 51 |
9 |
▶ | mXPUSHs(ret.detach()); |
Branch: 3 branches
| Branch | True | False | Total |
| mXPUSHs(ret.detach()); |
0 |
9 |
9 |
| mXPUSHs(ret.detach()); |
0 |
0 |
0 |
| mXPUSHs(ret.detach()); |
9 |
0 |
9 |
|
| 52 |
9 |
| XSRETURN(1); |
| 53 |
108 |
| }); } |
| 54 |
|
| |
| 55 |
|
| static PERL_ITHREADS_LOCAL CV* proto = (CV*)Sub::create(&XS_function_call).detach(); |
| 56 |
|
| |
| 57 |
53 |
| Sub create_sub (IFunctionCaller* fc) { |
| 58 |
53 |
▶ | auto ret = Sub::clone_anon_xsub(proto); |
Branch: 2 branches
| Branch | True | False | Total |
| auto ret = Sub::clone_anon_xsub(proto); |
53 |
0 |
53 |
| auto ret = Sub::clone_anon_xsub(proto); |
53 |
0 |
53 |
|
| 59 |
53 |
▶ | auto mg = ret.payload_attach(fc, &out_marker); |
Branch: 1 branch
| Branch | True | False | Total |
| auto mg = ret.payload_attach(fc, &out_marker); |
53 |
0 |
53 |
|
| 60 |
53 |
| mg->mg_flags |= MGf_DUP; |
| 61 |
53 |
| return ret; |
| 62 |
|
| } |
| 63 |
|
| |
| 64 |
|
| #ifdef USE_ITHREADS |
| 65 |
|
| SubPad* SubPad::get (Sub sub) { |
| 66 |
|
| auto ptr = static_cast<SubPad*>(sub.payload(&in_marker).ptr); |
| 67 |
|
| if (ptr) return ptr; |
| 68 |
|
| ptr = new SubPad(sub); |
| 69 |
|
| ptr->retain(); |
| 70 |
|
| auto mg = sub.payload_attach(ptr, &in_marker); |
| 71 |
|
| mg->mg_flags |= MGf_DUP; |
| 72 |
|
| mg->mg_obj = sub.get(); |
| 73 |
|
| return ptr; |
| 74 |
|
| } |
| 75 |
|
| |
| 76 |
|
| SubPad::SubPad (const Sub& sub) : pad({{aTHX, sub}}) {} |
| 77 |
|
| |
| 78 |
|
| const Sub& SubPad::get_sub () const { |
| 79 |
|
| std::lock_guard<std::mutex> lock(mutex); |
| 80 |
|
| dTHX; |
| 81 |
|
| for (auto& p : pad) if (p.first == aTHX) return p.second; |
| 82 |
|
| throw "calling typemap'ed perl function from wrong thread or after perl global destroy / thread destroy"; |
| 83 |
|
| } |
| 84 |
|
| |
| 85 |
|
| void SubPad::add_sub (const Sub& sub) { |
| 86 |
|
| std::lock_guard<std::mutex> lock(mutex); |
| 87 |
|
| pad.push_back({aTHX, sub}); |
| 88 |
|
| } |
| 89 |
|
| |
| 90 |
|
| void SubPad::remove_sub () { |
| 91 |
|
| std::lock_guard<std::mutex> lock(mutex); |
| 92 |
|
| dTHX; |
| 93 |
|
| for (size_t i = 0; i < pad.size(); ++i) { |
| 94 |
|
| if (pad[i].first != aTHX) continue; |
| 95 |
|
| pad.erase(pad.begin() + i); |
| 96 |
|
| return; |
| 97 |
|
| } |
| 98 |
|
| throw "panic: empty typemap'ed perl function"; |
| 99 |
|
| } |
| 100 |
|
| #endif |
| 101 |
|
| |
| 102 |
|
| }} |