Adjust the thread counting to use atomic adds. This should prevent the previous scenario where many more threads than the limit could actually be spawned.

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@28775 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Rene Gollent 2008-12-04 03:28:19 +00:00
parent 83868e649d
commit 71279b24dd
2 changed files with 10 additions and 7 deletions

View File

@ -87,19 +87,22 @@ status_t
RegistrarThreadManager::LaunchThread(RegistrarThread *thread)
{
status_t err = thread ? B_OK : B_BAD_VALUE;
if (!err)
err = fThreadCount < kThreadLimit ? (status_t)B_OK : (status_t)B_NO_MORE_THREADS;
if (!err) {
if (atomic_add(&fThreadCount, 1) >= kThreadLimit) {
err = B_NO_MORE_THREADS;
atomic_add(&fThreadCount, -1);
}
}
if (!err) {
fThreads.push_back(thread);
fThreadCount++;
err = thread->Run();
if (err) {
std::list<RegistrarThread*>::iterator i;
for (i = fThreads.begin(); i != fThreads.end();) {
if ((*i) == thread) {
i = fThreads.erase(i);
fThreadCount--;
atomic_add(&fThreadCount, -1);
break;
} else
++i;
@ -234,7 +237,7 @@ std::list<RegistrarThread*>::iterator&
RegistrarThreadManager::RemoveThread(std::list<RegistrarThread*>::iterator &i)
{
delete *i;
fThreadCount--;
atomic_add(&fThreadCount, -1);
return (i = fThreads.erase(i));
}

View File

@ -33,13 +33,13 @@ public:
uint ThreadCount() const;
static const uint kThreadLimit = 12;
static const int kThreadLimit = 12;
private:
std::list<RegistrarThread*>::iterator& RemoveThread(std::list<RegistrarThread*>::iterator &i);
std::list<RegistrarThread*> fThreads;
uint fThreadCount;
vint32 fThreadCount;
};
#endif // THREAD_MANAGER_H