< prev index next >

test/hotspot/gtest/utilities/test_lockFreeStack.cpp

Print this page




   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 #include "precompiled.hpp"
  25 #include "memory/allocation.inline.hpp"
  26 #include "runtime/atomic.hpp"
  27 #include "runtime/orderAccess.hpp"
  28 #include "utilities/globalDefinitions.hpp"
  29 #include "utilities/lockFreeStack.hpp"
  30 #include "threadHelper.inline.hpp"
  31 #include "unittest.hpp"
  32 #include <new>
  33 
  34 class LockFreeStackTestElement {
  35   typedef LockFreeStackTestElement Element;
  36 
  37   Element* volatile _entry;
  38   Element* volatile _entry1;
  39   size_t _id;
  40 
  41   static Element* volatile* entry_ptr(Element& e) { return &e._entry; }
  42   static Element* volatile* entry1_ptr(Element& e) { return &e._entry1; }
  43 
  44 public:
  45   LockFreeStackTestElement(size_t id = 0) : _entry(), _entry1(), _id(id) {}
  46   size_t id() const { return _id; }
  47   void set_id(size_t value) { _id = value; }


 209   volatile bool _ready;
 210 
 211 public:
 212   LockFreeStackTestThread(Semaphore* post,
 213                           uint id,
 214                           TestStack* from,
 215                           TestStack* to,
 216                           volatile size_t* processed,
 217                           size_t process_limit) :
 218     JavaTestThread(post),
 219     _id(id),
 220     _from(from),
 221     _to(to),
 222     _processed(processed),
 223     _process_limit(process_limit),
 224     _local_processed(0),
 225     _ready(false)
 226   {}
 227 
 228   virtual void main_run() {
 229     OrderAccess::release_store_fence(&_ready, true);
 230     while (true) {
 231       Element* e = _from->pop();
 232       if (e != NULL) {
 233         _to->push(*e);
 234         Atomic::inc(_processed);
 235         ++_local_processed;
 236       } else if (OrderAccess::load_acquire(_processed) == _process_limit) {
 237         tty->print_cr("thread %u processed " SIZE_FORMAT, _id, _local_processed);
 238         return;
 239       }
 240     }
 241   }
 242 
 243   bool ready() const { return OrderAccess::load_acquire(&_ready); }
 244 };
 245 
 246 TEST_VM(LockFreeStackTest, stress) {
 247   Semaphore post;
 248   TestStack initial_stack;
 249   TestStack start_stack;
 250   TestStack middle_stack;
 251   TestStack final_stack;
 252   volatile size_t stage1_processed = 0;
 253   volatile size_t stage2_processed = 0;
 254 
 255   const size_t nelements = 10000;
 256   Element* elements = NEW_C_HEAP_ARRAY(Element, nelements, mtOther);
 257   for (size_t id = 0; id < nelements; ++id) {
 258     ::new (&elements[id]) Element(id);
 259     initial_stack.push(elements[id]);
 260   }
 261   ASSERT_EQ(nelements, initial_stack.length());
 262 
 263   // - stage1 threads pop from start_stack and push to middle_stack.




   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 #include "precompiled.hpp"
  25 #include "memory/allocation.inline.hpp"
  26 #include "runtime/atomic.hpp"

  27 #include "utilities/globalDefinitions.hpp"
  28 #include "utilities/lockFreeStack.hpp"
  29 #include "threadHelper.inline.hpp"
  30 #include "unittest.hpp"
  31 #include <new>
  32 
  33 class LockFreeStackTestElement {
  34   typedef LockFreeStackTestElement Element;
  35 
  36   Element* volatile _entry;
  37   Element* volatile _entry1;
  38   size_t _id;
  39 
  40   static Element* volatile* entry_ptr(Element& e) { return &e._entry; }
  41   static Element* volatile* entry1_ptr(Element& e) { return &e._entry1; }
  42 
  43 public:
  44   LockFreeStackTestElement(size_t id = 0) : _entry(), _entry1(), _id(id) {}
  45   size_t id() const { return _id; }
  46   void set_id(size_t value) { _id = value; }


 208   volatile bool _ready;
 209 
 210 public:
 211   LockFreeStackTestThread(Semaphore* post,
 212                           uint id,
 213                           TestStack* from,
 214                           TestStack* to,
 215                           volatile size_t* processed,
 216                           size_t process_limit) :
 217     JavaTestThread(post),
 218     _id(id),
 219     _from(from),
 220     _to(to),
 221     _processed(processed),
 222     _process_limit(process_limit),
 223     _local_processed(0),
 224     _ready(false)
 225   {}
 226 
 227   virtual void main_run() {
 228     Atomic::release_store_fence(&_ready, true);
 229     while (true) {
 230       Element* e = _from->pop();
 231       if (e != NULL) {
 232         _to->push(*e);
 233         Atomic::inc(_processed);
 234         ++_local_processed;
 235       } else if (Atomic::load_acquire(_processed) == _process_limit) {
 236         tty->print_cr("thread %u processed " SIZE_FORMAT, _id, _local_processed);
 237         return;
 238       }
 239     }
 240   }
 241 
 242   bool ready() const { return Atomic::load_acquire(&_ready); }
 243 };
 244 
 245 TEST_VM(LockFreeStackTest, stress) {
 246   Semaphore post;
 247   TestStack initial_stack;
 248   TestStack start_stack;
 249   TestStack middle_stack;
 250   TestStack final_stack;
 251   volatile size_t stage1_processed = 0;
 252   volatile size_t stage2_processed = 0;
 253 
 254   const size_t nelements = 10000;
 255   Element* elements = NEW_C_HEAP_ARRAY(Element, nelements, mtOther);
 256   for (size_t id = 0; id < nelements; ++id) {
 257     ::new (&elements[id]) Element(id);
 258     initial_stack.push(elements[id]);
 259   }
 260   ASSERT_EQ(nelements, initial_stack.length());
 261 
 262   // - stage1 threads pop from start_stack and push to middle_stack.


< prev index next >