Coder Social home page Coder Social logo

Comments (6)

Quuxplusone avatar Quuxplusone commented on July 17, 2024

I am 99.999% confident that you don't need this ability. As you say, "I'm admittedly not entirely sure how necessary it is ... I don't feel safe having to omit volatile" — but as engineers we should deal in actualities, not "feelings." For now, I recommend that you do

#define Volatile

Volatile inplace_function<int()> f;
Volatile int x = f();

etc., and reopen this issue only if that solution produces incorrect or inefficient codegen for your program.

from sg14.

phillipjohnston avatar phillipjohnston commented on July 17, 2024

I use inplace function in multiple embedded programs as callbacks w/ the exact usage you describe. Volatile is not necessary.

from sg14.

phillipjohnston avatar phillipjohnston commented on July 17, 2024

I'm also not sure what exactly a volatile inplace_function would accomplish. You're telling the compiler not to perform read or write optimizations related to that variable. It makes sense for variables representing hardware registers, but not something like inplace_function. Also worth re-stating: volatile != atomic.

from sg14.

TBBle avatar TBBle commented on July 17, 2024

Wouldn't volatile be appropriate if the function variable itself, e.g., f, was set in both the general code and the interrupt handler? The volatile would ensure the mainline code doesn't cache the function pointer's target between invocations if the interrupt handler has been run.

Perhaps there's rules for interrupt/signal handlers in C++ that mean this isn't necessary, but I haven't checked.

Something like:

volatile inplace_function<void(Status, bool /*isTerminal*/)> progress;

void do_thing(auto status_callback)
{
 f = callback;
 set_signal_handler(signal_hander);
 if (f) f(STARTING, false);
 open(thing); // Signal handler could run here
 if (f) f(COMPLETED, true);
 f = {};
 set_signal_handler({});
}

void signal_handler()
{
 if (f) f(SIGNALLED, true);
 f = {};
}

It seems that without volatile, that second if (f) in do_thing might be optimised away.

from sg14.

Quuxplusone avatar Quuxplusone commented on July 17, 2024

@TBBle: In your sample code, what prevents the signal handler from possibly running during the assignment f = {} in do_thing? If it ran in the middle of that assignment, it'd see f in a bad (half-assigned) state.

Real code that wants to do this kind of thing (and I'm not sure there is any) should probably store their callback in an atomic_shared_ptr<inplace_function<Sig>> f or an atomic<inplace_function<Sig>*> f, not a (non-atomic) inplace_function<Sig> f which is susceptible to tearing.

from sg14.

TBBle avatar TBBle commented on July 17, 2024

Huh, yeah. For some reason I was thinking that signals only went off when control was handled to the kernel/libc using something like open. Weird...

Anyway, you're right. A totally-async signal does mean you need an atomic to be able to write to it at all.

from sg14.

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.