Skip to content

Commit

Permalink
JXFunctionTask: handle possibility that task may be deleted while run…
Browse files Browse the repository at this point in the history
…ning
  • Loading branch information
jafl committed Dec 29, 2024
1 parent 89f6072 commit 5867651
Show file tree
Hide file tree
Showing 21 changed files with 99 additions and 51 deletions.
4 changes: 2 additions & 2 deletions libjfs/code/JXFSBindingManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include "JFSBinding.h" // for CommandType
#include <jx-af/jcore/JError.h>

class JXFunctionTask;
class JXIdleTask;
class JFSBindingList;
class JXFSEditBindingsDialog;
class JXFSRunCommandDialog;
Expand Down Expand Up @@ -45,7 +45,7 @@ class JXFSBindingManager : virtual public JBroadcaster
private:

JFSBindingList* itsBindingList;
JXFunctionTask* itsUpdateBindingListTask;
JXIdleTask* itsUpdateBindingListTask;

JXFSRunCommandDialog* itsRunCmdDialog;
JXFSRunFileDialog* itsRunFileDialog; // nullptr unless processing files
Expand Down
4 changes: 2 additions & 2 deletions libjx/code/JXCSFDialogBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class JXScrollbarSet;
class JXNewDirButton;
class JXCurrentPathMenu;
class JXIdleTask;
class JXUrgentFunctionTask;
class JXUrgentTask;

