File: ThreadTest.cpp

package info (click to toggle)
storm-lang 0.7.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 52,028 kB
  • sloc: ansic: 261,471; cpp: 140,432; sh: 14,891; perl: 9,846; python: 2,525; lisp: 2,504; asm: 860; makefile: 678; pascal: 70; java: 52; xml: 37; awk: 12
file content (80 lines) | stat: -rw-r--r-- 1,678 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include "stdafx.h"
#include "Compiler/Debug.h"
#include "Utils/Semaphore.h"

using namespace storm::debug;

static Link *createList(nat n) {
	Link *start = null;
	Link *prev = null;
	Engine &e = gEngine();

	for (nat i = 0; i < n; i++) {
		Link *now = new (e) Link;
		now->value = i;

		if (prev == null) {
			start = now;
		} else {
			prev->next = now;
		}
		prev = now;
	}

	// Trigger a GC to shake things up a bit!
	e.gc.collect();
	return start;
}

static Semaphore waitSema(0);
static bool threadOk = false;

static void threadFn() {
	gEngine().gc.attachThread();

	const nat count = 10000;

	Link *start = createList(count);
	threadOk = checkList(start, count);

	gEngine().gc.detachThread(os::Thread::current());
	waitSema.up();
}

BEGIN_TEST(GcThreadTest, GcThreads) {
	// Test so that other threads are properly GC:d.
	Engine &e = gEngine();

	os::Thread::spawn(util::simpleVoidFn(&threadFn), e.threadGroup);
	waitSema.down();
	CHECK(threadOk);

	// Now run in this thread as well to ensure we did not break anything by deattaching the thread.
	const nat count = 10000;
	Link *start = createList(count);
	CHECK(checkList(start, count));

} END_TEST

// Should be large enough to trigger a garbage collection.
static const nat count = 5000;

static void uthreadFn() {
	Link *start = createList(count);
	os::UThread::leave();
	threadOk = checkList(start, count);
}

BEGIN_TEST(GcUThreadTest, GcThreads) {
	threadOk = false;

	os::UThread::spawn(util::simpleVoidFn(&uthreadFn));
	Link *start = createList(count);
	os::UThread::leave();
	createList(count);
	os::UThread::leave();

	// Should be done by now!
	CHECK(threadOk);
	CHECK(checkList(start, count));
} END_TEST