Ticket #366: fork_completion_callback.diff

File fork_completion_callback.diff, 3.8 KB (added by dmik, 7 years ago)
  • include/InnoTekLIBC/fork.h

     
    6060    /** The callback is only called in the parent context. */
    6161    __LIBC_FORK_CTX_PARENT = 1,
    6262    /** The callback is called in both parent and child contexts. */
    63     __LIBC_FORK_CTX_BOTH = 2
     63    __LIBC_FORK_CTX_BOTH = 2,
     64    /** The mask for the calling context values. */
     65    __LIBC_FORK_CTX_MASK = 0xf,
     66    /** Flag for pfnCompletionCallback to add a callback to the end of the
     67     * list so that it will be called last. */
     68    __LIBC_FORK_CTX_FLAGS_LAST = 0x1000
    6469} __LIBC_FORKCTX;
    6570
    6671/**
     
    379384     *                      a non zero rc argument indicates failure.
    380385     * @param   pvArg       Argument to pass to pfnCallback as 3rd argument.
    381386     * @param   enmContext  __LIBC_FORK_CTX_CHILD, __LIBC_FORK_CTX_PARENT, or
    382      *                      __LIBC_FORK_CTX_BOTH.
     387     *                      __LIBC_FORK_CTX_BOTH. May be ORed with
     388     *                      __LIBC_FORK_CTX_FLAGS_LAST to add the callback to
     389     *                      the end of the list so that it will be called after
     390     *                      all other registered callbacks.
    383391     *
    384392     * @remark  Use with care, the memory used to remember these is taken from the
    385393     *          fork buffer.
  • src/lib/sys/libcfork.c

     
    21992199 *                      a non zero rc argument indicates failure.
    22002200 * @param   pvArg       Argument to pass to pfnCallback as 3rd argument.
    22012201 * @param   enmContext  __LIBC_FORK_CTX_CHILD, __LIBC_FORK_CTX_PARENT, or
    2202  *                      __LIBC_FORK_CTX_BOTH.
     2202 *                      __LIBC_FORK_CTX_BOTH. May be ORed with
     2203 *                      __LIBC_FORK_CTX_FLAGS_LAST to add the callback to
     2204 *                      the end of the list so that it will be called after
     2205 *                      all other registered callbacks.
    22032206 *
    22042207 * @remark  Use with care, the memory used to remember these is taken from the
    22052208 *          fork buffer.
     
    22192222            LIBCLOG_RETURN_INT(rc);
    22202223    }
    22212224
     2225    int fLast = enmContext & __LIBC_FORK_CTX_FLAGS_LAST;
     2226    enmContext &= __LIBC_FORK_CTX_MASK;
     2227
    22222228    /*
    22232229     * Add it to the completion callbacks.
    22242230     * These are eating of the buffer from the end of it.
     
    22272233    pForkHandle->cbBufLeft  -= sizeof(__LIBC_FORKCOMPLETIONCALLBACK);
    22282234    pForkHandle->cCompletionCallbacks++;
    22292235    pForkHandle->papfnCompletionCallbacks--;
    2230     pForkHandle->papfnCompletionCallbacks->pfnCallback  = pfnCallback;
    2231     pForkHandle->papfnCompletionCallbacks->enmContext   = enmContext;
    2232     pForkHandle->papfnCompletionCallbacks->pvArg        = pvArg;
     2236    if (fLast)
     2237    {
     2238        /* Add to the tail of the list to have it executed last */
     2239        unsigned cCallbacks = pForkHandle->cCompletionCallbacks - 1;
     2240        memmove(pForkHandle->papfnCompletionCallbacks, pForkHandle->papfnCompletionCallbacks + 1,
     2241                sizeof(__LIBC_FORKCOMPLETIONCALLBACK) * cCallbacks);
     2242        pForkHandle->papfnCompletionCallbacks[cCallbacks].pfnCallback  = pfnCallback;
     2243        pForkHandle->papfnCompletionCallbacks[cCallbacks].enmContext   = enmContext;
     2244        pForkHandle->papfnCompletionCallbacks[cCallbacks].pvArg        = pvArg;
     2245    }
     2246    else
     2247    {
     2248        /* Add to the head of the list to have it executed first */
     2249        pForkHandle->papfnCompletionCallbacks->pfnCallback  = pfnCallback;
     2250        pForkHandle->papfnCompletionCallbacks->enmContext   = enmContext;
     2251        pForkHandle->papfnCompletionCallbacks->pvArg        = pvArg;
     2252    }
    22332253
    22342254    LIBCLOG_RETURN_INT(0);
    22352255}