Skip to contentSkip to conversation

EINTR in notifier.c (mercury)

Xenomai Applied Matthias Schneider 3 participants 16 messages Mar 31, 2014 – Apr 11, 2014

lore
Share & tools
lore
#0 lore raw

Hi all,

still working on thread suspension in mercury, I noticed that some

threadobj_suspend() and threadobj_resume() calls seemed not to have the desired

effect. Analyzing the issue, I found out that sometimes the read operations on the pipe in notifier_wait() seem to return with EINTR, especially in heavily loaded systems. Restarting the read system call in that case made thread suspension a lot more reliable in my case.

I have attached a patch adding loops to deal with the EINTR situation in all

read and write operations of notifier.c.

Regards,
Matthias
-------------- next part --------------
A non-text attachment was scrubbed...
Name: notifier.c.patch
Type: text/x-patch
Size: 1626 bytes
Desc: not available
URL: <http://www.xenomai.org/pipermail/xenomai/attachments/20140331/afc8f5a6/attachment.bin>

#1 lore raw
Quoted 7 lines · Matthias Schneider

Hi all,

still working on thread suspension in mercury, I noticed that some

threadobj_suspend() and threadobj_resume() calls seemed not to have the desired

Quoted 9 lines

effect. Analyzing the issue, I found out that sometimes the read operations on the pipe in notifier_wait() seem to return with EINTR, especially in heavily loaded systems. Restarting the read system call in that case made thread suspension a lot more reliable in my case.

I have attached a patch adding loops to deal with the EINTR situation in all

read and write operations of notifier.c.

Agreed, we are reading from a deemed slow device. However, the patch is wrong in testing the first loop condition (notifier_sighandler). In addition, we don't need two nested loops, a single one would suffice with a proper test condition.

Nitpicking: please move all variable declarators to the outer local declaration block, and avoid compound braces for single-statement blocks.

Signature · 1 line
Philippe.
#2 lore raw
In reply to Matthias Schneider
Quoted 5 lines · Matthias Schneider

Hi all,

still working on thread suspension in mercury, I noticed that some

Quoted 8 lines

threadobj_suspend() and threadobj_resume() calls seemed not to have the desired

effect. Analyzing the issue, I found out that sometimes the read operations on the pipe in notifier_wait() seem to return with EINTR, especially in heavily loaded systems. Restarting the read system call in that case made thread suspension a lot more reliable in my case.

I have attached a patch adding loops to deal with the EINTR situation in all

You can probably avoid testing for EINTR if all signal handlers are registered with the SA_RESTART flag.

Signature · 1 line
                                                                Gilles.
#3 lore raw
Quoted 13 lines · Gilles Chanteperdrix
Quoted 12 lines · Matthias Schneider

Hi all,

still working on thread suspension in mercury, I noticed that some

threadobj_suspend() and threadobj_resume() calls seemed not to have the desired

effect. Analyzing the issue, I found out that sometimes the read operations on the pipe in notifier_wait() seem to return with EINTR, especially in heavily loaded systems. Restarting the read system call in that case made thread suspension a lot more reliable in my case.

Quoted 5 lines
Quoted 1 line

I have attached a patch adding loops to deal with the EINTR situation in all

You can probably avoid testing for EINTR if all signal handlers are registered with the SA_RESTART flag.

The app may not explicitly care for signals (granted, most would do, but we would then have to assume they do it right).

Signature · 1 line
Philippe.
#4 lore raw
Quoted 16 lines · Philippe Gerum
Quoted 15 lines · Gilles Chanteperdrix
Quoted 12 lines · Matthias Schneider

Hi all,

still working on thread suspension in mercury, I noticed that some

threadobj_suspend() and threadobj_resume() calls seemed not to have the desired

effect. Analyzing the issue, I found out that sometimes the read operations on the pipe in notifier_wait() seem to return with EINTR, especially in heavily loaded systems. Restarting the read system call in that case made thread suspension a lot more reliable in my case.

Quoted 12 lines
Quoted 5 lines
Quoted 1 line

I have attached a patch adding loops to deal with the EINTR situation

You can probably avoid testing for EINTR if all signal handlers are registered with the SA_RESTART flag.

The app may not explicitly care for signals (granted, most would do, but we would then have to assume they do it right).

Signature · 1 line
Philippe.

I do agree with Philippe, there are also several xenomai signals registered without SA_RESTART. I have attached an updated patch that should address all of Philippe's earlier points - including the incorrect loop (sorry about that).

Regards,
Matthias
-------------- next part --------------
A non-text attachment was scrubbed...
Name: notifier_eintr_2.patch
Type: text/x-patch
Size: 1570 bytes
Desc: not available
URL: <http://www.xenomai.org/pipermail/xenomai/attachments/20140331/c8e1a961/attachment.bin>

