Skip to content

Commit

Permalink
Sem: mt_notifyAtEnd()
Browse files Browse the repository at this point in the history
  • Loading branch information
fchn289 committed Oct 23, 2023
1 parent 89e41e7 commit 7d6b775
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
12 changes: 11 additions & 1 deletion src/thread/MT_Semaphore.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/
// ***********************************************************************************************
// - why/REQ:
// * let main thread mt_wait() other thread's input (eg from ThreadBack, MtInQueue)
// * let main thread mt_wait() other thread's input (eg from MsgSelf, ThreadBack, MtInQueue)
// * let other thread mt_notify() to wakeup main thread from mt_wait()
// . base on semaphore that is simple enough
// . std::latch/etc need c++20, too high vs semaphore (POSIX)
Expand Down Expand Up @@ -39,6 +39,16 @@ class MT_Semaphore
void mt_wait() { sem_wait(&mt_sem_); }
void mt_notify();

auto mt_notifyAtEnd(auto&& aFn)
{
return [aFn, this](auto&&... aArgs) -> decltype(aFn(aArgs...))
{
auto&& ret = aFn(forward<decltype(aArgs)>(aArgs)...);
mt_notify();
return ret;
};
}

// -------------------------------------------------------------------------------------------
private:
sem_t mt_sem_;
Expand Down
5 changes: 2 additions & 3 deletions ut/thread/MT_SemphoreTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,11 @@ TEST_F(MT_SemaphoreTest, GOLD_integrate_MsgSelf_ThreadBack_MtInQueue) // simula
// push
ThreadBack::newThread(
// entryFn
[this]
mt_waker_.mt_notifyAtEnd([this] // REQ: can notify (or rely on sem's timeout)
{
mtQ_.mt_push(make_shared<string>("a"));
mt_waker_.mt_notify(); // REQ: can notify (or rely on sem's timeout)
return true;
},
}),
// backFn
viaMsgSelf( // REQ: via MsgSelf
[this, &cb_info](bool aRet)
Expand Down

0 comments on commit 7d6b775

Please sign in to comment.