| ... | @@ -89,9 +89,10 @@ pub async fn pwritevWindows(loop: *Loop, fd: os.FileHandle, data: []const []cons | ... | @@ -89,9 +89,10 @@ pub async fn pwritevWindows(loop: *Loop, fd: os.FileHandle, data: []const []cons |
| 89 | if (data.len == 0) return; | 89 | if (data.len == 0) return; |
| 90 | if (data.len == 1) return await (async pwriteWindows(loop, fd, data[0], offset) catch unreachable); | 90 | if (data.len == 1) return await (async pwriteWindows(loop, fd, data[0], offset) catch unreachable); |
| 91 | | 91 | |
| 92 | const data_copy = std.mem.dupe(loop.allocator, []const u8, data); | 92 | const data_copy = try std.mem.dupe(loop.allocator, []const u8, data); |
| 93 | defer loop.allocator.free(data_copy); | 93 | defer loop.allocator.free(data_copy); |
| 94 | | 94 | |
| | 95 | // TODO do these in parallel |
| 95 | var off = offset; | 96 | var off = offset; |
| 96 | for (data_copy) |buf| { | 97 | for (data_copy) |buf| { |
| 97 | try await (async pwriteWindows(loop, fd, buf, off) catch unreachable); | 98 | try await (async pwriteWindows(loop, fd, buf, off) catch unreachable); |
| ... | @@ -120,6 +121,9 @@ pub async fn pwriteWindows(loop: *Loop, fd: os.FileHandle, data: []const u8, off | ... | @@ -120,6 +121,9 @@ pub async fn pwriteWindows(loop: *Loop, fd: os.FileHandle, data: []const u8, off |
| 120 | .OffsetHigh = @truncate(u32, offset >> 32), | 121 | .OffsetHigh = @truncate(u32, offset >> 32), |
| 121 | .hEvent = null, | 122 | .hEvent = null, |
| 122 | }; | 123 | }; |
| | 124 | loop.beginOneEvent(); |
| | 125 | errdefer loop.finishOneEvent(); |
| | 126 | |
| 123 | errdefer { | 127 | errdefer { |
| 124 | _ = windows.CancelIoEx(fd, &overlapped); | 128 | _ = windows.CancelIoEx(fd, &overlapped); |
| 125 | } | 129 | } |
| ... | @@ -192,9 +196,89 @@ pub async fn pwritevPosix(loop: *Loop, fd: os.FileHandle, data: []const []const | ... | @@ -192,9 +196,89 @@ pub async fn pwritevPosix(loop: *Loop, fd: os.FileHandle, data: []const []const |
| 192 | | 196 | |
| 193 | /// data - just the inner references - must live until preadv promise completes. | 197 | /// data - just the inner references - must live until preadv promise completes. |
| 194 | pub async fn preadv(loop: *Loop, fd: os.FileHandle, data: []const []u8, offset: usize) !usize { | 198 | pub async fn preadv(loop: *Loop, fd: os.FileHandle, data: []const []u8, offset: usize) !usize { |
| 195 | //const data_dupe = try mem.dupe(loop.allocator, []const u8, data); | 199 | assert(data.len != 0); |
| 196 | //defer loop.allocator.free(data_dupe); | 200 | switch (builtin.os) { |
| | 201 | builtin.Os.macosx, |
| | 202 | builtin.Os.linux, |
| | 203 | => return await (async preadvPosix(loop, fd, data, offset) catch unreachable), |
| | 204 | builtin.Os.windows, |
| | 205 | => return await (async preadvWindows(loop, fd, data, offset) catch unreachable), |
| | 206 | else => @compileError("Unsupported OS"), |
| | 207 | } |
| | 208 | } |
| | 209 | |
| | 210 | pub async fn preadvWindows(loop: *Loop, fd: os.FileHandle, data: []const []u8, offset: u64) !usize { |
| | 211 | assert(data.len != 0); |
| | 212 | if (data.len == 1) return await (async preadWindows(loop, fd, data[0], offset) catch unreachable); |
| | 213 | |
| | 214 | const data_copy = try std.mem.dupe(loop.allocator, []u8, data); |
| | 215 | defer loop.allocator.free(data_copy); |
| | 216 | |
| | 217 | // TODO do these in parallel? |
| | 218 | var off: usize = 0; |
| | 219 | var iov_i: usize = 0; |
| | 220 | var inner_off: usize = 0; |
| | 221 | while (true) { |
| | 222 | const v = data_copy[iov_i]; |
| | 223 | const amt_read = try await (async preadWindows(loop, fd, v[inner_off .. v.len-inner_off], offset + off) catch unreachable); |
| | 224 | off += amt_read; |
| | 225 | inner_off += amt_read; |
| | 226 | if (inner_off == v.len) { |
| | 227 | iov_i += 1; |
| | 228 | inner_off = 0; |
| | 229 | if (iov_i == data_copy.len) { |
| | 230 | return off; |
| | 231 | } |
| | 232 | } |
| | 233 | if (amt_read == 0) return off; // EOF |
| | 234 | } |
| | 235 | } |
| | 236 | |
| | 237 | pub async fn preadWindows(loop: *Loop, fd: os.FileHandle, data: []u8, offset: u64) !usize { |
| | 238 | // workaround for https://github.com/ziglang/zig/issues/1194 |
| | 239 | suspend { |
| | 240 | resume @handle(); |
| | 241 | } |
| | 242 | |
| | 243 | var resume_node = Loop.ResumeNode.Basic{ |
| | 244 | .base = Loop.ResumeNode{ |
| | 245 | .id = Loop.ResumeNode.Id.Basic, |
| | 246 | .handle = @handle(), |
| | 247 | }, |
| | 248 | }; |
| | 249 | const completion_key = @ptrToInt(&resume_node.base); |
| | 250 | _ = try os.windowsCreateIoCompletionPort(fd, loop.os_data.io_port, completion_key, undefined); |
| | 251 | var overlapped = windows.OVERLAPPED{ |
| | 252 | .Internal = 0, |
| | 253 | .InternalHigh = 0, |
| | 254 | .Offset = @truncate(u32, offset), |
| | 255 | .OffsetHigh = @truncate(u32, offset >> 32), |
| | 256 | .hEvent = null, |
| | 257 | }; |
| | 258 | loop.beginOneEvent(); |
| | 259 | errdefer loop.finishOneEvent(); |
| | 260 | |
| | 261 | errdefer { |
| | 262 | _ = windows.CancelIoEx(fd, &overlapped); |
| | 263 | } |
| | 264 | suspend { |
| | 265 | _ = windows.ReadFile(fd, data.ptr, @intCast(windows.DWORD, data.len), null, &overlapped); |
| | 266 | } |
| | 267 | var bytes_transferred: windows.DWORD = undefined; |
| | 268 | if (windows.GetOverlappedResult(fd, &overlapped, &bytes_transferred, windows.FALSE) == 0) { |
| | 269 | const err = windows.GetLastError(); |
| | 270 | return switch (err) { |
| | 271 | windows.ERROR.IO_PENDING => unreachable, |
| | 272 | windows.ERROR.OPERATION_ABORTED => error.OperationAborted, |
| | 273 | windows.ERROR.BROKEN_PIPE => error.BrokenPipe, |
| | 274 | else => os.unexpectedErrorWindows(err), |
| | 275 | }; |
| | 276 | } |
| | 277 | return usize(bytes_transferred); |
| | 278 | } |
| 197 | | 279 | |
| | 280 | /// data - just the inner references - must live until preadv promise completes. |
| | 281 | pub async fn preadvPosix(loop: *Loop, fd: os.FileHandle, data: []const []u8, offset: usize) !usize { |
| 198 | // workaround for https://github.com/ziglang/zig/issues/1194 | 282 | // workaround for https://github.com/ziglang/zig/issues/1194 |
| 199 | suspend { | 283 | suspend { |
| 200 | resume @handle(); | 284 | resume @handle(); |
| ... | @@ -287,8 +371,23 @@ pub async fn openPosix( | ... | @@ -287,8 +371,23 @@ pub async fn openPosix( |
| 287 | } | 371 | } |
| 288 | | 372 | |
| 289 | pub async fn openRead(loop: *Loop, path: []const u8) os.File.OpenError!os.FileHandle { | 373 | pub async fn openRead(loop: *Loop, path: []const u8) os.File.OpenError!os.FileHandle { |
| 290 | const flags = posix.O_LARGEFILE | posix.O_RDONLY | posix.O_CLOEXEC; | 374 | switch (builtin.os) { |
| 291 | return await (async openPosix(loop, path, flags, os.File.default_mode) catch unreachable); | 375 | builtin.Os.macosx, builtin.Os.linux => { |
| | 376 | const flags = posix.O_LARGEFILE | posix.O_RDONLY | posix.O_CLOEXEC; |
| | 377 | return await (async openPosix(loop, path, flags, os.File.default_mode) catch unreachable); |
| | 378 | }, |
| | 379 | |
| | 380 | builtin.Os.windows => return os.windowsOpen( |
| | 381 | loop.allocator, |
| | 382 | path, |
| | 383 | windows.GENERIC_READ, |
| | 384 | windows.FILE_SHARE_READ, |
| | 385 | windows.OPEN_EXISTING, |
| | 386 | windows.FILE_ATTRIBUTE_NORMAL | windows.FILE_FLAG_OVERLAPPED, |
| | 387 | ), |
| | 388 | |
| | 389 | else => @compileError("Unsupported OS"), |
| | 390 | } |
| 292 | } | 391 | } |
| 293 | | 392 | |
| 294 | /// Creates if does not exist. Truncates the file if it exists. | 393 | /// Creates if does not exist. Truncates the file if it exists. |
| ... | @@ -325,8 +424,23 @@ pub async fn openReadWrite( | ... | @@ -325,8 +424,23 @@ pub async fn openReadWrite( |
| 325 | path: []const u8, | 424 | path: []const u8, |
| 326 | mode: os.File.Mode, | 425 | mode: os.File.Mode, |
| 327 | ) os.File.OpenError!os.FileHandle { | 426 | ) os.File.OpenError!os.FileHandle { |
| 328 | const flags = posix.O_LARGEFILE | posix.O_RDWR | posix.O_CREAT | posix.O_CLOEXEC; | 427 | switch (builtin.os) { |
| 329 | return await (async openPosix(loop, path, flags, mode) catch unreachable); | 428 | builtin.Os.macosx, builtin.Os.linux => { |
| | 429 | const flags = posix.O_LARGEFILE | posix.O_RDWR | posix.O_CREAT | posix.O_CLOEXEC; |
| | 430 | return await (async openPosix(loop, path, flags, mode) catch unreachable); |
| | 431 | }, |
| | 432 | |
| | 433 | builtin.Os.windows => return os.windowsOpen( |
| | 434 | loop.allocator, |
| | 435 | path, |
| | 436 | windows.GENERIC_WRITE|windows.GENERIC_READ, |
| | 437 | windows.FILE_SHARE_WRITE | windows.FILE_SHARE_READ | windows.FILE_SHARE_DELETE, |
| | 438 | windows.OPEN_ALWAYS, |
| | 439 | windows.FILE_ATTRIBUTE_NORMAL | windows.FILE_FLAG_OVERLAPPED, |
| | 440 | ), |
| | 441 | |
| | 442 | else => @compileError("Unsupported OS"), |
| | 443 | } |
| 330 | } | 444 | } |
| 331 | | 445 | |
| 332 | /// This abstraction helps to close file handles in defer expressions | 446 | /// This abstraction helps to close file handles in defer expressions |
| ... | @@ -340,62 +454,64 @@ pub const CloseOperation = struct { | ... | @@ -340,62 +454,64 @@ pub const CloseOperation = struct { |
| 340 | os_data: OsData, | 454 | os_data: OsData, |
| 341 | | 455 | |
| 342 | const OsData = switch (builtin.os) { | 456 | const OsData = switch (builtin.os) { |
| 343 | builtin.Os.linux, | 457 | builtin.Os.linux, builtin.Os.macosx => OsDataPosix, |
| 344 | builtin.Os.macosx, | 458 | |
| 345 | => struct { | 459 | builtin.Os.windows => struct { |
| 346 | have_fd: bool, | | |
| 347 | close_req_node: RequestNode, | | |
| 348 | }, | | |
| 349 | builtin.Os.windows, | | |
| 350 | => struct { | | |
| 351 | handle: ?os.FileHandle, | 460 | handle: ?os.FileHandle, |
| 352 | }, | 461 | }, |
| | 462 | |
| 353 | else => @compileError("Unsupported OS"), | 463 | else => @compileError("Unsupported OS"), |
| 354 | }; | 464 | }; |
| 355 | | 465 | |
| | 466 | const OsDataPosix = struct { |
| | 467 | have_fd: bool, |
| | 468 | close_req_node: RequestNode, |
| | 469 | }; |
| | 470 | |
| 356 | pub fn start(loop: *Loop) (error{OutOfMemory}!*CloseOperation) { | 471 | pub fn start(loop: *Loop) (error{OutOfMemory}!*CloseOperation) { |
| 357 | const self = try loop.allocator.createOne(CloseOperation); | 472 | const self = try loop.allocator.createOne(CloseOperation); |
| 358 | self.* = CloseOperation{ | 473 | self.* = CloseOperation{ |
| 359 | .loop = loop, | 474 | .loop = loop, |
| 360 | .os_data = switch (builtin.os) { | 475 | .os_data = switch (builtin.os) { |
| 361 | builtin.Os.linux, | 476 | builtin.Os.linux, builtin.Os.macosx => initOsDataPosix(self), |
| 362 | builtin.Os.macosx, | 477 | builtin.Os.windows => OsData{ .handle = null }, |
| 363 | => OsData{ | | |
| 364 | .have_fd = false, | | |
| 365 | .close_req_node = RequestNode{ | | |
| 366 | .prev = null, | | |
| 367 | .next = null, | | |
| 368 | .data = Request{ | | |
| 369 | .msg = Request.Msg{ | | |
| 370 | .Close = Request.Msg.Close{ .fd = undefined }, | | |
| 371 | }, | | |
| 372 | .finish = Request.Finish{ .DeallocCloseOperation = self }, | | |
| 373 | }, | | |
| 374 | }, | | |
| 375 | }, | | |
| 376 | builtin.Os.windows, | | |
| 377 | => OsData{ .handle = null }, | | |
| 378 | else => @compileError("Unsupported OS"), | 478 | else => @compileError("Unsupported OS"), |
| 379 | }, | 479 | }, |
| 380 | }; | 480 | }; |
| 381 | return self; | 481 | return self; |
| 382 | } | 482 | } |
| 383 | | 483 | |
| | 484 | fn initOsDataPosix(self: *CloseOperation) OsData { |
| | 485 | return OsData{ |
| | 486 | .have_fd = false, |
| | 487 | .close_req_node = RequestNode{ |
| | 488 | .prev = null, |
| | 489 | .next = null, |
| | 490 | .data = Request{ |
| | 491 | .msg = Request.Msg{ |
| | 492 | .Close = Request.Msg.Close{ .fd = undefined }, |
| | 493 | }, |
| | 494 | .finish = Request.Finish{ .DeallocCloseOperation = self }, |
| | 495 | }, |
| | 496 | }, |
| | 497 | }; |
| | 498 | } |
| | 499 | |
| 384 | /// Defer this after creating. | 500 | /// Defer this after creating. |
| 385 | pub fn finish(self: *CloseOperation) void { | 501 | pub fn finish(self: *CloseOperation) void { |
| 386 | switch (builtin.os) { | 502 | switch (builtin.os) { |
| 387 | builtin.Os.linux, | 503 | builtin.Os.linux, |
| 388 | builtin.Os.macosx, | 504 | builtin.Os.macosx, |
| 389 | => { | 505 | => { |
| 390 | if (self.have_fd) { | 506 | if (self.os_data.have_fd) { |
| 391 | self.loop.posixFsRequest(&self.close_req_node); | 507 | self.loop.posixFsRequest(&self.os_data.close_req_node); |
| 392 | } else { | 508 | } else { |
| 393 | self.loop.allocator.destroy(self); | 509 | self.loop.allocator.destroy(self); |
| 394 | } | 510 | } |
| 395 | }, | 511 | }, |
| 396 | builtin.Os.windows, | 512 | builtin.Os.windows, |
| 397 | => { | 513 | => { |
| 398 | if (self.handle) |handle| { | 514 | if (self.os_data.handle) |handle| { |
| 399 | os.close(handle); | 515 | os.close(handle); |
| 400 | } | 516 | } |
| 401 | self.loop.allocator.destroy(self); | 517 | self.loop.allocator.destroy(self); |
| ... | @@ -409,12 +525,12 @@ pub const CloseOperation = struct { | ... | @@ -409,12 +525,12 @@ pub const CloseOperation = struct { |
| 409 | builtin.Os.linux, | 525 | builtin.Os.linux, |
| 410 | builtin.Os.macosx, | 526 | builtin.Os.macosx, |
| 411 | => { | 527 | => { |
| 412 | self.close_req_node.data.msg.Close.fd = handle; | 528 | self.os_data.close_req_node.data.msg.Close.fd = handle; |
| 413 | self.have_fd = true; | 529 | self.os_data.have_fd = true; |
| 414 | }, | 530 | }, |
| 415 | builtin.Os.windows, | 531 | builtin.Os.windows, |
| 416 | => { | 532 | => { |
| 417 | self.handle = handle; | 533 | self.os_data.handle = handle; |
| 418 | }, | 534 | }, |
| 419 | else => @compileError("Unsupported OS"), | 535 | else => @compileError("Unsupported OS"), |
| 420 | } | 536 | } |
| ... | @@ -426,11 +542,11 @@ pub const CloseOperation = struct { | ... | @@ -426,11 +542,11 @@ pub const CloseOperation = struct { |
| 426 | builtin.Os.linux, | 542 | builtin.Os.linux, |
| 427 | builtin.Os.macosx, | 543 | builtin.Os.macosx, |
| 428 | => { | 544 | => { |
| 429 | self.have_fd = false; | 545 | self.os_data.have_fd = false; |
| 430 | }, | 546 | }, |
| 431 | builtin.Os.windows, | 547 | builtin.Os.windows, |
| 432 | => { | 548 | => { |
| 433 | self.handle = null; | 549 | self.os_data.handle = null; |
| 434 | }, | 550 | }, |
| 435 | else => @compileError("Unsupported OS"), | 551 | else => @compileError("Unsupported OS"), |
| 436 | } | 552 | } |
| ... | @@ -441,12 +557,12 @@ pub const CloseOperation = struct { | ... | @@ -441,12 +557,12 @@ pub const CloseOperation = struct { |
| 441 | builtin.Os.linux, | 557 | builtin.Os.linux, |
| 442 | builtin.Os.macosx, | 558 | builtin.Os.macosx, |
| 443 | => { | 559 | => { |
| 444 | assert(self.have_fd); | 560 | assert(self.os_data.have_fd); |
| 445 | return self.close_req_node.data.msg.Close.fd; | 561 | return self.os_data.close_req_node.data.msg.Close.fd; |
| 446 | }, | 562 | }, |
| 447 | builtin.Os.windows, | 563 | builtin.Os.windows, |
| 448 | => { | 564 | => { |
| 449 | return self.handle.?; | 565 | return self.os_data.handle.?; |
| 450 | }, | 566 | }, |
| 451 | else => @compileError("Unsupported OS"), | 567 | else => @compileError("Unsupported OS"), |
| 452 | } | 568 | } |
| ... | @@ -949,15 +1065,15 @@ async fn testFsWatch(loop: *Loop) !void { | ... | @@ -949,15 +1065,15 @@ async fn testFsWatch(loop: *Loop) !void { |
| 949 | const read_contents = try await try async readFile(loop, file_path, 1024 * 1024); | 1065 | const read_contents = try await try async readFile(loop, file_path, 1024 * 1024); |
| 950 | assert(mem.eql(u8, read_contents, contents)); | 1066 | assert(mem.eql(u8, read_contents, contents)); |
| 951 | | 1067 | |
| 952 | // now watch the file | 1068 | //// now watch the file |
| 953 | var watch = try Watch(void).create(loop, 0); | 1069 | //var watch = try Watch(void).create(loop, 0); |
| 954 | defer watch.destroy(); | 1070 | //defer watch.destroy(); |
| 955 | | 1071 | |
| 956 | assert((try await try async watch.addFile(file_path, {})) == null); | 1072 | //assert((try await try async watch.addFile(file_path, {})) == null); |
| 957 | | 1073 | |
| 958 | const ev = try async watch.channel.get(); | 1074 | //const ev = try async watch.channel.get(); |
| 959 | var ev_consumed = false; | 1075 | //var ev_consumed = false; |
| 960 | defer if (!ev_consumed) cancel ev; | 1076 | //defer if (!ev_consumed) cancel ev; |
| 961 | | 1077 | |
| 962 | // overwrite line 2 | 1078 | // overwrite line 2 |
| 963 | const fd = try await try async openReadWrite(loop, file_path, os.File.default_mode); | 1079 | const fd = try await try async openReadWrite(loop, file_path, os.File.default_mode); |
| ... | @@ -967,11 +1083,11 @@ async fn testFsWatch(loop: *Loop) !void { | ... | @@ -967,11 +1083,11 @@ async fn testFsWatch(loop: *Loop) !void { |
| 967 | try await try async pwritev(loop, fd, []const []const u8{"lorem ipsum"}, line2_offset); | 1083 | try await try async pwritev(loop, fd, []const []const u8{"lorem ipsum"}, line2_offset); |
| 968 | } | 1084 | } |
| 969 | | 1085 | |
| 970 | ev_consumed = true; | 1086 | //ev_consumed = true; |
| 971 | switch ((try await ev).id) { | 1087 | //switch ((try await ev).id) { |
| 972 | WatchEventId.CloseWrite => {}, | 1088 | // WatchEventId.CloseWrite => {}, |
| 973 | WatchEventId.Delete => @panic("wrong event"), | 1089 | // WatchEventId.Delete => @panic("wrong event"), |
| 974 | } | 1090 | //} |
| 975 | | 1091 | |
| 976 | const contents_updated = try await try async readFile(loop, file_path, 1024 * 1024); | 1092 | const contents_updated = try await try async readFile(loop, file_path, 1024 * 1024); |
| 977 | assert(mem.eql(u8, contents_updated, | 1093 | assert(mem.eql(u8, contents_updated, |