help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] Why is "volatile" used in bash source code?


From: Peng Yu
Subject: Re: [Help-bash] Why is "volatile" used in bash source code?
Date: Wed, 6 Feb 2019 18:56:56 -0600

setjmp_* are called 10+ times.  Given that there are many variables,
how does a programmer know what variables should be volatile? Does
this seem to be a burden to the programmers?

> See that call to setjmp_nosigs()? That's a point of non-local
> modification - everything that is stored in local variables is saved off
> into the setjmp() state, then we CHANGE those local variables, and then
> after that point of change, some other code (probably the SIGINT handler
> when you type Ctrl-C) does a longjmp() to revert back to the shell
> initialization point.  So the question becomes what do you want the code
> to see after the longjmp() - the state of the variable when setjmp() was
> originally called, or the state of the variable at the time when
> longjmp() was called?  Marking the variable volatile tells the compiler
> that you explicitly want the latter semantics; otherwise, you get
> undefined behavior.  Reading 'man longjmp':
>
>        The compiler may optimize variables into registers, and
> longjmp()  may
>        restore  the values of other registers in addition to the stack
> pointer
>        and program counter.  Consequently, the values of  automatic
> variables
>        are  unspecified after a call to longjmp() if they meet all the
> follow‐
>        ing criteria:
>
>        ·  they are local to the function that made the corresponding
> setjmp()
>           call;
>
>        ·  their   values  are  changed  between  the  calls  to
> setjmp()  and
>           longjmp(); and
>
>        ·  they are not declared as volatile.
>
>        Analogous remarks apply for siglongjmp().
>
> Note that longjmp() is not a very common programming practice these
> days, but it IS very common in shells.  You'll see LOTS of code in bash
> that uses code that was very idiomatic 30 years ago, even if it is less
> commonly needed nowadays.

What is the corresponding common programming practice nowadays in C?
Are you referring to the exception handling as in C++ or other
languages?


-- 
Regards,
Peng



reply via email to

[Prev in Thread] Current Thread [Next in Thread]