Ignore:
Timestamp:
Jan 20, 2010, 5:01:54 PM (15 years ago)
Author:
[email protected]
Message:

<rdar://problem/7215063> Use GCD instead of an extra thread for FastMalloc scavenging on platforms where it is supported

Reviewed by Oliver Hunt.

Abstract the background scavenging slightly so that an alternate implementation that uses GCD can be used on platforms
where it is supported.

  • wtf/FastMalloc.cpp:

(WTF::TCMalloc_PageHeap::init):
(WTF::TCMalloc_PageHeap::initializeScavenger):
(WTF::TCMalloc_PageHeap::signalScavenger):
(WTF::TCMalloc_PageHeap::shouldContinueScavenging):
(WTF::TCMalloc_PageHeap::Delete):
(WTF::TCMalloc_PageHeap::periodicScavenge):

  • wtf/Platform.h:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/JavaScriptCore/wtf/FastMalloc.cpp

    r53151 r53579  
    392392#include <wtf/Vector.h>
    393393#endif
     394#if HAVE(DISPATCH_H)
     395#include <dispatch/dispatch.h>
     396#endif
     397
    394398
    395399#ifndef PRIuS
     
    13731377
    13741378#if USE_BACKGROUND_THREAD_TO_SCAVENGE_MEMORY
     1379  void initializeScavenger();
     1380  ALWAYS_INLINE void signalScavenger();
     1381  void scavenge();
     1382  ALWAYS_INLINE bool shouldContinueScavenging() const;
     1383
     1384#if !HAVE(DISPATCH_H)
    13751385  static NO_RETURN void* runScavengerThread(void*);
    1376 
    13771386  NO_RETURN void scavengerThread();
    1378 
    1379   void scavenge();
    1380 
    1381   inline bool shouldContinueScavenging() const;
    1382 
    1383   pthread_mutex_t m_scavengeMutex;
    1384 
    1385   pthread_cond_t m_scavengeCondition;
    13861387
    13871388  // Keeps track of whether the background thread is actively scavenging memory every kScavengeTimerDelayInSeconds, or
    13881389  // it's blocked waiting for more pages to be deleted.
    13891390  bool m_scavengeThreadActive;
     1391
     1392  pthread_mutex_t m_scavengeMutex;
     1393  pthread_cond_t m_scavengeCondition;
     1394#else // !HAVE(DISPATCH_H)
     1395  void periodicScavenge();
     1396
     1397  dispatch_queue_t m_scavengeQueue;
     1398  dispatch_source_t m_scavengeTimer;
     1399  bool m_scavengingScheduled;
     1400#endif
     1401
    13901402#endif  // USE_BACKGROUND_THREAD_TO_SCAVENGE_MEMORY
    13911403};
     
    14151427
    14161428#if USE_BACKGROUND_THREAD_TO_SCAVENGE_MEMORY
     1429  initializeScavenger();
     1430#endif  // USE_BACKGROUND_THREAD_TO_SCAVENGE_MEMORY
     1431}
     1432
     1433#if USE_BACKGROUND_THREAD_TO_SCAVENGE_MEMORY
     1434
     1435#if !HAVE(DISPATCH_H)
     1436
     1437void TCMalloc_PageHeap::initializeScavenger()
     1438{
    14171439  pthread_mutex_init(&m_scavengeMutex, 0);
    14181440  pthread_cond_init(&m_scavengeCondition, 0);
     
    14201442  pthread_t thread;
    14211443  pthread_create(&thread, 0, runScavengerThread, this);
    1422 #endif  // USE_BACKGROUND_THREAD_TO_SCAVENGE_MEMORY
    1423 }
    1424 
    1425 #if USE_BACKGROUND_THREAD_TO_SCAVENGE_MEMORY
     1444}
     1445
    14261446void* TCMalloc_PageHeap::runScavengerThread(void* context)
    14271447{
     
    14321452#endif
    14331453}
     1454
     1455ALWAYS_INLINE void TCMalloc_PageHeap::signalScavenger()
     1456{
     1457  if (!m_scavengeThreadActive && shouldContinueScavenging())
     1458    pthread_cond_signal(&m_scavengeCondition);
     1459}
     1460
     1461#else // !HAVE(DISPATCH_H)
     1462
     1463void TCMalloc_PageHeap::initializeScavenger()
     1464{
     1465  m_scavengeQueue = dispatch_queue_create("com.apple.JavaScriptCore.FastMallocSavenger", NULL);
     1466  m_scavengeTimer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, m_scavengeQueue);
     1467  dispatch_time_t startTime = dispatch_time(DISPATCH_TIME_NOW, kScavengeTimerDelayInSeconds * NSEC_PER_SEC);
     1468  dispatch_source_set_timer(m_scavengeTimer, startTime, kScavengeTimerDelayInSeconds * NSEC_PER_SEC, 1000 * NSEC_PER_USEC);
     1469  dispatch_source_set_context(m_scavengeTimer, this);
     1470  dispatch_source_set_event_handler(m_scavengeTimer, ^{ periodicScavenge(); });
     1471  m_scavengingScheduled = false;
     1472}
     1473
     1474ALWAYS_INLINE void TCMalloc_PageHeap::signalScavenger()
     1475{
     1476  if (!m_scavengingScheduled && shouldContinueScavenging()) {
     1477    m_scavengingScheduled = true;
     1478    dispatch_resume(m_scavengeTimer);
     1479  }
     1480}
     1481
     1482#endif
    14341483
    14351484void TCMalloc_PageHeap::scavenge()
     
    14681517}
    14691518
    1470 inline bool TCMalloc_PageHeap::shouldContinueScavenging() const
     1519ALWAYS_INLINE bool TCMalloc_PageHeap::shouldContinueScavenging() const
    14711520{
    14721521    return free_committed_pages_ > kMinimumFreeCommittedPageCount;
     
    17301779
    17311780  // Make sure the scavenge thread becomes active if we have enough freed pages to release some back to the system.
    1732   if (!m_scavengeThreadActive && shouldContinueScavenging())
    1733       pthread_cond_signal(&m_scavengeCondition);
     1781  signalScavenger();
    17341782#else
    17351783  IncrementalScavenge(n);
     
    22742322
    22752323#if USE_BACKGROUND_THREAD_TO_SCAVENGE_MEMORY
     2324
     2325#if !HAVE(DISPATCH_H)
    22762326#if OS(WINDOWS)
    22772327static void sleep(unsigned seconds)
     
    23032353  }
    23042354}
     2355
     2356#else
     2357
     2358void TCMalloc_PageHeap::periodicScavenge()
     2359{
     2360  {
     2361    SpinLockHolder h(&pageheap_lock);
     2362    pageheap->scavenge();
     2363  }
     2364
     2365  if (!shouldContinueScavenging()) {
     2366    m_scavengingScheduled = false;
     2367    dispatch_suspend(m_scavengeTimer);
     2368  }
     2369}
     2370#endif // HAVE(DISPATCH_H)
     2371
    23052372#endif
    23062373
Note: See TracChangeset for help on using the changeset viewer.