#5 lore raw
In reply to Philippe Gerum
Quoted 14 lines · Philippe Gerum
Quoted 13 lines · Gilles Chanteperdrix
Quoted 12 lines · Matthias Schneider

Hi all,

still working on thread suspension in mercury, I noticed that some

threadobj_suspend() and threadobj_resume() calls seemed not to have the desired

effect. Analyzing the issue, I found out that sometimes the read operations on the pipe in notifier_wait() seem to return with EINTR, especially in heavily loaded systems. Restarting the read system call in that case made thread suspension a lot more reliable in my case.

Quoted 9 lines
Quoted 5 lines
Quoted 1 line

I have attached a patch adding loops to deal with the EINTR situation in all

You can probably avoid testing for EINTR if all signal handlers are registered with the SA_RESTART flag.

The app may not explicitly care for signals (granted, most would do, but we would then have to assume they do it right).

Yes, applications may want to use signals to interrupt a call to read and voluntarily get EINTR anyway.

Signature · 1 line
					    Gilles.
#6 lore raw
Quoted 21 lines · Gilles Chanteperdrix
Quoted 20 lines · Philippe Gerum
Quoted 16 lines · Gilles Chanteperdrix
Quoted 11 lines · Matthias Schneider

Hi all,

still working on thread suspension in mercury, I noticed that some threadobj_suspend() and threadobj_resume() calls seemed not to have the desired effect. Analyzing the issue, I found out that sometimes the read operations on the pipe in notifier_wait() seem to return with EINTR, especially in heavily loaded systems. Restarting the read system call in that case made thread suspension a lot more reliable in my case.

I have attached a patch adding loops to deal with the EINTR situation in all

You can probably avoid testing for EINTR if all signal handlers are registered with the SA_RESTART flag.

Quoted 7 lines
Quoted 3 lines

The app may not explicitly care for signals (granted, most would do, but we would then have to assume they do it right).

Yes, applications may want to use signals to interrupt a call to read and voluntarily get EINTR anyway.
                        Gilles.

Please find attached a third version of my patch dealing with
EINTR syscalls. Note that I also stumbled over unexpected behavior
in threadobj_sleep and cancel_sync due to EINTRs. Considering that
mercury is using signals for user-space round-robin and thread
suspension, even without any application signals EINTR happens
quite often.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: eintr_3.patch
Type: text/x-patch
Size: 3040 bytes
Desc: not available
URL: <http://www.xenomai.org/pipermail/xenomai/attachments/20140406/c4a1fe20/attachment.bin>

#7 lore raw
Quoted 30 lines · Matthias Schneider
Quoted 21 lines · Gilles Chanteperdrix
Quoted 20 lines · Philippe Gerum
Quoted 16 lines · Gilles Chanteperdrix
Quoted 11 lines · Matthias Schneider

Hi all,

still working on thread suspension in mercury, I noticed that some threadobj_suspend() and threadobj_resume() calls seemed not to have the desired effect. Analyzing the issue, I found out that sometimes the read operations on the pipe in notifier_wait() seem to return with EINTR, especially in heavily loaded systems. Restarting the read system call in that case made thread suspension a lot more reliable in my case.

I have attached a patch adding loops to deal with the EINTR situation in all

You can probably avoid testing for EINTR if all signal handlers are registered with the SA_RESTART flag.

Quoted 7 lines
Quoted 3 lines

The app may not explicitly care for signals (granted, most would do, but we would then have to assume they do it right).

Yes, applications may want to use signals to interrupt a call to read and voluntarily get EINTR anyway.
Gilles.

Quoted 6 lines

Please find attached a third version of my patch dealing with EINTR syscalls. Note that I also stumbled over unexpected behavior in threadobj_sleep and cancel_sync due to EINTRs. Considering that mercury is using signals for user-space round-robin and thread suspension, even without any application signals EINTR happens quite often.

@@ -86,9 +88,13 @@ static void notifier_sighandler(int sig, siginfo_t *siginfo, void *uc)
if (nf->owner && nf->owner != tid)
continue;

- while (__STD(read(nf->psfd[0], &c, 1)) > 0)
- /* Callee must run async-safe code only. */
- nf->callback(nf);
+ do {
+ ret = __STD(read(nf->psfd[0], &c, 1));
+ if (ret > 0)
+ /* Callee must run async-safe code only. */
+ nf->callback(nf);
+ } while (ret > 0 || errno == EINTR);

In theory, we could receive a zero byte count on EOF, in which case we should not test errno, rather we should bail out immediately.

@@ -1093,7 +1093,9 @@ int threadobj_sleep(struct timespec *ts)
*/
current->run_state = __THREAD_S_DELAYED; threadobj_save_timeout(&current->core, ts);
- ret = -__RT(clock_nanosleep(CLOCK_COPPERPLATE, TIMER_ABSTIME, ts, NULL));
+ do
+ ret = -__RT(clock_nanosleep(CLOCK_COPPERPLATE, TIMER_ABSTIME, ts, NULL));

