Skip to content

Commit

Permalink
gapi(async): use lazy-initialization of async_service singleton
Browse files Browse the repository at this point in the history
  • Loading branch information
alalek committed Jan 15, 2021
1 parent 84676fe commit e43375c
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions modules/gapi/src/executor/gasync.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,16 @@ class async_service {

std::thread thrd;

public:
async_service() = default ;

public:
// singleton
static async_service& instance()
{
static async_service the_ctx;
return the_ctx;
}

void add_task(std::function<void()>&& t){
if (!thread_started)
{
Expand Down Expand Up @@ -87,6 +94,8 @@ class async_service {
cv.notify_one();
}
}

protected:
~async_service(){
if (thread_started && thrd.joinable())
{
Expand All @@ -99,7 +108,6 @@ class async_service {
}
};

async_service the_ctx;
}

namespace {
Expand Down Expand Up @@ -169,7 +177,7 @@ void async_apply(GComputation& gcomp, std::function<void(std::exception_ptr)>&&

call_with_callback(apply_l,std::move(callback), DummyContext{});
};
impl::the_ctx.add_task(l);
impl::async_service::instance().add_task(l);
}

std::future<void> async_apply(GComputation& gcomp, GRunArgs &&ins, GRunArgsP &&outs, GCompileArgs &&args){
Expand All @@ -183,7 +191,7 @@ std::future<void> async_apply(GComputation& gcomp, GRunArgs &&ins, GRunArgsP &&o
call_with_future(apply_l, prms.value, DummyContext{});
};

impl::the_ctx.add_task(l);
impl::async_service::instance().add_task(l);
return f;
}

Expand All @@ -196,7 +204,7 @@ void async_apply(GComputation& gcomp, std::function<void(std::exception_ptr)>&&

call_with_callback(apply_l,std::move(callback), ctx);
};
impl::the_ctx.add_task(l);
impl::async_service::instance().add_task(l);
}

std::future<void> async_apply(GComputation& gcomp, GRunArgs &&ins, GRunArgsP &&outs, GCompileArgs &&args, GAsyncContext& ctx){
Expand All @@ -210,7 +218,7 @@ std::future<void> async_apply(GComputation& gcomp, GRunArgs &&ins, GRunArgsP &&o
call_with_future(apply_l, prms.value, ctx);
};

impl::the_ctx.add_task(l);
impl::async_service::instance().add_task(l);
return f;

}
Expand All @@ -224,7 +232,7 @@ void async(GCompiled& gcmpld, std::function<void(std::exception_ptr)>&& callback
call_with_callback(apply_l,std::move(callback), DummyContext{});
};

impl::the_ctx.add_task(l);
impl::async_service::instance().add_task(l);
}

void async(GCompiled& gcmpld, std::function<void(std::exception_ptr)>&& callback, GRunArgs &&ins, GRunArgsP &&outs, GAsyncContext& ctx){
Expand All @@ -236,7 +244,7 @@ void async(GCompiled& gcmpld, std::function<void(std::exception_ptr)>&& callback
call_with_callback(apply_l,std::move(callback), ctx);
};

impl::the_ctx.add_task(l);
impl::async_service::instance().add_task(l);
}

std::future<void> async(GCompiled& gcmpld, GRunArgs &&ins, GRunArgsP &&outs){
Expand All @@ -250,7 +258,7 @@ std::future<void> async(GCompiled& gcmpld, GRunArgs &&ins, GRunArgsP &&outs){
call_with_future(apply_l, prms.value, DummyContext{});
};

impl::the_ctx.add_task(l);
impl::async_service::instance().add_task(l);
return f;

}
Expand All @@ -265,7 +273,7 @@ std::future<void> async(GCompiled& gcmpld, GRunArgs &&ins, GRunArgsP &&outs, GAs
call_with_future(apply_l, prms.value, ctx);
};

impl::the_ctx.add_task(l);
impl::async_service::instance().add_task(l);
return f;

}
Expand Down

0 comments on commit e43375c

Please sign in to comment.