File: Sync.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 (132 lines) | stat: -rw-r--r-- 2,348 bytes parent folder | download | duplicates (2)
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include "stdafx.h"
#include "Sync.h"
#include "Thread.h"
#include "UThread.h"

namespace os {

	Sema::Sema(nat count) : count(count) {}

	Sema::~Sema() {}

	void Sema::up() {
		UThreadData *toWake = null;

		{
			util::Lock::L z(lock);

			toWake = waiting.pop();
			if (!toWake) {
				// No thread to wake.
				count++;
				return;
			}
		}

		// Wake the thread up. Note: We can not do that inside the lock. Otherwise, the thread might
		// wake up before we have time to release the lock, which could cause us to crash if this
		// semaphore was deallocated by the thread that we woke (a typical use case for semaphores).
		toWake->owner()->wake(toWake);
	}

	void Sema::down() {
		UThreadState *state = null;

		{
			util::Lock::L z(lock);
			if (count > 0) {
				count--;
				return;
			}

			// Add us to the waiting queue.
			if (!state)
				state = UThreadState::current();
			waiting.push(state->runningThread());
		}

		// Wait until we're woken up again.
		state->wait();
	}


	/**
	 * Lock
	 */

	Lock::Lock() : sema(1) {}

	Lock::~Lock() {}

	Lock::L::L(Lock &l) : owner(l) {
		owner.lock();
	}

	Lock::L::~L() {
		owner.unlock();
	}

	void Lock::lock() {
		sema.down();
	}

	void Lock::unlock() {
		sema.up();
	}

	/**
	 * Event.
	 */

	Event::Event() : s(0) {}

	Event::~Event() {
		set();
	}

	void Event::set() {
		atomicWrite(s, 1);

		// Wake all threads. We need to be a bit careful here. We need to be aware of the situation
		// where a woken thread could deallocate this event. As such, we must not hold the lock
		// while waking the threads! We solve this by storing all threads in a separate, local, list
		// and then waking them all.
		InlineList<UThreadData> local;
		{
			util::Lock::L z(lock);
			while (UThreadData *data = waiting.pop())
				local.push(data);
		}

		// Now we can take our time and wake the threads.
		while (UThreadData *toWake = local.pop())
			toWake->owner()->wake(toWake);
	}

	void Event::clear() {
		atomicWrite(s, 0);
	}

	bool Event::isSet() {
		return atomicRead(s) == 1;
	}

	void Event::wait() {
		if (atomicRead(s) == 1)
			return;

		// Wait...

		UThreadState *state = null;
		{
			util::Lock::L z(lock);
			// Add us to the waiting queue.
			state = UThreadState::current();
			waiting.push(state->runningThread());
		}

		// Wait until we're woken up again.
		state->wait();
	}

}