We should definitely pass and use the "remain" field in clock_nanosleep(), not to restart a complete wait each time we get interrupted.

Signature · 1 line
Philippe.
#8 lore raw
Quoted 10 lines · Philippe Gerum
(quoted diff) +2 -1
@@ -1093,7 +1093,9 @@ int threadobj_sleep(struct timespec *ts)
 	 */
 	current->run_state = __THREAD_S_DELAYED;
 	threadobj_save_timeout(&current->core, ts);
-	ret = -__RT(clock_nanosleep(CLOCK_COPPERPLATE, TIMER_ABSTIME, ts, NULL));
+	do
+		ret = -__RT(clock_nanosleep(CLOCK_COPPERPLATE, TIMER_ABSTIME, ts, NULL));
 

We should definitely pass and use the "remain" field in clock_nanosleep(), not to restart a complete wait each time we get interrupted.

The remain field only makes sense without TIMER_ABSTIME. With TIMER_ABSTIME, since the wake-up time is absolute, passing it again to clock_nanosleep should be fine.

Signature · 1 line
                                                                Gilles.
#9 lore raw
Quoted 9 lines · Gilles Chanteperdrix
Quoted 8 lines · Philippe Gerum
(quoted diff) +2 -1
@@ -1093,7 +1093,9 @@ int threadobj_sleep(struct timespec *ts)
  	 */
  	current->run_state = __THREAD_S_DELAYED;
  	threadobj_save_timeout(&current->core, ts);
-	ret = -__RT(clock_nanosleep(CLOCK_COPPERPLATE, TIMER_ABSTIME, ts, NULL));
+	do
+		ret = -__RT(clock_nanosleep(CLOCK_COPPERPLATE, TIMER_ABSTIME, ts, NULL));
 
Quoted 7 lines
Quoted 2 lines

We should definitely pass and use the "remain" field in clock_nanosleep(), not to restart a complete wait each time we get interrupted.

The remain field only makes sense without TIMER_ABSTIME. With TIMER_ABSTIME, since the wake-up time is absolute, passing it again to clock_nanosleep should be fine.

Yes, sorry. That's right.

Signature · 1 line
Philippe.
#10 lore raw
In reply to Philippe Gerum

----- Original Message -----

Quoted 80 lines

From: Philippe Gerum <[email protected]>
To: Matthias Schneider <[email protected]>; "[email protected]" <[email protected]> Cc:
Sent: Sunday, April 6, 2014 5:04 PM
Subject: Re: [Xenomai] EINTR in notifier.c (mercury)

Quoted 1 line · Matthias Schneider
Quoted 1 line

On Monday, March 31, 2014 7:50 PM, Gilles Chanteperdrix

Quoted 8 lines · <[email protected]> wrote:
Quoted 8 lines
Quoted 8 lines · Philippe Gerum
Quoted 5 lines · Gilles Chanteperdrix
Quoted 4 lines · Matthias Schneider

  Hi all,

  still working on thread suspension in mercury, I noticed

that some

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

  threadobj_suspend() and threadobj_resume() calls seemed

not to have the desired

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

  effect. Analyzing the issue, I found out that sometimes

the read operations on

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

  the pipe in notifier_wait() seem to return with EINTR,

especially in

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

  heavily loaded systems. Restarting the read system call

in that case made

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

  thread suspension a lot more reliable in my case.

  I have attached a patch adding loops to deal with the

EINTR situation in all

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

  You can probably avoid testing for EINTR if all signal

handlers are

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 2 lines

  registered with the SA_RESTART flag.

  The app may not explicitly care for signals (granted, most would

do, but

Quoted 14 lines
Quoted 6 lines
Quoted 2 lines

  we would then have to assume they do it right).

Yes, applications may want to use signals to interrupt a call to read and voluntarily get EINTR anyway.
                          Gilles.

Please find attached a third version of my patch dealing with EINTR syscalls. Note that I also stumbled over unexpected behavior in threadobj_sleep and cancel_sync due to EINTRs. Considering that mercury is using signals for user-space round-robin and thread suspension, even without any application signals EINTR happens quite often.

(quoted diff) +6 -3
@@ -86,9 +88,13 @@ static void notifier_sighandler(int sig, siginfo_t *siginfo, 
 void *uc)
         if (nf->owner && nf->owner != tid)
             continue;
 
-        while (__STD(read(nf->psfd[0], &c, 1)) > 0)
-            /* Callee must run async-safe code only. */
-            nf->callback(nf);
+        do {
+            ret = __STD(read(nf->psfd[0], &c, 1)); 
+            if (ret > 0)
+                /* Callee must run async-safe code only. */
+                nf->callback(nf);
+        } while (ret > 0 || errno == EINTR);
 

In theory, we could receive a zero byte count on EOF, in which case we should not test errno, rather we should bail out immediately.

