From d33592cfdffb985002cdb24c7a35c45f88a74e6d Mon Sep 17 00:00:00 2001 From: andreas Date: Sun, 24 Nov 2024 16:16:35 +0100 Subject: [PATCH] directly use SemaphoreHandle_t as pointer --- lib/exampletask/GwExampleTask.cpp | 4 ++-- lib/socketserver/GwTcpClient.cpp | 6 +++--- lib/usercode/GwUserCode.cpp | 29 ++++++++++++++--------------- lib/usercode/GwUserCode.h | 5 +++-- src/main.cpp | 13 +++++++------ 5 files changed, 29 insertions(+), 28 deletions(-) diff --git a/lib/exampletask/GwExampleTask.cpp b/lib/exampletask/GwExampleTask.cpp index 9b4cbcdc..821dd570 100644 --- a/lib/exampletask/GwExampleTask.cpp +++ b/lib/exampletask/GwExampleTask.cpp @@ -156,11 +156,11 @@ class ExampleWebData{ vSemaphoreDelete(lock); } void set(int v){ - GWSYNCHRONIZED(&lock); + GWSYNCHRONIZED(lock); data=v; } int get(){ - GWSYNCHRONIZED(&lock); + GWSYNCHRONIZED(lock); return data; } }; diff --git a/lib/socketserver/GwTcpClient.cpp b/lib/socketserver/GwTcpClient.cpp index e5f914b4..27923000 100644 --- a/lib/socketserver/GwTcpClient.cpp +++ b/lib/socketserver/GwTcpClient.cpp @@ -246,7 +246,7 @@ void GwTcpClient::resolveHost(String host) { LOG_DEBUG(GwLog::LOG,"start resolving %s",host.c_str()); { - GWSYNCHRONIZED(&locker); + GWSYNCHRONIZED(locker); resolvedAddress.resolved = false; } state = C_RESOLVING; @@ -283,12 +283,12 @@ void GwTcpClient::resolveHost(String host) void GwTcpClient::setResolved(IPAddress addr, bool valid){ LOG_DEBUG(GwLog::LOG,"setResolved %s, valid=%s", addr.toString().c_str(),(valid?"true":"false")); - GWSYNCHRONIZED(&locker); + GWSYNCHRONIZED(locker); resolvedAddress.address=addr; resolvedAddress.resolved=valid; state=C_RESOLVED; } GwTcpClient::ResolvedAddress GwTcpClient::getResolved(){ - GWSYNCHRONIZED(&locker); + GWSYNCHRONIZED(locker); return resolvedAddress; } \ No newline at end of file diff --git a/lib/usercode/GwUserCode.cpp b/lib/usercode/GwUserCode.cpp index 08164b61..1b007f8e 100644 --- a/lib/usercode/GwUserCode.cpp +++ b/lib/usercode/GwUserCode.cpp @@ -75,7 +75,7 @@ class TaskInterfacesStorage{ lock=xSemaphoreCreateMutex(); } bool set(const String &name, GwApi::TaskInterfaces::Ptr v){ - GWSYNCHRONIZED(&lock); + GWSYNCHRONIZED(lock); auto vit=values.find(name); if (vit != values.end()){ vit->second.updates++; @@ -90,7 +90,7 @@ class TaskInterfacesStorage{ return true; } GwApi::TaskInterfaces::Ptr get(const String &name, int &result){ - GWSYNCHRONIZED(&lock); + GWSYNCHRONIZED(lock); auto it = values.find(name); if (it == values.end()) { @@ -102,7 +102,7 @@ class TaskInterfacesStorage{ } bool update(const String &name, std::functionf){ - GWSYNCHRONIZED(&lock); + GWSYNCHRONIZED(lock); auto vit=values.find(name); bool rt=false; int mode=0; @@ -160,7 +160,7 @@ class TaskApi : public GwApiInternal { GwApiInternal *api=nullptr; int sourceId; - SemaphoreHandle_t *mainLock; + SemaphoreHandle_t mainLock; SemaphoreHandle_t localLock; std::map> counter; std::map webHandlers; @@ -172,7 +172,7 @@ class TaskApi : public GwApiInternal public: TaskApi(GwApiInternal *api, int sourceId, - SemaphoreHandle_t *mainLock, + SemaphoreHandle_t mainLock, const String &name, TaskInterfacesStorage *s, bool init=false) @@ -236,14 +236,14 @@ class TaskApi : public GwApiInternal vSemaphoreDelete(localLock); }; virtual void fillStatus(GwJsonDocument &status){ - GWSYNCHRONIZED(&localLock); + GWSYNCHRONIZED(localLock); if (! counterUsed) return; for (auto it=counter.begin();it != counter.end();it++){ it->second.toJson(status); } }; virtual int getJsonSize(){ - GWSYNCHRONIZED(&localLock); + GWSYNCHRONIZED(localLock); if (! counterUsed) return 0; int rt=0; for (auto it=counter.begin();it != counter.end();it++){ @@ -252,7 +252,7 @@ class TaskApi : public GwApiInternal return rt; }; virtual void increment(int idx,const String &name,bool failed=false){ - GWSYNCHRONIZED(&localLock); + GWSYNCHRONIZED(localLock); counterUsed=true; auto it=counter.find(idx); if (it == counter.end()) return; @@ -260,18 +260,18 @@ class TaskApi : public GwApiInternal else (it->second.add(name)); }; virtual void reset(int idx){ - GWSYNCHRONIZED(&localLock); + GWSYNCHRONIZED(localLock); counterUsed=true; auto it=counter.find(idx); if (it == counter.end()) return; it->second.reset(); }; virtual void remove(int idx){ - GWSYNCHRONIZED(&localLock); + GWSYNCHRONIZED(localLock); counter.erase(idx); } virtual int addCounter(const String &name){ - GWSYNCHRONIZED(&localLock); + GWSYNCHRONIZED(localLock); counterUsed=true; counterIdx++; //avoid the need for an empty counter constructor @@ -289,7 +289,7 @@ class TaskApi : public GwApiInternal return api->addXdrMapping(def); } virtual void registerRequestHandler(const String &url,HandlerFunction handler){ - GWSYNCHRONIZED(&localLock); + GWSYNCHRONIZED(localLock); webHandlers[url]=handler; } virtual void addCapability(const String &name, const String &value){ @@ -316,7 +316,7 @@ class TaskApi : public GwApiInternal { GwApi::HandlerFunction handler; { - GWSYNCHRONIZED(&localLock); + GWSYNCHRONIZED(localLock); auto it = webHandlers.find(url); if (it == webHandlers.end()) { @@ -345,10 +345,9 @@ class TaskApi : public GwApiInternal } }; -GwUserCode::GwUserCode(GwApiInternal *api,SemaphoreHandle_t *mainLock){ +GwUserCode::GwUserCode(GwApiInternal *api){ this->logger=api->getLogger(); this->api=api; - this->mainLock=mainLock; this->taskData=new TaskInterfacesStorage(this->logger); } GwUserCode::~GwUserCode(){ diff --git a/lib/usercode/GwUserCode.h b/lib/usercode/GwUserCode.h index c055411c..af020047 100644 --- a/lib/usercode/GwUserCode.h +++ b/lib/usercode/GwUserCode.h @@ -41,13 +41,14 @@ class TaskInterfacesStorage; class GwUserCode{ GwLog *logger; GwApiInternal *api; - SemaphoreHandle_t *mainLock; + SemaphoreHandle_t mainLock=nullptr; TaskInterfacesStorage *taskData; void startAddOnTask(GwApiInternal *api,GwUserTask *task,int sourceId,String name); public: ~GwUserCode(); typedef std::map Capabilities; - GwUserCode(GwApiInternal *api, SemaphoreHandle_t *mainLock); + GwUserCode(GwApiInternal *api); + void begin(SemaphoreHandle_t mainLock){this->mainLock=mainLock;} void startUserTasks(int baseId); void startInitTasks(int baseId); void startAddonTask(String name,TaskFunction_t task, int id); diff --git a/src/main.cpp b/src/main.cpp index dd549447..c12f906d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -235,17 +235,17 @@ void SendNMEA0183Message(const tNMEA0183Msg &NMEA0183Msg, int sourceId,bool conv class CalibrationValues { using Map=std::map; Map values; - SemaphoreHandle_t lock; + SemaphoreHandle_t lock=nullptr; public: CalibrationValues(){ lock=xSemaphoreCreateMutex(); } void set(const String &name,double value){ - GWSYNCHRONIZED(&lock); + GWSYNCHRONIZED(lock); values[name]=value; } bool get(const String &name, double &value){ - GWSYNCHRONIZED(&lock); + GWSYNCHRONIZED(lock); auto it=values.find(name); if (it==values.end()) return false; value=it->second; @@ -373,7 +373,7 @@ bool delayedRestart(){ },"reset",2000,&logger,0,NULL) == pdPASS; } ApiImpl *apiImpl=new ApiImpl(MIN_USER_TASK); -GwUserCode userCodeHandler(apiImpl,&mainLock); +GwUserCode userCodeHandler(apiImpl); #define JSON_OK "{\"status\":\"OK\"}" #define JSON_INVALID_PASS F("{\"status\":\"invalid password\"}") @@ -788,6 +788,7 @@ void setup() { logger.setWriter(new DefaultLogWriter()); #endif boatData.begin(); + userCodeHandler.begin(mainLock); userCodeHandler.startInitTasks(MIN_USER_TASK); channels.preinit(); config.stopChanges(); @@ -937,7 +938,7 @@ void setup() { logger.logDebug(GwLog::LOG,"starting addon tasks"); logger.flush(); { - GWSYNCHRONIZED(&mainLock); + GWSYNCHRONIZED(mainLock); userCodeHandler.startUserTasks(MIN_USER_TASK); } timers.addAction(HEAP_REPORT_TIME,[](){ @@ -967,7 +968,7 @@ void handleSendAndRead(bool handleRead){ void loopRun() { //logger.logDebug(GwLog::DEBUG,"main loop start"); monitor.reset(); - GWSYNCHRONIZED(&mainLock); + GWSYNCHRONIZED(mainLock); logger.flush(); monitor.setTime(1); gwWifi.loop();