Coder Social home page Coder Social logo

atomic shared pointer about mlib HOT 1 CLOSED

p-p-h-d avatar p-p-h-d commented on May 14, 2024
atomic shared pointer

from mlib.

Comments (1)

P-p-H-d avatar P-p-H-d commented on May 14, 2024

Proposition for _set_atomic we store temporary NULL to the atomic_ptr struct to request an exclusive access to the data (this looks like a lock and other operations need to handle NULL) :

void shared_ptr_set_atomic(ptr a, atomic_ptr *ptr)
{
  // Get exclusive access to the data
  p = atomic_load(ptr);
  do {
    if (p == NULL) {
      // TODO: exponential backoff
      p = atomic_load(ptr);
      continue;
    }
  } while (!atomic_compare_exchange_weak(ptr, &p, NULL));
  // p has exclusive access to the pointer
  p->data->cpt ++;
  a->data = p->data;
  atomic_exchange (ptr, p);
}

This prevents using NULL which obliges atomic shared pointer to point to a created object...

Other alternative solution is to use the bit 0 to mark the pointer as being updated, preventing other from using it (TBC only clear):

void shared_ptr_set_atomic(ptr a, atomic_ptr *ptr)
{
  // Get exclusive access to the data
  p = atomic_load(ptr);
  do {
    if ( (p&1) != 0) {
      // TODO: exponential backoff
      p = atomic_load(ptr);
      continue;
    }
  } while (!atomic_compare_exchange_weak(ptr, &p, p|1));
 // Exclusive access (kind of lock).
  p->data->cpt ++;
  a->data = p->data;
  atomic_set (ptr, p);
}

Other implementation seems to have it hard to be lock-free: cf. llvm-mirror/libcxx@5fec82d

from mlib.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.