(quoted diff) +2 -1
@@ -1093,7 +1093,9 @@ int threadobj_sleep(struct timespec *ts)
      */
     current->run_state = __THREAD_S_DELAYED;
     threadobj_save_timeout(&current->core, ts);
-    ret = -__RT(clock_nanosleep(CLOCK_COPPERPLATE, TIMER_ABSTIME, ts, NULL));
+    do
+        ret = -__RT(clock_nanosleep(CLOCK_COPPERPLATE, TIMER_ABSTIME, ts, 
 NULL));
 
Quoted 7 lines

We should definitely pass and use the "remain" field in clock_nanosleep(), not to restart a complete wait each time we get interrupted.

Signature · 1 line
Philippe.

I have attached a new version of the patch leaving the loop on ret == 0. Note that this will be a new behavior in compared to before the patch.

Gilles already pointed out that TIMER_ABSTIME allows restarting the same syscall in threadobj_sleep.

Regards,
Matthias
-------------- next part --------------
A non-text attachment was scrubbed...
Name: eintr_4.patch
Type: text/x-patch
Size: 3055 bytes
Desc: not available
URL: <http://www.xenomai.org/pipermail/xenomai/attachments/20140407/439744ed/attachment.bin>

#11 lore raw
Quoted 93 lines · Matthias Schneider

----- Original Message -----

Quoted 84 lines

From: Philippe Gerum <[email protected]>
To: Matthias Schneider <[email protected]>; "[email protected]" <[email protected]> Cc:
Sent: Sunday, April 6, 2014 5:04 PM
Subject: Re: [Xenomai] EINTR in notifier.c (mercury)

Quoted 1 line · Matthias Schneider
Quoted 1 line

On Monday, March 31, 2014 7:50 PM, Gilles Chanteperdrix

Quoted 8 lines · <[email protected]> wrote:
Quoted 8 lines
Quoted 8 lines · Philippe Gerum
    On 03/31/2014 01:27 PM, Gilles Chanteperdrix wrote:
Quoted 5 lines · Matthias Schneider
Quoted 4 lines

Hi all,

still working on thread suspension in mercury, I noticed

that some

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

threadobj_suspend() and threadobj_resume() calls seemed

not to have the desired

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

effect. Analyzing the issue, I found out that sometimes

the read operations on

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

the pipe in notifier_wait() seem to return with EINTR,

especially in

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

heavily loaded systems. Restarting the read system call

in that case made

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

thread suspension a lot more reliable in my case.

I have attached a patch adding loops to deal with the

EINTR situation in all

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

You can probably avoid testing for EINTR if all signal

handlers are

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 2 lines

registered with the SA_RESTART flag.

The app may not explicitly care for signals (granted, most would

do, but

Quoted 14 lines
Quoted 6 lines
Quoted 2 lines

we would then have to assume they do it right).

Yes, applications may want to use signals to interrupt a call to read and voluntarily get EINTR anyway.
Gilles.

Please find attached a third version of my patch dealing with EINTR syscalls. Note that I also stumbled over unexpected behavior in threadobj_sleep and cancel_sync due to EINTRs. Considering that mercury is using signals for user-space round-robin and thread suspension, even without any application signals EINTR happens quite often.

(quoted diff) +6 -3
@@ -86,9 +88,13 @@ static void notifier_sighandler(int sig, siginfo_t *siginfo,
 void *uc)
         if (nf->owner && nf->owner != tid)
             continue;
 
-        while (__STD(read(nf->psfd[0], &c, 1)) > 0)
-            /* Callee must run async-safe code only. */
-            nf->callback(nf);
+        do {
+            ret = __STD(read(nf->psfd[0], &c, 1));
+            if (ret > 0)
+                /* Callee must run async-safe code only. */
+                nf->callback(nf);
+        } while (ret > 0 || errno == EINTR);
 

In theory, we could receive a zero byte count on EOF, in which case we should not test errno, rather we should bail out immediately.

(quoted diff) +2 -1
@@ -1093,7 +1093,9 @@ int threadobj_sleep(struct timespec *ts)
      */
     current->run_state = __THREAD_S_DELAYED;
     threadobj_save_timeout(&current->core, ts);
-    ret = -__RT(clock_nanosleep(CLOCK_COPPERPLATE, TIMER_ABSTIME, ts, NULL));
+    do
+        ret = -__RT(clock_nanosleep(CLOCK_COPPERPLATE, TIMER_ABSTIME, ts,
 NULL));
 

We should definitely pass and use the "remain" field in clock_nanosleep(), not to restart a complete wait each time we get interrupted.

Quoted 3 lines

--
Philippe.

I have attached a new version of the patch leaving the loop on ret == 0. Note that this will be a new behavior in compared to before the patch.

Quoted 3 lines

