non-local reference count pointer

A

ank

Hi, all.

I've come to think of the idea of
automatic initialization/deinitialization of
non-local reference count pointer.

I've made an assumption that the user of the pointer
only read pointer after acquire the reference (increment the ref count)
and when finished using it, the user will release the ref count.

Suppose that we have atomic operations such as:
1. atomic_t atomic_inc(pVal)
2. atomic_t atomic_dec(pVal)
3. bool atomic_cas(pVal, oldValue, newValue)
4. atomic_t atomic_exchange(pVal, newValue)

Then, the pseudocode for initialization/deinitialization
of the non-local pointer to object would be like this.
In addition, no other functions will ever write to
the value of the pointer and reference count.

// initialization of non-local shared pointer
void acquireSharedObject(Object*& sh_objPtr, atomic_t& sh_count)
{
//assert(sh_count >= 0);
for (atomic_t count;;) {
count = sh_count;
if (count > 0) {
if (atomic_cas(&sh_count, count, count + 1))
return;
continue;
}

if (!sh_objPtr) {
Object* obj = createObject();
if (atomic_cas(&sh_objPtr, 0, obj)) {
atomic_inc(&sh_count);
return;
}
destroyObject(obj);
}
suspend_thread(...);
}
}

// deinitialization of non-local shared pointer
void releaseSharedObject(Object*& sh_objPtr, atomic_t& sh_count)
{
if (atomic_dec(&sh_count) == 0) {
Object* obj = atomic_exchange(&sh_objPtr, 0);
if (obj) destroyObject(obj);
}
}

I have a very little experience to prove that the algorithm
is safe in multithreaded environment but I have been analyzing it
thoroughly.

Is this code safe in multithreaded environment or not?
(I mean for both correctness and safety)
If not, what could be done to improve it?

If anyone has done the same attempt like me but able to prove
correctness and safety, I would like a good suggestion on this topic.
(In fact, I believe that there are many people who had thought about
this
idea before me.)

Thanks for every comments, suggestions in advance.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 
M

mlimber

[cross-posting deleted]
Hi, all.

I've come to think of the idea of
automatic initialization/deinitialization of
non-local reference count pointer.

I've made an assumption that the user of the pointer
only read pointer after acquire the reference (increment the ref count)
and when finished using it, the user will release the ref count.

Suppose that we have atomic operations such as:
1. atomic_t atomic_inc(pVal)
2. atomic_t atomic_dec(pVal)
3. bool atomic_cas(pVal, oldValue, newValue)
4. atomic_t atomic_exchange(pVal, newValue)

Then, the pseudocode for initialization/deinitialization
of the non-local pointer to object would be like this.
In addition, no other functions will ever write to
the value of the pointer and reference count.

// initialization of non-local shared pointer
void acquireSharedObject(Object*& sh_objPtr, atomic_t& sh_count)
{
//assert(sh_count >= 0);
for (atomic_t count;;) {
count = sh_count;
if (count > 0) {
if (atomic_cas(&sh_count, count, count + 1))
return;
continue;
}

if (!sh_objPtr) {
Object* obj = createObject();
if (atomic_cas(&sh_objPtr, 0, obj)) {
atomic_inc(&sh_count);
return;
}
destroyObject(obj);
}
suspend_thread(...);
}
}

// deinitialization of non-local shared pointer
void releaseSharedObject(Object*& sh_objPtr, atomic_t& sh_count)
{
if (atomic_dec(&sh_count) == 0) {
Object* obj = atomic_exchange(&sh_objPtr, 0);
if (obj) destroyObject(obj);
}
}

I have a very little experience to prove that the algorithm
is safe in multithreaded environment but I have been analyzing it
thoroughly.

Is this code safe in multithreaded environment or not?
(I mean for both correctness and safety)
If not, what could be done to improve it?

If anyone has done the same attempt like me but able to prove
correctness and safety, I would like a good suggestion on this topic.
(In fact, I believe that there are many people who had thought about
this
idea before me.)

Thanks for every comments, suggestions in advance.

You probably want to try on comp.programming.threads or similar since
threading is technically off-topic here (see
http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.9).

Cheers! --M
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
SterlingLa
Top