[vmkit-commits] [vmkit] r73881 - in /vmkit/trunk: include/mvm/Threads/Locks.h include/mvm/Threads/Thread.h lib/Mvm/CommonThread/ctthread.cpp

Nicolas Geoffray nicolas.geoffray at lip6.fr
Mon Jun 22 07:23:58 PDT 2009


Author: geoffray
Date: Mon Jun 22 09:23:42 2009
New Revision: 73881

URL: http://llvm.org/viewvc/llvm-project?rev=73881&view=rev
Log:
Change the implementation of spin locks to optimize the case of
SMP machines.


Modified:
    vmkit/trunk/include/mvm/Threads/Locks.h
    vmkit/trunk/include/mvm/Threads/Thread.h
    vmkit/trunk/lib/Mvm/CommonThread/ctthread.cpp

Modified: vmkit/trunk/include/mvm/Threads/Locks.h
URL: http://llvm.org/viewvc/llvm-project/vmkit/trunk/include/mvm/Threads/Locks.h?rev=73881&r1=73880&r2=73881&view=diff

==============================================================================
--- vmkit/trunk/include/mvm/Threads/Locks.h (original)
+++ vmkit/trunk/include/mvm/Threads/Locks.h Mon Jun 22 09:23:42 2009
@@ -245,7 +245,6 @@
           TFatLock* obj = TFatLock::allocate(O);
           uintptr_t val = ((uintptr_t)obj >> 1) | FatMask;
 loop:
-          uint32 count = 0;
           while (lock) {
             if (lock & FatMask) {
 #ifdef USE_GC_BOEHM
@@ -253,7 +252,7 @@
 #endif
               goto end;
             }
-            else mvm::Thread::yield(&count);
+            else mvm::Thread::yield();
           }
         
           uintptr_t test = __sync_val_compare_and_swap((uintptr_t*)&lock, 0, val);
@@ -350,13 +349,16 @@
   SpinLock() { locked = 0; }
 
 
-  /// acquire - Acquire the spin lock, doing an active loop. When the lock
-  /// is already held, yield the processor.
+  /// acquire - Acquire the spin lock, doing an active loop.
   ///
   void acquire() {
-    uint32 count = 0;
-    while (llvm_atomic_cmp_swap_i8(&locked, 0, 1))
-      mvm::Thread::yield(&count);
+    for (uint32 count = 0; count < 1000; ++count) {
+      uint8 res = __sync_val_compare_and_swap(&locked, 0, 1);
+      if (!res) return;
+    }
+    
+    while (__sync_val_compare_and_swap(&locked, 0, 1))
+      mvm::Thread::yield();
   }
 
   /// release - Release the spin lock. This must be called by the thread

Modified: vmkit/trunk/include/mvm/Threads/Thread.h
URL: http://llvm.org/viewvc/llvm-project/vmkit/trunk/include/mvm/Threads/Thread.h?rev=73881&r1=73880&r2=73881&view=diff

==============================================================================
--- vmkit/trunk/include/mvm/Threads/Thread.h (original)
+++ vmkit/trunk/include/mvm/Threads/Thread.h Mon Jun 22 09:23:42 2009
@@ -10,6 +10,8 @@
 #ifndef MVM_THREAD_H
 #define MVM_THREAD_H
 
+#include <sched.h>
+
 #include "types.h"
 
 namespace mvm {
@@ -101,12 +103,9 @@
   
   /// yield - Yield the processor to another thread.
   ///
-  static void yield(void);
-  
-  /// yield - Yield the processor to another thread. If the thread has been
-  /// askink for yield already a number of times (n), then do a small sleep.
-  ///
-  static void yield(unsigned int* n);
+  static void yield(void) {
+    sched_yield();
+  }
   
   /// kill - Kill the thread with the given pid by sending it a signal.
   ///

Modified: vmkit/trunk/lib/Mvm/CommonThread/ctthread.cpp
URL: http://llvm.org/viewvc/llvm-project/vmkit/trunk/lib/Mvm/CommonThread/ctthread.cpp?rev=73881&r1=73880&r2=73881&view=diff

==============================================================================
--- vmkit/trunk/lib/Mvm/CommonThread/ctthread.cpp (original)
+++ vmkit/trunk/lib/Mvm/CommonThread/ctthread.cpp Mon Jun 22 09:23:42 2009
@@ -23,21 +23,6 @@
 
 using namespace mvm;
 
-void Thread::yield() {
-  sched_yield();
-}
-
-void Thread::yield(unsigned int *c) {
-  if(++(*c) & 3)
-    sched_yield();
-  else {
-    struct timespec ts; 
-    ts.tv_sec = 0;
-    ts.tv_nsec = 2000;
-    nanosleep(&ts, 0); 
-  }
-}
-
 int Thread::kill(void* tid, int signo) {
   return pthread_kill((pthread_t)tid, signo);
 }





More information about the vmkit-commits mailing list