pthread_mutex_destroy in 2.2 rc2
As you suggested, I've upgraded to xenomai 2.2 rc3.
I still see the same phenomenon.
I'm attaching code that will reproduce the error.
(When you ^c out of the main loop, the pthread_cleanup_pop is called, and the call to pthread_mutex_destroy returns 0x10.)
//******************************************************************** #include <termios.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/signal.h>
#include <sys/types.h>
#include <sys/select.h>
#include <error.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <time.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/ioctl.h>
#include <netdb.h>
#include <pthread.h>
#include <unistd.h>
#include <mqueue.h>
#include <sys/mman.h>pthread_t Main_Exec_thread_id;
#define DPRINT(arg) printf arg
pthread_mutex_t testmutex;
#define MAIN_TASK_PRI 1
#define MAIN_STACK_SIZE 8192
pthread_attr_t Main_Exec_attr;
struct sched_param Main_Exec_parm;
int Main_PID;
void shut_down (void *par)
{
printf ("shutting down, pid = %08x\n", getpid()) ;
int res = pthread_mutex_destroy (&testmutex);
printf ("pthread_mutex_destroy returned %08x\n", res) ;
printf ("MAIN EXEC THREAD EXITING!\n") ;}
void error_exit (int sval)
{
void *val;
DPRINT( ("error_exit, pid = %08x, signal = %08x\n", getpid (), sval)); if (getpid () == Main_PID)
{
printf ("cancelling main thread\n");
pthread_cancel (Main_Exec_thread_id);
printf ("waiting for main thread to exit\n");
pthread_join (Main_Exec_thread_id, &val);
printf ("exiting\n") ; }
}
void *MainExecThread (void *dummy)
{
printf ("Main Exec Thread starting, pid = %08x\n", getpid()) ;
pthread_cleanup_push ((void(*)(void*))shut_down, NULL);
pthread_mutexattr_t attrib;
int status = pthread_mutexattr_init (&attrib);
if (status != 0)
DPRINT( ("pthread_mutexattr_init error\n"));
status = pthread_mutex_init (&testmutex, &attrib);
if (status != 0)
DPRINT( ("pthread_mutex_init error\n") );
pause ();
DPRINT( ("MAIN EXEC PAUSE RETURNED\n") );
pthread_cleanup_pop (true);
return 0;
}int main (int argc, char *argv[])
{
Main_PID = getpid ();
printf ("main pid = %08x\n", Main_PID) ;
signal (SIGINT, error_exit);
signal (SIGQUIT, error_exit);
signal (SIGHUP, error_exit);
signal (SIGTERM, error_exit);
mlockall (MCL_CURRENT | MCL_FUTURE);
pthread_attr_init (&Main_Exec_attr);
pthread_attr_setinheritsched(&Main_Exec_attr, 1);
pthread_attr_setschedpolicy(&Main_Exec_attr, SCHED_FIFO);
pthread_attr_setstacksize(&Main_Exec_attr, MAIN_STACK_SIZE); Main_Exec_parm.sched_priority = MAIN_TASK_PRI; pthread_attr_setschedparam(&Main_Exec_attr, &Main_Exec_parm);
pthread_create (&Main_Exec_thread_id, &Main_Exec_attr, (void*(*)(void*))MainExecThread, 0);
pause ();
DPRINT( ("MAIN PAUSE RETURNED\n") );
exit (0);-----Original Message-----
From: Gilles Chanteperdrix [mailto:[email protected]]
Sent: Thursday, June 29, 2006 7:17 PM
To: Landau, Bracha
Cc: [email protected]
Subject: Re: [Xenomai-help] pthread_mutex_destroy in 2.2 rc2
Quoted 1 line · Bracha
I'm using Xenomai 2.2 rc2 on an MPC8247 board.
You should be using 2.2 rc3, it implements per-process cleanup, so even if pthread_mutex_destroy fails, if the mutex is not shared between several processes, it will automatically be destroyed when the application terminates.
Quoted 2 lines
When I try to destroy a mutex I get error x10 (EBUSY). The mutex is not locked.
EBUSY is also returned if the mutex is currently "bound" to a condition variable, that is, if some thread is currently blocked in a call to pthread_cond_wait using the mutex as second argument. In this case, the application is expected to cancel the thread blocked in the call to pthread_cond_wait, the said thread is expected to have pthread_cleanup_pushed a cleanup function unlocking the mutex. Only then the application can destroy the mutex with pthread_mutex_destroy.
When I run the same code with the regular pthreads library the function does not return an error.
Quoted 1 line
Is this a Xenomai bug?
Maybe, maybe not. Could you provide a small program showing the bug ?
Signature · 4 lines
Gilles Chanteperdrix. *********************************************************************************** This email message and any attachments thereto are intended only for use by the addressee(s) named above, and may contain legally privileged and/or confidential information. If the reader of this message is not the intended recipient, or the employee or agent responsible to deliver it to the intended recipient, you are hereby notified that any dissemination, distribution or copying of this communication is strictly prohibited. If you have received this communication in error, please immediately notify the [email protected] and destroy the original message. ***********************************************************************************
Quoted 4 lines · Bracha
As you suggested, I've upgraded to xenomai 2.2 rc3.
I still see the same phenomenon.
I'm attaching code that will reproduce the error.
(When you ^c out of the main loop, thepthread_cleanup_popis called, and the call topthread_mutex_destroyreturns0x10.)
I did not make myself clear...
I did not meant that 2.2 rc3 would solve your issue, simply that it has automatic per-process cleanup, so, it does not matter if your application fail to destroy some condition variables, the condition variable is destroyed automatically anyway. It is simpler for you to work with this version as long as the bug you met is not solved.
As for the pthread_cleanup_push/pthread_cleanup_pop, it was meant to be the solution if the EBUSY had been due to a thread blocked in a call to pthread_cond_wait during the application cleanup.
I have a look at your code.
Signature · 1 line
Gilles Chanteperdrix.
Quoted 4 lines · Bracha
As you suggested, I've upgraded to xenomai 2.2 rc3.
I still see the same phenomenon.
I'm attaching code that will reproduce the error.
(When you ^c out of the main loop, thepthread_cleanup_popis called, and the call topthread_mutex_destroyreturns0x10.)
Thanks for the piece of code. The error was due to a missing initialization, that got unseen under simulation because simulator allocated memory was always zero. It is now fixed in the repository. You can apply the attached patch to 2.2 rc3 to fix it.
Signature · 1 line
Gilles Chanteperdrix.
skins/posix/mutex.c +1 -0
@@ -132,6 +132,7 @@inith (&mutex->link); mutex->attr = *attr; mutex->count =0 ; + mutex->condvars =0 ;appendq (mutexq, &mutex->link);