Problem of thread creation with Xenomai Posix 2
OK, thank you.
I wrote a new program in which there is no call to my function Console. There is just calls to printf at the end of the program. Thus, we can be sure that the problem comes from the pthread_create call.
New program :
#include <sys/mman.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/time.h>
#include <string.h>
#include <stdio.h>
#define NB_ELEMENTS 50
int j;
char Text [NB_ELEMENTS];
int func(volatile int* i)
{
return (*i)++;
}void AddConsole (char val)
{
Text[j] = val;
j++;
}void ReadTabConsole (void)
{
int i;
int NbConsole = j;
for (i=0; i<NbConsole; i++)
{
printf("%d\n",Text[i]);
j--;
}
}void* thread1(void * context) {
volatile int i;
volatile int result;
AddConsole(5);
while (i < 1000) {
result = func(&i);
}
AddConsole(6);
return NULL;
}void* thread2(void * context) {
volatile int i;
volatile int result;
AddConsole(7);
while (i < 1000) {
result = func(&i);
}
AddConsole(8);
// Lecture du tableau de console
ReadTabConsole();
return NULL;
}void* thread_main(void *arg) {
pthread_attr_t attr;
pthread_t p1;
pthread_t p2;
struct sched_param sch;
struct sched_param sch1;
int result;
pthread_attr_init(&attr);
pthread_attr_setinheritsched(&attr,PTHREAD_EXPLICIT_SCHED);
pthread_attr_setschedpolicy(&attr, SCHED_FIFO);
sch.sched_priority = 25;
pthread_attr_setschedparam(&attr, &sch);
AddConsole(1);
pthread_create(&p1, &attr, thread1, NULL);
AddConsole(2);
sch.sched_priority = 20;
pthread_attr_setschedparam(&attr, &sch);
AddConsole(3);
result = pthread_create(&p2, &attr, thread2, NULL);
if (result != 0)
{
AjoutConsole(0);
}
AddConsole(4);
pthread_attr_destroy(&attr);
while (1) {
sleep(500);
}
return NULL;
}int main(int argc, char** argv) {
j = 0;
pthread_attr_t attr;
pthread_t p;
struct sched_param sch;
mlockall(MCL_CURRENT|MCL_FUTURE);
pthread_attr_init(&attr);
pthread_attr_setinheritsched(&attr,PTHREAD_EXPLICIT_SCHED);
pthread_attr_setschedpolicy(&attr, SCHED_FIFO);
sch.sched_priority = 30;
pthread_attr_setschedparam(&attr, &sch);
pthread_create(&p, &attr, thread_main, NULL);
pthread_attr_destroy(&attr);
while (1) {
sleep(500);
}
return 0;
}Result in Linux environment(Linux 2.6.20 kernel configurated preemptive) :
1
2
3
4
5
6
7
8
Result in Xenomai environment(Xenomai 2.3.4 kernel) :
1
2
3
5
6
4
7
8
OK, we can see that there is the same problem. T1 thread prempts Main Thread during the pthread_create of T2 with Xenomai and not with Linux standard. I would like to know why.
I don't that there is a difference beetween the posix Linux pthread_create and the posix Xenomai pthread_create ?
If pthread_create suspends a short time its caller, why we don t have the same behaviour in the two cases ?
In my opinion, there is a problem with Xenomai, no ?
I hope that you could help me. thank you
On Fri, Feb 15, 2008 at 9:10 AM, Fabien MAHOT > OK, we can see that there is the same problem. T1 thread prempts Main
Quoted 6 lines
Thread during the
pthread_createof T2 with Xenomai and not with Linux standard. I would like to know why.I don't that there is a difference beetween the posix Linux
pthread_createand the posix Xenomaipthread_create?
Quoted 6 lines
If
pthread_createsuspends a short time its caller, why we don t have the same behaviour in the two cases ?In my opinion, there is a problem with Xenomai, no ?
I hope that you could help me. thank you
Sorry, I did not make myself clear: it is xenomai implementation of pthread_create which suspends its caller. But as I said in my previous answer this does not contradict the posix standard as far as I can tell. This implementation needs to work around two different glibc bugs, which explains how it is implemented.
Signature · 1 line
Gilles Chanteperdrix
Quoted 7 lines · Fabien MAHOT
OK, we can see that there is the same problem. T1 thread prempts Main Thread during the
pthread_createof T2 with Xenomai and not with Linux standard. I would like to know why.I don't that there is a difference beetween the posix Linux
pthread_createand the posix Xenomaipthread_create?
Quoted 6 lines
If
pthread_createsuspends a short time its caller, why we don t have the same behaviour in the two cases ?In my opinion, there is a problem with Xenomai, no ?
I hope that you could help me. thank you
Could you try the attached patch ? It should give you the illusion that pthread_create caller is not suspended if your glibc has none of the bugs we worked around.
That said, I really think that you should not rely on this behaviour of pthread_create, because it is not garanteed by the posix standard, besides, it will not work with broken glibcs. And in fact, a usual rule is not to rely on scheduler priorities for mutual exclusion, but rather to make it explicit by using mutex or semaphores.
Signature · 1 line
Gilles Chanteperdrix
skins/posix/thread.c +43 -27
@@ -25,6 +25,8 @@extern int __pse51_muxid; +static pthread_attr_t default_attr; +static void (*old_sigharden_handler)(int sig);static void __pthread_sigharden_handler (int sig)
@@ -95,12 +97,8 @@ static void *__pthread_trampoline(void * old_sigharden_handler =signal (SIGHARDEN , &__pthread_sigharden_handler); -/* Broken pthread libs ignore some of the thread attribute specs -passed to pthread_create(3), so we force the scheduling policy -once again here. */ param.sched_priority = iargs->prio; policy = iargs->policy; -__real_pthread_setschedparam (tid, policy, ¶m);/* Do _not_ inline the call to pthread_self() in the syscall macro: this trashes the syscall regs on some archs. */
@@ -118,6 +116,11 @@ static void *__pthread_trampoline(void *__real_sem_post (&iargs->sync);if (!err) { +/* Broken pthread libs ignore some of the thread attribute specs +passed to pthread_create(3), so we force the scheduling policy +once again here. */ +__real_pthread_setschedparam (tid, policy, ¶m); +if (policy !=SCHED_OTHER )XENOMAI_SYSCALL1 (__xn_sys_migrate,XENOMAI_XENO_DOMAIN ); status =start (cookie);
@@ -137,29 +140,24 @@ int __wrap_pthread_create(pthread_t *tidstruct sched_param param;pthread_t ltid; -if (!attr) { - policy =SCHED_OTHER ; - iargs.prio =0 ; - }else { -pthread_attr_getinheritsched (attr, &inherit); -if (inherit ==PTHREAD_EXPLICIT_SCHED ) { -pthread_attr_getschedpolicy (attr, &policy); -pthread_attr_getschedparam (attr, ¶m); - }else -/* inherit == PTHREAD_INHERIT_SCHED */ -__wrap_pthread_getschedparam (pthread_self (), - &policy, ¶m); - iargs.prio = param.sched_priority; - -/* Work around linuxthreads shortcoming: it doesn't believe -that it could have RT power as non-root and fails the -thread creation overeagerly. */ -memcpy (&iattr, attr,sizeof (pthread_attr_t)); - param.sched_priority =0 ; -pthread_attr_setschedpolicy (&iattr,SCHED_OTHER ); -pthread_attr_setschedparam (&iattr, ¶m); - attr = &iattr; - } +if (!attr) + attr = &default_attr; + +pthread_attr_getinheritsched (attr, &inherit); +if (inherit ==PTHREAD_EXPLICIT_SCHED ) { +pthread_attr_getschedpolicy (attr, &policy); +pthread_attr_getschedparam (attr, ¶m); + }else +/* inherit == PTHREAD_INHERIT_SCHED */ +__wrap_pthread_getschedparam (pthread_self (), + &policy, ¶m); + iargs.prio = param.sched_priority; + +/* Get the created thread to temporarily inherit pthread_create caller +priority */ +memcpy (&iattr, attr,sizeof (pthread_attr_t)); +pthread_attr_setinheritsched (&iattr,PTHREAD_INHERIT_SCHED ); + attr = &iattr;/* First start a native POSIX thread, then associate a Xenomai shadow to it. */
@@ -171,6 +169,19 @@ int __wrap_pthread_create(pthread_t *tid__real_sem_init (&iargs.sync,0 ,0 ); err =__real_pthread_create (<id, attr, &__pthread_trampoline, &iargs); +if (err ==EPERM ) { +/* Work around linuxthreads shortcoming: it doesn't believe +that it could have RT power as non-root and fails the +thread creation overeagerly. */ +pthread_attr_setinheritsched (&iattr,PTHREAD_EXPLICIT_SCHED ); + param.sched_priority =0 ; +pthread_attr_setschedpolicy (&iattr,SCHED_OTHER ); +pthread_attr_setschedparam (&iattr, ¶m); + + err =__real_pthread_create (<id, attr, + &__pthread_trampoline, &iargs); + } +if (!err)while (__real_sem_wait (&iargs.sync) && errno ==EINTR ) ;__real_sem_destroy (&iargs.sync);
@@ -257,3 +268,8 @@ int __wrap_pthread_kill(pthread_t threadreturn err; } + +static __attribute__((constructor))void pse51_thread_init (void ) +{ +pthread_attr_init (&default_attr); +}