File: HandleStream.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 (638 lines) | stat: -rw-r--r-- 16,198 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
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
#include "stdafx.h"
#include "HandleStream.h"
#include "Core/Exception.h"
#include "OS/IORequest.h"
#include <limits>

namespace storm {

	// Helper to make a Duration into a ms interval, clamping as necessary.
	static nat toMs(Duration duration) {
		Long ms = duration.inMs();
		if (ms < 0)
			return 0;
		if (ms > std::numeric_limits<nat>::max())
			return std::numeric_limits<nat>::max();
		return nat(ms);
	}

	static void attach(os::Handle h, os::Thread &attached) {
		if (attached == os::Thread::invalid) {
			attached = os::Thread::current();
			attached.attach(h);
		}
	}

	/**
	 * System-specific helpers. These all behave as if the handle was blocking.
	 */

#if defined(WINDOWS)

	void close(os::Handle &h, os::Thread &attached) {
		os::Handle toClose = h;
		h = os::Handle();

		CloseHandle(toClose.v());
	}

	static PeekReadResult read(os::Handle h, os::Thread &attached, void *dest, Nat limit,
							Bool noPos, sys::ErrorCode &errorCode, Duration timeout = Duration()) {
		attach(h, attached);

		os::IORequest request(h, attached, toMs(timeout));

		LARGE_INTEGER pos;
		pos.QuadPart = 0;
		if (!noPos && SetFilePointerEx(h.v(), pos, &pos, FILE_CURRENT)) {
			// There seems to be a poblem when reading from the end of a file asynchronously.
			LARGE_INTEGER len;
			len.QuadPart = 0;
			GetFileSizeEx(h.v(), &len);
			if (pos.QuadPart >= len.QuadPart) {
				return PeekReadResult::end();
			}

			request.Offset = pos.LowPart;
			request.OffsetHigh = pos.HighPart;
		} else {
			// If we can not seek, it means that the offset should be zero.
			request.Offset = 0;
			request.OffsetHigh = 0;
		}

		int error = 0;
		// Note: When using ReadFile with sockets, it will sometimes indicate synchronous
		// completion *and* notify the IO completion port. Therefore, we will not trust the return
		// value, and assume asynchronous completion anyway.
		if (!ReadFile(h.v(), dest, DWORD(limit), NULL, &request))
			error = GetLastError();

		if (error == ERROR_IO_PENDING || error == 0) {
			// Completing async...
			request.wake.down();

			if (!noPos) {
				// Advance the file pointer.
				pos.QuadPart = request.bytes;
				SetFilePointerEx(h.v(), pos, NULL, FILE_CURRENT);
			}

			// Was the operation cancelled?
			if (request.bytes == 0 && request.error == ERROR_OPERATION_ABORTED)
				return PeekReadResult::timeout();
			// Error?
			if (request.error)
				errorCode = fromSystemError(request.error);

		} else {
			// Failed.
			request.bytes = 0;
			errorCode = fromSystemError(error);
		}

		return PeekReadResult::success(request.bytes);
	}

	static Nat write(os::Handle h, os::Thread &attached, const void *src, Nat limit,
					Bool noPos, sys::ErrorCode &errorCode, Duration timeout = Duration()) {
		attach(h, attached);

		os::IORequest request(h, attached, toMs(timeout));

		LARGE_INTEGER pos;
		pos.QuadPart = 0;
		if (!noPos && SetFilePointerEx(h.v(), pos, &pos, FILE_CURRENT)) {
			// All is well.
			request.Offset = pos.LowPart;
			request.OffsetHigh = pos.HighPart;
		} else {
			// If we can not seek, it means that the offset should be zero.
			request.Offset = 0;
			request.OffsetHigh = 0;
		}

		int error = 0;
		// Note: When using WriteFile with sockets, it will sometimes indicate synchronous
		// completion *and* notify the IO completion port. Therefore, we will not trust the return
		// value, and assume asynchronous completion anyway.
		if (!WriteFile(h.v(), src, DWORD(limit), NULL, &request))
			error = GetLastError();

		if (error == ERROR_IO_PENDING || error == 0) {
			// Completing async...
			request.wake.down();

			// Advance the file pointer.
			if (!noPos) {
				pos.QuadPart = request.bytes;
				SetFilePointerEx(h.v(), pos, NULL, FILE_CURRENT);
			}

			// Error?
			if (request.error != 0)
				errorCode = fromSystemError(request.error);

		} else {
			// Failed.
			request.bytes = 0;
			errorCode = fromSystemError(error);
		}

		return request.bytes;
	}

	static void seek(os::Handle h, Word to) {
		LARGE_INTEGER pos;
		pos.QuadPart = to;
		SetFilePointerEx(h.v(), pos, NULL, FILE_BEGIN);
	}

