File: IORequest.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 (158 lines) | stat: -rw-r--r-- 3,741 bytes parent folder | download
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
#include "stdafx.h"
#include "IORequest.h"
#include "Thread.h"

namespace os {

#if defined(WINDOWS)

	IORequest::IORequest(Handle handle, const Thread &thread, nat timeout)
		: wake(0), handle(handle), bytes(0), error(0), thread(thread) {

		Internal = 0;
		InternalHigh = 0;
		Offset = 0;
		OffsetHigh = 0;
		Pointer = NULL;
		hEvent = NULL;
		thread.threadData()->ioComplete.attach();

		if (timeout) {
			sleep.until = UThreadState::sleepTarget(timeout);
			sleep.request = this;
			thread.threadData()->uState.addSleep(&sleep);
		}
	}

	IORequest::~IORequest() {
		if (sleep.request)
			thread.threadData()->uState.cancelSleep(&sleep);
		thread.threadData()->ioComplete.detach();
	}

	void IORequest::complete(nat bytes) {
		this->bytes = bytes;
		this->error = 0;
		wake.up();
	}

	void IORequest::failed(nat bytes, int error) {
		this->bytes = bytes;
		this->error = error;
		wake.up();
	}

	void IOTimeoutSleep::signal() {
		if (request) {
			// This causes this particular IO request to be cancelled as normal. That means we don't
			// have to do anything special to handle it, even if it was partially completed.
			CancelIoEx(request->handle.v(), request);
		}
		request = null;
	}

#elif defined(LINUX_IO_URING)

	IORequest::IORequest(Handle handle, const Thread &thread)
		: thread(thread), result(0), timeout(false), deallocate(false) {

		zeroMem(request);
		request.fd = handle.v();
	}

	int IORequest::submit(nat timeout) {
		io().attach(Handle(request.fd), this);
		if (timeout) {
			sleep.until = UThreadState::sleepTarget(timeout);
			sleep.request = this;
			thread.threadData()->uState.addSleep(&sleep);
		}
		wake.wait();
		return result;
	}

	int IORequest::submitAndDetach(nat timeout) {
		io().attachAndRemove(Handle(request.fd), this);
		if (timeout) {
			sleep.until = UThreadState::sleepTarget(timeout);
			sleep.request = this;
			thread.threadData()->uState.addSleep(&sleep);
		}
		wake.wait();
		return result;
	}

	void IORequest::submitDetachDelete(nat timeout) {
		deallocate = true;
		io().attachAndRemove(Handle(request.fd), this);
		if (timeout) {
			sleep.until = UThreadState::sleepTarget(timeout);
			sleep.request = this;
			thread.threadData()->uState.addSleep(&sleep);
		}
	}

	IORequest::~IORequest() {
		if (sleep.request)
			thread.threadData()->uState.cancelSleep(&sleep);
		io().detach(Handle(request.fd), this);
	}

	IOHandle &IORequest::io() {
		return thread.threadData()->ioComplete;
	}

	void IORequest::onFinish(int result) {
		this->result = result;
		bool deallocate = this->deallocate;

		wake.set();

		// Note: We don't touch 'this' after calling 'set'. If we are stack allocated by another
		// thread, we might be deallocated by now.
		if (deallocate)
			delete this;
	}

	void IOTimeoutSleep::signal() {
		if (request) {
			// Ask to cancel.
			request->timeout = true;
			request->io().cancel(request);
		}
		request = null;
	}


#elif defined(POSIX)

	IORequest::IORequest(Handle handle, Type type, const Thread &thread, nat timeout)
		: type(type), closed(false), timeout(false), handle(handle), thread(thread) {

		thread.threadData()->ioComplete.attach(handle, this);
		if (timeout) {
			sleep.until = UThreadState::sleepTarget(timeout);
			sleep.request = this;
			thread.threadData()->uState.addSleep(&sleep);
		}
	}

	IORequest::~IORequest() {
		if (sleep.request)
			thread.threadData()->uState.cancelSleep(&sleep);
		thread.threadData()->ioComplete.detach(handle, this);
	}

	void IOTimeoutSleep::signal() {
		if (request) {
			// We can just cancel it immediately. On UNIX, we always rely on OS-level buffers, so
			// nothing is transferred outside of the calls to read/write.
			request->timeout = true;
			request->wake.set();
		}
		request = null;
	}

#endif

}