Skip to content
This repository has been archived by the owner on Aug 23, 2023. It is now read-only.

Commit

Permalink
os: windows: eliminate a deadlock
Browse files Browse the repository at this point in the history
  • Loading branch information
corwinn committed Mar 21, 2023
1 parent fba54e3 commit 94f1c67
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
28 changes: 21 additions & 7 deletions async/h3r_taskthread.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ class TaskThread final
public TaskThread() : _tproc {*this}, _thr {_tproc}, Task {*this} {}
public ~TaskThread()
{
while (! _running)
while (! _running) //DL_HUNTER printf ("<%p> ~ waiting\n", this);
;
_tproc.stop = true;
_nothing_to_do.GoGoGo ();
// printf ("<%p> GoGoGo\n", this);
//DL_HUNTER printf ("<%p> ~ GoGoGo\n", this);
_thr.Stop ();
}

Expand All @@ -92,8 +92,18 @@ class TaskThread final
____ {_p._d_lock};
return _v = v;
}
public operator bool() const { return _v; }
public bool operator !() const { return !_v; }
public operator bool() const
{
__pointless_verbosity::CriticalSection_Acquire_finally_release
____ {_p._d_lock};
return _v;
}
public bool operator !() const
{
__pointless_verbosity::CriticalSection_Acquire_finally_release
____ {_p._d_lock};
return !_v;
}
} _dirty;
// setup part
inline IAsyncTask & Do(IAsyncTask * p)
Expand Down Expand Up @@ -144,10 +154,14 @@ class TaskThread final
_running = true;
while (! _tproc.stop) {
// _nothing_to_do.Lock () is unlocked while the thread waits, only
//DL_HUNTER printf ("<%p> wait\n", this);
_nothing_to_do.Wait ();
//DL_HUNTER printf ("<%p> work\n", this);
// OS::Log_stdout ("p:%p, _tproc.dirty:%d" EOL,
// &_tproc, (_tproc._dirty ? 1 : 0));
if (_tproc._dirty) Do (); // laundry
//DL_HUNTER printf ("<%p> work done: dirty: %d\n", this,
// (bool)_tproc._dirty);
// This can't continue - I'll have to change the CC. Because when
// a rendering requests 60 distinct resources, this here introduces
// significant delay - render-wise. If I lower the poll interval it
Expand All @@ -165,14 +179,14 @@ class TaskThread final

private inline IAsyncTask & Do(IAsyncTask * it)
{
// printf ("<%p> Do(it)\n", this);
//DL_HUNTER printf ("<%p> Do(it)\n", this);
auto & result = _tproc.Do (it);
// Because the task thread might fail to actually reach the line where
// this state is set, prior the task issuer steps in here.
while (! _running)
while (! _running) //DL_HUNTER printf ("<%p> Set waiting\n", this);
;
_nothing_to_do.GoGoGo ();
// printf ("<%p> GoGoGo\n", this);
//DL_HUNTER printf ("<%p> Set GoGoGo\n", this);
return result;
}

Expand Down
Binary file added diagrams/async_whiteboard.dia
Binary file not shown.
4 changes: 4 additions & 0 deletions h3r_game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ void Game::SilentLog(bool v)
/*static*/ void Game::ProcessThings()
{
if (Game::MainWindow) Game::MainWindow->ProcessMessages ();
// A tight loop makes ResManagerInit above infinite ?!
// The pointless sync at the BoolProperty at the TaskThread resolves it,
// but I'm adding this here just in case, until I understand whats going on.
else OS::Thread::Sleep (1);//TODO understand the odd deadlock
}

/*static*/ Stream * Game::GetResource(const String & name)
Expand Down

0 comments on commit 94f1c67

Please sign in to comment.