Bugfix of Exception Handling

Signed-off-by: tomoaki <tomoaki@tomy-tech.com>
This commit is contained in:
tomoaki
2021-05-03 19:20:52 +09:00
parent 8a2d28a5e4
commit 4478eafc8d
11 changed files with 43 additions and 30 deletions

View File

@@ -30,7 +30,7 @@ char* currentDateTime(void);
BrokerRecvTask::BrokerRecvTask(Gateway* gateway)
{
_gateway = gateway;
_gateway->attach((Thread*) this);
Runnable::threadNo =_gateway->attach((Thread*) this);
_light = nullptr;
setTaskName("BrokerRecvTask");
}

View File

@@ -34,7 +34,7 @@ char* currentDateTime();
BrokerSendTask::BrokerSendTask(Gateway* gateway)
{
_gateway = gateway;
_gateway->attach((Thread*) this);
Runnable::threadNo =_gateway->attach((Thread*) this);
_gwparams = nullptr;
_light = nullptr;
setTaskName("BrokerSendTask");

View File

@@ -30,7 +30,7 @@ char* currentDateTime(void);
ClientRecvTask::ClientRecvTask(Gateway* gateway)
{
_gateway = gateway;
_gateway->attach((Thread*) this);
Runnable::threadNo =_gateway->attach((Thread*) this);
_sensorNetwork = _gateway->getSensorNetwork();
setTaskName("ClientRecvTask");
}

View File

@@ -29,7 +29,7 @@ char* currentDateTime(void);
ClientSendTask::ClientSendTask(Gateway* gateway)
{
_gateway = gateway;
_gateway->attach((Thread*) this);
Runnable::threadNo =_gateway->attach((Thread*) this);
_sensorNetwork = _gateway->getSensorNetwork();
setTaskName("ClientSendTask");
}

View File

@@ -45,7 +45,7 @@ char* currentDateTime(void);
PacketHandleTask::PacketHandleTask(Gateway* gateway)
{
_gateway = gateway;
_gateway->attach((Thread*) this);
Runnable::threadNo =_gateway->attach((Thread*) this);
_mqttConnection = new MQTTGWConnectionHandler(_gateway);
_mqttPublish = new MQTTGWPublishHandler(_gateway);
_mqttSubscribe = new MQTTGWSubscribeHandler(_gateway);

View File

@@ -13,12 +13,13 @@
* Contributors:
* Tomoaki Yamaguchi - initial API and implementation and/or initial documentation
**************************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <signal.h>
#include <sys/types.h>
#include <unistd.h>
#include <Timer.h>
#include <exception>
#include <getopt.h>
@@ -252,13 +253,17 @@ MultiTaskProcess::MultiTaskProcess()
theMultiTaskProcess = this;
_threadCount = 0;
_stopCount = 0;
_abortThreadNo = -1;
}
MultiTaskProcess::~MultiTaskProcess()
{
for (int i = 0; i < _threadCount; i++)
{
_threadList[i]->stop();
if ( i != _abortThreadNo)
{
_threadList[i]->stop();
}
}
}
@@ -281,20 +286,20 @@ void MultiTaskProcess::run(void)
while (true)
{
if (theProcess->checkSignal() == SIGINT )
if (theProcess->checkSignal() == SIGINT || _abortThreadNo > -1)
{
return;
}
else if (_stopCount > 0)
{
throw Exception("Task stopped !!\n\n");
}
sleep(1);
}
}
void MultiTaskProcess::waitStop(void)
{
/* Let threads exit from Select() */
pid_t pid = getpid();
kill(pid, SIGINT);
while (_stopCount < _threadCount)
{
sleep(1);
@@ -309,11 +314,19 @@ void MultiTaskProcess::threadStopped(void)
}
void MultiTaskProcess::attach(Thread* thread)
void MultiTaskProcess::abort(int threadNo)
{
_abortThreadNo = threadNo;
threadStopped();
}
int MultiTaskProcess::attach(Thread* thread)
{
int indexNo = 0;
_mutex.lock();
if (_threadCount < MQTTSNGW_MAX_TASK)
{
indexNo = _threadCount;
_threadList[_threadCount] = thread;
_threadCount++;
}
@@ -323,6 +336,7 @@ void MultiTaskProcess::attach(Thread* thread)
throw Exception("Full of Threads");
}
_mutex.unlock();
return indexNo;
}
int MultiTaskProcess::getParam(const char* parameter, char* value)

View File

@@ -85,13 +85,15 @@ public:
void run(void);
void waitStop(void);
void threadStopped(void);
void attach(Thread* thread);
int attach(Thread* thread);
void abort(int threadNo);
private:
Thread* _threadList[MQTTSNGW_MAX_TASK];
Mutex _mutex;
int _threadCount;
int _stopCount;
int _abortThreadNo;
};
/*=====================================

View File

@@ -118,6 +118,7 @@ public:
Runnable(){}
virtual ~Runnable(){}
virtual void EXECRUN(){}
int threadNo {0};
};
#define MAGIC_WORD_FOR_THREAD \
@@ -130,9 +131,9 @@ public: void EXECRUN() \
} \
catch ( Exception &ex ) \
{ \
theMultiTaskProcess->threadStopped(); \
WRITELOG("%s catch exception\n", getTaskName()); \
ex.writeMessage(); \
theMultiTaskProcess->abort(threadNo); \
} \
}
@@ -151,6 +152,7 @@ public:
void stop(void);
const char* getTaskName(void);
void setTaskName(const char* name);
void abort(int threadNo);
private:
static void* _run(void*);
pthread_t _threadID;

View File

@@ -43,6 +43,5 @@ int main(int argc, char** argv)
{
ex.writeMessage();
WRITELOG("ABORT Gateway!!!\n\n\n");
abort();
}
}