VirtualBox

Changeset 3184 in kBuild for trunk/src/lib/nt


Ignore:
Timestamp:
Mar 23, 2018 10:36:43 PM (7 years ago)
Author:
bird
Message:

kFsCache,dir-nt-bird: Added locking to the cache to make it accessible from winchildren worker threads.

Location:
trunk/src/lib/nt
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/lib/nt/kFsCache.c

    r3082 r3184  
    577577    {
    578578        KU8 *pbExtra = (KU8 *)pObj + cbObj;
     579
     580        KFSCACHE_LOCK(pCache); /** @todo reduce the amount of work done holding the lock */
    579581
    580582        pCache->cbObjects += cbObj + cbNames;
     
    662664            pDirObj->fPopulated         = K_FALSE;
    663665        }
     666
     667        KFSCACHE_UNLOCK(pCache);
    664668    }
    665669    else
     
    710714#endif
    711715        {
     716            /* No locking needed here, kFsCacheCreateObject takes care of that. */
    712717            return kFsCacheCreateObject(pCache, pParent,
    713718                                        szName, cchName, pwszName, cwcName,
     
    17291734{
    17301735    KFSLOOKUPERROR enmIgnored;
     1736    KBOOL          fRet;
     1737    KFSCACHE_LOCK(pCache);
    17311738    if (   pDir->fPopulated
    17321739        && !pDir->fNeedRePopulating
    17331740        && (   pDir->Obj.uCacheGen == KFSOBJ_CACHE_GEN_IGNORE
    17341741            || pDir->Obj.uCacheGen == pCache->auGenerations[pDir->Obj.fFlags & KFSOBJ_F_USE_CUSTOM_GEN]) )
    1735         return K_TRUE;
    1736     return kFsCachePopuplateOrRefreshDir(pCache, pDir, penmError ? penmError : &enmIgnored);
     1742        fRet = K_TRUE;
     1743    else
     1744        fRet = kFsCachePopuplateOrRefreshDir(pCache, pDir, penmError ? penmError : &enmIgnored);
     1745    KFSCACHE_UNLOCK(pCache);
     1746    return fRet;
    17371747}
    17381748
     
    27602770    if (ppLastAncestor)
    27612771        *ppLastAncestor = NULL;
     2772    KFSCACHE_LOCK(pCache);
    27622773    for (;;)
    27632774    {
     
    27982809            if (ppLastAncestor)
    27992810                *ppLastAncestor = kFsCacheObjRetainInternal(&pParent->Obj);
     2811            KFSCACHE_UNLOCK(pCache);
    28002812            return NULL;
    28012813        }
     
    28182830            {
    28192831                if (pChild)
    2820                     return kFsCacheObjRetainInternal(pChild);
     2832                {
     2833                    kFsCacheObjRetainInternal(pChild);
     2834                    KFSCACHE_UNLOCK(pCache);
     2835                    return pChild;
     2836                }
    28212837                *penmError = KFSLOOKUPERROR_NOT_FOUND;
    28222838            }
     
    28252841            if (ppLastAncestor)
    28262842                *ppLastAncestor = kFsCacheObjRetainInternal(&pParent->Obj);
     2843            KFSCACHE_UNLOCK(pCache);
    28272844            return NULL;
    28282845        }
     
    28432860                if (ppLastAncestor)
    28442861                    *ppLastAncestor = kFsCacheObjRetainInternal(&pParent->Obj);
     2862                KFSCACHE_UNLOCK(pCache);
    28452863                return NULL;
    28462864            }
    2847             return kFsCacheObjRetainInternal(pChild);
     2865            kFsCacheObjRetainInternal(pChild);
     2866            KFSCACHE_UNLOCK(pCache);
     2867            return pChild;
    28482868        }
    28492869
     
    28592879            if (ppLastAncestor)
    28602880                *ppLastAncestor = kFsCacheObjRetainInternal(&pParent->Obj);
     2881            KFSCACHE_UNLOCK(pCache);
    28612882            return NULL;
    28622883        }
     
    28682889            if (ppLastAncestor)
    28692890                *ppLastAncestor = kFsCacheObjRetainInternal(&pParent->Obj);
     2891            KFSCACHE_UNLOCK(pCache);
    28702892            return NULL;
    28712893        }
     
    28762898            if (ppLastAncestor)
    28772899                *ppLastAncestor = kFsCacheObjRetainInternal(&pParent->Obj);
     2900            KFSCACHE_UNLOCK(pCache);
    28782901            return NULL;
    28792902        }
     
    28812904
    28822905    /* not reached */
     2906    KFSCACHE_UNLOCK(pCache);
    28832907    return NULL;
    28842908}
     
    29142938    if (ppLastAncestor)
    29152939        *ppLastAncestor = NULL;
     2940    KFSCACHE_LOCK(pCache);
    29162941    for (;;)
    29172942    {
     
    29522977            if (ppLastAncestor)
    29532978                *ppLastAncestor = kFsCacheObjRetainInternal(&pParent->Obj);
     2979            KFSCACHE_UNLOCK(pCache);
    29542980            return NULL;
    29552981        }
     
    29722998            {
    29732999                if (pChild)
    2974                     return kFsCacheObjRetainInternal(pChild);
     3000                {
     3001                    kFsCacheObjRetainInternal(pChild);
     3002                    KFSCACHE_UNLOCK(pCache);
     3003                    return pChild;
     3004                }
    29753005                *penmError = KFSLOOKUPERROR_NOT_FOUND;
    29763006            }
     
    29793009            if (ppLastAncestor)
    29803010                *ppLastAncestor = kFsCacheObjRetainInternal(&pParent->Obj);
     3011            KFSCACHE_UNLOCK(pCache);
    29813012            return NULL;
    29823013        }
     
    29973028                if (ppLastAncestor)
    29983029                    *ppLastAncestor = kFsCacheObjRetainInternal(&pParent->Obj);
     3030                KFSCACHE_UNLOCK(pCache);
    29993031                return NULL;
    30003032            }
    3001             return kFsCacheObjRetainInternal(pChild);
     3033            kFsCacheObjRetainInternal(pChild);
     3034            KFSCACHE_UNLOCK(pCache);
     3035            return pChild;
    30023036        }
    30033037
     
    30133047            if (ppLastAncestor)
    30143048                *ppLastAncestor = kFsCacheObjRetainInternal(&pParent->Obj);
     3049            KFSCACHE_UNLOCK(pCache);
    30153050            return NULL;
    30163051        }
     
    30233058            if (ppLastAncestor)
    30243059                *ppLastAncestor = kFsCacheObjRetainInternal(&pParent->Obj);
     3060            KFSCACHE_UNLOCK(pCache);
    30253061            return NULL;
    30263062        }
     
    30313067            if (ppLastAncestor)
    30323068                *ppLastAncestor = kFsCacheObjRetainInternal(&pParent->Obj);
     3069            KFSCACHE_UNLOCK(pCache);
    30333070            return NULL;
    30343071        }
    30353072    }
    30363073
     3074    KFSCACHE_UNLOCK(pCache);
    30373075    return NULL;
    30383076}
     
    36773715PKFSOBJ kFsCacheLookupA(PKFSCACHE pCache, const char *pszPath, KFSLOOKUPERROR *penmError)
    36783716{
    3679     KU32 uHashPath;
    3680     KU32 cchPath = (KU32)kFsCacheStrHashEx(pszPath, &uHashPath);
    3681     return kFsCacheLookupHashedA(pCache, pszPath, cchPath, uHashPath, penmError);
     3717    KU32    uHashPath;
     3718    KU32    cchPath = (KU32)kFsCacheStrHashEx(pszPath, &uHashPath);
     3719    PKFSOBJ pObj;
     3720    KFSCACHE_LOCK(pCache);
     3721    pObj = kFsCacheLookupHashedA(pCache, pszPath, cchPath, uHashPath, penmError);
     3722    KFSCACHE_UNLOCK(pCache);
     3723    return pObj;
    36823724}
    36833725
     
    37033745PKFSOBJ kFsCacheLookupW(PKFSCACHE pCache, const wchar_t *pwszPath, KFSLOOKUPERROR *penmError)
    37043746{
    3705     KU32 uHashPath;
    3706     KU32 cwcPath = (KU32)kFsCacheUtf16HashEx(pwszPath, &uHashPath);
    3707     return kFsCacheLookupHashedW(pCache, pwszPath, cwcPath, uHashPath, penmError);
     3747    KU32    uHashPath;
     3748    KU32    cwcPath = (KU32)kFsCacheUtf16HashEx(pwszPath, &uHashPath);
     3749    PKFSOBJ pObj;
     3750    KFSCACHE_LOCK(pCache);
     3751    pObj = kFsCacheLookupHashedW(pCache, pwszPath, cwcPath, uHashPath, penmError);
     3752    KFSCACHE_UNLOCK(pCache);
     3753    return pObj;
    37083754}
    37093755
     
    37313777PKFSOBJ kFsCacheLookupWithLengthA(PKFSCACHE pCache, const char *pchPath, KSIZE cchPath, KFSLOOKUPERROR *penmError)
    37323778{
    3733     return kFsCacheLookupHashedA(pCache, pchPath, (KU32)cchPath, kFsCacheStrHashN(pchPath, cchPath), penmError);
     3779    KU32    uHashPath = kFsCacheStrHashN(pchPath, cchPath);
     3780    PKFSOBJ pObj;
     3781    KFSCACHE_LOCK(pCache);
     3782    pObj = kFsCacheLookupHashedA(pCache, pchPath, (KU32)cchPath, uHashPath, penmError);
     3783    KFSCACHE_UNLOCK(pCache);
     3784    return pObj;
    37343785}
    37353786
     
    37573808PKFSOBJ kFsCacheLookupWithLengthW(PKFSCACHE pCache, const wchar_t *pwcPath, KSIZE cwcPath, KFSLOOKUPERROR *penmError)
    37583809{
    3759     return kFsCacheLookupHashedW(pCache, pwcPath, (KU32)cwcPath, kFsCacheUtf16HashN(pwcPath, cwcPath), penmError);
     3810    KU32    uHashPath = kFsCacheUtf16HashN(pwcPath, cwcPath);
     3811    PKFSOBJ pObj;
     3812    KFSCACHE_LOCK(pCache);
     3813    pObj = kFsCacheLookupHashedW(pCache, pwcPath, (KU32)cwcPath, uHashPath, penmError);
     3814    KFSCACHE_UNLOCK(pCache);
     3815    return pObj;
    37603816}
    37613817
     
    37753831PKFSOBJ kFsCacheLookupNoMissingA(PKFSCACHE pCache, const char *pszPath, KFSLOOKUPERROR *penmError)
    37763832{
    3777     PKFSOBJ pObj = kFsCacheLookupA(pCache, pszPath, penmError);
     3833    PKFSOBJ pObj;
     3834    KFSCACHE_LOCK(pCache); /* probably not necessary */
     3835    pObj = kFsCacheLookupA(pCache, pszPath, penmError);
    37783836    if (pObj)
    37793837    {
    37803838        if (pObj->bObjType != KFSOBJ_TYPE_MISSING)
     3839        {
     3840            KFSCACHE_UNLOCK(pCache);
    37813841            return pObj;
     3842        }
    37823843
    37833844        kFsCacheObjRelease(pCache, pObj);
    37843845        *penmError = KFSLOOKUPERROR_NOT_FOUND;
    37853846    }
     3847    KFSCACHE_UNLOCK(pCache);
    37863848    return NULL;
    37873849}
     
    38023864PKFSOBJ kFsCacheLookupNoMissingW(PKFSCACHE pCache, const wchar_t *pwszPath, KFSLOOKUPERROR *penmError)
    38033865{
    3804     PKFSOBJ pObj = kFsCacheLookupW(pCache, pwszPath, penmError);
     3866    PKFSOBJ pObj;
     3867    KFSCACHE_LOCK(pCache); /* probably not necessary */
     3868    pObj = kFsCacheLookupW(pCache, pwszPath, penmError);
    38053869    if (pObj)
    38063870    {
    38073871        if (pObj->bObjType != KFSOBJ_TYPE_MISSING)
     3872        {
     3873            KFSCACHE_UNLOCK(pCache);
    38083874            return pObj;
     3875        }
    38093876
    38103877        kFsCacheObjRelease(pCache, pObj);
    38113878        *penmError = KFSLOOKUPERROR_NOT_FOUND;
    38123879    }
     3880    KFSCACHE_UNLOCK(pCache);
    38133881    return NULL;
    38143882}
     
    38283896    kHlpAssert(pObj->pParent == NULL);
    38293897    kHlpAssert(pObj->u32Magic == KFSOBJ_MAGIC);
     3898    KFSCACHE_LOCK(pCache);
    38303899
    38313900    KFSCACHE_LOG(("Destroying %s/%s, type=%d, pObj=%p, pszWhere=%s\n",
     
    38913960
    38923961        default:
     3962            KFSCACHE_UNLOCK(pCache);
    38933963            return 0;
    38943964    }
     
    39183988    }
    39193989
     3990    KFSCACHE_UNLOCK(pCache);
     3991
    39203992    kHlpFree(pObj);
    39213993    return 0;
     
    39394011        kHlpAssert(pObj->u32Magic == KFSOBJ_MAGIC);
    39404012
    3941         cRefs = --pObj->cRefs;
     4013        cRefs = _InterlockedDecrement(&pObj->cRefs);
    39424014        if (cRefs)
    39434015            return cRefs;
     
    39644036        kHlpAssert(pObj->u32Magic == KFSOBJ_MAGIC);
    39654037
    3966         cRefs = --pObj->cRefs;
     4038        cRefs = _InterlockedDecrement(&pObj->cRefs);
    39674039        if (cRefs)
    39684040            return cRefs;
     
    39854057    kHlpAssert(pObj->u32Magic == KFSOBJ_MAGIC);
    39864058
    3987     cRefs = ++pObj->cRefs;
     4059    cRefs = _InterlockedIncrement(&pObj->cRefs);
    39884060    kHlpAssert(cRefs < 16384);
    39894061    return cRefs;
     
    40084080{
    40094081    kHlpAssert(cbUserData >= sizeof(*pNew));
     4082    KFSCACHE_LOCK(pCache);
     4083
    40104084    if (kFsCacheObjGetUserData(pCache, pObj, uKey) == NULL)
    40114085    {
     
    40174091            pNew->pNext         = pObj->pUserDataHead;
    40184092            pObj->pUserDataHead = pNew;
     4093            KFSCACHE_UNLOCK(pCache);
    40194094            return pNew;
    40204095        }
    40214096    }
    40224097
     4098    KFSCACHE_UNLOCK(pCache);
    40234099    return NULL;
    40244100}
     
    40394115    kHlpAssert(pCache->u32Magic == KFSCACHE_MAGIC);
    40404116    kHlpAssert(pObj->u32Magic == KFSOBJ_MAGIC);
     4117    KFSCACHE_LOCK(pCache);
    40414118
    40424119    for (pCur = pObj->pUserDataHead; pCur; pCur = pCur->pNext)
    40434120        if (pCur->uKey == uKey)
     4121        {
     4122            KFSCACHE_UNLOCK(pCache);
    40444123            return pCur;
     4124        }
     4125
     4126    KFSCACHE_UNLOCK(pCache);
    40454127    return NULL;
    40464128}
     
    40584140KBOOL kFsCacheObjGetFullPathA(PKFSOBJ pObj, char *pszPath, KSIZE cbPath, char chSlash)
    40594141{
     4142    /** @todo No way of to do locking here w/o pCache parameter; need to verify
     4143     *        that we're only access static data! */
    40604144    KSIZE off = pObj->cchParent;
    40614145    kHlpAssert(pObj->u32Magic == KFSOBJ_MAGIC);
     
    41124196KBOOL kFsCacheObjGetFullPathW(PKFSOBJ pObj, wchar_t *pwszPath, KSIZE cwcPath, wchar_t wcSlash)
    41134197{
     4198    /** @todo No way of to do locking here w/o pCache parameter; need to verify
     4199     *        that we're only access static data! */
    41144200    KSIZE off = pObj->cwcParent;
    41154201    kHlpAssert(pObj->u32Magic == KFSOBJ_MAGIC);
     
    41684254KBOOL kFsCacheObjGetFullShortPathA(PKFSOBJ pObj, char *pszPath, KSIZE cbPath, char chSlash)
    41694255{
     4256    /** @todo No way of to do locking here w/o pCache parameter; need to verify
     4257     *        that we're only access static data! */
    41704258    KSIZE off = pObj->cchShortParent;
    41714259    kHlpAssert(pObj->u32Magic == KFSOBJ_MAGIC);
     
    42224310KBOOL kFsCacheObjGetFullShortPathW(PKFSOBJ pObj, wchar_t *pwszPath, KSIZE cwcPath, wchar_t wcSlash)
    42234311{
     4312    /** @todo No way of to do locking here w/o pCache parameter; need to verify
     4313     *        that we're only access static data! */
    42244314    KSIZE off = pObj->cwcShortParent;
    42254315    kHlpAssert(pObj->u32Magic == KFSOBJ_MAGIC);
     
    43034393    UniStr.MaximumLength = UniStr.Length + sizeof(wchar_t);
    43044394
     4395/** @todo potential race against kFsCacheInvalidateDeletedDirectoryA   */
    43054396    MyInitializeObjectAttributes(&ObjAttr, &UniStr, OBJ_CASE_INSENSITIVE, pFileObj->pParent->hDir, NULL /*pSecAttr*/);
    43064397
     
    43544445{
    43554446    kHlpAssert(pCache->u32Magic == KFSOBJ_MAGIC);
     4447    KFSCACHE_LOCK(pCache);
     4448
    43564449    pCache->auGenerationsMissing[0]++;
    43574450    kHlpAssert(pCache->uGenerationMissing < KU32_MAX);
     4451
    43584452    KFSCACHE_LOG(("Invalidate missing %#x\n", pCache->auGenerationsMissing[0]));
     4453    KFSCACHE_UNLOCK(pCache);
    43594454}
    43604455
     
    43684463{
    43694464    kHlpAssert(pCache->u32Magic == KFSOBJ_MAGIC);
     4465    KFSCACHE_LOCK(pCache);
    43704466
    43714467    pCache->auGenerationsMissing[0]++;
     
    43824478                  pCache->auGenerationsMissing[0], pCache->auGenerations[0],
    43834479                  pCache->auGenerationsMissing[1], pCache->auGenerations[1]));
     4480    KFSCACHE_UNLOCK(pCache);
    43844481}
    43854482
     
    43944491{
    43954492    kHlpAssert(pCache->u32Magic == KFSOBJ_MAGIC);
     4493    KFSCACHE_LOCK(pCache);
     4494
    43964495    pCache->auGenerationsMissing[1]++;
    43974496    kHlpAssert(pCache->auGenerationsMissing[1] < KU32_MAX);
     4497
    43984498    KFSCACHE_LOG(("Invalidate missing custom %#x\n", pCache->auGenerationsMissing[1]));
     4499    KFSCACHE_UNLOCK(pCache);
    43994500}
    44004501
     
    44104511{
    44114512    kHlpAssert(pCache->u32Magic == KFSOBJ_MAGIC);
     4513    KFSCACHE_LOCK(pCache);
     4514
    44124515    pCache->auGenerations[1]++;
    44134516    kHlpAssert(pCache->auGenerations[1] < KU32_MAX);
    44144517    pCache->auGenerationsMissing[1]++;
    44154518    kHlpAssert(pCache->auGenerationsMissing[1] < KU32_MAX);
     4519
    44164520    KFSCACHE_LOG(("Invalidate both custom %#x/%#x\n", pCache->auGenerationsMissing[1], pCache->auGenerations[1]));
     4521    KFSCACHE_UNLOCK(pCache);
    44174522}
    44184523
     
    44604565    if (pRoot)
    44614566    {
     4567        KFSCACHE_LOCK(pCache);
    44624568        if (pRoot->bObjType == KFSOBJ_TYPE_DIR)
    44634569            kFsCacheApplyFlagsToTree((PKFSDIR)pRoot, KU32_MAX, KFSOBJ_F_USE_CUSTOM_GEN);
    44644570        else
    44654571            pRoot->fFlags |= KFSOBJ_F_USE_CUSTOM_GEN;
     4572        KFSCACHE_UNLOCK(pCache);
    44664573        return K_TRUE;
    44674574    }
     
    44824589    KFSLOOKUPERROR  enmError;
    44834590    PKFSOBJ         pFsObj;
     4591
     4592    KFSCACHE_LOCK(pCache);
    44844593
    44854594    /* Is absolute without any '..' bits? */
     
    45124621                pDir->fNeedRePopulating = K_TRUE;
    45134622                pDir->Obj.uCacheGen = pCache->auGenerations[pDir->Obj.fFlags & KFSOBJ_F_USE_CUSTOM_GEN] - 1;
    4514                 kFsCacheObjRetainInternal(&pDir->Obj);
     4623                kFsCacheObjRelease(pCache, &pDir->Obj);
     4624                KFSCACHE_UNLOCK(pCache);
    45154625                return K_TRUE;
    45164626            }
     
    45204630            KFSCACHE_LOG(("kFsCacheInvalidateDeletedDirectoryA: Trying to invalidate a non-directory: bObjType=%d %s\n",
    45214631                          pFsObj->bObjType, pszDir));
    4522         kFsCacheObjRetainInternal(pFsObj);
     4632        kFsCacheObjRelease(pCache, pFsObj);
    45234633    }
    45244634    else
    45254635        KFSCACHE_LOG(("kFsCacheInvalidateDeletedDirectoryA: '%s' was not found\n", pszDir));
     4636    KFSCACHE_UNLOCK(pCache);
    45264637    return K_FALSE;
    45274638}
     
    45984709            pCache->cbUtf16Paths            = 0;
    45994710#endif
     4711
     4712#ifdef KFSCACHE_CFG_LOCKING
     4713            InitializeCriticalSection(&pCache->u.CritSect);
     4714#endif
    46004715            return pCache;
    46014716        }
  • trunk/src/lib/nt/kFsCache.h

    r2967 r3184  
    5555/** The max UTF-16 name length. */
    5656#define KFSCACHE_CFG_MAX_UTF16_NAME         (256*2 + 16)
     57/** Enables locking of the cache and thereby making it thread safe. */
     58#define KFSCACHE_CFG_LOCKING                1
    5759
    5860
     
    372374    PKFSOBJ             pFsObj;
    373375} KFSHASHW;
     376#endif
     377
     378
     379/** @def KFSCACHE_LOCK
     380 *  Locks the cache exclusively. */
     381/** @def KFSCACHE_UNLOCK
     382 *  Counterpart to KFSCACHE_LOCK. */
     383#ifdef KFSCACHE_CFG_LOCKING
     384# define KFSCACHE_LOCK(a_pCache)        EnterCriticalSection(&(a_pCache)->u.CritSect)
     385# define KFSCACHE_UNLOCK(a_pCache)      LeaveCriticalSection(&(a_pCache)->u.CritSect)
     386#else
     387# define KFSCACHE_LOCK(a_pCache)        do { } while (0)
     388# define KFSCACHE_UNLOCK(a_pCache)      do { } while (0)
    374389#endif
    375390
     
    440455    KFSDIR              RootDir;
    441456
     457#ifdef KFSCACHE_CFG_LOCKING
     458    /** Critical section protecting the cache. */
     459    union
     460    {
     461# ifdef _WINBASE_
     462        CRITICAL_SECTION    CritSect;
     463# endif
     464        KU64                abPadding[2 * 4 + 4 * sizeof(void *)];
     465    } u;
     466#endif
     467
    442468    /** File system hash table for ANSI filename strings. */
    443469    PKFSHASHA           apAnsiPaths[KFSCACHE_CFG_PATH_HASH_TAB_SIZE];
Note: See TracChangeset for help on using the changeset viewer.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette