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:
@@ -87,19 +87,22 @@ status_t
|
|||||||
RegistrarThreadManager::LaunchThread(RegistrarThread *thread)
|
RegistrarThreadManager::LaunchThread(RegistrarThread *thread)
|
||||||
{
|
{
|
||||||
status_t err = thread ? B_OK : B_BAD_VALUE;
|
status_t err = thread ? B_OK : B_BAD_VALUE;
|
||||||
if (!err)
|
if (!err) {
|
||||||
err = fThreadCount < kThreadLimit ? (status_t)B_OK : (status_t)B_NO_MORE_THREADS;
|
if (atomic_add(&fThreadCount, 1) >= kThreadLimit) {
|
||||||
|
err = B_NO_MORE_THREADS;
|
||||||
|
atomic_add(&fThreadCount, -1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (!err) {
|
if (!err) {
|
||||||
fThreads.push_back(thread);
|
fThreads.push_back(thread);
|
||||||
fThreadCount++;
|
|
||||||
err = thread->Run();
|
err = thread->Run();
|
||||||
if (err) {
|
if (err) {
|
||||||
std::list<RegistrarThread*>::iterator i;
|
std::list<RegistrarThread*>::iterator i;
|
||||||
for (i = fThreads.begin(); i != fThreads.end();) {
|
for (i = fThreads.begin(); i != fThreads.end();) {
|
||||||
if ((*i) == thread) {
|
if ((*i) == thread) {
|
||||||
i = fThreads.erase(i);
|
i = fThreads.erase(i);
|
||||||
fThreadCount--;
|
atomic_add(&fThreadCount, -1);
|
||||||
break;
|
break;
|
||||||
} else
|
} else
|
||||||
++i;
|
++i;
|
||||||
@@ -234,7 +237,7 @@ std::list<RegistrarThread*>::iterator&
|
|||||||
RegistrarThreadManager::RemoveThread(std::list<RegistrarThread*>::iterator &i)
|
RegistrarThreadManager::RemoveThread(std::list<RegistrarThread*>::iterator &i)
|
||||||
{
|
{
|
||||||
delete *i;
|
delete *i;
|
||||||
fThreadCount--;
|
atomic_add(&fThreadCount, -1);
|
||||||
return (i = fThreads.erase(i));
|
return (i = fThreads.erase(i));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -33,13 +33,13 @@ public:
|
|||||||
|
|
||||||
uint ThreadCount() const;
|
uint ThreadCount() const;
|
||||||
|
|
||||||
static const uint kThreadLimit = 12;
|
static const int kThreadLimit = 12;
|
||||||
private:
|
private:
|
||||||
|
|
||||||
std::list<RegistrarThread*>::iterator& RemoveThread(std::list<RegistrarThread*>::iterator &i);
|
std::list<RegistrarThread*>::iterator& RemoveThread(std::list<RegistrarThread*>::iterator &i);
|
||||||
|
|
||||||
std::list<RegistrarThread*> fThreads;
|
std::list<RegistrarThread*> fThreads;
|
||||||
uint fThreadCount;
|
vint32 fThreadCount;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // THREAD_MANAGER_H
|
#endif // THREAD_MANAGER_H
|
||||||
|
|||||||
Reference in New Issue
Block a user