Skip to content

Commit

Permalink
try shared_ptr + detach
Browse files Browse the repository at this point in the history
  • Loading branch information
benoitdm-oslandia committed Dec 2, 2024
1 parent 2ccbdb8 commit 1ec44a0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
23 changes: 13 additions & 10 deletions src/server/qgsfcgiserverresponse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ QgsSocketMonitoringThread::QgsSocketMonitoringThread( std::shared_ptr<QgsFeedbac
: mFeedback( feedback )
, mIpcFd( -1 )
{
setObjectName( "FCGI socket monitor" );
Q_ASSERT( mFeedback );

#if defined(Q_OS_UNIX) && !defined(Q_OS_ANDROID)
Expand All @@ -82,10 +81,6 @@ QgsSocketMonitoringThread::QgsSocketMonitoringThread( std::shared_ptr<QgsFeedbac
QStringLiteral( "FCGIServer" ),
Qgis::MessageLevel::Warning );
}

// Suicide the thread when it ends
connect( this, &QThread::finished, this, &QThread::deleteLater );

#endif
}

Expand All @@ -94,6 +89,11 @@ void QgsSocketMonitoringThread::setResponseFinished( bool responseFinished )
mIsResponseFinished = responseFinished;
}

void QgsSocketMonitoringThread::setShared( std::shared_ptr<QgsSocketMonitoringThread> ptr )
{
mySelf = ptr;
}

void QgsSocketMonitoringThread::run( )
{
if ( mIpcFd < 0 )
Expand Down Expand Up @@ -134,6 +134,7 @@ void QgsSocketMonitoringThread::run( )
QgsDebugMsgLevel( QStringLiteral( "FCGIServer: socket monitoring quits: no more socket." ), 2 );
}
#endif
mySelf = nullptr;
}


Expand All @@ -149,16 +150,18 @@ QgsFcgiServerResponse::QgsFcgiServerResponse( QgsServerRequest::Method method )
setDefaultHeaders();

// This is not a unique_ptr because we want the response to not depend on the thread lifecycle.
mSocketMonitoringThread = new QgsSocketMonitoringThread( mFeedback );
mSocketMonitoringThread->start();
mSocketMonitoringThread = std::make_shared<QgsSocketMonitoringThread>( mFeedback );
mSocketMonitoringThread->setShared( mSocketMonitoringThread );
std::thread mThread = std::thread( &QgsSocketMonitoringThread::run, mSocketMonitoringThread );
mThread.detach();
}

QgsFcgiServerResponse::~QgsFcgiServerResponse()
{
mFinished = true;
if ( mSocketMonitoringThread )
// This will allow the thread to finish sleeping and exit its while loop in the background, without us needing to wait for it to finish.
mSocketMonitoringThread->setResponseFinished( mFinished );
// if ( mSocketMonitoringThread )
// This will allow the thread to finish sleeping and exit its while loop in the background, without us needing to wait for it to finish.
mSocketMonitoringThread->setResponseFinished( mFinished );
QgsDebugMsgLevel( QStringLiteral( "FCGIServer: response quits." ), 2 );
}

Expand Down
8 changes: 4 additions & 4 deletions src/server/qgsfcgiserverresponse.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,8 @@
* \brief Thread used to monitor the fcgi socket
* \since QGIS 3.36
*/
class QgsSocketMonitoringThread: public QThread
class QgsSocketMonitoringThread
{
Q_OBJECT

public:

/**
Expand All @@ -50,11 +48,13 @@ class QgsSocketMonitoringThread: public QThread
void run( );

void setResponseFinished( bool responseFinished );
void setShared( std::shared_ptr<QgsSocketMonitoringThread> ptr );

private:
bool mIsResponseFinished = false;
std::shared_ptr<QgsFeedback> mFeedback;
int mIpcFd = -1;
std::shared_ptr<QgsSocketMonitoringThread> mySelf;
};

/**
Expand Down Expand Up @@ -123,7 +123,7 @@ class SERVER_EXPORT QgsFcgiServerResponse: public QgsServerResponse
QgsServerRequest::Method mMethod;
int mStatusCode = 0;

QPointer<QgsSocketMonitoringThread> mSocketMonitoringThread;
std::shared_ptr<QgsSocketMonitoringThread> mSocketMonitoringThread;
std::shared_ptr<QgsFeedback> mFeedback;
};

Expand Down

0 comments on commit 1ec44a0

Please sign in to comment.