The interruption is done by revoking the file descriptors (dupping a /dev/null into it) and sending the signal SIGUSR2 to the thread. Even if the dupping doesn't interrupt the file operation, SIGUSR2 does, and on restart it will find that the file descriptor now returns to /dev/null and return immediately. Triceps defines a SIGUSR2 handler that does nothing, but you can override it with a custom one.
For this to work, Triceps needs to know, which file descriptors are to be revoked, which is achieved by registering for tracking them in the TrieadOwner. To avoid revoking the unrelated descriptors, they must be unregistered before closing. The normal sequence is:
- open a file descriptor
- register it for tracking
- do the file operations
- unregister the file descriptor
- close it
The lowest-level calls deal with the raw tracking:
$to->trackFd($fd);
Register the file descriptor (obtained with a fileno() or such) for tracking. The repeated calls for the same descriptor have no effect.
$to->forgetFd($fd);
Unregister the file descriptor. If the descriptor is not registered, the call is ignored.
The next level of the API deals with the file handles, extracting the file descriptors from them as needed.
$to->track(FILE);
Get a file descriptor from the file handle and track it.
$to->forget(FILE);
Get a file descriptor from the file handle and unregister it.
$to->close(FILE);
Unegister the file handle's descriptor then close the file handle. It's a convenience wrapper, to make the unregistering easier to remember.
The correct sequence might be hard to follow if the code involves some dying and evals catching these deaths. To handle that, the next level of the API provides the automatic tracking by scope. The scoping is done with the class Triceps::TrackedFile.Which is probably easier to describe right here.
A TrackedFile object keeps a reference of a file handle and also knows of its file descriptor being tracked by the TrieadOwner (so yes, it has a reference to the TrieadOwner too). Until the TrackedFile is destroyed, the file handle in it will never have its reference count go to 0, so it will never be automatically closed and destroyed. And when the TrackedFile is destroyed, first it tells the TrieadOwner to forget the file descriptor and only then unreferences the file handle, preserving the correct sequence.
And the scope of the TrackedFile is controlled by the scope of a variable that keeps a reference to it. If it's a local/my variable, TrackedFile will be destroyed on leaving the block, if it's a field in an object, it will be destroyed when the object is destroyed or the field is reset. Basically, the usual Perl scope business in Triceps.
If you want to close the file handle before leaving the scope of TrackedFile, don't call close() on the file handle. Instead, call close on the TrackedFile:
$trf->close();
This will again properly untrack the file descriptor, and then close the file handle, and remember that it has been closed, so no seconds attempt at that will be done when the TrackedFile gets destroyed.
There also are a couple of getter methods on the TrackedFile:
$fd = $trf->fd();
Get the tracked file descriptor. If the file handle has been already closed, will return -1.
$fh = $trf->get();
Get the tracked file handle. If the file handle had already been closed, will confess.
Now we get back to the TrieadOwner.
$trf = $to->makeTrackedFile(FILE);
Create a TrackedFile object from a file handle.
$trf = $to->makeTrackedFileFd(FILE, $fd);
The more low-level way to construct a TrackedFile, with specifying the file descriptor as a separate explicit argument. makeTrackedFile() is pretty much a wrapper that calls fileno() on the file handle and then calls makeTrackedFileFd().
And the following methods combine the loading of a file descriptor from the App and tracking it by the TrieadOwner. They are the most typically used interface for passing around the file handles through the App.
($trf, $file) = $to->trackDupFile($name, $mode);
Load the dupped file handle from the App, create an IO::Handle file object from it according to the $mode (in either r/w/a/r+/w+/a+ or </>/>>/+</+>/+>> format), and make a TrackedFile from it. Returns a pair of the TrackedFile object and the created file handle. The App still keeps the original file descriptor. The $mode must be consistent with the original mode of the file stored into the App. $name is the name used to store the file descriptor into the App.
($trf, $file) = $to->trackGetFile($name, $mode);
Similar to trackDupFile() only the file descriptor is moved from the App, and the App forgets about it.
($trf, $file) = $to->trackDupSocket($name, $mode);
($trf, $file) = $to->trackGetSocket($name, $mode);
Similar to the File versions, only create a file handle object of the class IO::Socket::INET.
($trf, $file) = $to->trackDupClass($name, $mode, $class);
($trf, $file) = $to->trackGetClass($name, $mode, $class);
Similar to the File versions, only creates the file handle of an arbitrary subclass of IO::Handle (with the name specified in $class). So for example if you ever want to load an IPv6 or Unix domain socket, these are the methods to use.
No comments:
Post a Comment