	static Word tell(os::Handle h) {
		LARGE_INTEGER pos;
		pos.QuadPart = 0;
		SetFilePointerEx(h.v(), pos, &pos, FILE_CURRENT);
		return pos.QuadPart;
	}

	static Word length(os::Handle h) {
		LARGE_INTEGER len;
		GetFileSizeEx(h.v(), &len);
		return len.QuadPart;
	}

	static os::Handle dupHandle(os::Handle src) {
		if (!src)
			return os::Handle();

		HANDLE dest = INVALID_HANDLE_VALUE;
		HANDLE proc = GetCurrentProcess();
		if (!DuplicateHandle(proc, src.v(), proc, &dest, DUPLICATE_SAME_ACCESS, FALSE, 0)) {
			PLN(L"Failed to duplicate handle: " << GetLastError());
			return os::Handle();
		}

		return dest;
	}

	static os::Handle openStd(DWORD id, bool input) {
		return dupHandle(GetStdHandle(id));
	}

#elif defined(LINUX_IO_URING)

	void close(os::Handle &h, os::Thread &attached) {
		if (h != os::Handle()) {
			// Since 'close' might be called from a destructor, it is not always called on the
			// proper OS thread. Since we now rarely use Semaphore to block OS threads entirely,
			// this is usually not a large problem (if we did, we could deadlock since the result to
			// our IOring request would be posted to a thread that is potentially blocked for
			// something else). Regardless, inter-thread communication is not ideal in destructors
			// regardless, so we try to avoid it.
			if (attached == os::Thread::current()) {
				// Closed from the proper thread. Detach and post a close request as a single operation.
				os::IORequest r(h, attached);
				r.request.opcode = IORING_OP_CLOSE;

				// submitAndDetach may block, so update 'h' already here so that we don't try to
				// close the same fd multiple times.
				h = os::Handle();
				r.submitAndDetach();
			} else if (attached != os::Thread::invalid) {
				// Posted from a different thread. Do not block this thread by heap-allocating the
				// request and asking it to deallocate itself once completed.
				os::IORequest *r = new os::IORequest(h, attached);
				r->request.opcode = IORING_OP_CLOSE;

				h = os::Handle();
				r->submitDetachDelete();
			} else {
				// This is not likely to happen, just close the fd normally as a last resort.
				::close(h.v());
			}
		}

		attached = os::Thread::invalid;
		h = os::Handle();
	}

	static PeekReadResult read(os::Handle h, os::Thread &attached, void *dest, Nat limit,
							Bool noPos, sys::ErrorCode &errorCode, Duration timeout = Duration()) {
		attach(h, attached);

		os::IORequest r(h, attached);

		r.request.opcode = IORING_OP_READ;
		r.request.addr = reinterpret_cast<size_t>(dest);
		r.request.len = limit;
		r.request.off = -1; // Advances the file pointer as usual.

		int result = r.submit(toMs(timeout));
		// Note: ECANCELED means that the timeout was reached, or that the file was closed during
		// the operation (since we cancel it then). We don't report that as an error, since none of
		// the other implementations do.
		if (result < 0 && result != -ECANCELED)
			errorCode = fromSystemError(-result);

		if (result <= 0)
			return r.timeout ? PeekReadResult::timeout() : PeekReadResult::end();
		else
			return PeekReadResult::success(Nat(result));
	}

	static Nat write(os::Handle h, os::Thread &attached, const void *src, Nat limit,
					Bool noPos, sys::ErrorCode &errorCode, Duration timeout = Duration()) {
		attach(h, attached);

		os::IORequest r(h, attached);

		r.request.opcode = IORING_OP_WRITE;
		r.request.addr = reinterpret_cast<size_t>(src);
		r.request.len = limit;
		r.request.off = -1; // Advances the file pointer as usual.

		int result = r.submit(toMs(timeout));
		if (result < 0 && result != -ECANCELED)
			errorCode = fromSystemError(-result);

		if (result <= 0)
			return 0;
		else
			return Nat(result);
	}

	static void seek(os::Handle h, Word to) {
		lseek64(h.v(), to, SEEK_SET);
	}

	static Word tell(os::Handle h) {
		off64_t r = lseek64(h.v(), 0, SEEK_CUR);
		if (r < 0)
			return 0;
		return Word(r);
	}

	static Word length(os::Handle h) {
		off64_t old = lseek64(h.v(), 0, SEEK_CUR);
		if (old < 0)
			return 0;

		off64_t size = lseek64(h.v(), 0, SEEK_END);
		lseek64(h.v(), old, SEEK_SET);
		return Word(size);
	}

	static os::Handle dupHandle(os::Handle src) {
		return os::Handle(dup(src.v()));
	}


#elif defined(POSIX)

