File: spin_lock_test.cpp

package info (click to toggle)
ruby-passenger 3.0.13debian-1%2Bdeb7u2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 15,920 kB
  • sloc: cpp: 99,104; ruby: 18,098; ansic: 9,846; sh: 8,632; python: 141; makefile: 30
file content (59 lines) | stat: -rw-r--r-- 1,292 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#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);
	}
}