[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH] Add errno property to condition objects
From: |
Mario Domenech Goulart |
Subject: |
[PATCH] Add errno property to condition objects |
Date: |
Sat, 06 Jan 2024 19:43:04 +0100 |
Hi,
The attached patch adds errno to condition objects produced by
procedures that change it.
Motivation for this patch: at the moment, it is difficult to write
race condition-free code in some cases. For example, trying to delete
a file that might not exist. The naive approach would be:
(when (file-exists? the-file)
(delete-file the-file))
This is subject to race conditions, as by the time delete-file is
called, the result of file-exists? might not be valid anymore (e.g.,
code in another thread or process might have deleted it). Another
approach would be trying to delete the file unconditionally and
checking errno:
(handle-exceptions exn
(unless (= (errno) errno/noent)
(signal exn))
(delete-file the-file))
This is also subject to race conditions, as by the time (errno) is
called, it might reflect the result of an error produced by a
procedure executed on another thread.
The attached patch addresses those use-cases by injecting errno into
condition objects at the time they are created, where errno is
guaranteed to have been set by procedures whose underlying C calls set
it, as units that provide those procedures are compiled with
disable-interrupts. With this, something like the following snippet
can be used for race-free code to delete files that might not exist:
(handle-exceptions exn
(unless (= (get-condition-property exn 'exn 'errno) errno/noent)
(signal exn))
(delete-file the-file))
The patch is rather large, but mostly straightforward. It basically
adds two new internal procedures, ##sys#error/errno and
##sys#signal-hook/errno (analogous to ##sys#error and ##sys#signal-hook,
respectively) which are now used by procedures which call
##sys#update-errno.
General notes about the availability of errno in condition objects
have been added to the documentation of modules which provide
procedures that update errno.
All the best.
Mario
--
http://parenteses.org/mario
0001-Add-errno-property-to-condition-objects.patch
Description: Text Data
- [PATCH] Add errno property to condition objects,
Mario Domenech Goulart <=