Gilles already pointed out that TIMER_ABSTIME allows restarting the same syscall in threadobj_sleep.

Merged, thanks.

Signature · 1 line
Philippe.
#12 lore raw
In reply to Matthias Schneider
Quoted 93 lines · Matthias Schneider

----- Original Message -----

Quoted 84 lines

From: Philippe Gerum <[email protected]>
To: Matthias Schneider <[email protected]>; "[email protected]" <[email protected]> Cc:
Sent: Sunday, April 6, 2014 5:04 PM
Subject: Re: [Xenomai] EINTR in notifier.c (mercury)

Quoted 1 line · Matthias Schneider
Quoted 1 line

On Monday, March 31, 2014 7:50 PM, Gilles Chanteperdrix

Quoted 8 lines · <[email protected]> wrote:
Quoted 8 lines
Quoted 8 lines · Philippe Gerum
    On 03/31/2014 01:27 PM, Gilles Chanteperdrix wrote:
Quoted 5 lines · Matthias Schneider
Quoted 4 lines

Hi all,

still working on thread suspension in mercury, I noticed

that some

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

threadobj_suspend() and threadobj_resume() calls seemed

not to have the desired

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

effect. Analyzing the issue, I found out that sometimes

the read operations on

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

the pipe in notifier_wait() seem to return with EINTR,

especially in

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

heavily loaded systems. Restarting the read system call

in that case made

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

thread suspension a lot more reliable in my case.

I have attached a patch adding loops to deal with the

EINTR situation in all

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

You can probably avoid testing for EINTR if all signal

handlers are

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 2 lines

registered with the SA_RESTART flag.

The app may not explicitly care for signals (granted, most would

do, but

Quoted 14 lines
Quoted 6 lines
Quoted 2 lines

we would then have to assume they do it right).

Yes, applications may want to use signals to interrupt a call to read and voluntarily get EINTR anyway.
Gilles.

Please find attached a third version of my patch dealing with EINTR syscalls. Note that I also stumbled over unexpected behavior in threadobj_sleep and cancel_sync due to EINTRs. Considering that mercury is using signals for user-space round-robin and thread suspension, even without any application signals EINTR happens quite often.

(quoted diff) +6 -3
@@ -86,9 +88,13 @@ static void notifier_sighandler(int sig, siginfo_t *siginfo,
 void *uc)
         if (nf->owner && nf->owner != tid)
             continue;
 
-        while (__STD(read(nf->psfd[0], &c, 1)) > 0)
-            /* Callee must run async-safe code only. */
-            nf->callback(nf);
+        do {
+            ret = __STD(read(nf->psfd[0], &c, 1));
+            if (ret > 0)
+                /* Callee must run async-safe code only. */
+                nf->callback(nf);
+        } while (ret > 0 || errno == EINTR);
 

In theory, we could receive a zero byte count on EOF, in which case we should not test errno, rather we should bail out immediately.

(quoted diff) +2 -1
@@ -1093,7 +1093,9 @@ int threadobj_sleep(struct timespec *ts)
      */
     current->run_state = __THREAD_S_DELAYED;
     threadobj_save_timeout(&current->core, ts);
-    ret = -__RT(clock_nanosleep(CLOCK_COPPERPLATE, TIMER_ABSTIME, ts, NULL));
+    do
+        ret = -__RT(clock_nanosleep(CLOCK_COPPERPLATE, TIMER_ABSTIME, ts,
 NULL));
 

We should definitely pass and use the "remain" field in clock_nanosleep(), not to restart a complete wait each time we get interrupted.

Quoted 3 lines

--
Philippe.

I have attached a new version of the patch leaving the loop on ret == 0. Note that this will be a new behavior in compared to before the patch.

Quoted 3 lines

Gilles already pointed out that TIMER_ABSTIME allows restarting the same syscall in threadobj_sleep.

Yes, but unfortunately, that part of the patch is still broken and introduced a regression of other copperplate-based APÏs. Several RTOS implement some kind of thread/task_unblock() service, which shall cause sleep calls based on threadobj_sleep() to return upon receiving the SIGRELS notification via a signal. This is the meaning of the comment heading that code.

So the proper fix should go to the call site in the pSOS emulator, instead of changing the generic behavior in threadobj_sleep().

Signature · 1 line
Philippe.
#13 lore raw
Quoted 114 lines · Philippe Gerum
Quoted 101 lines · Matthias Schneider

----- Original Message -----

Quoted 86 lines

From: Philippe Gerum <[email protected]>
To: Matthias Schneider <[email protected]>; "[email protected]" <[email protected]>
Cc:
Sent: Sunday, April 6, 2014 5:04 PM
Subject: Re: [Xenomai] EINTR in notifier.c (mercury)

Quoted 1 line · Matthias Schneider
Quoted 1 line

On Monday, March 31, 2014 7:50 PM, Gilles Chanteperdrix