class JXCSFDialogBase : public JXModalDialogDirector
{
Expand Down Expand Up @@ -105,7 +105,7 @@ class JXCSFDialogBase : public JXModalDialogDirector
JXTextCheckbox* itsShowHiddenCB;
JXCurrentPathMenu* itsCurrPathMenu;

JXUrgentFunctionTask* itsSelectPrevDirTask; // nullptr unless queued
JXUrgentTask* itsSelectPrevDirTask; // nullptr unless queued

static JString theState;
static JString thePath;
Expand Down
4 changes: 2 additions & 2 deletions libjx/code/JXContainer.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class JXWindow;
class JXWidget;
class JXWindowPainter;
class JXCursorAnimator;
class JXFunctionTask;
class JXIdleTask;
class JXFontManager;
class JXSelectionManager;
class JXDNDManager;
Expand Down Expand Up @@ -265,7 +265,7 @@ class JXContainer : virtual public JBroadcaster
bool itsCursorVisibleFlag;
JCursorIndex itsCurrCursor;
JXCursorAnimator* itsCursorAnim;
JXFunctionTask* itsCursorAnimTask;
JXIdleTask* itsCursorAnimTask;

// hint

Expand Down
4 changes: 2 additions & 2 deletions libjx/code/JXDirTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class JString;
class JDirInfo;
class JDirEntry;
class JXImage;
class JXFunctionTask;
class JXIdleTask;

class JXDirTable : public JXTable
{
Expand Down Expand Up @@ -97,7 +97,7 @@ class JXDirTable : public JXTable

JDirInfo* itsDirInfo; // not owned
JArray<bool>* itsActiveCells;
JXFunctionTask* itsDirUpdateTask;
JXIdleTask* itsDirUpdateTask;

bool itsIgnoreSelChangesFlag; // true while cleaning selection
bool itsAllowSelectFilesFlag;
Expand Down
4 changes: 2 additions & 2 deletions libjx/code/JXDockWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class JXHorizDockPartition;
class JXVertDockPartition;
class JXTabGroup;
class JXHintDirector;
class JXFunctionTask;
class JXIdleTask;

class JXDockWidget : public JXWidgetSet
{
Expand Down Expand Up @@ -93,7 +93,7 @@ class JXDockWidget : public JXWidgetSet
JPoint itsMinSize;
JXPartition* itsChildPartition; // nullptr if contains docked windows
JXHintDirector* itsHintDirector; // nullptr unless showing hint; not owned
JXFunctionTask* itsDeleteHintTask; // nullptr unless showing hint
JXIdleTask* itsDeleteHintTask; // nullptr unless showing hint

private:

Expand Down
1 change: 0 additions & 1 deletion libjx/code/JXDocumentManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@
#include "JXDisplay.h"
#include "JXColorManager.h"
#include "JXChooseFileDialog.h"
#include "JXFunctionTask.h"
#include "jXGlobals.h"
#include <jx-af/jcore/JStringIterator.h>
#include <jx-af/jcore/jFileUtil.h>
Expand Down
20 changes: 10 additions & 10 deletions libjx/code/JXDocumentManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ class JXImage;
class JXDocument;
class JXFileDocument;
class JXDocumentMenu;
class JXFunctionTask;
class JXUrgentFunctionTask;
class JXIdleTask;
class JXUrgentTask;

class JXDocumentManager : virtual public JBroadcaster
{
Expand Down Expand Up @@ -123,18 +123,18 @@ class JXDocumentManager : virtual public JBroadcaster

private:

JArray<DocInfo>* itsDocList;
JIndex itsNewDocCount;
JArray<DocInfo>* itsDocList;
JIndex itsNewDocCount;

JArray<FileMap>* itsFileMap;
JArray<FileMap>* itsFileMap;

const bool itsWantShortcutFlag;
const bool itsWantShortcutFlag;

bool itsPerformSafetySaveFlag;
JXFunctionTask* itsSafetySaveTask;
bool itsPerformSafetySaveFlag;
JXIdleTask* itsSafetySaveTask;

JXUrgentFunctionTask* itsUpdateDocMenuTask; // nullptr unless pending
JXImage* itsDefaultMenuIcon; // not owned
JXUrgentTask* itsUpdateDocMenuTask; // nullptr unless pending
JXImage* itsDefaultMenuIcon; // not owned

private:

Expand Down
33 changes: 30 additions & 3 deletions libjx/code/JXFunctionTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ JXFunctionTask::JXFunctionTask
JXIdleTask(period),
itsFunction(f),
itsName(jnew JString(name)),
itsIsOneShotFlag(oneShot)
itsIsOneShotFlag(oneShot),
itsDeletedFlag(nullptr)
{
}

Expand All @@ -38,6 +39,11 @@ JXFunctionTask::JXFunctionTask

JXFunctionTask::~JXFunctionTask()
{
if (itsDeletedFlag != nullptr)
{
*itsDeletedFlag = true;
}

jdelete itsName;
}

Expand All @@ -52,9 +58,30 @@ JXFunctionTask::Perform
const Time delta
)
{
bool deleted = false;
itsDeletedFlag = &deleted;

itsFunction();
if (itsIsOneShotFlag)

if (!deleted)
{
jdelete this;
itsDeletedFlag = nullptr;

if (itsIsOneShotFlag)
{
jdelete this;
}
}
}

/******************************************************************************
ToString (virtual)
******************************************************************************/

JString
JXFunctionTask::ToString()
const
{
return JXIdleTask::ToString() + " -- " + *itsName;
}
4 changes: 4 additions & 0 deletions libjx/code/JXFunctionTask.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ class JXFunctionTask : public JXIdleTask

~JXFunctionTask() override;

JString ToString() const override;

protected:

void Perform(const Time delta) override;
Expand All @@ -31,6 +33,8 @@ class JXFunctionTask : public JXIdleTask
std::function<void()> itsFunction;
JString* itsName;
const bool itsIsOneShotFlag;

bool* itsDeletedFlag;
};

#endif
14 changes: 14 additions & 0 deletions libjx/code/JXIdleTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ JXIdleTask::JXIdleTask
itsPeriod(period),
itsElapsedTime(0)
{
assert( JXApplication::IsUIThread() );
}

/******************************************************************************
Expand All @@ -60,6 +61,7 @@ JXIdleTask::JXIdleTask

JXIdleTask::~JXIdleTask()
{
assert( JXApplication::IsUIThread() );
Stop();
}

Expand Down Expand Up @@ -112,3 +114,15 @@ JXIdleTask::Ready
}
return ready;
}

/******************************************************************************
ToString (virtual)
******************************************************************************/

JString
JXIdleTask::ToString()
const
{
return JString((JUInt64)(void*)this, JString::kBase16);
}
4 changes: 4 additions & 0 deletions libjx/code/JXIdleTask.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#include <jx-af/jcore/jTypes.h>
#include <X11/Xlib.h>

class JString;

class JXIdleTask
{
friend class JXApplication;
Expand All @@ -31,6 +33,8 @@ class JXIdleTask

void ResetTimer();

virtual JString ToString() const;

protected:

bool Ready(const Time delta, Time* maxSleepTime);
Expand Down
4 changes: 2 additions & 2 deletions libjx/code/JXImageWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#include "jx-af/jcore/JXPM.h"

class JXImage;
class JXUrgentFunctionTask;
class JXUrgentTask;

class JXImageWidget : public JXScrollableWidget
{
Expand Down Expand Up @@ -57,7 +57,7 @@ class JXImageWidget : public JXScrollableWidget
JXImage* itsImage; // can be nullptr
bool itsOwnsImageFlag;

JXUrgentFunctionTask* itsAdjustBoundsTask;
JXUrgentTask* itsAdjustBoundsTask;

private:

Expand Down
2 changes: 1 addition & 1 deletion libjx/code/JXPrefsManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class JXPrefsManager : public JPrefsManager

private:

JXFunctionTask* itsSafetySaveTask;
JXIdleTask* itsSafetySaveTask;
const JPrefID itsCSFPrefID;
};

Expand Down
4 changes: 2 additions & 2 deletions libjx/code/JXProgressDisplay.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class JXTEBase;
class JXStaticText;
class JXProgressIndicator;
class JXPGMessageDirector;
class JXFunctionTask;
class JXIdleTask;

class JXProgressDisplay : public JProgressDisplay, virtual public JBroadcaster
{
Expand Down Expand Up @@ -64,7 +64,7 @@ class JXProgressDisplay : public JProgressDisplay, virtual public JBroadcaster

JXPGMessageDirector* itsMessageDirector; // nullptr unless messages during process

JXFunctionTask* itsContinueTask;
JXIdleTask* itsContinueTask;
boost::fibers::condition_variable itsCondition;
boost::fibers::mutex itsMutex;
bool itsContinueFlag;
Expand Down
4 changes: 2 additions & 2 deletions libjx/code/JXScrollableWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

class JXScrollbar;
class JXScrollbarSet;
class JXUrgentFunctionTask;
class JXUrgentTask;

class JXScrollableWidget : public JXWidget
{
Expand Down Expand Up @@ -93,7 +93,7 @@ class JXScrollableWidget : public JXWidget
bool itsAdjustingFlag; // if should ignore scrollbar messages
bool itsShouldRedrawFlag; // if Redraw() when scrolled

JXUrgentFunctionTask* itsAdjustScrollbarTask;
JXUrgentTask* itsAdjustScrollbarTask;

// If these are positive, they override the defaults.

Expand Down
4 changes: 2 additions & 2 deletions libjx/code/JXSearchTextDialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class JXTextButton;
class JXInputField;
class JXTextCheckbox;
class JXStringHistoryMenu;
class JXFunctionTask;
class JXIdleTask;
class JXSearchTextButton;

class JXSearchTextDialog : public JXWindowDirector
Expand Down Expand Up @@ -119,7 +119,7 @@ class JXSearchTextDialog : public JXWindowDirector
JXTEBase* itsTE; // can be nullptr; not owned
JRegex* itsRegex;
JInterpolate* itsInterpolator;
JXFunctionTask* itsUpdateTask;
JXIdleTask* itsUpdateTask;

Atom itsAtoms[ kAtomCount ];

Expand Down
14 changes: 7 additions & 7 deletions libjx/code/JXSharedPrefsManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include <jx-af/jcore/jTime.h>

class JPrefsFile;
class JXFunctionTask;
class JXIdleTask;
class JXSharedPrefObject;

class JXSharedPrefsManager : virtual public JBroadcaster
Expand Down Expand Up @@ -57,12 +57,12 @@ class JXSharedPrefsManager : virtual public JBroadcaster

private:

JPrefsFile* itsFile; // nullptr unless working
bool itsChangedFlag; // used when re-entrant
JXFunctionTask* itsUpdateTask;
JString itsSignalFileName; // can be empty
time_t itsSignalModTime; // last time signal file was modified
bool itsWasNewFlag; // true => we created the file
JPrefsFile* itsFile; // nullptr unless working
bool itsChangedFlag; // used when re-entrant
JXIdleTask* itsUpdateTask;
JString itsSignalFileName; // can be empty
time_t itsSignalModTime; // last time signal file was modified
bool itsWasNewFlag; // true => we created the file

// this section exists because JCore values cannot update themselves

Expand Down
4 changes: 2 additions & 2 deletions libjx/code/JXToolBar.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class JNamedTreeNode;
class JXMenu;
class JXMenuBar;
class JXTextMenu;
class JXFunctionTask;
class JXIdleTask;
class JXUrgentFunctionTask;

class JXToolBar : public JXWidgetSet, public JPrefObject
Expand Down Expand Up @@ -76,7 +76,7 @@ class JXToolBar : public JXWidgetSet, public JPrefObject
bool itsInNewGroupMode;
JPtrArray<JXToolBarButton>* itsButtons;
JPtrArray<JXMenu>* itsMenus;
JXFunctionTask* itsTimerTask;
JXIdleTask* itsTimerTask;
JXMenuBar* itsMenuBar;
JSize itsCurrentButtonHeight;
JXWidgetSet* itsToolBarSet;
Expand Down
Loading

0 comments on commit 5867651

Please sign in to comment.