Browse Source

improved performance of Critical locks by about 50 % on windows

Kolja Strohm 1 year ago
parent
commit
45cfbc0a96
3 changed files with 15 additions and 14 deletions
  1. 4 8
      Bild.cpp
  2. 9 4
      Critical.cpp
  3. 2 2
      Thread.cpp

+ 4 - 8
Bild.cpp

@@ -1333,10 +1333,6 @@ void Bild::drawLinieAlpha(Punkt a, Punkt b, int fc)
         }
         int maxP = (int)(sqrt((float)(xlen * xlen + ylen * ylen)) + 0.5);
         int count = 0;
-        int alpha = ((fc >> 24) & 0xFF);
-        int na = (0x100 - alpha);
-        int i1 = (alpha * (fc & 0xFF00FF)) >> 8;
-        int i2 = (alpha * (fc & 0x00FF00)) >> 8;
         if (alpha3D)
         {
             while (!((int)(x + 0.5) == b.x && (int)(y + 0.5) == b.y)
@@ -2931,11 +2927,11 @@ int Bild::getAverageColor() const
     double b = 0;
     for (int i = 0; i < size.x * size.y; i++)
     {
-        float currentA = (fc[i] >> 24) / 255.f;
+        float currentA = (float)(fc[i] >> 24) / 255.f;
         a = a + currentA;
-        r = r + ((fc[i] >> 16) & 0xFF) * currentA;
-        g = g + ((fc[i] >> 8) & 0xFF) * currentA;
-        b = b + (fc[i] & 0xFF) * currentA;
+        r = r + (double)((float)((fc[i] >> 16) & 0xFF) * currentA);
+        g = g + (double)((float)((fc[i] >> 8) & 0xFF) * currentA);
+        b = b + (double)((float)(fc[i] & 0xFF) * currentA);
     }
     a = a / (size.x * size.y);
     r = (r / (size.x * size.y)) / a;

+ 9 - 4
Critical.cpp

@@ -24,12 +24,17 @@ Critical::~Critical()
     DeleteCriticalSection(&cs);
 }
 
+pthread_t CachedCurrentThread()
+{
+    volatile thread_local static  pthread_t t = GetCurrentThread();
+    return (pthread_t)t;
+}
+
 // sperrt das Objekt
 void Critical::lock()
 {
-    pthread_t t = GetCurrentThread();
     getThreadRegister()->lock();
-    Thread* tmp = getThreadRegister()->zThread(t);
+    Thread* tmp = getThreadRegister()->zThread(CachedCurrentThread());
     if (tmp) tmp->addCriticalLock();
     getThreadRegister()->unlock();
     EnterCriticalSection(&cs);
@@ -42,7 +47,7 @@ bool Critical::tryLock()
 {
     if (lockCount > 0) return false;
     getThreadRegister()->lock();
-    Thread* tmp = getThreadRegister()->zThread(GetCurrentThread());
+    Thread* tmp = getThreadRegister()->zThread(CachedCurrentThread());
     if (tmp) tmp->addCriticalLock();
     getThreadRegister()->unlock();
     EnterCriticalSection(&cs);
@@ -60,7 +65,7 @@ void Critical::unlock()
     {
         if (owner
             && GetThreadId(owner->getThreadHandle())
-                   != GetThreadId(GetCurrentThread()))
+                   != GetThreadId(CachedCurrentThread()))
             throw std::runtime_error("A Thread that does not own a Critical "
                                      "Object trys to unlock it");
         tmp = owner;

+ 2 - 2
Thread.cpp

@@ -252,12 +252,12 @@ void ThreadRegister::addClosedThread(pthread_t handle)
 // handle: Ein handle zu dem gesuchten Thread
 Thread* ThreadRegister::zThread(pthread_t handle)
 {
+    if (handle == 0) return 0;
     EnterCriticalSection(&cs);
     for (auto i : threads)
     {
         if (i->getThreadHandle()
-            && GetThreadId(i->getThreadHandle()) == GetThreadId(handle)
-            && GetThreadId(handle) != 0)
+            && i->getThreadHandle() == handle)
         {
             LeaveCriticalSection(&cs);
             return i;