Ignore:
Timestamp:
Mar 21, 2018, 3:21:23 PM (7 years ago)
Author:
bird
Message:

kmk/win: Make outsource the writing part of kmk_builtin_append to a worker thread to try avoid blocking the main kmk thread.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/kmk/w32/winchildren.c

    r3169 r3172  
    110110    /** kmkbuiltin command. */
    111111    WINCHILDTYPE_BUILT_IN,
     112    /** kmkbuiltin_append result write out. */
     113    WINCHILDTYPE_APPEND,
    112114    /** kSubmit job. */
    113115    WINCHILDTYPE_SUBMIT,
     
    188190            char          **papszEnv;
    189191        } BuiltIn;
     192
     193        /** Data for WINCHILDTYPE_APPEND.   */
     194        struct
     195        {
     196            /** The filename. */
     197            char           *pszFilename;
     198            /** How much to append. */
     199            size_t          cbAppend;
     200            /** What to append. */
     201            char           *pszAppend;
     202            /** Whether to truncate the file. */
     203            int             fTruncate;
     204        } Append;
    190205
    191206        /** Data for WINCHILDTYPE_SUBMIT.   */
     
    16721687
    16731688/**
     1689 * Childcare worker: handle append write-out.
     1690 *
     1691 * @param   pWorker             The worker.
     1692 * @param   pChild              The kSubmit child.
     1693 */
     1694static void mkWinChildcareWorkerThreadHandleAppend(PWINCHILDCAREWORKER pWorker, PWINCHILD pChild)
     1695{
     1696    int fd = open(pChild->u.Append.pszFilename,
     1697                  pChild->u.Append.fTruncate
     1698                  ? O_WRONLY | O_TRUNC  | O_CREAT | _O_NOINHERIT | _O_BINARY
     1699                  : O_WRONLY | O_APPEND | O_CREAT | _O_NOINHERIT | _O_BINARY,
     1700                  0666);
     1701    if (fd >= 0)
     1702    {
     1703        ssize_t cbWritten = write(fd, pChild->u.Append.pszAppend, pChild->u.Append.cbAppend);
     1704        if (cbWritten == (ssize_t)pChild->u.Append.cbAppend)
     1705        {
     1706            if (close(fd) >= 0)
     1707            {
     1708                pChild->iExitCode = 0;
     1709                return;
     1710            }
     1711            fprintf(stderr, "kmk_builtin_append: close failed on '%s': %u (%s)\n",
     1712                    pChild->u.Append.pszFilename, errno, strerror(errno));
     1713        }
     1714        else
     1715            fprintf(stderr, "kmk_builtin_append: error writing %lu bytes to on '%s': %u (%s)\n",
     1716                    pChild->u.Append.cbAppend, pChild->u.Append.pszFilename, errno, strerror(errno));
     1717        close(fd);
     1718    }
     1719    else
     1720        fprintf(stderr, "kmk_builtin_append: error opening '%s': %u (%s)\n",
     1721                pChild->u.Append.pszFilename, errno, strerror(errno));
     1722    pChild->iExitCode = 1;
     1723}
     1724
     1725/**
    16741726 * Childcare worker: handle kSubmit job.
    16751727 *
     
    17841836                    case WINCHILDTYPE_BUILT_IN:
    17851837                        mkWinChildcareWorkerThreadHandleBuiltIn(pWorker, pChild);
     1838                        break;
     1839                    case WINCHILDTYPE_APPEND:
     1840                        mkWinChildcareWorkerThreadHandleAppend(pWorker, pChild);
    17861841                        break;
    17871842                    case WINCHILDTYPE_SUBMIT:
     
    20042059            break;
    20052060
     2061        case WINCHILDTYPE_APPEND:
     2062            if (pChild->u.Append.pszFilename)
     2063            {
     2064                free(pChild->u.Append.pszFilename);
     2065                pChild->u.Append.pszFilename = NULL;
     2066            }
     2067            if (pChild->u.Append.pszAppend)
     2068            {
     2069                free(pChild->u.Append.pszAppend);
     2070                pChild->u.Append.pszAppend = NULL;
     2071            }
     2072            break;
     2073
    20062074        case WINCHILDTYPE_SUBMIT:
    20072075            if (pChild->u.Submit.pvSubmitWorker)
     
    22832351
    22842352/**
     2353 * Interface used by append.c for do the slow file system part.
     2354 *
     2355 * This will append the given buffer to the specified file and free the buffer.
     2356 *
     2357 * @returns 0 on success, windows status code on failure.
     2358 *
     2359 * @param   pszFilename     The name of the file to append to.
     2360 * @param   ppszAppend      What to append.  The pointer pointed to is set to
     2361 *                          NULL once we've taken ownership of the buffer and
     2362 *                          promise to free it.
     2363 * @param   cbAppend        How much to append.
     2364 * @param   fTruncate       Whether to truncate the file before appending to it.
     2365 * @param   pMkChild        The make child structure.
     2366 * @param   pPid            Where to return the pid.
     2367 */
     2368int MkWinChildCreateAppend(const char *pszFilename, char **ppszAppend, size_t cbAppend, int fTruncate,
     2369                           struct child *pMkChild, pid_t *pPid)
     2370{
     2371    size_t    cbFilename = strlen(pszFilename) + 1;
     2372    PWINCHILD pChild     = mkWinChildNew(WINCHILDTYPE_APPEND);
     2373    pChild->pMkChild            = pMkChild;
     2374    pChild->u.Append.fTruncate  = fTruncate;
     2375    pChild->u.Append.pszAppend  = *ppszAppend;
     2376    pChild->u.Append.cbAppend   = cbAppend;
     2377    pChild->u.Append.pszFilename = (char *)memcpy(xmalloc(cbFilename), pszFilename, cbFilename);
     2378    *ppszAppend = NULL;
     2379    return mkWinChildPushToCareWorker(pChild, pPid);
     2380}
     2381
     2382/**
    22852383 * Interface used by kSubmit.c for registering stuff to wait on.
    22862384 *
     
    23372435                    pChild->iSignal = iSignal;
    23382436                    break;
    2339 
    23402437#ifdef KMK
    2341 
    23422438                case WINCHILDTYPE_SUBMIT:
    23432439                {
     
    23562452
    23572453#endif /* KMK */
    2358 
    23592454                default:
    23602455                    assert(0);
     
    24182513#ifdef KMK
    24192514        case WINCHILDTYPE_BUILT_IN:
    2420             break;
     2515        case WINCHILDTYPE_APPEND:
    24212516        case WINCHILDTYPE_SUBMIT:
    2422             break;
    24232517        case WINCHILDTYPE_REDIRECT:
    24242518            break;
Note: See TracChangeset for help on using the changeset viewer.