	void close(os::Handle &h, os::Thread &attached) {
		if (attached != os::Thread::invalid)
			attached.detach(h);

		::close(h.v());
		attached = os::Thread::invalid;
		h = os::Handle();
	}

	struct WaitResult {
		bool closed;
		bool timeout;
	};

	// Returns 'false' if the handle was closed (by us) during the operation.
	static WaitResult doWait(os::Handle h, os::Thread &attached, os::IORequest::Type type, Duration timeout) {
		attach(h, attached);

		os::IORequest request(h, type, attached, toMs(timeout));
		request.wake.wait();
		WaitResult r = {
			request.closed,
			request.timeout
		};
		return r;
	}

	static PeekReadResult read(os::Handle h, os::Thread &attached, void *dest, Nat limit,
							Bool noPos, sys::ErrorCode &errorCode, Duration timeout = Duration()) {
		while (true) {
			ssize_t r = ::read(h.v(), dest, size_t(limit));
			if (r >= 0)
				return PeekReadResult::success(Nat(r));

			int error = errno;
			if (error == EINTR) {
				// Aborted by a signal. Retry.
				continue;
			} else if (error == EAGAIN) {
				// Wait for more data.
				WaitResult r = doWait(h, attached, os::IORequest::read, timeout);
				if (r.timeout)
					return PeekReadResult::timeout();
				if (r.closed)
					break;
			} else {
				// Other error.
				errorCode = fromSystemError(error);
				break;
			}
		}

		return PeekReadResult::end();
	}

	static Nat write(os::Handle h, os::Thread &attached, const void *src, Nat limit,
					Bool noPos, sys::ErrorCode &errorCode, Duration timeout = Duration()) {
		while (true) {
			ssize_t r = ::write(h.v(), src, size_t(limit));
			if (r >= 0)
				return Nat(r);

			int error = errno;
			if (error == EINTR) {
				// Aborted by a signal. Retry.
				continue;
			} else if (error == EAGAIN) {
				// Wait for more data.
				WaitResult r = doWait(h, attached, os::IORequest::write, timeout);
				if (r.closed || r.timeout)
					break;
			} else {
				// Other error.
				errorCode = fromSystemError(error);
				break;
			}
		}

		return 0;
	}

	static void seek(os::Handle h, Word to) {
		lseek64(h.v(), to, SEEK_SET);
	}

	static Word tell(os::Handle h) {
		off64_t r = lseek64(h.v(), 0, SEEK_CUR);
		if (r < 0)
			return 0;
		return Word(r);
	}

	static Word length(os::Handle h) {
		off64_t old = lseek64(h.v(), 0, SEEK_CUR);
		if (old < 0)
			return 0;

		off64_t size = lseek64(h.v(), 0, SEEK_END);
		lseek64(h.v(), old, SEEK_SET);
		return Word(size);
	}

	static os::Handle dupHandle(os::Handle src) {
		return os::Handle(dup(src.v()));
	}

#else
#error "Please implement file IO for your OS."
#endif

	/**
	 * Regular input stream.
	 */

	HandleIStream::HandleIStream(os::Handle h)
		: handle(h),
		  attachedTo(os::Thread::invalid),
		  noClose(false),
		  currError(sys::none) {}

	HandleIStream::HandleIStream(os::Handle h, os::Thread t)
		: handle(h),
		  attachedTo(t),
		  noClose(false),
		  currError(sys::none) {}

	// HandleIStream::HandleIStream(const HandleIStream &o)
	// 	: PeekIStream(o),
	// 	  handle(dupHandle(o.handle)),
	// 	  attachedTo(os::Thread::invalid) {}

	HandleIStream::HandleIStream(const HandleIStream &o)
		: PeekIStream(o), attachedTo(os::Thread::invalid), noClose(o.noClose), currError(o.currError) {
		throw new (this) NotSupported(S("Copying HandleIStream"));
	}

	HandleIStream::~HandleIStream() {
		if (handle)
			storm::close(handle, attachedTo);
	}

	Bool HandleIStream::more() {
		if (!handle)
			return false;

		return PeekIStream::more();
	}

	void HandleIStream::close() {
		if (handle && !noClose)
			storm::close(handle, attachedTo);
		handle = os::Handle();

		PeekIStream::close();
	}

	PeekReadResult HandleIStream::doRead(byte *to, Nat count) {
		if (handle)
			return storm::read(handle, attachedTo, to, count, noPos, currError);
		else
			return PeekReadResult::end();
	}

	/**
	 * Random access stream.
	 */

	HandleRIStream::HandleRIStream(os::Handle h)
		: handle(h),
		  attachedTo(os::Thread::invalid),
		  noClose(false),
		  currError(sys::none) {}

