File: StdIoThread.h

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 (95 lines) | stat: -rw-r--r-- 2,061 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
#pragma once
#include "Core/Io/StdStream.h"
#include "OS/Condition.h"
#include "Utils/Lock.h"

namespace storm {

	/**
	 * A thread dedicated to synchronizing standard IO (at least on Windows, where we can not do
	 * async IO on these handles). This thread is not exposed to the Gc, as it only manages pointers
	 * which are pinned on other (suspended) thread's stacks.
	 */
	class StdIo : NoCopy {
	public:
		// Create - spawns a thread.
		StdIo();

		// Destroy - terminates the thread again.
		~StdIo();

		// Post an IO-request.
		void post(StdRequest *request);

	private:
		// Pointer to the first and last nodes. Protected using the lock.
		StdRequest *first;
		StdRequest *last;

		// Lock protecting 'first' and 'last'.
		util::Lock lock;

		// Condition for synchronization.
		os::Condition cond;

		// Post a message to the thread.
		void postThread(StdRequest *request);

		// Platform-specific initialization and destruction.
		void platformInit();
		void platformDestroy();

		// Thread management.
		void startThread();
		void waitThread();
		bool hasThread();

		// Read-write functionality.
		void doNop(StdRequest *r);
		void doRead(StdRequest *r);
		void doWrite(StdRequest *r);

		// Try to perform a nonblocking read operation. Returns 'true' on success.
		bool tryRead(StdRequest *r);

		// Lock input.
		void lockInput();
		bool tryLockInput();
		void unlockInput();

		// Main function for the IO thread.
		void main();

#if defined(WINDOWS)
		// Thread handle for the running thread.
		HANDLE thread;

		// All handles we need so we do not need to query the OS all the time.
		HANDLE handles[3];

		// Lock for doing input.
		CRITICAL_SECTION inputLock;

		// Startup function.
		friend DWORD WINAPI ioMain(void *param);

#elif defined(POSIX)
		// Thread handle.
		pthread_t thread;
		bool threadStarted;

		// All handles used.
		int handles[3];

		// Lock for doing input.
		pthread_mutex_t inputLock;

		// Startup function.
		friend void *ioMain(void *param);

#else
#error "Please implement STDIO for your platform!"
#endif
	};

}