Quoted 8 lines · <[email protected]> wrote:
Quoted 8 lines
Quoted 8 lines · Philippe Gerum
    On 03/31/2014 01:27 PM, Gilles Chanteperdrix wrote:
Quoted 5 lines · Matthias Schneider
Quoted 4 lines

Hi all,

still working on thread suspension in mercury, I noticed

that some

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

threadobj_suspend() and threadobj_resume() calls seemed

not to have the desired

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

effect. Analyzing the issue, I found out that sometimes

the read operations on

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

the pipe in notifier_wait() seem to return with EINTR,

especially in

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

heavily loaded systems. Restarting the read system call

in that case made

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

thread suspension a lot more reliable in my case.

I have attached a patch adding loops to deal with the

EINTR situation in all

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

You can probably avoid testing for EINTR if all signal

handlers are

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 2 lines

registered with the SA_RESTART flag.

The app may not explicitly care for signals (granted, most would

do, but

Quoted 15 lines
Quoted 7 lines
Quoted 2 lines

we would then have to assume they do it right).

Yes, applications may want to use signals to interrupt a call to
read
and voluntarily get EINTR anyway.
Gilles.

Please find attached a third version of my patch dealing with EINTR syscalls. Note that I also stumbled over unexpected behavior in threadobj_sleep and cancel_sync due to EINTRs. Considering that mercury is using signals for user-space round-robin and thread suspension, even without any application signals EINTR happens quite often.

(quoted diff) +6 -3
@@ -86,9 +88,13 @@ static void notifier_sighandler(int sig, siginfo_t
 *siginfo,
 void *uc)
         if (nf->owner && nf->owner != tid)
             continue;
 
-        while (__STD(read(nf->psfd[0], &c, 1)) > 0)
-            /* Callee must run async-safe code only. */
-            nf->callback(nf);
+        do {
+            ret = __STD(read(nf->psfd[0], &c, 1));
+            if (ret > 0)
+                /* Callee must run async-safe code only. */
+                nf->callback(nf);
+        } while (ret > 0 || errno == EINTR);
 

In theory, we could receive a zero byte count on EOF, in which case we should
not test errno, rather we should bail out immediately.

(quoted diff) +2 -1
@@ -1093,7 +1093,9 @@ int threadobj_sleep(struct timespec *ts)
      */
     current->run_state = __THREAD_S_DELAYED;
     threadobj_save_timeout(&current->core, ts);
-    ret = -__RT(clock_nanosleep(CLOCK_COPPERPLATE, TIMER_ABSTIME,
 ts, NULL));
+    do
+        ret = -__RT(clock_nanosleep(CLOCK_COPPERPLATE,
 TIMER_ABSTIME, ts,
 NULL));
Quoted 8 lines

We should definitely pass and use the "remain" field in clock_nanosleep(), not to restart a complete wait each time we get interrupted.

--
Philippe.

I have attached a new version of the patch leaving the loop on ret ==
0. Note
that this will be a new behavior in compared to before the patch.

Quoted 4 lines

Gilles already pointed out that TIMER_ABSTIME allows restarting the same syscall
in threadobj_sleep.

Yes, but unfortunately, that part of the patch is still broken and introduced a regression of other copperplate-based APÏs. Several RTOS implement some kind of thread/task_unblock() service, which shall cause sleep calls based on threadobj_sleep() to return upon receiving the SIGRELS notification via a signal. This is the meaning of the comment heading that code.

Quoted 3 lines

So the proper fix should go to the call site in the pSOS emulator, instead of changing the generic behavior in threadobj_sleep().

Or threadobj_sleep() should be made smarter, specifically detecting SIGRELS in the mercury case for aborting the wait.

Signature · 1 line
Philippe.
#14 lore raw

----- Original Message -----

Quoted 138 lines

From: Philippe Gerum <[email protected]>
To: Matthias Schneider <[email protected]>; "[email protected]" <[email protected]> Cc:
Sent: Friday, April 11, 2014 5:07 PM
Subject: Re: [Xenomai] EINTR in notifier.c (mercury)

Quoted 5 lines · Philippe Gerum
Quoted 4 lines · Matthias Schneider

----- Original Message -----

Quoted 2 lines

From: Philippe Gerum <[email protected]>
To: Matthias Schneider <[email protected]>;

"[email protected]"

Quoted 12 lines
Quoted 12 lines
Quoted 12 lines

<[email protected]>
Cc:
Sent: Sunday, April 6, 2014 5:04 PM
Subject: Re: [Xenomai] EINTR in notifier.c (mercury)

Quoted 1 line · Matthias Schneider
Quoted 1 line

  On Monday, March 31, 2014 7:50 PM, Gilles Chanteperdrix

Quoted 4 lines · <[email protected]> wrote:
Quoted 4 lines
Quoted 4 lines · Philippe Gerum
Quoted 1 line · Gilles Chanteperdrix

    On 03/31/2014 12:34 PM, Matthias Schneider

Quoted 4 lines · wrote:
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

    Hi all,

    still working on thread suspension in

mercury, I noticed

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

that some

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

    threadobj_suspend() and threadobj_resume()

calls seemed

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

not to have the desired

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

    effect. Analyzing the issue, I found out

that sometimes

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

the read operations on

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

    the pipe in notifier_wait() seem to return

with EINTR,

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

especially in

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

    heavily loaded systems. Restarting the read

system call

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

in that case made

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

    thread suspension a lot more reliable in my

case.

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

    I have attached a patch adding loops to

deal with the

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

EINTR situation in all

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

    You can probably avoid testing for EINTR if all

signal

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

handlers are

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 2 lines

    registered with the SA_RESTART flag.

    The app may not explicitly care for signals

(granted, most would

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

do, but

Quoted 4 lines
Quoted 4 lines
Quoted 2 lines

    we would then have to assume they do it right).

  Yes, applications may want to use signals to interrupt a

call to

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 3 lines

read
  and voluntarily get EINTR anyway.
                            Gilles.

  Please find attached a third version of my patch dealing with EINTR syscalls. Note that I also stumbled over unexpected

behavior

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

  in threadobj_sleep and cancel_sync due to EINTRs. Considering

that

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

  mercury is using signals for user-space round-robin and

thread

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

  suspension, even without any application signals EINTR

happens

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 1 line

  quite often.

@@ -86,9 +88,13 @@ static void notifier_sighandler(int sig,

siginfo_t

Quoted 55 lines
Quoted 39 lines
Quoted 30 lines

*siginfo,
void *uc)
          if (nf->owner && nf->owner != tid)
              continue;

-        while (__STD(read(nf->psfd[0], &c, 1)) > 0) -            /* Callee must run async-safe code only. */ -            nf->callback(nf);
+        do {
+            ret = __STD(read(nf->psfd[0], &c, 1)); +            if (ret > 0)
+                /* Callee must run async-safe code only. */ +                nf->callback(nf);
+        } while (ret > 0 || errno == EINTR);

In theory, we could receive a zero byte count on EOF, in which case we should
not test errno, rather we should bail out immediately.

@@ -1093,7 +1093,9 @@ int threadobj_sleep(struct timespec *ts)
      */
      current->run_state = __THREAD_S_DELAYED; threadobj_save_timeout(&current->core, ts);
-    ret = -__RT(clock_nanosleep(CLOCK_COPPERPLATE, TIMER_ABSTIME, ts, NULL));
+    do
+        ret = -__RT(clock_nanosleep(CLOCK_COPPERPLATE, TIMER_ABSTIME, ts,
NULL));

Quoted 8 lines

We should definitely pass and use the "remain" field in clock_nanosleep(), not to restart a complete wait each time we get interrupted.

--
Philippe.

Quoted 8 lines

I have attached a new version of the patch leaving the loop on ret ==
0. Note
that this will be a new behavior in compared to before the patch.

Gilles already pointed out that TIMER_ABSTIME allows restarting the same syscall
in threadobj_sleep.

Yes, but unfortunately, that part of the patch is still broken and introduced a regression of other copperplate-based APÏs. Several RTOS implement some kind of thread/task_unblock() service, which shall cause sleep calls based on threadobj_sleep() to return upon receiving the SIGRELS notification via a signal. This is the meaning of the comment heading that code.

Quoted 11 lines
Quoted 3 lines

So the proper fix should go to the call site in the pSOS emulator, instead of changing the generic behavior in threadobj_sleep().

Or threadobj_sleep() should be made smarter, specifically detecting SIGRELS in the mercury case for aborting the wait.

Signature · 1 line
Philippe.

I think I would prefer to encapsulate that in threadobj_sleep(). Would storing a flag in threadobj_current() in unblock_sighandler() and checking this in threadobj_sleep() be an option? Sorry for having overlooked this,

Matthias

#15 lore raw
Quoted 151 lines · Matthias Schneider

----- Original Message -----

Quoted 146 lines

From: Philippe Gerum <[email protected]>
To: Matthias Schneider <[email protected]>; "[email protected]" <[email protected]> Cc:
Sent: Friday, April 11, 2014 5:07 PM
Subject: Re: [Xenomai] EINTR in notifier.c (mercury)

Quoted 5 lines · Philippe Gerum
Quoted 4 lines · Matthias Schneider

----- Original Message -----

Quoted 2 lines

From: Philippe Gerum <[email protected]>
To: Matthias Schneider <[email protected]>;

"[email protected]"

Quoted 12 lines
Quoted 12 lines
Quoted 12 lines

<[email protected]>
Cc:
Sent: Sunday, April 6, 2014 5:04 PM
Subject: Re: [Xenomai] EINTR in notifier.c (mercury)

Quoted 1 line · Matthias Schneider
Quoted 1 line

On Monday, March 31, 2014 7:50 PM, Gilles Chanteperdrix

Quoted 4 lines · <[email protected]> wrote:
Quoted 4 lines
Quoted 4 lines · Philippe Gerum
      On 03/31/2014 01:27 PM, Gilles Chanteperdrix wrote:
Quoted 1 line
      On 03/31/2014 12:34 PM, Matthias Schneider
Quoted 4 lines · wrote:
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

Hi all,

still working on thread suspension in

mercury, I noticed

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

that some

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

threadobj_suspend() and threadobj_resume()

calls seemed

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

not to have the desired

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

effect. Analyzing the issue, I found out

that sometimes

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

the read operations on

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

the pipe in notifier_wait() seem to return

with EINTR,

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

especially in

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

heavily loaded systems. Restarting the read

system call

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

in that case made

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

thread suspension a lot more reliable in my

case.

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

I have attached a patch adding loops to

deal with the

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

EINTR situation in all

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

You can probably avoid testing for EINTR if all

signal

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

handlers are

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 2 lines

registered with the SA_RESTART flag.

The app may not explicitly care for signals

(granted, most would

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

do, but

Quoted 4 lines
Quoted 4 lines
Quoted 2 lines

we would then have to assume they do it right).

Yes, applications may want to use signals to interrupt a

call to

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 3 lines

read
and voluntarily get EINTR anyway.
Gilles.

Please find attached a third version of my patch dealing with EINTR syscalls. Note that I also stumbled over unexpected

behavior

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

in threadobj_sleep and cancel_sync due to EINTRs. Considering

that

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

mercury is using signals for user-space round-robin and

thread

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

suspension, even without any application signals EINTR

happens

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 1 line

quite often.

@@ -86,9 +88,13 @@ static void notifier_sighandler(int sig,

siginfo_t

Quoted 55 lines
Quoted 39 lines
Quoted 30 lines
  *siginfo,
  void *uc)
           if (nf->owner && nf->owner != tid)
               continue;

- while (__STD(read(nf->psfd[0], &c, 1)) > 0)
- /* Callee must run async-safe code only. */
- nf->callback(nf);
+ do {
+ ret = __STD(read(nf->psfd[0], &c, 1));
+ if (ret > 0)
+ /* Callee must run async-safe code only. */
+ nf->callback(nf);
+ } while (ret > 0 || errno == EINTR);

In theory, we could receive a zero byte count on EOF, in which case we should
not test errno, rather we should bail out immediately.

@@ -1093,7 +1093,9 @@ int threadobj_sleep(struct timespec *ts)

        */
       current->run_state = __THREAD_S_DELAYED;
       threadobj_save_timeout(&current->core, ts);

- ret = -__RT(clock_nanosleep(CLOCK_COPPERPLATE, TIMER_ABSTIME,
ts, NULL));
+ do
+ ret = -__RT(clock_nanosleep(CLOCK_COPPERPLATE,
TIMER_ABSTIME, ts,
NULL));

Quoted 8 lines

We should definitely pass and use the "remain" field in clock_nanosleep(), not to restart a complete wait each time we get interrupted.

--
Philippe.

Quoted 8 lines

I have attached a new version of the patch leaving the loop on ret ==
0. Note
that this will be a new behavior in compared to before the patch.

Gilles already pointed out that TIMER_ABSTIME allows restarting the same syscall
in threadobj_sleep.

Yes, but unfortunately, that part of the patch is still broken and introduced a regression of other copperplate-based APÏs. Several RTOS implement some kind of thread/task_unblock() service, which shall cause sleep calls based on threadobj_sleep() to return upon receiving the SIGRELS notification via a signal. This is the meaning of the comment heading that code.

Quoted 3 lines

So the proper fix should go to the call site in the pSOS emulator, instead of changing the generic behavior in threadobj_sleep().

Or threadobj_sleep() should be made smarter, specifically detecting SIGRELS in the mercury case for aborting the wait.

Quoted 7 lines
Quoted 3 lines

--
Philippe.

I think I would prefer to encapsulate that in threadobj_sleep(). Would storing a flag in threadobj_current() in unblock_sighandler() and checking this in threadobj_sleep() be an option?

We'll have to mate threadobj_unblock() and threadobj_sleep() logically instead, to make this work for both cobalt and mercury cores, but yes, using a TCB-based flag for handling this specific problem is the way to go. This way, only SIGRELS notifications would make threadobj_sleep() abort with EINTR as expected, silently resuming sleep after other signals.

Since the pSOS emulator should never receive SIGRELS, we should not have to handle EINTR from tm_wkafter/tm_wkwhen() actually.

I'm working on a patch.

Sorry for having

Quoted 2 lines

overlooked this,

No problem, copperplate still lacks documentation anyway.

Signature · 1 line
Philippe.