	HandleRIStream::HandleRIStream(os::Handle h, os::Thread t)
		: handle(h),
		  attachedTo(t),
		  noClose(false),
		  currError(sys::none) {}

	// HandleRIStream::HandleRIStream(const HandleRIStream &o)
	// 	: handle(dupHandle(o.handle)),
	// 	  attachedTo(os::Thread::invalid) {}

	HandleRIStream::HandleRIStream(const HandleRIStream &o) : attachedTo(os::Thread::invalid), noClose(false), currError(o.currError) {
		throw new (this) NotSupported(S("Copying HandleIStream"));
	}

	HandleRIStream::~HandleRIStream() {
		if (handle)
			storm::close(handle, attachedTo);
	}

	void HandleRIStream::deepCopy(CloneEnv *e) {
		// Nothing needs to be done.
	}

	Bool HandleRIStream::more() {
		if (!handle)
			return false;

		return tell() < length();
	}

	Buffer HandleRIStream::read(Buffer b) {
		Nat start = b.filled();

		if (!handle)
			return b;

		if (start >= b.count())
			return b;

		PeekReadResult r = storm::read(handle, attachedTo, b.dataPtr() + start, b.count() - start, noPos, currError);
		b.filled(r.bytesRead() + start);
		return b;
	}

	Buffer HandleRIStream::peek(Buffer b) {
		if (!handle)
			return b;

		Word pos = tell();
		b = read(b);
		seek(pos);

		return b;
	}

	void HandleRIStream::close() {
		if (handle && !noClose)
			storm::close(handle, attachedTo);
		handle = os::Handle();
	}

	RIStream *HandleRIStream::randomAccess() {
		return this;
	}

	void HandleRIStream::seek(Word to) {
		storm::seek(handle, to);
	}

	Word HandleRIStream::tell() {
		if (!handle)
			return 0;

		return storm::tell(handle);
	}

	Word HandleRIStream::length() {
		if (!handle)
			return 0;

		return storm::length(handle);
	}

	/**
	 * With timeout.
	 */

	HandleTimeoutIStream::HandleTimeoutIStream(os::Handle handle) : HandleIStream(handle), timeout() {}

	HandleTimeoutIStream::HandleTimeoutIStream(os::Handle handle, os::Thread attachedTo) : HandleIStream(handle), timeout() {}

	PeekReadResult HandleTimeoutIStream::doRead(byte *to, Nat count) {
		if (handle)
			return storm::read(handle, attachedTo, to, count, noPos, currError, timeout);
		else
			return PeekReadResult::end();
	}

	/**
	 * Output stream.
	 */

	HandleOStream::HandleOStream(os::Handle h)
		: handle(h),
		  attachedTo(os::Thread::invalid),
		  noClose(false),
		  currError(sys::none) {}

	HandleOStream::HandleOStream(os::Handle h, os::Thread t)
		: handle(h),
		  attachedTo(t),
		  noClose(false),
		  currError(sys::none) {}

	// HandleOStream::HandleOStream(const HandleOStream &o)
	// 	: handle(dupHandle(o.handle)),
	// 	  attachedTo(os::Thread::invalid) {}

	HandleOStream::HandleOStream(const HandleOStream &o)
		: attachedTo(os::Thread::invalid), noClose(o.noClose), currError(o.currError) {
		throw new (this) NotSupported(S("Copying HandleIStream"));
	}

	HandleOStream::~HandleOStream() {
		if (handle)
			storm::close(handle, attachedTo);
	}

	Nat HandleOStream::write(Buffer to, Nat start) {
		Nat consumed = 0;
		start = min(start, to.filled());
		if (handle) {
			while (start < to.filled()) {
				Nat r = storm::write(handle, attachedTo, to.dataPtr() + start, to.filled() - start, noPos, currError);
				if (r == 0)
					break;
				start += r;
				consumed += r;
			}
		}
		return consumed;
	}

	void HandleOStream::close() {
		if (handle && !noClose)
			storm::close(handle, attachedTo);
		handle = os::Handle();
	}

	/**
	 * With timeout.
	 */

	HandleTimeoutOStream::HandleTimeoutOStream(os::Handle h) : HandleOStream(h), timeout() {}

	HandleTimeoutOStream::HandleTimeoutOStream(os::Handle h, os::Thread attachedTo)
		: HandleOStream(h, attachedTo), timeout() {}

	Nat HandleTimeoutOStream::write(Buffer to, Nat start) {
		Nat consumed = 0;
		start = min(start, to.filled());
		if (handle) {
			while (start < to.filled()) {
				Nat r = storm::write(handle, attachedTo, to.dataPtr() + start, to.filled() - start, noPos, currError, timeout);
				if (r == 0)
					break;
				start += r;
				consumed += r;
			}
		}
		return consumed;
	}

}