File: StdIoThread.cpp

package info (click to toggle)
storm-lang 0.7.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 52,004 kB
  • sloc: ansic: 261,462; cpp: 140,405; 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 (266 lines) | stat: -rw-r--r-- 5,099 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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#include "stdafx.h"
#include "StdIoThread.h"

namespace storm {

	StdIo::StdIo() : first(null), last(null) {
		platformInit();
	}

	StdIo::~StdIo() {
		if (hasThread()) {
			StdRequest quit(stdIn, null, 0);
			postThread(&quit);

			// Wait for the thread to terminate.
			waitThread();
		}
		platformDestroy();
	}

	void StdIo::doNop(StdRequest *r) {
		r->count = 0;
		r->wait.up();
	}

	void StdIo::post(StdRequest *r) {
		// No accidental 'terminate' messages.
		if (!r->buffer) {
			doNop(r);
			return;
		}

		switch (r->stream) {
		case stdIn: {
			// See if we can read now, otherwise post to the thread.
			bool ok = false;
			if (tryLockInput()) {
				ok = tryRead(r);
				unlockInput();
			}

			if (!ok)
				postThread(r);
			break;
		}
		case stdOut:
		case stdError:
			// Output to the streams directly.
			// TODO: We should queue these requests if we realize they will take time.
			doWrite(r);
			break;
		default:
			// Invalid stream...
			doNop(r);
			break;
		}
	}

	void StdIo::postThread(StdRequest *r) {
		{
			util::Lock::L z(lock);
			if (last == null) {
				// One and only node!
				last = first = r;
			} else {
				last->next = r;
				last = r;
			}

			// Is the thread actually started?
			if (!hasThread())
				startThread();
		}

		cond.signal();
	}

	void StdIo::main() {
		// Note: currently input is blocking output, which is bad. We should do something clever about that!

		while (true) {
			StdRequest *now = null;
			{
				util::Lock::L z(lock);
				now = first;
				if (now)
					first = now->next;
				if (last == now)
					last = null;
			}

			// Anything to do?
			if (now == null) {
				cond.wait();
				continue;
			}

			// Got a request!
			if (now->buffer == null) {
				// We shall terminate...
				return;
			}

			if (now->stream == stdIn) {
				// Special care is needed when reading...
				lockInput();
				doRead(now);
				unlockInput();
			} else {
				doWrite(now);
			}
		}
	}

#if defined(WINDOWS)

	void StdIo::platformInit() {
		thread = NULL;
		handles[stdIn] = GetStdHandle(STD_INPUT_HANDLE);
		handles[stdOut] = GetStdHandle(STD_OUTPUT_HANDLE);
		handles[stdError] = GetStdHandle(STD_ERROR_HANDLE);
		InitializeCriticalSection(&inputLock);
	}

	void StdIo::platformDestroy() {
		DeleteCriticalSection(&inputLock);
	}

	void StdIo::startThread() {
		thread = CreateThread(NULL, 1024 * 3, &ioMain, this, 0, NULL);
	}

	void StdIo::waitThread() {
		WaitForSingleObject(thread, 500);
	}

	bool StdIo::hasThread() {
		return thread != NULL;
	}

	void StdIo::lockInput() {
		EnterCriticalSection(&inputLock);
	}

	bool StdIo::tryLockInput() {
		return TryEnterCriticalSection(&inputLock) ? true : false;
	}

	void StdIo::unlockInput() {
		LeaveCriticalSection(&inputLock);
	}

	void StdIo::doRead(StdRequest *r) {
		HANDLE handle = handles[r->stream];
		DWORD result = 0;
		ReadFile(handle, r->buffer, r->count, &result, NULL);
		r->count = Nat(result);
		r->wait.up();
	}

	bool StdIo::tryRead(StdRequest *r) {
		// NOTE: This does not seem to work... At least not when inside of Emacs...
		return false;

		HANDLE handle = handles[r->stream];
		// Stdin handle becomes signaling when there is data to read.
		if (WaitForSingleObject(handle, 0) == WAIT_OBJECT_0) {
			// Safe to read!
			doRead(r);
			return true;
		} else {
			return false;
		}
	}

	void StdIo::doWrite(StdRequest *r) {
		HANDLE handle = handles[r->stream];
		DWORD result = 0;
		WriteFile(handle, r->buffer, r->count, &result, NULL);
		r->count = Nat(result);
		r->wait.up();
	}

	DWORD WINAPI ioMain(void *param) {
		StdIo *me = (StdIo *)param;
		me->main();
		return 0;
	}

#elif defined(POSIX)

	void StdIo::platformInit() {
		threadStarted = false;
		handles[stdIn] = STDIN_FILENO;
		handles[stdOut] = STDOUT_FILENO;
		handles[stdError] = STDERR_FILENO;
		pthread_mutex_init(&inputLock, null);
	}

	void StdIo::platformDestroy() {
		pthread_mutex_destroy(&inputLock);
	}

	void *ioMain(void *param);

	void StdIo::startThread() {
		// TODO? Reduce stack space for this thread?
		pthread_create(&thread, NULL, &ioMain, this);
		threadStarted = true;
	}

	void StdIo::waitThread() {
		// TODO? Add timeout?
		pthread_join(thread, NULL);
	}

	bool StdIo::hasThread() {
		return threadStarted;
	}

	void StdIo::lockInput() {
		pthread_mutex_lock(&inputLock);
	}

	bool StdIo::tryLockInput() {
		return pthread_mutex_trylock(&inputLock) == 0;
	}

	void StdIo::unlockInput() {
		pthread_mutex_unlock(&inputLock);
	}

	void StdIo::doRead(StdRequest *r) {
		int handle = handles[r->stream];
		ssize_t result = read(handle, r->buffer, r->count);
		if (result < 0)
			r->count = 0;
		else
			r->count = Nat(result);
		r->wait.up();
	}

	bool StdIo::tryRead(StdRequest *r) {
		// TODO: Temporarily switch to nonblocking IO for this fd?
		return false;
	}

	void StdIo::doWrite(StdRequest *r) {
		int handle = handles[r->stream];
		ssize_t result = write(handle, r->buffer, r->count);
		if (result < 0)
			r->count = 0;
		else
			r->count = Nat(result);
		r->wait.up();
	}

	void *ioMain(void *param) {
		StdIo *me = (StdIo *)param;
		me->main();
		return null;
	}

#endif

}