• DOS mutex

    From Jcurtis@VERT to ALL on Wednesday, June 04, 2025 10:58:42
    I have a shared list. User code adds to it, interrupt code drains it.
    The user update sequence must be atomic, so the list can't be corrupted
    by interrupt code. Typical method is a cli/sti critical section around
    the user update sequence.

    But I avoid disabling interrupts when possible, and x86 xchg does the
    trick. Indirect addresssing using [bx] was the only way I could make it
    work. If anyone has a better trick, please advise.

    --- code works with Borland C++ 3.1 ---

    int data [] = {3,14,29,33,0,0,0,0,0}; /* list count in element [0] */
    int *w = data; /* do not disturb when [0] == 0 */

    void
    main ()
    {
    _DX = 0; /* list count == 0 means mutex */

    asm mov bx, w; /* get list count and set mutex */
    asm xchg dx, [bx]; /* with CPU interrupts inhibited */

    w[++_DX] = 55; /* add a new element to the list */

    asm mov bx, w; /* bx got trashed, load it again */
    asm xchg dx, [bx]; /* updates count and clears mutex */

    _AX = 0; /* stopping place for debugger */
    }

    * SLMR 2.1a *
    ---
    þ Synchronet þ Vertrauen þ Home of Synchronet þ [vert/cvs/bbs].synchro.net
  • From Jcurtis@VERT to ALL on Wednesday, June 04, 2025 17:36:51
    Re: DOS mutex
    By: Jcurtis to ALL on Wed Jun 04 2025 10:58 am

    If anyone has a better trick

    Looks like something was lost along the way:

    int w = data;

    should be

    int *w = data;

    The pointer indirection is what makes the [bx] two-instruction sequence necessary. If "w" was a regular variable, one asm instruction is enough.
    ---
    þ Synchronet þ Vertrauen þ Home of Synchronet þ [vert/cvs/bbs].synchro.net