2 * Copyright 2001-2015, Haiku.
3 * Distributed under the terms of the MIT License.
6 * Erik Jaesler (erik@cgsoftware.com)
10 //! Maintains a global list of all loopers in a given team.
13 #include "LooperList.h"
26 BLooperList gLooperList
;
29 BLooperList::BLooperList()
31 fLock("BLooperList lock")
51 BLooperList::IsLocked()
53 return fLock
.IsLocked();
58 BLooperList::AddLooper(BLooper
* looper
)
60 BAutolock
locker(fLock
);
62 if (!IsLooperValid(looper
)) {
64 = find_if(fData
.begin(), fData
.end(), EmptySlotPred
);
65 if (i
== fData
.end()) {
66 fData
.push_back(LooperData(looper
));
77 BLooperList::IsLooperValid(const BLooper
* looper
)
79 BAutolock
locker(fLock
);
82 return find_if(fData
.begin(), fData
.end(),
83 FindLooperPred(looper
)) != fData
.end();
88 BLooperList::RemoveLooper(BLooper
* looper
)
90 BAutolock
locker(fLock
);
93 LooperDataIterator i
= find_if(fData
.begin(), fData
.end(),
94 FindLooperPred(looper
));
95 if (i
!= fData
.end()) {
105 BLooperList::GetLooperList(BList
* list
)
107 BAutolock
locker(fLock
);
110 for (uint32 i
= 0; i
< fData
.size(); ++i
) {
112 list
->AddItem(fData
[i
].looper
);
118 BLooperList::CountLoopers()
120 BAutolock
locker(fLock
);
122 return (int32
)fData
.size();
127 BLooperList::LooperAt(int32 index
)
129 BAutolock
locker(fLock
);
132 BLooper
* looper
= NULL
;
133 if (index
< (int32
)fData
.size())
134 looper
= fData
[(uint32
)index
].looper
;
141 BLooperList::LooperForThread(thread_id thread
)
143 BAutolock
locker(fLock
);
146 BLooper
* looper
= NULL
;
148 = find_if(fData
.begin(), fData
.end(), FindThreadPred(thread
));
149 if (i
!= fData
.end())
157 BLooperList::LooperForName(const char* name
)
159 BAutolock
locker(fLock
);
162 BLooper
* looper
= NULL
;
164 = find_if(fData
.begin(), fData
.end(), FindNamePred(name
));
165 if (i
!= fData
.end())
173 BLooperList::LooperForPort(port_id port
)
175 BAutolock
locker(fLock
);
178 BLooper
* looper
= NULL
;
180 = find_if(fData
.begin(), fData
.end(), FindPortPred(port
));
181 if (i
!= fData
.end())
189 BLooperList::InitAfterFork()
191 // We need to reinitialize the locker to get a new semaphore
192 new (&fLock
) BLocker("BLooperList lock");
198 BLooperList::EmptySlotPred(LooperData
& data
)
200 return data
.looper
== NULL
;
205 BLooperList::AssertLocked()
208 debugger("looperlist is not locked; proceed at great risk!");
212 // #pragma mark - BLooperList::LooperData
215 BLooperList::LooperData::LooperData()
222 BLooperList::LooperData::LooperData(BLooper
* looper
)
229 BLooperList::LooperData::LooperData(const LooperData
& other
)
235 BLooperList::LooperData
&
236 BLooperList::LooperData::operator=(const LooperData
& other
)
239 looper
= other
.looper
;
246 BLooperList::FindLooperPred::operator()(BLooperList::LooperData
& data
)
248 return data
.looper
&& looper
== data
.looper
;
253 BLooperList::FindThreadPred::operator()(LooperData
& data
)
255 return data
.looper
&& thread
== data
.looper
->Thread();
260 BLooperList::FindNamePred::operator()(LooperData
& data
)
262 return data
.looper
&& !strcmp(name
, data
.looper
->Name());
267 BLooperList::FindPortPred::operator()(LooperData
& data
)
269 return data
.looper
&& port
== _get_looper_port_(data
.looper
);
272 } // namespace BPrivate