Skip to content
Snippets Groups Projects
Commit 5c2b2f94 authored by Avi Kivity's avatar Avi Kivity
Browse files

semaphore: convert to an intrusive list

Intrusive lists are faster since they require no allocations.
parent b9953a90
No related branches found
No related tags found
No related merge requests found
......@@ -11,13 +11,12 @@ void semaphore::post(unsigned units)
_val += units;
auto i = _waiters.begin();
while (_val > 0 && i != _waiters.end()) {
auto p = i++;
auto& wr = *p;
auto wr = i++;
if (wr->units <= _val) {
_val -= wr->units;
wr->owner->wake();
wr->owner = nullptr;
_waiters.erase(p);
_waiters.erase(wr);
}
}
});
......@@ -34,7 +33,7 @@ void semaphore::wait(unsigned units)
} else {
wr.owner = sched::thread::current();
wr.units = units;
_waiters.push_back(&wr);
_waiters.push_back(wr);
wait = true;
}
});
......
......@@ -2,7 +2,7 @@
#define SEMAPHORE_HH_
#include <osv/mutex.h>
#include <list>
#include <boost/intrusive/list.hpp>
#include <sched.hh>
class semaphore {
......@@ -14,11 +14,13 @@ public:
private:
unsigned _val;
mutex _mtx;
struct wait_record {
struct wait_record : boost::intrusive::list_base_hook<> {
sched::thread* owner;
unsigned units;
};
std::list<wait_record*> _waiters;
boost::intrusive::list<wait_record,
boost::intrusive::base_hook<wait_record>,
boost::intrusive::constant_time_size<false>> _waiters;
};
#endif /* SEMAPHORE_HH_ */
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment