You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by GitBox <gi...@apache.org> on 2021/09/26 12:14:45 UTC

[GitHub] [incubator-nuttx] patacongo opened a new issue #4612: exit() and pthread_exit() limitations

patacongo opened a new issue #4612:
URL: https://github.com/apache/incubator-nuttx/issues/4612


   The main thread of a task should be able to call pthread_exit(), of course, calling pthread_self() first.  Certainly this behavior is not prohibited by the specifications at OpenGroup.org and there are several posts that specifically claim that this is possible.  See for example:  https://programmer.help/blogs/the-use-of-pthread-exit-in-main-thread.html
   
   In this case, the main thread of the process/task group will terminate, but the process/task group will persist and other threads will continue running.  I believe that calling pthread_exit() from the main thread will currently lead to an assertion or crash due to the different forms of the main and pthread TCBs and the fact that global data is retained in the main thread's stack.
   
   Similarly, a pthread should be able to call exit() and terminate the entire process/  See for example https://www.informit.com/articles/article.aspx?p=2085690&seqNum=5.  In fact, this is often the case when exit() is called from a signal handler in a multi-threaded task because we never know for sure which thread the signal handler will run on.
   
   Again, a crash would be likely due to the same reasoning.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] patacongo edited a comment on issue #4612: exit() and pthread_exit() limitations

Posted by GitBox <gi...@apache.org>.
patacongo edited a comment on issue #4612:
URL: https://github.com/apache/incubator-nuttx/issues/4612#issuecomment-927310808


   Wouldn't you also need some kind of reference count on the task data so that it persists until the last thread exits?  Where would the task data reside if the main thread calls pthread_exit()?


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] xiaoxiang781216 commented on issue #4612: exit() and pthread_exit() limitations

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 commented on issue #4612:
URL: https://github.com/apache/incubator-nuttx/issues/4612#issuecomment-927322056


   The reference count is already there: group_s(and then task_info_s) get destroyed only after the last thread in the process exit.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] patacongo commented on issue #4612: exit() and pthread_exit() limitations

Posted by GitBox <gi...@apache.org>.
patacongo commented on issue #4612:
URL: https://github.com/apache/incubator-nuttx/issues/4612#issuecomment-927310808


   Wouldn't you also need some kind of reference count on the task data so that it persists until the last thread exits?


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] patacongo closed issue #4612: exit() and pthread_exit() limitations

Posted by GitBox <gi...@apache.org>.
patacongo closed issue #4612:
URL: https://github.com/apache/incubator-nuttx/issues/4612


   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] xiaoxiang781216 edited a comment on issue #4612: exit() and pthread_exit() limitations

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 edited a comment on issue #4612:
URL: https://github.com/apache/incubator-nuttx/issues/4612#issuecomment-927301640


   > The main thread of a task should be able to call pthread_exit(), of course, calling pthread_self() first. Certainly this behavior is not prohibited by the specifications at OpenGroup.org and there are several posts that specifically claim that this is possible. See for example: https://programmer.help/blogs/the-use-of-pthread-exit-in-main-thread.html
   > 
   > In this case, the main thread of the process/task group will terminate, but the process/task group will persist and other threads will continue running. I believe that calling pthread_exit() from the main thread will currently lead to an assertion or crash due to the different forms of the main and pthread TCBs
   
   Yes, this is major reason why we can't handle main thread and pthread equally. The best fix could be unify two tcb struct into one and:
   
   1. Move main thread specific data to task_info_s
   2. Move pthread specific data to tls_info_s
   
   > and the fact that global data is retained in the main thread's stack.
   > 
   
   It's fixed by patch: https://github.com/apache/incubator-nuttx/pull/3992
   
   > Similarly, a pthread should be able to call exit() and terminate the entire process/ See for example https://www.informit.com/articles/article.aspx?p=2085690&seqNum=5. In fact, this is often the case when exit() is called from a signal handler in a multi-threaded task because we never know for sure which thread the signal handler will run on.
   > 
   > Again, a crash would be likely due to the same reasoning.
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] patacongo commented on issue #4612: exit() and pthread_exit() limitations

Posted by GitBox <gi...@apache.org>.
patacongo commented on issue #4612:
URL: https://github.com/apache/incubator-nuttx/issues/4612#issuecomment-927339156


   Here is a test case:
   
   ```
   static void *thread_func(FAR void *parameter)
   {
     /* Wait a bit then call exit */
   
     printf("Child: Started\n");
     sleep(1);
   
     printf("Child: exit()\n");
     exit(0);
   }
   
   int main(int argc, FAR char *argv[])
   {
     pthread_t thread;
     int status;
   
     /* Start the thread instances */
   
     printf("main: Starting thread\n");
     status = pthread_create(&thread, NULL, thread_func, (pthread_addr_t)NULL);
     if (status != 0)
       {
         printf("ERROR in thread creation: %d\n", status);
       }
   
     /* And exit the main thread */
   
     printf("main: pthread_exit()\n");
     pthread_exit(NULL);
   }
   
   ```
   When I run this test, I get:
   
   ```
   nsh> exit
   main: Starting thread
   Child: Started
   main: pthread_exit()
   nsh> Child: exit()
   
   nsh>
   ```
   
   So I conclude that there is no problem.  Sorry for the false alarm.  I will close this issue now.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] patacongo commented on issue #4612: exit() and pthread_exit() limitations

Posted by GitBox <gi...@apache.org>.
patacongo commented on issue #4612:
URL: https://github.com/apache/incubator-nuttx/issues/4612#issuecomment-927330675


   Should this issue be closed?  Is there no longer an issue?
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] xiaoxiang781216 commented on issue #4612: exit() and pthread_exit() limitations

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 commented on issue #4612:
URL: https://github.com/apache/incubator-nuttx/issues/4612#issuecomment-927491299


   I could like to add a new issue to unify task and thread tcb into one.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] xiaoxiang781216 commented on issue #4612: exit() and pthread_exit() limitations

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 commented on issue #4612:
URL: https://github.com/apache/incubator-nuttx/issues/4612#issuecomment-927301640


   > The main thread of a task should be able to call pthread_exit(), of course, calling pthread_self() first. Certainly this behavior is not prohibited by the specifications at OpenGroup.org and there are several posts that specifically claim that this is possible. See for example: https://programmer.help/blogs/the-use-of-pthread-exit-in-main-thread.html
   > 
   > In this case, the main thread of the process/task group will terminate, but the process/task group will persist and other threads will continue running. I believe that calling pthread_exit() from the main thread will currently lead to an assertion or crash due to the different forms of the main and pthread TCBs
   
   Yes, this is major reason why we can't handle main thread and pthread equally. The best fix could be unify two tcb struct into one and:
   
   1. Move main thread specific data to task_info
   2. Move pthread specific data to tls_info
   
   > and the fact that global data is retained in the main thread's stack.
   > 
   
   It's fixed by patch: https://github.com/apache/incubator-nuttx/pull/3992
   
   > Similarly, a pthread should be able to call exit() and terminate the entire process/ See for example https://www.informit.com/articles/article.aspx?p=2085690&seqNum=5. In fact, this is often the case when exit() is called from a signal handler in a multi-threaded task because we never know for sure which thread the signal handler will run on.
   > 
   > Again, a crash would be likely due to the same reasoning.
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org