mirror of
https://github.com/logos-storage/nim-chronos.git
synced 2026-01-09 08:53:08 +00:00
* exception tracking This PR adds minimal exception tracking to chronos, moving the goalpost one step further. In particular, it becomes invalid to raise exceptions from `callSoon` callbacks: this is critical for writing correct error handling because there's no reasonable way that a user of chronos can possibly _reason_ about exceptions coming out of there: the event loop will be in an indeterminite state when the loop is executing an _random_ callback. As expected, there are several issues in the error handling of chronos: in particular, it will end up in an inconsistent internal state whenever the selector loop operations fail, because the internal state update functions are not written in an exception-safe way. This PR turns this into a Defect, which probably is not the optimal way of handling things - expect more work to be done here. Some API have no way of reporting back errors to callers - for example, when something fails in the accept loop, there's not much it can do, and no way to report it back to the user of the API - this has been fixed with the new accept flow - the old one should be deprecated. Finally, there is information loss in the API: in composite operations like `poll` and `waitFor` there's no way to differentiate internal errors from user-level errors originating from callbacks. * store `CatchableError` in future * annotate proc's with correct raises information * `selectors2` to avoid non-CatchableError IOSelectorsException * `$` should never raise * remove unnecessary gcsafe annotations * fix exceptions leaking out of timer waits * fix some imports * functions must signal raising the union of all exceptions across all platforms to enable cross-platform code * switch to unittest2 * add `selectors2` which supercedes the std library version and fixes several exception handling issues in there * fixes * docs, platform-independent eh specifiers for some functions * add feature flag for strict exception mode also bump version to 3.0.0 - _most_ existing code should be compatible with this version of exception handling but some things might need fixing - callbacks, existing raises specifications etc. * fix AsyncCheck for non-void T
114 lines
4.2 KiB
Nim
114 lines
4.2 KiB
Nim
#
|
|
# Chronos SendFile
|
|
# (c) Copyright 2018-Present
|
|
# Status Research & Development GmbH
|
|
#
|
|
# Licensed under either of
|
|
# Apache License, version 2.0, (LICENSE-APACHEv2)
|
|
# MIT license (LICENSE-MIT)
|
|
|
|
## This module provides cross-platform wrapper for ``sendfile()`` syscall.
|
|
|
|
{.push raises: [Defect].}
|
|
|
|
when defined(nimdoc):
|
|
proc sendfile*(outfd, infd: int, offset: int, count: var int): int =
|
|
## Copies data between file descriptor ``infd`` and ``outfd``. Because this
|
|
## copying is done within the kernel, ``sendfile()`` is more efficient than
|
|
## the combination of ``read(2)`` and ``write(2)``, which would require
|
|
## transferring data to and from user space.
|
|
##
|
|
## ``infd`` should be a file descriptor opened for reading and
|
|
## ``outfd`` should be a descriptor opened for writing.
|
|
##
|
|
## The ``infd`` argument must correspond to a file which supports
|
|
## ``mmap(2)``-like operations (i.e., it cannot be a socket).
|
|
##
|
|
## ``offset`` the file offset from which ``sendfile()`` will start reading
|
|
## data from ``infd``.
|
|
##
|
|
## ``count`` is the number of bytes to copy between the file descriptors.
|
|
## On exit ``count`` will hold number of bytes actually transferred between
|
|
## file descriptors.
|
|
##
|
|
## If the transfer was successful, the number of bytes written to ``outfd``
|
|
## is stored in ``count``, and ``0`` returned. Note that a successful call to
|
|
## ``sendfile()`` may write fewer bytes than requested; the caller should
|
|
## be prepared to retry the call if there were unsent bytes.
|
|
##
|
|
## On error, ``-1`` is returned.
|
|
|
|
when defined(linux) or defined(android):
|
|
|
|
proc osSendFile*(outfd, infd: cint, offset: ptr int, count: int): int
|
|
{.importc: "sendfile", header: "<sys/sendfile.h>".}
|
|
|
|
proc sendfile*(outfd, infd: int, offset: int, count: var int): int =
|
|
var o = offset
|
|
result = osSendFile(cint(outfd), cint(infd), addr o, count)
|
|
if result >= 0:
|
|
count = result
|
|
result = 0
|
|
|
|
elif defined(freebsd) or defined(openbsd) or defined(netbsd) or
|
|
defined(dragonflybsd):
|
|
import posix, os
|
|
type
|
|
SendfileHeader* {.importc: "sf_hdtr",
|
|
header: """#include <sys/types.h>
|
|
#include <sys/socket.h>
|
|
#include <sys/uio.h>""",
|
|
pure, final.} = object
|
|
|
|
proc osSendFile*(outfd, infd: cint, offset: uint, size: uint,
|
|
hdtr: ptr SendfileHeader, sbytes: ptr uint,
|
|
flags: int): int {.importc: "sendfile",
|
|
header: """#include <sys/types.h>
|
|
#include <sys/socket.h>
|
|
#include <sys/uio.h>""".}
|
|
|
|
proc sendfile*(outfd, infd: int, offset: int, count: var int): int =
|
|
var o = 0'u
|
|
result = osSendFile(cint(infd), cint(outfd), uint(offset), uint(count), nil,
|
|
addr o, 0)
|
|
if result >= 0:
|
|
count = int(o)
|
|
result = 0
|
|
else:
|
|
let err = osLastError()
|
|
if int(err) == EAGAIN:
|
|
count = int(o)
|
|
result = 0
|
|
else:
|
|
result = -1
|
|
|
|
elif defined(macosx):
|
|
import posix, os
|
|
type
|
|
SendfileHeader* {.importc: "struct sf_hdtr",
|
|
header: """#include <sys/types.h>
|
|
#include <sys/socket.h>
|
|
#include <sys/uio.h>""",
|
|
pure, final.} = object
|
|
|
|
proc osSendFile*(fd, s: cint, offset: int, size: ptr int,
|
|
hdtr: ptr SendfileHeader,
|
|
flags: int): int {.importc: "sendfile",
|
|
header: """#include <sys/types.h>
|
|
#include <sys/socket.h>
|
|
#include <sys/uio.h>""".}
|
|
|
|
proc sendfile*(outfd, infd: int, offset: int, count: var int): int =
|
|
var o = count
|
|
result = osSendFile(cint(infd), cint(outfd), offset, addr o, nil, 0)
|
|
if result >= 0:
|
|
count = int(o)
|
|
result = 0
|
|
else:
|
|
let err = osLastError()
|
|
if int(err) == EAGAIN:
|
|
count = int(o)
|
|
result = 0
|
|
else:
|
|
result = -1
|