From 462fa965ae01312b38d17b67c7f42513f84c6dd0 Mon Sep 17 00:00:00 2001 From: Bryan Roe Date: Sun, 6 Dec 2020 00:33:39 -0800 Subject: [PATCH] Fixed spinlocks for ARCHID 7,9,13 --- makefile | 5 ++- microstack/ILibParsers.c | 80 ++++++++++++++++++++++++++++++++++------ microstack/ILibParsers.h | 60 +++++++++++++++++------------- 3 files changed, 106 insertions(+), 39 deletions(-) diff --git a/makefile b/makefile index a237731..45940db 100644 --- a/makefile +++ b/makefile @@ -220,7 +220,7 @@ ifeq ($(ARCHID),7) ARCHNAME = mips CC = $(PATH_MIPS)mipsel-linux-gcc STRIP = $(PATH_MIPS)mipsel-linux-strip -CEXTRA = -D_FORTIFY_SOURCE=2 -D_NOILIBSTACKDEBUG -D_NOFSWATCHER -Wformat -Wformat-security -fno-strict-aliasing +CEXTRA = -D_FORTIFY_SOURCE=2 -D_NOILIBSTACKDEBUG -D_NOFSWATCHER -Wformat -Wformat-security -fno-strict-aliasing -DILIBCHAIN_GLOBAL_LOCK CFLAGS += -DBADMATH IPADDR_MONITOR_DISABLE = 1 IFADDR_DISABLE = 1 @@ -251,6 +251,7 @@ STRIP = $(PATH_ARM5)arm-none-linux-gnueabi-strip KVM = 0 LMS = 0 CFLAGS += -D_NOFSWATCHER +CFLAGS += -DILIBCHAIN_GLOBAL_LOCK CEXTRA = -fno-strict-aliasing endif @@ -261,7 +262,7 @@ CC = $(PATH_POGO)arm-none-linux-gnueabi-gcc STRIP = $(PATH_POGO)arm-none-linux-gnueabi-strip KVM = 0 LMS = 0 -CEXTRA = -D_FORTIFY_SOURCE=2 -D_NOILIBSTACKDEBUG -D_NOFSWATCHER -Wformat -Wformat-security -fno-strict-aliasing +CEXTRA = -D_FORTIFY_SOURCE=2 -D_NOILIBSTACKDEBUG -D_NOFSWATCHER -Wformat -Wformat-security -fno-strict-aliasing -DILIBCHAIN_GLOBAL_LOCK endif # Official Linux POKY diff --git a/microstack/ILibParsers.c b/microstack/ILibParsers.c index b81f8af..ddfaa80 100644 --- a/microstack/ILibParsers.c +++ b/microstack/ILibParsers.c @@ -1928,12 +1928,17 @@ void *ILibCreateChainEx(int extraMemorySize) { struct ILibBaseChain *RetVal; + if (ILibChainLock_RefCounter == 0) + { + sem_init(&ILibChainLock, 0, 1); + } + ILibChainLock_RefCounter++; + #if defined(WIN32) || defined(_WIN32_WCE) WORD wVersionRequested; WSADATA wsaData; wVersionRequested = MAKEWORD(2, 0); - if (WSAStartup(wVersionRequested, &wsaData) != 0) { ILIBCRITICALEXIT(1); } #endif RetVal = ILibMemory_Allocate(sizeof(ILibBaseChain), extraMemorySize, NULL, NULL); @@ -1948,12 +1953,6 @@ void *ILibCreateChainEx(int extraMemorySize) #endif RetVal->TerminateFlag = 0; - if (ILibChainLock_RefCounter == 0) - { - sem_init(&ILibChainLock, 0, 1); - } - ILibChainLock_RefCounter++; - RetVal->Timer = ILibCreateLifeTime(RetVal); #if defined(WIN32) @@ -2408,7 +2407,7 @@ ILibExportMethod void ILibChain_Continue(void *Chain, ILibChain_Link **modules, // FD_SET(root->TerminatePipe[0], &readset); #endif - sem_wait(&ILibChainLock); + while (ILibLinkedList_GetCount(((ILibBaseChain*)Chain)->LinksPendingDelete) > 0) { chain->node = ILibLinkedList_GetNode_Head(((ILibBaseChain*)Chain)->LinksPendingDelete); @@ -2422,7 +2421,6 @@ ILibExportMethod void ILibChain_Continue(void *Chain, ILibChain_Link **modules, ILibChain_FreeLink(module); } } - sem_post(&ILibChainLock); // // The actual Select Statement @@ -3763,7 +3761,7 @@ ILibExportMethod void ILibStartChain(void *Chain) // FD_SET(chain->TerminatePipe[0], &readset); #endif - sem_wait(&ILibChainLock); + while (ILibLinkedList_GetCount(((ILibBaseChain*)Chain)->LinksPendingDelete) > 0) { chain->node = ILibLinkedList_GetNode_Head(((ILibBaseChain*)Chain)->LinksPendingDelete); @@ -3777,7 +3775,6 @@ ILibExportMethod void ILibStartChain(void *Chain) ILibChain_FreeLink(module); } } - sem_post(&ILibChainLock); // // The actual Select Statement @@ -10842,3 +10839,64 @@ int ILibLinkedList_FileBacked_AddTail(ILibLinkedList_FileBacked_Root* root, char fflush(source); return 0; } + +#if defined(ILIBCHAIN_GLOBAL_LOCK) +int g_ILibSpinLock_acquired = 0; +#ifdef WIN32 +DWORD g_ILibSpinLock_acquired_threadid; +#else +pthread_t g_ILibSpinLock_acquired_threadid; +#endif + +void ILibSpinLock_Init(ILibSpinLock *lock) +{ + if (ILibChainLock_RefCounter == 0) + { + ++ILibChainLock_RefCounter; + sem_init(&ILibChainLock, 0, 1); + } + *lock = &ILibChainLock; +} +void ILibSpinLock_UnLock(ILibSpinLock *lock) +{ + sem_wait(*lock); + g_ILibSpinLock_acquired = 0; + sem_post(*lock); +} +void ILibSpinLock_Lock(ILibSpinLock *lock) +{ + int i = 1; + while (i != 0) + { + sem_wait(*lock); + if (g_ILibSpinLock_acquired == 0) + { + g_ILibSpinLock_acquired = 1; +#ifdef WIN32 + g_ILibSpinLock_acquired_threadid = GetCurrentThreadId(); +#else + g_ILibSpinLock_acquired_threadid = pthread_self(); +#endif + i = 0; + } + else + { +#ifdef WIN32 + if (g_ILibSpinLock_acquired_threadid == GetCurrentThreadId()) { i = 0; } +#else + if (pthread_equal(g_ILibSpinLock_acquired_threadid, pthread_self())) { i = 0; } +#endif + } + sem_post(*lock); + if (i != 0) + { +#ifdef WIN32 + YieldProcessor(); +#else + sched_yield(); +#endif + } + } +} +#endif + diff --git a/microstack/ILibParsers.h b/microstack/ILibParsers.h index f24d69a..d5fea38 100644 --- a/microstack/ILibParsers.h +++ b/microstack/ILibParsers.h @@ -116,27 +116,6 @@ struct sockaddr_in6; #include #endif -#ifdef WIN32 - typedef volatile long ILibSpinLock; -#else - typedef volatile int ILibSpinLock; -#endif -static inline void ILibSpinLock_Init(ILibSpinLock *lock) { *lock = 0; } -static inline void ILibSpinLock_UnLock(ILibSpinLock *lock) { *lock = 0; } -static inline void ILibSpinLock_Lock(ILibSpinLock *lock) -{ -#ifdef WIN32 - while (!InterlockedCompareExchange(lock, 1, 0)) - { - YieldProcessor(); - } -#else - while (!__sync_bool_compare_and_swap(lock, 0, 1)) - { - sched_yield(); - } -#endif -} #ifdef WIN32 #define ILIB_CURRENT_THREAD (unsigned int)GetCurrentThreadId() @@ -237,11 +216,6 @@ char *ILibWideToUTF8_stupidEx(WCHAR* wstr, int wstrBYTESIZE, char *buffer, int b #endif - - - - - int ILibGetLocalTime(char *dest, int destLen); long ILibGetTimeStamp(); @@ -331,6 +305,40 @@ long ILibGetTimeStamp(); #endif +#if defined(ILIBCHAIN_GLOBAL_LOCK) + typedef sem_t* ILibSpinLock; + void ILibSpinLock_Init(ILibSpinLock *lock); + void ILibSpinLock_UnLock(ILibSpinLock *lock); + void ILibSpinLock_Lock(ILibSpinLock *lock); +#else +#ifdef WIN32 + typedef volatile long ILibSpinLock; +#else + typedef volatile int ILibSpinLock; +#endif + static inline void ILibSpinLock_Init(ILibSpinLock *lock) { *lock = 0; } + static inline void ILibSpinLock_UnLock(ILibSpinLock *lock) { *lock = 0; } + static inline void ILibSpinLock_Lock(ILibSpinLock *lock) + { +#ifdef WIN32 + while (!InterlockedCompareExchange(lock, 1, 0)) + { + YieldProcessor(); + } +#else + while (!__sync_bool_compare_and_swap(lock, 0, 1)) + { + sched_yield(); + } +#endif + } +#endif + + + + + + #if !defined(WIN32) #define __fastcall