Manta Interactive Ray Tracer Development Mailing List

Text archives Help


[Manta] r2006 - trunk/Core/Thread


Chronological Thread 
  • From: boulos@sci.utah.edu
  • To: manta@sci.utah.edu
  • Subject: [Manta] r2006 - trunk/Core/Thread
  • Date: Thu, 24 Jan 2008 19:22:25 -0700 (MST)

Author: boulos
Date: Thu Jan 24 19:22:24 2008
New Revision: 2006

Added:
   trunk/Core/Thread/AtomicCounter_x86.cc
Modified:
   trunk/Core/Thread/AtomicCounter.h
   trunk/Core/Thread/Thread_pthreads.cc
Log:
Core/Thread/AtomicCounter.h

 Removing nasty tabs, but more importantly adding an int for local
 storage (for optimized x86 implementation)

Core/Thread/AtomicCounter_x86.cc

 That optimized x86 atomic counter I wrote in September. Finally committed.

Core/Thread/Thread_pthreads.cc

 Include MachineParameters.h and if MANTA_X86 is one, use the
 optimized atomic counter.


Modified: trunk/Core/Thread/AtomicCounter.h
==============================================================================
--- trunk/Core/Thread/AtomicCounter.h   (original)
+++ trunk/Core/Thread/AtomicCounter.h   Thu Jan 24 19:22:24 2008
@@ -48,7 +48,7 @@
 class AtomicCounter_private;
 
 /**************************************

+
  CLASS
  AtomicCounter
 
@@ -96,9 +96,9 @@
   // This does not return AtomicCounter& like a normal ++
   // operator would, because it would destroy atomicity
   int operator++();
-    
+
   //////////
-  //   Increment the counter and return the old value
+  //    Increment the counter and return the old value
   int operator++(int);
 
   //////////
@@ -106,7 +106,7 @@
   // This does not return AtomicCounter& like a normal --
   // operator would, because it would destroy atomicity
   int operator--();
-    
+
   //////////
   // Decrement the counter and return the old value
   int operator--(int);
@@ -118,6 +118,9 @@
 private:
   const char* name_;
   AtomicCounter_private* priv_;
+  // NOTE(boulos): We could additionally add a check for MANTA_X86
+  // here as this is the only code that relies on it.
+  int value;
 
   // Cannot copy them
   AtomicCounter(const AtomicCounter&);

Added: trunk/Core/Thread/AtomicCounter_x86.cc
==============================================================================
--- (empty file)
+++ trunk/Core/Thread/AtomicCounter_x86.cc      Thu Jan 24 19:22:24 2008
@@ -0,0 +1,131 @@
+/*
+   For more information, please see: http://software.sci.utah.edu
+
+   The MIT License
+
+   Copyright (c) 2004 Scientific Computing and Imaging Institute,
+   University of Utah.
+
+   License for the specific language governing rights and limitations under
+   Permission is hereby granted, free of charge, to any person obtaining a
+   copy of this software and associated documentation files (the "Software"),
+   to deal in the Software without restriction, including without limitation
+   the rights to use, copy, modify, merge, publish, distribute, sublicense,
+   and/or sell copies of the Software, and to permit persons to whom the
+   Software is furnished to do so, subject to the following conditions:
+
+   The above copyright notice and this permission notice shall be included
+   in all copies or substantial portions of the Software.
+
+   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 
MERCHANTABILITY,
+   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+   THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+   DEALINGS IN THE SOFTWARE.
+*/
+
+
+
+/*
+ *  AtomicCounter: Thread-safe integer variable written in x86 assembly
+ *
+ *  Written by:
+ *   Author: Solomon Boulos
+ *   Department of Computer Science
+ *   University of Utah
+ *   Date: 10-Sep-2007
+ *
+ */
+
+#include <Core/Thread/AtomicCounter.h>
+
+using Manta::AtomicCounter;
+
+AtomicCounter::AtomicCounter(const char* name)
+  : name_(name), priv_(0), value(0)
+{
+  if(!Thread::isInitialized()){
+    if(getenv("THREAD_SHOWINIT"))
+      fprintf(stderr, "AtomicCounter: %s\n", name);
+    Thread::initialize();
+  }
+}
+
+AtomicCounter::AtomicCounter(const char* name, int value)
+  : name_(name), value(value) {
+}
+
+AtomicCounter::~AtomicCounter() {
+}
+
+AtomicCounter::operator int() const {
+  return value;
+}
+
+int
+AtomicCounter::operator++() {
+  volatile int return_val = 1;
+
+  __asm__ __volatile__(
+      "lock;\n"
+      "xaddl %1, %0;\n" :
+      "=m" (value), "=r"(return_val) :
+      "m" (value) , "r" (return_val) :
+      /* no unknown clobbers */
+    );
+  return return_val + 1;
+}
+
+int
+AtomicCounter::operator++(int) {
+  volatile int return_val = 1;
+
+  __asm__ __volatile__(
+      "lock;\n"
+      "xaddl %1, %0;\n" :
+      "=m" (value), "=r"(return_val) :
+      "m" (value) , "r" (return_val) :
+      /* no unknown clobbers */
+    );
+  return return_val;
+}
+
+int
+AtomicCounter::operator--() {
+  volatile int return_val = -1;
+  __asm__ __volatile__(
+      "lock;\n"
+      "xaddl %1, %0;\n" :
+      "=m" (value), "=r"(return_val) :
+      "m" (value) , "r" (return_val) :
+      /* no unknown clobbers */
+    );
+  return return_val - 1;
+}
+
+int
+AtomicCounter::operator--(int) {
+  volatile int return_val = -1;
+  __asm__ __volatile__(
+      "lock;\n"
+      "xaddl %1, %0;\n" :
+      "=m" (value), "=r"(return_val) :
+      "m" (value) , "r" (return_val) :
+      /* no unknown clobbers */
+    );
+  // The exchange returns the old value
+  return return_val;
+}
+
+void
+AtomicCounter::set(int v) {
+  __asm__ __volatile__(
+    "lock;\n"
+    "xchgl %1, %0\n" :
+    "=m" (value), "=r" (v) :
+    "m" (value), "r" (v) :
+    /* no unknown clobbers */
+    );
+}

Modified: trunk/Core/Thread/Thread_pthreads.cc
==============================================================================
--- trunk/Core/Thread/Thread_pthreads.cc        (original)
+++ trunk/Core/Thread/Thread_pthreads.cc        Thu Jan 24 19:22:24 2008
@@ -77,6 +77,9 @@
 #include <Core/Thread/ThreadGroup.h>
 #include <Core/Thread/WorkQueue.h>
 #include <Core/Thread/Thread_unix.h>
+
+#include <MachineParameters.h>
+
 #include <errno.h>
 extern "C" {
 #include <semaphore.h>
@@ -157,7 +160,12 @@
 
 #ifndef __ia64__
 #include <Core/Thread/Barrier_default.cc>
+
+#ifdef MANTA_X86
+#include <Core/Thread/AtomicCounter_x86.cc>
+#else
 #include <Core/Thread/AtomicCounter_default.cc>
+#endif
 #endif
 #include <Core/Thread/CrowdMonitor_default.cc>
 




  • [Manta] r2006 - trunk/Core/Thread, boulos, 01/24/2008

Archive powered by MHonArc 2.6.16.

Top of page