[cherry-pick] WTF Threading leaks kernel objects on platforms that use pthreads
authormhahnenberg@apple.com <mhahnenberg@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 21 Aug 2012 23:16:24 +0000 (23:16 +0000)
committerGerrit Code Review <gerrit@gerrit.vlan144.tizendev.org>
Tue, 25 Jun 2013 06:23:07 +0000 (06:23 +0000)
commitffa5da6fcf24e9354275d96e9c0c257127b9ef87
treed42e0561bf1a27f5050e16a0309e0295bd9a0c5a
parentd13149678aa7e06b4c33a816b9dcb004e6a36761
[cherry-pick] WTF Threading leaks kernel objects on platforms that use pthreads

[Title] WTF Threading leaks kernel objects on platforms that use pthreads
[Issues] DCM-2005
[Problem] Layout is broken while flicking under on DynamicBox icon of PhotoViewer
[Solution] Routines of destorying pthreads fixed not to make memory leaked
[Cherry-Picker] Hojong Han <hojong.han@samsung.com>

WTF Threading leaks kernel objects on platforms that use pthreads
https://bugs.webkit.org/show_bug.cgi?id=94636

Reviewed by Geoffrey Garen.

Source/WTF:

Creating lots of Web workers on Mac platforms leaks lots of Mach ports. Eventually, the
process can exhaust its allocation of Mach ports from the kernel, which can then cause
all sorts of badness, including the inability to allocate new virtual memory from the
kernel. ThreadingPthreads.cpp and ThreadIdentifierDataPthreads.cpp need to be refactored
so that we do not leak these kernel resources. I would assume that we also leak kernel
resources on other pthreads platforms as well.

* wtf/ThreadIdentifierDataPthreads.cpp:
(WTF):
(WTF::ThreadIdentifierData::~ThreadIdentifierData): Now calls the event threadDidExit, which
handles all relevant tear-down of the thread metadata in the thread map.
* wtf/ThreadingPthreads.cpp: Added a new class called PthreadState that encapsulates the
state of a thread and the possible transitions between those states.
(PthreadState):
(WTF::PthreadState::PthreadState):
(WTF::PthreadState::joinableState): Returns the current state of the pthread.
(WTF::PthreadState::pthreadHandle): Returns the pthread_t for this particular thread. This needs to
remain valid even after the thread has exited because somebody could come along at any time in the
future and call join on the thread.
(WTF::PthreadState::didBecomeDetached): Signals that the thread was detached.
(WTF::PthreadState::didExit): Signals that the thread's exit destructor (~ThreadIdentifierData) has run.
(WTF::PthreadState::didJoin): Signals that the thread has been joined on successfully.
(WTF::PthreadState::hasExited): Returns whether or not the thread's exit destructor has run.
(WTF):
(WTF::identifierByPthreadHandle): Changed to also check hasExited() on the threads it finds in the map. We
should only have one valid pthread_t in the map, but there are other pthread_t's that need to remain in the
thread map for when somebody joins on that thread id later.
(WTF::establishIdentifierForPthreadHandle): Changed to put the allocate the new PthreadState data structure and
put it in the map.
(WTF::pthreadHandleForIdentifierWithLockAlreadyHeld):
(WTF::wtfThreadEntryPoint):
(WTF::waitForThreadCompletion): We now do the relevant cleanup after the specified thread has been
successfully joined on depending on if the joined thread has already exited.
(WTF::detachThread): Performs relevant cleanup if the thread has already exited. Otherwise signals to the
PthreadState that the thread has been detached.
(WTF::threadDidExit): Function called by ~ThreadIdentifierData to indicate that the thread has exited.
Only cleans up after itself if the thread isn't Joinable (i.e. Joined or Detached).

LayoutTests:

Added a test that creates a bunch of workers that immediately return. This should stress
the new WTF threading code on platforms that use pthreads, so any major regressions in correctness
will probably cause this test to fail since it creates both joinable and detached threads.

* fast/js/create-lots-of-workers-expected.txt: Added.
* fast/js/create-lots-of-workers.html: Added.
* fast/js/resources/empty-worker.js: Added.

git-svn-id: http://svn.webkit.org/repository/webkit/trunk@126208 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Conflicts:

LayoutTests/ChangeLog
Source/WTF/ChangeLog

Change-Id: I287841829550ce4940c280cbc38cecc5d2bce981

Conflicts:

LayoutTests/ChangeLog
LayoutTests/ChangeLog
LayoutTests/fast/js/create-lots-of-workers-expected.txt [new file with mode: 0644]
LayoutTests/fast/js/create-lots-of-workers.html [new file with mode: 0644]
LayoutTests/fast/js/resources/empty-worker.js [new file with mode: 0644]
Source/WTF/ChangeLog
Source/WTF/wtf/ThreadIdentifierDataPthreads.cpp
Source/WTF/wtf/ThreadingPthreads.cpp