Sha256: b46d4901207e090da6c896627fd5536d816b039ba591c20566184cd7b6e76d07

Contents?: true

Size: 1.26 KB

Versions: 93

Compression:

Stored size: 1.26 KB

Contents

#include "../tut/tut.h"
#include <boost/bind.hpp>
#include <boost/thread.hpp>
#include <oxt/spin_lock.hpp>

using namespace boost;
using namespace oxt;

namespace tut {
	struct spin_lock_test {
		boost::mutex continue_mutex;
		boost::condition_variable continue_cond;
		bool continue_ok;
		spin_lock lock;
		volatile unsigned int counter;
		
		spin_lock_test() {
			counter = 0;
			continue_ok = false;
		}
		
		void loop_increment(unsigned int inc) {
			{
				boost::mutex::scoped_lock l1(continue_mutex);
				while (!continue_ok) {
					continue_cond.wait(l1);
				}
			}
			
			for (unsigned int i = 0; i < inc; i++) {
				spin_lock::scoped_lock l2(lock);
				counter++;
			}
		}
	};
	
	DEFINE_TEST_GROUP(spin_lock_test);

	TEST_METHOD(1) {
		boost::thread thr1(boost::bind(&spin_lock_test::loop_increment, this, 100000));
		boost::thread thr2(boost::bind(&spin_lock_test::loop_increment, this, 100000));
		boost::thread thr3(boost::bind(&spin_lock_test::loop_increment, this, 100000));
		boost::thread thr4(boost::bind(&spin_lock_test::loop_increment, this, 100000));
		
		{
			boost::mutex::scoped_lock l(continue_mutex);
			continue_ok = true;
			continue_cond.notify_all();
		}
		
		thr1.join();
		thr2.join();
		thr3.join();
		thr4.join();
		
		ensure_equals(counter, 400000u);
	}
}

Version data entries

93 entries across 93 versions & 4 rubygems

Version Path
passenger-4.0.60 test/oxt/spin_lock_test.cpp
passenger-5.0.4 test/oxt/spin_lock_test.cpp
passenger-5.0.3 test/oxt/spin_lock_test.cpp
passenger-5.0.2 test/oxt/spin_lock_test.cpp
passenger-5.0.1 test/oxt/spin_lock_test.cpp
passenger-5.0.0.rc2 test/oxt/spin_lock_test.cpp
passenger-5.0.0.rc1 test/oxt/spin_lock_test.cpp
passenger-4.0.59 test/oxt/spin_lock_test.cpp
passenger-5.0.0.beta3 test/oxt/spin_lock_test.cpp
passenger-4.0.58 test/oxt/spin_lock_test.cpp
passenger-4.0.57 test/oxt/spin_lock_test.cpp
passenger-5.0.0.beta2 test/oxt/spin_lock_test.cpp
passenger-4.0.56 test/oxt/spin_lock_test.cpp
passenger-4.0.55 test/oxt/spin_lock_test.cpp
passenger-5.0.0.beta1 test/oxt/spin_lock_test.cpp
passenger-4.0.53 test/oxt/spin_lock_test.cpp
passenger-4.0.52 test/oxt/spin_lock_test.cpp
passenger-4.0.51 test/oxt/spin_lock_test.cpp
passenger-4.0.50 test/oxt/spin_lock_test.cpp
passenger-4.0.49 test/oxt/spin_lock_test.cpp