Ignore:
Timestamp:
Dec 1, 2008, 6:19:35 AM (16 years ago)
Author:
[email protected]
Message:

2008-12-01 David Levin <[email protected]>

Reviewed by Alexey Proskuryakov.

https://bugs.webkit.org/show_bug.cgi?id=22567
Make HashTable work as expected with respect to threads. Specifically, it has class-level
thread safety and constant methods work on constant objects without synchronization.


No observable change in behavior, so no test.

  • wtf/HashTable.cpp: (WTF::hashTableStatsMutex): (WTF::HashTableStats::~HashTableStats): (WTF::HashTableStats::recordCollisionAtCount): Guarded variable access with a mutex.
  • wtf/HashTable.h: (WTF::::lookup): (WTF::::lookupForWriting): (WTF::::fullLookupForWriting): (WTF::::add): (WTF::::reinsert): (WTF::::remove): (WTF::::rehash): Changed increments of static variables to use atomicIncrement.

(WTF::::invalidateIterators):
(WTF::addIterator):
(WTF::removeIterator):
Guarded mutable access with a mutex.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/JavaScriptCore/wtf/HashTable.h

    r35900 r38859  
    11/*
    22 * Copyright (C) 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
     3 * Copyright (C) 2008 David Levin <[email protected]>
    34 *
    45 * This library is free software; you can redistribute it and/or
     
    2526#include "HashTraits.h"
    2627#include <wtf/Assertions.h>
     28#include <wtf/Threading.h>
    2729
    2830namespace WTF {
     
    4345    struct HashTableStats {
    4446        ~HashTableStats();
     47        // All of the variables are accessed in ~HashTableStats when the static struct is destroyed.
     48
     49        // The following variables are all atomically incremented when modified.
    4550        static int numAccesses;
    46         static int numCollisions;
    47         static int collisionGraph[4096];
    48         static int maxCollisions;
    4951        static int numRehashes;
    5052        static int numRemoves;
    5153        static int numReinserts;
     54
     55        // The following variables are only modified in the recordCollisionAtCount method within a mutex.
     56        static int maxCollisions;
     57        static int numCollisions;
     58        static int collisionGraph[4096];
     59
    5260        static void recordCollisionAtCount(int count);
    5361    };
     
    202210#if CHECK_HASHTABLE_ITERATORS
    203211    public:
     212        // Any modifications of the m_next or m_previous of an iterator that is in a linked list of a HashTable::m_iterator,
     213        // should be guarded with m_table->m_mutex.
    204214        mutable const HashTableType* m_table;
    205215        mutable const_iterator* m_next;
     
    398408#if CHECK_HASHTABLE_ITERATORS
    399409    public:
     410        // All access to m_iterators should be guarded with m_mutex.
    400411        mutable const_iterator* m_iterators;
     412        mutable Mutex m_mutex;
    401413#endif
    402414    };
     
    467479
    468480#if DUMP_HASHTABLE_STATS
    469         ++HashTableStats::numAccesses;
     481        atomicIncrement(&HashTableStats::numAccesses);
    470482        int probeCount = 0;
    471483#endif
     
    512524
    513525#if DUMP_HASHTABLE_STATS
    514         ++HashTableStats::numAccesses;
     526        atomicIncrement(&HashTableStats::numAccesses);
    515527        int probeCount = 0;
    516528#endif
     
    564576
    565577#if DUMP_HASHTABLE_STATS
    566         ++HashTableStats::numAccesses;
     578        atomicIncrement(&HashTableStats::numAccesses);
    567579        int probeCount = 0;
    568580#endif
     
    624636
    625637#if DUMP_HASHTABLE_STATS
    626         ++HashTableStats::numAccesses;
     638        atomicIncrement(&HashTableStats::numAccesses);
    627639        int probeCount = 0;
    628640#endif
     
    739751        ASSERT(!isDeletedBucket(*(lookupForWriting(Extractor::extract(entry)).first)));
    740752#if DUMP_HASHTABLE_STATS
    741         ++HashTableStats::numReinserts;
     753        atomicIncrement(&HashTableStats::numReinserts);
    742754#endif
    743755
     
    802814    {
    803815#if DUMP_HASHTABLE_STATS
    804         ++HashTableStats::numRemoves;
     816        atomicIncrement(&HashTableStats::numRemoves);
    805817#endif
    806818
     
    888900#if DUMP_HASHTABLE_STATS
    889901        if (oldTableSize != 0)
    890             ++HashTableStats::numRehashes;
     902            atomicIncrement(&HashTableStats::numRehashes);
    891903#endif
    892904
     
    10171029    void HashTable<Key, Value, Extractor, HashFunctions, Traits, KeyTraits>::invalidateIterators()
    10181030    {
     1031        MutexLocker lock(m_mutex);
    10191032        const_iterator* next;
    10201033        for (const_iterator* p = m_iterators; p; p = next) {
     
    10381051            it->m_next = 0;
    10391052        } else {
     1053            MutexLocker lock(table->m_mutex);
    10401054            ASSERT(table->m_iterators != it);
    10411055            it->m_next = table->m_iterators;
     
    10591073            ASSERT(!it->m_previous);
    10601074        } else {
     1075            MutexLocker lock(it->m_table->m_mutex);
    10611076            if (it->m_next) {
    10621077                ASSERT(it->m_next->m_previous == it);
Note: See TracChangeset for help on using the changeset viewer.