Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.arch.embedded > #27959

Re: "Boost context" for task switching on embedded Linux on ARM

From Niklas Holsti <niklas.holsti@tidorum.invalid>
Newsgroups comp.arch.embedded
Subject Re: "Boost context" for task switching on embedded Linux on ARM
Date 2018-08-08 08:21 +0300
Organization Tidorum Ltd
Message-ID <fsvcu2Fbed6U1@mid.individual.net> (permalink)
References (6 earlier) <j80gmd5mmlrvh8o99uh2va8kjolkh8bv39@4ax.com> <87pnyvgxuk.fsf@nightsong.com> <c1njmd1rue1r9ot26igdd05nnfffp9ngns@4ax.com> <fsuauuF4h4vU1@mid.individual.net> <9srkmdlg3n1b7rgf7kcjo7uh3i1gijhdh9@4ax.com>

Show all headers | View raw


On 18-08-08 07:42 , George Neuner wrote:
> On Tue, 7 Aug 2018 22:41:18 +0300, Niklas Holsti
> <niklas.holsti@tidorum.invalid> wrote:
>
>> On 18-08-07 22:10 , George Neuner wrote:
>>> On Mon, 06 Aug 2018 11:21:23 -0700, Paul Rubin
>>> <no.email@nospam.invalid> wrote:
>>>
>>>> George Neuner <gneuner2@comcast.net> writes:
>>>>> Multi-tasking with [setjmp/jmp_buf] is another issue
>>    ...
>>>>
>>>> This sounds pretty horrendous and compiler and hardware dependent.  You
>>>> are basically writing a miniature OS in your scheduler.
>>   ...
>>>> It helps if the compiler docs explicitly say it's supposed to work.
>>>
>>> Actually, the C standard says it works - at least if you read between
>>> the lines.
>>
>> C11 says it is undefined -- see below.
>>
>>> [Section] 7.13 guarantees that CPU state[*] sufficient to restore
>>> the calling environment of setjmp will be preserved in the jmp_buf
>>> structure, and that that state [modulo setjmp's return value] will be
>>> restored by calling longjmp.
>>>
>>> Although the standard does not address it's use in multitasking, the
>>> description of the operation of setjmp/longjmp matches quite well the
>>> description of a register state task switcher.
>>
>> I don't remember which version of C the OP intends to use, but the C11
>> draft (n1570.pdf) contains this text in section 7.13.2.1, for longjmp:
>>
>> "The longjmp function restores the environment saved by the most recent
>> invocation of the setjmp macro in the same invocation of the program
>> with the corresponding jmp_buf argument. If ... the invocation was from
>> another thread of execution ... the behavior is undefined."
>
> Yes.  setjmp/longjmp is not *intended* to be used for multitasking.
> That has made clear already in C89/90.
>
> However, almost every C program does things that technically are
> "undefined" or "implementation defined" according to the standard. And
> things get used for more than they originally were intended.

True, but not desirable.

> The point is not whether it's legal, but whether it works.  You can
> multitask in C with setjmp/longjmp if you do it right.

You can multitask in C with foo/bar if you implement foo and bar 
"right". Sure, for a simple thread context setjmp/longjmp can have some 
of the required properties of foo and bar, but may also have some 
undesirable properties.

The undefinedness of a cross-thread longjmp can be far more serious 
than, say, shifting or overflowing a signed integer. A longjmp is 
intended to *abandon* the current execution point and return to a point 
*lower* (earlier) in the call stack. Some C or C++ systems might 
implement this by unwinding the stack, frame by frame, until the setjmp 
frame is reached. This will have drastic "undefinedness" if the setjmp 
refers to another stack...

It is not advisable, today, to try to misuse setjmp/longjmp for thread 
switching.

-- 
Niklas Holsti
Tidorum Ltd
niklas holsti tidorum fi
       .      @       .

Back to comp.arch.embedded | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

"Boost context" for task switching on embedded Linux on ARM gp.kiwi@gmail.com - 2018-08-03 05:52 -0700
  Re: "Boost context" for task switching on embedded Linux on ARM George Neuner <gneuner2@comcast.net> - 2018-08-04 03:28 -0400
    Re: "Boost context" for task switching on embedded Linux on ARM Niklas Holsti <niklas.holsti@tidorum.invalid> - 2018-08-04 11:13 +0300
      Re: "Boost context" for task switching on embedded Linux on ARM George Neuner <gneuner2@comcast.net> - 2018-08-04 07:08 -0400
        Re: "Boost context" for task switching on embedded Linux on ARM George Neuner <gneuner2@comcast.net> - 2018-08-04 07:22 -0400
          Re: "Boost context" for task switching on embedded Linux on ARM graeme.prentice@gmail.com - 2018-08-04 04:49 -0700
            Re: "Boost context" for task switching on embedded Linux on ARM "gp...@gmail.com" <graeme.prentice@gmail.com> - 2018-08-04 05:25 -0700
              Re: "Boost context" for task switching on embedded Linux on ARM graeme.prentice@gmail.com - 2018-08-04 06:01 -0700
                Re: "Boost context" for task switching on embedded Linux on ARM George Neuner <gneuner2@comcast.net> - 2018-08-06 04:17 -0400
                Re: "Boost context" for task switching on embedded Linux on ARM Paul Rubin <no.email@nospam.invalid> - 2018-08-06 11:21 -0700
                Re: "Boost context" for task switching on embedded Linux on ARM Niklas Holsti <niklas.holsti@tidorum.invalid> - 2018-08-06 22:48 +0300
                Re: "Boost context" for task switching on embedded Linux on ARM gp.kiwi@gmail.com - 2018-08-07 04:43 -0700
                Re: "Boost context" for task switching on embedded Linux on ARM George Neuner <gneuner2@comcast.net> - 2018-08-07 15:10 -0400
                Re: "Boost context" for task switching on embedded Linux on ARM Niklas Holsti <niklas.holsti@tidorum.invalid> - 2018-08-07 22:41 +0300
                Re: "Boost context" for task switching on embedded Linux on ARM George Neuner <gneuner2@comcast.net> - 2018-08-08 00:42 -0400
                Re: "Boost context" for task switching on embedded Linux on ARM Niklas Holsti <niklas.holsti@tidorum.invalid> - 2018-08-08 08:21 +0300
                Re: "Boost context" for task switching on embedded Linux on ARM George Neuner <gneuner2@comcast.net> - 2018-08-09 10:55 -0400
                Re: "Boost context" for task switching on embedded Linux on ARM Niklas Holsti <niklas.holsti@tidorum.invalid> - 2018-08-09 22:14 +0300
            Re: "Boost context" for task switching on embedded Linux on ARM George Neuner <gneuner2@comcast.net> - 2018-08-06 04:04 -0400
              Re: "Boost context" for task switching on embedded Linux on ARM gp.kiwi@gmail.com - 2018-08-06 04:09 -0700
                Re: "Boost context" for task switching on embedded Linux on ARM George Neuner <gneuner2@comcast.net> - 2018-08-06 12:29 -0400

csiph-web