Comments (6)
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.
I use inplace function in multiple embedded programs as callbacks w/ the exact usage you describe. Volatile is not necessary.
from sg14.
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.
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.
@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.
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)
- [slot_map] constructors HOT 1
- [inplace_function] benchmark HOT 4
- [slot_map] Can you get a key using an iterator? HOT 5
- [slot_map] R1/R2 criticisms HOT 12
- [inplace_function] Ambiguous overloads. HOT 5
- [inplace_function] Should [](){return 42;} be convertible to inplace_function<void()>? HOT 1
- [inplace_function] make it usable without exceptions HOT 2
- inplace_function doesn't handle arguments with rvalue references well HOT 1
- [inplace function] Problems assigning if my starting point is empty/nullptr HOT 4
- [inplace_function] Const-callability means thread-safety HOT 4
- [inplace_function] Giant Sized Buffers Required for Compilation HOT 5
- License is missing on some includes HOT 1
- `sg14::ring_span`: clear () member function gone awol HOT 2
- [inplace_function] Opt-in "safe" default constructed inplace_function HOT 7
- Provide CMake & Conan installation support
- slot_map: custom key causing clang to seg fault. HOT 1
- [inplace_function] usage at shared library boundaries HOT 5
- sg14::span_ring: class template argument deduction
- [inplace_function.h] Copyright holder HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from sg14.