Olin – Defining a new operating primitive for event-driven services
Picking just the most fun aspect to reply to, because this is too long already :)<p>&gt; would this look? I&#x27;m interested by what you mean by this<p>One area UNIX fails at is a single global flat system interface, it makes evolutionary change difficult without introducing permanent &quot;variant&quot; system calls (e.g. openat()) that must be supported in tandem with the original interface, in addition to an automatic penalty for any innovation that might wish to add more, because it must be supported forever.<p>To skirt around this, one popular option in UNIX land is instead not to define any interface: just dump whatever magic is required into some ioctl() and claim it is device-specific, and so we end up with what an interface that isn&#x27;t quite guaranteed and definitely unofficial, but that&#x27;s okay because it&#x27;s driver-specific, but then a second driver comes along and adds a similar interface, and now we have 2 options for frobbing a particular device, with no obviously correct choice, and no guarantee frobbing will be supported that way forever. In other words, we got the worst of both worlds by trying to ignore impermanence.<p>(Just a side note, a real-life example of those ioctls might be e.g. how Linux used to support asking the filesystem driver for a mapping of unused sectors, or how you set or query the FAT filesystem&#x27;s DOS filesystem label)<p>UNIX exports APIs for all of this, and it&#x27;s mostly frozen in stone forever:<p>- Rewinding tape drives<p>- Configuring start&#x2F;stop bits of serial lines<p>- Filesystem operations with fixed semantics designed around a local storage device, with uid&#x2F;gid&#x2F;mode security, and with extended attributes and ACLs bolted on the side<p>- Multiple variants of file locking schemes that are all perfectly useless in multithreaded apps<p>- A security model that&#x27;s baked in at the API level (e.g. chown()).<p>Just taking the silly &#x27;rewinding tapes&#x27; example, planning an API for permanent compatibility with a problem space that 99.9% of people no longer have, rising to 100% at some point eventually, it&#x27;s easy in hindsight to see this interface should be modularized somehow, and preferably made entirely optional.<p>How that modularity looks is definitely up for debate.. looking at Windows for one approach, in COM (and ignoring its baroque useability!) handles export only one interface (IUnknown) whose sole method allows enumerating the contracts the handle (object) supports. For example here is DirectShow: <a href="https:&#x2F;&#x2F;docs.microsoft.com&#x2F;en-us&#x2F;windows&#x2F;desktop&#x2F;directshow&#x2F;alphabetical-list-of-directshow-interfaces" rel="nofollow">https:&#x2F;&#x2F;docs.microsoft.com&#x2F;en-us&#x2F;windows&#x2F;desktop&#x2F;directshow&#x2F;...</a> , see how a single input can implement IDvdControl, IAMTVTuner, both, or neither, and if neither are implemented, the handle is still useable via IPin and related interfaces that actually control how data flows from the input.<p>Mapping that to UNIX, a handle might have protocols like IUnixSecurity, ITapeOperations, IBraindeadLocking, ILinuxSpecificLocking, IStream, all or none etc., where a handle has no guaranteed fixed vocabularity perhaps except for the most fundamental expected anywhere that handle is found (in the case of a file, perhaps that is IStream).<p>By exporting the impermanence down to consumer code, accessing any particular interface becomes an explicit affair, because you must always request it upfront. You can still implement a media player program by wiring up some filter graph using a handle you received from someplace else, but disable the rewind and eject buttons if querying those interfaces failed -- this is literally how some stuff works on Windows:<p><pre><code>    handle = ask_user_for_media_input()
    wire_up_graph(handle)
    if(supported(handle, &quot;dvd player&quot;)) {
        enable_eject_button();
    }
</code></pre>
Meanwhile at the top of the stack, outside of absolutely having to support some core interfaces that can never go away, the cost of adding and retiring sundry features is reduced, as the reality of permanence that has always existed has been pushed down the stack, where end users can choose to accept or ignore it.<p>---<p>An ideal interface might resemble a predefined root handle or sentinel value with the ability to construct new handles bound to some protocol. Imagine a language like (but not quite) protocolbuffer services, where those read&#x2F;write&#x2F;open&#x2F;close&#x2F;sync calls were methods of a particular named interface. The IDL would output wrappers to speak it. Probably already you can see room for splitting &#x27;sync&#x27; out into some kind of IBufferedStream optional interface.<p>Maybe like:<p><pre><code>    #include &lt;os.h&gt;                             &#x2F;&#x2F; Invoke(handle, char *interface, void *in, void *out) -&gt; bool
                                                &#x2F;&#x2F; Close(handle) -&gt; void
    #include &lt;os&#x2F;interface.h&gt;                   &#x2F;&#x2F; GetInterface(handle, char *interface) -&gt; void *
                                                &#x2F;&#x2F; Require(handle, char *interface) -&gt; void *
    #include &lt;os&#x2F;services&#x2F;console.h&gt;            &#x2F;&#x2F; IF_CONSOLE, Console, Console_write
    #include &lt;os&#x2F;services&#x2F;filesystem.h&gt;         &#x2F;&#x2F; IF_FILESYSTEM, Filesystem, Filesystem_open, Filesystem_error
    #include &lt;os&#x2F;services&#x2F;stream.h&gt;             &#x2F;&#x2F; IF_STREAM, Stream_read, Stream_write
    #include &lt;os&#x2F;services&#x2F;buffered_stream.h&gt;    &#x2F;&#x2F; IF_BUFFERED_STREAM, BufferedStream_sync

    int main(void)
    {
        Filesystem fs = Require(NULL, IF_FILESYSTEM);
        Console console = Require(NULL, IF_CONSOLE);

        File *fp;
        if((fp = Filesystem_open(fs, &quot;&#x2F;etc&#x2F;passwd&quot;, O_RDWR)) == NULL) {
            Console_write(console, &quot;open failed: %s&quot;, Filesystem_error(fs));
            return 1;
        }

        if(File_write(fp, &quot;foo\n&quot;) == -1) {
            Console_write(console, &quot;write failed: %s&quot;, Filesystem_error(fs));
            return 1;
        }

        &#x2F;&#x2F; Optional. Underlying Invoke() will fail if interface absent.
        BufferedStream_sync((BufferedStream) fp);

        return 0;
    }

</code></pre>
A real killer would be other than simplifying that calling style, somehow unifying it with the calling convention for linked modules, so by replacing a handle you could direct an OS-level protocol to a library, or perhaps better, the act of calling an OS service caused an optional library to be loaded and bound to the handle you were returned, say in the case of an ancient ITapeDrive interface that&#x27;s long since stopped being a once-upon-a-time core feature.<p>But the main point from the example is probably the notion of an OS with only two core interfaces - Invoke() and Close()<p>Another place you could look for inspiration of this sort is Objective C -- a similar concept is built into its guts, and its dynamic dispatch msgsend() function is optimized to all hell to match!