Re: [RFC] Atomic Reference Counting - Do you think this would be useful to Boost?
"Chris Thomasson" <cristom@comcast.net> wrote in message
news:ibqdnbGk-uGYvrbYnZ2dnUVZ_vmdnZ2d@comcast.com...
Currently, Boost doesn't provide support for "true" atomically thread-safe
atomic reference counting:
[...]
I have created an experimental prototype of one of my atomic reference
counting algorithms I posted over in c.p.t:
[...]
Does anybody think that Boost could make use of this level of
thread-safety?
Here is an example of what you can do with the C++-based abstraction layer
that I wrapped around my refcount C API; one I release it of course. We can
use it to manage access to a "shared buffer" object of sorts:
(contrived quick pseudo-code!)
#define MYBUFMAXSZ() 4096
#define MYBUFOVERRUNSZ() 0
// quick-and-crude generic buffer
class mybuf {
char *m_base;
char *m_buf;
std::size_t m_sz;
public:
// setup our various smart pointer types...
typedef vzoom::sync::ptr::local<mybuf> ptr_local_t;
typedef vzoom::sync::ptr::weak<mybuf> ptr_weak_t;
typedef vzoom::sync::ptr::strong<mybuf> ptr_strong_t;
private:
static mybuf* myallocator_malloc(std::size_t &sz) {
// crude-and-quick buffer "sanity" check
sz += MYBUFOVERRUNSZ;
if (sz >= MYBUFMAXSZ) { throw; }
mybuf *buf = std::malloc(sz /* adjust for pad and align */);
if (! buf) { assert(buf); throw; }
return buf;
}
static void myallocator_free(mybuf *buf) {
assert(buf); std::free(buf /* adjust for pad and align */);
}
public:
mybuf() throw() : m_base(0), m_buf(0), m_sz(0) {}
mybuf(char const *src)
: m_base(0), m_buf(0), m_sz(0) {
if (! src) { assert(src); throw; }
m_sz = strlen(src) + 1;
m_base = myallocator_malloc(m_sz);
m_buf = std::strcpy(m_base, src);
assert(m_base && m_buf);
}
mybuf(char const *src, char const *concat)
: m_base(0), m_buf(0), m_sz(0) {
if (! src || ! concat) { assert(src && concat); throw; }
m_sz = strlen(src) + strlen(concat) + 2;
m_base = myallocator_malloc(m_sz);
m_buf = std::strcpy(m_base, src);
m_buf = std::strcat(m_buf, concat);
assert(m_base && m_buf);
}
~mybuf() throw() {
try { myallocator_free(m_base); } catch(...) { assert(false);
std::abort(); }
}
public:
// rollback a previous local concatenation
void rollback_local(...) {
// ...
}
// resets a previous concatenation
void reset_local(char const *src, char const *concat) {
// ...
}
public:
char const* c_ptr() const { return m_buf; }
std::size_t bufsize() const { return m_sz; }
};
#include <stdio.h>
#include <string.h>
static mybuf::ptr_strong_t g_shared_buf(new mybuf("InitialValue"));
void threads_a_to_whatever_in_parallel(...) {
for(...) {
// misc. app logic ... {
// read the shared buffer
do {
// read the shared buffer
mybuf::ptr_local_t cmp = g_shared_buf, xchg;
// check for null... :)
if (! cmp) { break; }
if (! xchg) {
// create a local buffer and update it; do this only one time!
xchg.reset(new mybuf(cmp, "ANewAppendedValue"));
} else {
// rollback the previous update; the CAS failed, we need to
locally reset...
xchg->rollback_local(...);
// re-update the local buffer.
xchg->reset_local(cmp, "ANewAppendedValue");
}
// try and update the shared buffer with our local buffer
} while(! g_shared_buf.cas(cmp, xchg));
// some more misc. app logic ...
}
}
You can have atomically thread-safe, mostly lock-free COW-based C++ string
objects with my counting algorithm...
Any thoughts?
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]