提交 05f41512 创建 作者: Nickolai Zeldovich's avatar Nickolai Zeldovich

use 64-bit compare-and-swap for versioned pointers (48-bit ptr, 16-bit ver),

instead of 128-bit cmpxchg16. minor speedup, even though the profiler said that cmpxchg16 was the top offender.
上级 412c2695
#include "atomic.hh" #include "atomic.hh"
template<class T> template<class T>
struct vptr { struct vptr64 {
u128 _a; typedef u128 __inttype;
T ptr() const { return (T) (_a & 0xffffffffffffffffULL); } typedef T __ptrtype;
__inttype _a;
T ptr() const { return (T) iptr(); }
u64 iptr() const { return _a & 0xffffffffffffffffULL; }
u64 v() const { return _a >> 64; } u64 v() const { return _a >> 64; }
vptr(T p, u64 v) : _a((((u128)v)<<64) | (u64) p) {} vptr64(T p, u64 v) : _a((((u128)v)<<64) | (u64) p) {}
vptr(u128 a) : _a(a) {} vptr64(u128 a) : _a(a) {}
}; };
template<class T> template<class T>
struct vptr48 {
typedef u64 __inttype;
typedef T __ptrtype;
__inttype _a;
T ptr() const {
u64 i = iptr();
if (i & (1ULL << 47))
i += 0xffffULL << 48;
return (T) i;
}
u64 iptr() const { return _a & 0xffffffffffffULL; }
u16 v() const { return _a >> 48; }
vptr48(T p, u16 v) : _a((((u64)v)<<48) | (((u64)p) & 0xffffffffffffULL)) {}
vptr48(u64 a) : _a(a) {}
};
template<class VPTR>
class versioned { class versioned {
private: private:
std::atomic<u128> _a; std::atomic<typename VPTR::__inttype> _a;
public: public:
vptr<T> load() { return vptr<T>(_a.load()); } VPTR load() { return VPTR(_a.load()); }
bool compare_exchange(const vptr<T> &expected, T desired) { bool compare_exchange(const VPTR &expected, typename VPTR::__ptrtype desired) {
vptr<T> n(desired, expected.v()); VPTR n(desired, expected.v());
return cmpxch(&_a, expected._a, n._a); return cmpxch(&_a, expected._a, n._a);
} }
}; };
...@@ -31,7 +54,7 @@ struct kmem { ...@@ -31,7 +54,7 @@ struct kmem {
char name[MAXNAME]; char name[MAXNAME];
u64 size; u64 size;
u64 ninit; u64 ninit;
versioned<run*> freelist; versioned<vptr48<run*>> freelist;
std::atomic<u64> nfree; std::atomic<u64> nfree;
} __mpalign__; } __mpalign__;
......
...@@ -19,7 +19,7 @@ struct header { ...@@ -19,7 +19,7 @@ struct header {
}; };
struct freelist { struct freelist {
versioned<header*> buckets[KMMAX+1]; versioned<vptr48<header*>> buckets[KMMAX+1];
char name[MAXNAME]; char name[MAXNAME];
}; };
......
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论