On Mon, Feb 25, 2008 at 03:01:39PM +0530, Chirag Jog wrote:
> Hi,
>        The pi-tests don't use the librttest infrastructure and simply
>        duplicate code.
>        This patch ensures that those tests use librttest

The same comments apply to this patch as well. pthread_create takes
an argument of the thread struct and that is as designed.
This patch needs to be redone

        -Dinakar



> 
> 
> sbrk_mutex.c |   21 +------
> testpi-1.c   |  160 ++++++++++-----------------------------------------------
> testpi-2.c   |  162 ++++++++++-----------------------------------------------
> testpi-4.c   |  165 
> +++++++++++------------------------------------------------
> testpi-5.c   |   10 +--
> testpi-6.c   |    9 +--
> 6 files changed, 101 insertions(+), 426 deletions(-)
> 
> Signed-Off-By: Chirag <[EMAIL PROTECTED]>
> 
> 
> 
> diff --git a/testcases/realtime/func/pi-tests/sbrk_mutex.c 
> b/testcases/realtime/func/pi-tests/sbrk_mutex.c
> index 5d6aeb5..9b64105 100644
> --- a/testcases/realtime/func/pi-tests/sbrk_mutex.c
> +++ b/testcases/realtime/func/pi-tests/sbrk_mutex.c
> @@ -55,7 +55,6 @@
>  #define DELAY 1000 /* how long to sleep in the worker thread in us */
> 
>  static pthread_mutex_t *mutexes[NUM_MUTEXES];
> -static pthread_t threads[NUM_THREADS];
> 
>  static int run_jvmsim=0;
> 
> @@ -149,28 +148,14 @@ int main(int argc, char* argv[])
> 
>       /* start children threads to walk the array, grabbing the locks */
>       for (t = 0; t < NUM_THREADS; t++) {
> -             pthread_attr_t attr;
> -             struct sched_param param;
> -
> -             param.sched_priority = sched_get_priority_min(SCHED_FIFO);
> -             pthread_attr_init(&attr);
> -             pthread_attr_setinheritsched (&attr, PTHREAD_EXPLICIT_SCHED);
> -             pthread_attr_setschedparam(&attr, &param);
> -             pthread_attr_setschedpolicy(&attr, SCHED_FIFO);
> -
> -             if (pthread_create(&threads[t], &attr, worker_thread, 
> (void*)t)) {
> -                     perror("pthread_create failed");
> +             if (create_fifo_thread(worker_thread, (void*)t, 
> sched_get_priority_min(SCHED_FIFO))) {
> +                     perror("create_fifo_thread failed");
>               }
> -
> -             pthread_attr_destroy(&attr);
>       }
> 
>       /* wait for the children to complete */
>       printf("joining threads\n");
> -     for (t = 0; t < NUM_THREADS; t++) {
> -             pthread_join(threads[t], NULL);
> -     }
> -
> +     join_threads();
>       /* destroy all the mutexes */
>       for (m = 0; m < NUM_MUTEXES; m++) {
>               if (mutexes[m]) {
> diff --git a/testcases/realtime/func/pi-tests/testpi-1.c 
> b/testcases/realtime/func/pi-tests/testpi-1.c
> index 0c0b62a..90f42b7 100644
> --- a/testcases/realtime/func/pi-tests/testpi-1.c
> +++ b/testcases/realtime/func/pi-tests/testpi-1.c
> @@ -88,29 +88,11 @@ typedef void* (*entrypoint_t)(void*);
> 
>  pthread_mutex_t glob_mutex;
> 
> -/*typedef struct thread {
> -  int priority;
> -  int policy;
> -  entrypoint_t func;
> -  pthread_attr_t attr;
> -  pthread_t handle;
> -  pthread_mutex_t mutex;
> -  pthread_cond_t cond;
> -  int flags;
> -  int count;
> -} Thread;*/
> -
>  typedef struct thread Thread;
> 
> -Thread arg1, arg2, arg3, arg4, arg5;
> -
> -int strartThread(Thread* thr);
> -void stopThread(Thread* thr);
> -void joinThread(Thread* thr);
> -
> -void* func_nonrt(void* arg)
> +void* func_nonrt()
>  {
> -  Thread* pthr = (Thread*)arg;
> +  Thread thr, *pthr = &thr;          
>    int rc, i, j, policy, tid = gettid();
>    struct sched_param schedp;
>  //  unsigned long mask = 1;
> @@ -118,6 +100,7 @@ void* func_nonrt(void* arg)
>    CPU_ZERO(&mask);
>    CPU_SET(0,&mask);
> 
> +  pthr->pthread = pthread_self();
>    rc = sched_setaffinity(0, sizeof(mask), &mask);
>    if (rc < 0) {
>       printf("Thread %d: Can't set affinity: %d %s\n", tid, rc, strerror(rc));
> @@ -125,12 +108,12 @@ void* func_nonrt(void* arg)
>    }
>    rc = sched_getaffinity(0, sizeof(mask), &mask);
> 
> -  printf("Thread started %d on CPU %ld\n", pthr->priority, 
> (long)mask.__bits);
> +  printf("Thread started %d on CPU %ld\n", schedp.sched_priority, 
> (long)mask.__bits);
>    pthread_getschedparam(pthr->pthread, &policy, &schedp);
> -  printf("Thread running %d\n", pthr->priority);
> +  printf("Thread running %d\n", schedp.sched_priority);
> 
>    pthread_mutex_lock(&glob_mutex);
> -  printf("Thread %d at start pthread pol %d pri %d - Got global lock\n", 
> pthr->priority,
> +  printf("Thread %d at start pthread pol %d pri %d - Got global lock\n", 
> schedp.sched_priority,
>          policy, schedp.sched_priority);
> 
>    pthread_barrier_wait(&barrier);
> @@ -139,7 +122,7 @@ void* func_nonrt(void* arg)
>      if (i%100 == 0) {
>        sched_getparam(tid, &schedp);
>        policy = sched_getscheduler(tid);
> -      printf("Thread %d(%d) loop %d pthread pol %d pri %d\n", tid, 
> pthr->priority, i,
> +      printf("Thread %d(%d) loop %d pthread pol %d pri %d\n", tid, 
> schedp.sched_priority, i,
>          policy, schedp.sched_priority);
>        fflush(NULL);
>      }
> @@ -149,7 +132,7 @@ void* func_nonrt(void* arg)
>        pthread_mutex_unlock(&(pthr->mutex));
>      }
>      if ((pthr->flags & THREAD_STOP) != 0 ) {
> -      printf("Stopping thread %d\n", pthr->priority);
> +      printf("Stopping thread %d\n", schedp.sched_priority);
>        break;
>      }
>    }
> @@ -157,15 +140,17 @@ void* func_nonrt(void* arg)
>    return NULL;
>  }
> 
> -void* func_rt(void* arg)
> +void* func_rt()
>  {
> -  Thread* pthr = (Thread*)arg;
> +  Thread thr, *pthr = &thr;
>    int rc, i, j, policy, tid = gettid();
>    struct sched_param schedp;
>    cpu_set_t mask;
>    CPU_ZERO(&mask);
>    CPU_SET(0,&mask);
> 
> +  pthr->pthread = pthread_self();
> +  pthr->pthread = pthread_self();
>    rc = sched_setaffinity(0, sizeof(mask), &mask);
>    if (rc < 0) {
>       printf("Thread %d: Can't set affinity: %d %s\n", tid, rc, strerror(rc));
> @@ -173,14 +158,14 @@ void* func_rt(void* arg)
>    }
>    rc = sched_getaffinity(0, sizeof(mask), &mask);
> 
> -  printf("Thread started %d on CPU %ld\n", pthr->priority, 
> (long)mask.__bits);
> +  printf("Thread started %d on CPU %ld\n", schedp.sched_priority, 
> (long)mask.__bits);
>    pthread_getschedparam(pthr->pthread, &policy, &schedp);
> 
>    pthread_barrier_wait(&barrier);
> 
> -  printf("Thread running %d\n", pthr->priority);
> +  printf("Thread running %d\n", schedp.sched_priority);
>    pthread_mutex_lock(&glob_mutex);
> -  printf("Thread %d at start pthread pol %d pri %d - Got global lock\n", 
> pthr->priority,
> +  printf("Thread %d at start pthread pol %d pri %d - Got global lock\n", 
> schedp.sched_priority,
>          policy, schedp.sched_priority);
> 
>    /* we just use the mutex as something to slow things down */
> @@ -192,7 +177,7 @@ void* func_rt(void* arg)
>      if (i%100 == 0) {
>        sched_getparam(tid, &schedp);
>        policy = sched_getscheduler(tid);
> -      printf("Thread %d(%d) loop %d pthread pol %d pri %d\n", tid, 
> pthr->priority, i,
> +      printf("Thread %d(%d) loop %d pthread pol %d pri %d\n", tid, 
> schedp.sched_priority, i,
>          policy, schedp.sched_priority);
>        fflush(NULL);
>      }
> @@ -202,7 +187,7 @@ void* func_rt(void* arg)
>        pthread_mutex_unlock(&(pthr->mutex));
>      }
>      if ((pthr->flags & THREAD_STOP) != 0 ) {
> -      printf("Stopping thread %d\n", pthr->priority);
> +      printf("Stopping thread %d\n", schedp.sched_priority);
>        break;
>      }
>    }
> @@ -210,15 +195,16 @@ void* func_rt(void* arg)
>    return NULL;
>  }
> 
> -void* func_noise(void* arg)
> +void* func_noise()
>  {
> -  Thread* pthr = (Thread*)arg;
> +  Thread thr, *pthr = &thr;
>    int rc, i, j, policy, tid = gettid();
>    struct sched_param schedp;
>    cpu_set_t mask;
>    CPU_ZERO(&mask);
>    CPU_SET(0, &mask);
> 
> +  pthr->pthread = pthread_self();
>    rc = sched_setaffinity(0, sizeof(mask), &mask);
>    if (rc < 0) {
>       printf("Thread %d: Can't set affinity: %d %s\n", tid, rc, strerror(rc));
> @@ -226,18 +212,18 @@ void* func_noise(void* arg)
>    }
>    rc = sched_getaffinity(0, sizeof(mask), &mask);
> 
> -  printf("Noise Thread started %d on CPU %ld\n", pthr->priority, 
> (long)mask.__bits);
> +  printf("Noise Thread started %d on CPU %ld\n", schedp.sched_priority, 
> (long)mask.__bits);
>    pthread_getschedparam(pthr->pthread, &policy, &schedp);
> 
>    pthread_barrier_wait(&barrier);
> 
> -  printf("Noise Thread running %d\n", pthr->priority);
> +  printf("Noise Thread running %d\n", schedp.sched_priority);
> 
>    for (i=0;i<10000;i++) {
>      if (i%100 == 0) {
>        sched_getparam(tid, &schedp);
>        policy = sched_getscheduler(tid);
> -      printf("Noise Thread %d(%d) loop %d pthread pol %d pri %d\n", tid, 
> pthr->priority, i,
> +      printf("Noise Thread %d(%d) loop %d pthread pol %d pri %d\n", tid, 
> schedp.sched_priority, i,
>          policy, schedp.sched_priority);
>        fflush(NULL);
>      }
> @@ -246,83 +232,10 @@ void* func_noise(void* arg)
>        pthread_mutex_lock(&(pthr->mutex));
>        pthread_mutex_unlock(&(pthr->mutex));
>      }
> -    if ((pthr->flags & THREAD_STOP) != 0 ) {
> -      printf("Stopping Noise thread %d\n", pthr->priority);
> -      break;
> -    }
>    }
>    return NULL;
>  }
> 
> -int startThread(Thread* thrd)
> -{
> -  struct sched_param schedp;
> -  pthread_condattr_t condattr;
> -  int retc, policy, inherit;
> -
> -  printf("Start thread priority %d\n", thrd->priority);
> -  if (pthread_attr_init(&(thrd->attr)) != 0) {
> -    printf("Attr init failed");
> -    exit(2);
> -  }
> -  thrd->flags = 0;
> -  memset(&schedp, 0, sizeof(schedp));
> -  schedp.sched_priority = thrd->priority;
> -  policy = thrd->policy;
> -
> -  if (pthread_attr_setschedpolicy(&(thrd->attr), policy) != 0) {
> -    printf("Can't set policy %d\n", policy);
> -  }
> -  if (pthread_attr_getschedpolicy(&(thrd->attr), &policy) != 0) {
> -    printf("Can't get policy\n");
> -  } else {
> -    printf("Policy in attribs is %d\n", policy);
> -  }
> -  if (pthread_attr_setschedparam(&(thrd->attr), &schedp) != 0) {
> -    printf("Can't set params");
> -  }
> -  if (pthread_attr_getschedparam(&(thrd->attr), &schedp) != 0) {
> -    printf("Can't get params");
> -  } else {
> -    printf("Priority in attribs is %d\n", schedp.sched_priority);
> -  }
> -  if (pthread_attr_setinheritsched(&(thrd->attr), PTHREAD_EXPLICIT_SCHED) != 
> 0) {
> -    printf("Can't set inheritsched\n");
> -  }
> -  if (pthread_attr_getinheritsched(&(thrd->attr), &inherit) != 0) {
> -    printf("Can't get inheritsched\n");
> -  } else {
> -    printf("inherit sched in attribs is %d\n", inherit);
> -  }
> -  if ((retc = pthread_mutex_init(&(thrd->mutex), NULL)) != 0) {
> -    printf("Failed to init mutex: %d\n", retc);
> -  }
> -  if (pthread_condattr_init(&condattr) != 0) {
> -    printf("Failed to init condattr\n");
> -  }
> -  if (pthread_cond_init(&(thrd->cond), &condattr) != 0) {
> -    printf("Failed to init cond\n");
> -  }
> -  retc = pthread_create(&(thrd->pthread),&(thrd->attr), thrd->func, thrd);
> -  printf("Create returns %d\n\n", retc);
> -  return retc;
> -}
> -
> -void stopThread(Thread* thr)
> -{
> -  thr->flags += THREAD_STOP;
> -  joinThread(thr);
> -}
> -
> -void joinThread(Thread* thr)
> -{
> -  void* ret = NULL;
> -  if (pthread_join(thr->pthread, &ret) != 0) {
> -    printf("Join failed\n");
> -  }
> -  printf("Join gave %p\n", ret);
> -}
> -
>  /*
>   * Test pthread creation at different thread priorities.
>   */
> @@ -354,13 +267,6 @@ int main(int argc, char* argv[]) {
>       exit(-1);
>    }
>    retc = sched_getaffinity(0, sizeof(mask), &mask);
> -
> -  arg1.policy = SCHED_OTHER; arg1.priority = 0;  arg1.func = func_nonrt;
> -  arg2.policy = SCHED_RR;    arg2.priority = 20; arg2.func = func_rt;
> -  arg3.policy = SCHED_RR;    arg3.priority = 30; arg3.func = func_rt;
> -  arg4.policy = SCHED_RR;    arg4.priority = 40; arg4.func = func_rt;
> -  arg5.policy = SCHED_RR;    arg5.priority = 40; arg5.func = func_noise;
> -
>    for (i=0;i<argc;i++) {
>      if (strcmp(argv[i],"nopi") == 0) nopi = 1;
>    }
> @@ -384,23 +290,15 @@ int main(int argc, char* argv[]) {
>      }
>    }
> 
> -  startThread(&arg1);
> -  startThread(&arg2);
> -  startThread(&arg3);
> -  startThread(&arg4);
> -  startThread(&arg5);
> +  create_other_thread(func_nonrt,NULL); 
> +  create_rr_thread(func_rt, NULL, 20); 
> +  create_rr_thread(func_rt, NULL, 30); 
> +  create_rr_thread(func_rt, NULL, 40); 
> +  create_rr_thread(func_noise, NULL, 40); 
> 
>    sleep(10);
> -
> +  join_threads();
>    printf("Stopping threads\n");
> -  stopThread(&arg1);
> -  stopThread(&arg2);
> -  stopThread(&arg3);
> -  stopThread(&arg4);
> -  stopThread(&arg5);
> -  
> -  printf("Thread counts %d %d %d %d %d\n",arg1.id, arg2.id,
> -      arg3.id, arg4.id, arg5.id);
>    printf("Done\n");
>    printf("Criteria:Low Priority Thread should Preempt Higher Priority Noise 
> Thread\n");
> 
> diff --git a/testcases/realtime/func/pi-tests/testpi-2.c 
> b/testcases/realtime/func/pi-tests/testpi-2.c
> index 9c60e68..63d4be9 100644
> --- a/testcases/realtime/func/pi-tests/testpi-2.c
> +++ b/testcases/realtime/func/pi-tests/testpi-2.c
> @@ -87,35 +87,18 @@ typedef void* (*entrypoint_t)(void*);
> 
>  pthread_mutex_t glob_mutex;
> 
> -/*typedef struct thread {
> -  int priority;
> -  int policy;
> -  entrypoint_t func;
> -  pthread_attr_t attr;
> -  pthread_t handle;
> -  pthread_mutex_t mutex;
> -  pthread_cond_t cond;
> -  int flags;
> -  int count;
> -} Thread;*/
> -
>  typedef struct thread Thread;
> 
> -Thread arg1, arg2, arg3, arg4, arg5;
> -
> -int strartThread(Thread* thr);
> -void stopThread(Thread* thr);
> -void joinThread(Thread* thr);
> -
> -void* func_lowrt(void* arg)
> +void* func_lowrt()
>  {
> -  Thread* pthr = (Thread*)arg;
> +  Thread thr,*pthr = &thr ;
>    int rc, i, j, policy, tid = gettid();
>    struct sched_param schedp;
>    cpu_set_t mask;
>    CPU_ZERO(&mask);
>    CPU_SET(0, &mask);
> 
> +  pthr->pthread =pthread_self();
>    rc = sched_setaffinity(0, sizeof(mask), &mask);
>    if (rc < 0) {
>       printf("Thread %d: Can't set affinity: %d %s\n", tid, rc, strerror(rc));
> @@ -123,12 +106,12 @@ void* func_lowrt(void* arg)
>    }
>    rc = sched_getaffinity(0, sizeof(mask), &mask);
> 
> -  printf("Thread started %d on CPU %ld\n", pthr->priority, 
> (long)mask.__bits);
> +  printf("Thread started %d on CPU %ld\n", schedp.sched_priority, 
> (long)mask.__bits);
>    pthread_getschedparam(pthr->pthread, &policy, &schedp);
> -  printf("Thread running %d\n", pthr->priority);
> +  printf("Thread running %d\n", schedp.sched_priority);
> 
>    pthread_mutex_lock(&glob_mutex);
> -  printf("Thread %d at start pthread pol %d pri %d - Got global lock\n", 
> pthr->priority,
> +  printf("Thread %d at start pthread pol %d pri %d - Got global lock\n", 
> schedp.sched_priority,
>          policy, schedp.sched_priority);
>    /* Wait for other RT threads to start up */
>    pthread_barrier_wait(&barrier);
> @@ -137,7 +120,7 @@ void* func_lowrt(void* arg)
>      if (i%100 == 0) {
>        sched_getparam(tid, &schedp);
>        policy = sched_getscheduler(tid);
> -      printf("Thread %d(%d) loop %d pthread pol %d pri %d\n", tid, 
> pthr->priority, i,
> +      printf("Thread %d(%d) loop %d pthread pol %d pri %d\n", tid, 
> schedp.sched_priority, i,
>          policy, schedp.sched_priority);
>        fflush(NULL);
>      }
> @@ -147,7 +130,7 @@ void* func_lowrt(void* arg)
>        pthread_mutex_unlock(&(pthr->mutex));
>      }
>      if ((pthr->flags & THREAD_STOP) != 0 ) {
> -      printf("Stopping thread %d\n", pthr->priority);
> +      printf("Stopping thread %d\n", schedp.sched_priority);
>        break;
>      }
>    }
> @@ -155,15 +138,16 @@ void* func_lowrt(void* arg)
>    return NULL;
>  }
> 
> -void* func_rt(void* arg)
> +void* func_rt()
>  {
> -  Thread* pthr = (Thread*)arg;
> +  Thread thr,*pthr = &thr ;  
>    int rc, i, j, policy, tid = gettid();
>    struct sched_param schedp;
>    cpu_set_t mask;
>    CPU_ZERO(&mask);
>    CPU_SET(0, &mask);
> 
> +  pthr->pthread =pthread_self();
>    rc = sched_setaffinity(0, sizeof(mask), &mask);
>    if (rc < 0) {
>       printf("Thread %d: Can't set affinity: %d %s\n", tid, rc, strerror(rc));
> @@ -171,15 +155,15 @@ void* func_rt(void* arg)
>    }
>    rc = sched_getaffinity(0, sizeof(mask), &mask);
> 
> -  printf("Thread started %d on CPU %ld\n", pthr->priority, 
> (long)mask.__bits);
> +  printf("Thread started %d on CPU %ld\n", schedp.sched_priority, 
> (long)mask.__bits);
>    pthread_getschedparam(pthr->pthread, &policy, &schedp);
> 
>    /* Let the lower prio RT task grab the lock */
>    pthread_barrier_wait(&barrier);
> 
> -  printf("Thread running %d\n", pthr->priority);
> +  printf("Thread running %d\n", schedp.sched_priority);
>    pthread_mutex_lock(&glob_mutex);
> -  printf("Thread %d at start pthread pol %d pri %d - Got global lock\n", 
> pthr->priority,
> +  printf("Thread %d at start pthread pol %d pri %d - Got global lock\n", 
> schedp.sched_priority,
>          policy, schedp.sched_priority);
> 
>    /* we just use the mutex as something to slow things down */
> @@ -191,7 +175,7 @@ void* func_rt(void* arg)
>      if (i%100 == 0) {
>        sched_getparam(tid, &schedp);
>        policy = sched_getscheduler(tid);
> -      printf("Thread %d(%d) loop %d pthread pol %d pri %d\n", tid, 
> pthr->priority, i,
> +      printf("Thread %d(%d) loop %d pthread pol %d pri %d\n", tid, 
> schedp.sched_priority, i,
>          policy, schedp.sched_priority);
>        fflush(NULL);
>      }
> @@ -201,7 +185,7 @@ void* func_rt(void* arg)
>        pthread_mutex_unlock(&(pthr->mutex));
>      }
>      if ((pthr->flags & THREAD_STOP) != 0 ) {
> -      printf("Stopping thread %d\n", pthr->priority);
> +      printf("Stopping thread %d\n", schedp.sched_priority);
>        break;
>      }
>    }
> @@ -209,15 +193,16 @@ void* func_rt(void* arg)
>    return NULL;
>  }
> 
> -void* func_noise(void* arg)
> +void* func_noise()
>  {
> -  Thread* pthr = (Thread*)arg;
> +  Thread thr,*pthr = &thr ;  
>    int rc, i, j, policy, tid = gettid();
>    struct sched_param schedp;
>    cpu_set_t mask;
>    CPU_ZERO(&mask);
>    CPU_SET(0, &mask);
> 
> +  pthr->pthread =pthread_self();
>    rc = sched_setaffinity(0, sizeof(mask), &mask);
>    if (rc < 0) {
>       printf("Thread %d: Can't set affinity: %d %s\n", tid, rc, strerror(rc));
> @@ -225,18 +210,18 @@ void* func_noise(void* arg)
>    }
>    rc = sched_getaffinity(0, sizeof(mask), &mask);
> 
> -  printf("Noise Thread started %d on CPU %ld\n", pthr->priority, 
> (long)mask.__bits);
> +  printf("Noise Thread started %d on CPU %ld\n", schedp.sched_priority, 
> (long)mask.__bits);
>    pthread_getschedparam(pthr->pthread, &policy, &schedp);
> 
>    pthread_barrier_wait(&barrier);
> 
> -  printf("Noise Thread running %d\n", pthr->priority);
> +  printf("Noise Thread running %d\n", schedp.sched_priority);
> 
>    for (i=0;i<10000;i++) {
>      if (i%100 == 0) {
>        sched_getparam(tid, &schedp);
>        policy = sched_getscheduler(tid);
> -      printf("Noise Thread %d(%d) loop %d pthread pol %d pri %d\n", tid, 
> pthr->priority, i,
> +      printf("Noise Thread %d(%d) loop %d pthread pol %d pri %d\n", tid, 
> schedp.sched_priority, i,
>          policy, schedp.sched_priority);
>        fflush(NULL);
>      }
> @@ -245,83 +230,10 @@ void* func_noise(void* arg)
>        pthread_mutex_lock(&(pthr->mutex));
>        pthread_mutex_unlock(&(pthr->mutex));
>      }
> -    if ((pthr->flags & THREAD_STOP) != 0 ) {
> -      printf("Stopping Noise thread %d\n", pthr->priority);
> -      break;
> -    }
>    }
>    return NULL;
>  }
> 
> -int startThread(Thread* thrd)
> -{
> -  struct sched_param schedp;
> -  pthread_condattr_t condattr;
> -  int retc, policy, inherit;
> -
> -  printf("Start thread priority %d\n", thrd->priority);
> -  if (pthread_attr_init(&(thrd->attr)) != 0) {
> -    printf("Attr init failed");
> -    exit(2);
> -  }
> -  thrd->flags = 0;
> -  memset(&schedp, 0, sizeof(schedp));
> -  schedp.sched_priority = thrd->priority;
> -  policy = thrd->policy;
> -
> -  if (pthread_attr_setschedpolicy(&(thrd->attr), policy) != 0) {
> -    printf("Can't set policy %d\n", policy);
> -  }
> -  if (pthread_attr_getschedpolicy(&(thrd->attr), &policy) != 0) {
> -    printf("Can't get policy\n");
> -  } else {
> -    printf("Policy in attribs is %d\n", policy);
> -  }
> -  if (pthread_attr_setschedparam(&(thrd->attr), &schedp) != 0) {
> -    printf("Can't set params");
> -  }
> -  if (pthread_attr_getschedparam(&(thrd->attr), &schedp) != 0) {
> -    printf("Can't get params");
> -  } else {
> -    printf("Priority in attribs is %d\n", schedp.sched_priority);
> -  }
> -  if (pthread_attr_setinheritsched(&(thrd->attr), PTHREAD_EXPLICIT_SCHED) != 
> 0) {
> -    printf("Can't set inheritsched\n");
> -  }
> -  if (pthread_attr_getinheritsched(&(thrd->attr), &inherit) != 0) {
> -    printf("Can't get inheritsched\n");
> -  } else {
> -    printf("inherit sched in attribs is %d\n", inherit);
> -  }
> -  if ((retc = pthread_mutex_init(&(thrd->mutex), NULL)) != 0) {
> -    printf("Failed to init mutex: %d\n", retc);
> -  }
> -  if (pthread_condattr_init(&condattr) != 0) {
> -    printf("Failed to init condattr\n");
> -  }
> -  if (pthread_cond_init(&(thrd->cond), &condattr) != 0) {
> -    printf("Failed to init cond\n");
> -  }
> -  retc = pthread_create(&(thrd->pthread),&(thrd->attr), thrd->func, thrd);
> -  printf("Create returns %d\n\n", retc);
> -  return retc;
> -}
> -
> -void stopThread(Thread* thr)
> -{
> -  thr->flags += THREAD_STOP;
> -  joinThread(thr);
> -}
> -
> -void joinThread(Thread* thr)
> -{
> -  void* ret = NULL;
> -  if (pthread_join(thr->pthread, &ret) != 0) {
> -    printf("Join failed\n");
> -  }
> -  printf("Join gave %p\n", ret);
> -}
> -
>  /*
>   * Test pthread creation at different thread priorities.
>   */
> @@ -350,16 +262,8 @@ int main(int argc, char* argv[]) {
>    }
>    retc = sched_getaffinity(0, sizeof(mask), &mask);
> 
> -  arg1.policy = SCHED_RR;    arg1.priority = 10; arg1.func = func_lowrt;
> -  arg2.policy = SCHED_RR;    arg2.priority = 20; arg2.func = func_rt;
> -  arg3.policy = SCHED_FIFO;  arg3.priority = 30; arg3.func = func_rt;
> -  arg4.policy = SCHED_FIFO;  arg4.priority = 40; arg4.func = func_rt;
> -  arg5.policy = SCHED_RR;    arg5.priority = 40; arg5.func = func_noise;
> -
>    for (i=0;i<argc;i++) {
>      if (strcmp(argv[i],"nopi") == 0) nopi = 1;
> -    if (strcmp(argv[i],"SCHED_RR") == 0) arg5.policy = SCHED_RR;
> -    if (strcmp(argv[i],"SCHED_FIFO") == 0) arg5.policy = SCHED_FIFO;
>    }
> 
>    if (!nopi) {
> @@ -383,24 +287,16 @@ int main(int argc, char* argv[]) {
>      printf("pthread_barrier_init failed: %s\n", strerror(retc));
>      exit(retc);
>    }
> +  create_rr_thread(func_lowrt, NULL, 10);
> +  create_rr_thread(func_rt, NULL, 20);
> +  create_fifo_thread(func_rt, NULL, 30);
> +  create_fifo_thread(func_rt, NULL, 40);
> +  create_rr_thread(func_noise, NULL, 40);
> 
> -  startThread(&arg1);
> -  startThread(&arg2);
> -  startThread(&arg3);
> -  startThread(&arg4);
> -  startThread(&arg5);
> -
> -  sleep(10);
> +   sleep(10);
> 
>    printf("Stopping threads\n");
> -  stopThread(&arg1);
> -  stopThread(&arg2);
> -  stopThread(&arg3);
> -  stopThread(&arg4);
> -  stopThread(&arg5);
> -
> -  printf("Thread counts %d %d %d %d %d\n",arg1.id, arg2.id,
> -      arg3.id, arg4.id, arg5.id);
> +  join_threads();
>    printf("Done\n");
>    printf("Criteria: Low Priority Thread and High Priority Thread should 
> prempt each other multiple times\n");
>    return 0;
> diff --git a/testcases/realtime/func/pi-tests/testpi-4.c 
> b/testcases/realtime/func/pi-tests/testpi-4.c
> index f805e61..c404875 100644
> --- a/testcases/realtime/func/pi-tests/testpi-4.c
> +++ b/testcases/realtime/func/pi-tests/testpi-4.c
> @@ -88,35 +88,19 @@ typedef void* (*entrypoint_t)(void*);
> 
>  pthread_mutex_t *glob_mutex;
> 
> -/*typedef struct thread {
> -  int priority;
> -  int policy;
> -  entrypoint_t func;
> -  pthread_attr_t attr;
> -  pthread_t handle;
> -  pthread_mutex_t mutex;
> -  pthread_cond_t cond;
> -  int flags;
> -  int count;
> -} Thread;*/
> -
>  typedef struct thread Thread;
> 
> -Thread arg1, arg2, arg3, arg4, arg5;
> -
> -int strartThread(Thread* thr);
> -void stopThread(Thread* thr);
> -void joinThread(Thread* thr);
> -
> -void* func_nonrt(void* arg)
> +void* func_nonrt()
>  {
> -  Thread* pthr = (Thread*)arg;
> +  
> +  Thread thr,* pthr = &thr;  
>    int rc, i, j, policy, tid = gettid();
>    struct sched_param schedp;
>    cpu_set_t mask;
>    CPU_ZERO(&mask);
>    CPU_SET(0, &mask);
> -
> +  
> +  pthr->pthread = pthread_self();
>    rc = sched_setaffinity(0, sizeof(mask), &mask);
>    if (rc < 0) {
>       printf("Thread %d: Can't set affinity: %d %s\n", tid, rc, strerror(rc));
> @@ -124,12 +108,12 @@ void* func_nonrt(void* arg)
>    }
>    rc = sched_getaffinity(0, sizeof(mask), &mask);
> 
> -  printf("Thread started %d on CPU %ld\n", pthr->priority, 
> (long)mask.__bits);
> +  printf("Thread started %d on CPU %ld\n", schedp.sched_priority, 
> (long)mask.__bits);
>    pthread_getschedparam(pthr->pthread, &policy, &schedp);
> -  printf("Thread running %d\n", pthr->priority);
> +  printf("Thread running %d\n", schedp.sched_priority);
> 
>    pthread_mutex_lock(glob_mutex);
> -  printf("Thread %d at start pthread pol %d pri %d - Got global lock\n", 
> pthr->priority,
> +  printf("Thread %d at start pthread pol %d pri %d - Got global lock\n", 
> schedp.sched_priority,
>          policy, schedp.sched_priority);
>    pthread_barrier_wait(&barrier);
> 
> @@ -137,7 +121,7 @@ void* func_nonrt(void* arg)
>      if (i%100 == 0) {
>        sched_getparam(tid, &schedp);
>        policy = sched_getscheduler(tid);
> -      printf("Thread %d(%d) loop %d pthread pol %d pri %d\n", tid, 
> pthr->priority, i,
> +      printf("Thread %d(%d) loop %d pthread pol %d pri %d\n", tid, 
> schedp.sched_priority, i,
>          policy, schedp.sched_priority);
>        fflush(NULL);
>      }
> @@ -146,24 +130,21 @@ void* func_nonrt(void* arg)
>        pthread_mutex_lock(&(pthr->mutex));
>        pthread_mutex_unlock(&(pthr->mutex));
>      }
> -    if ((pthr->flags & THREAD_STOP) != 0 ) {
> -      printf("Stopping thread %d\n", pthr->priority);
> -      break;
> -    }
>    }
>    pthread_mutex_unlock(glob_mutex);
>    return NULL;
>  }
> 
> -void* func_rt(void* arg)
> +void* func_rt()
>  {
> -  Thread* pthr = (Thread*)arg;
> +  Thread thr,* pthr = &thr;  
>    int rc, i, j, policy, tid = gettid();
>    struct sched_param schedp;
>    cpu_set_t mask;
>    CPU_ZERO(&mask);
>    CPU_SET(0, &mask);
> -
> +  
> +  pthr->pthread = pthread_self();
>    rc = sched_setaffinity(0, sizeof(mask), &mask);
>    if (rc < 0) {
>       printf("Thread %d: Can't set affinity: %d %s\n", tid, rc, strerror(rc));
> @@ -171,13 +152,13 @@ void* func_rt(void* arg)
>    }
>    rc = sched_getaffinity(0, sizeof(mask), &mask);
> 
> -  printf("Thread started %d on CPU %ld\n", pthr->priority, 
> (long)mask.__bits);
> +  printf("Thread started %d on CPU %ld\n", schedp.sched_priority, 
> (long)mask.__bits);
>    pthread_getschedparam(pthr->pthread, &policy, &schedp);
>    pthread_barrier_wait(&barrier);
> 
> -  printf("Thread running %d\n", pthr->priority);
> +  printf("Thread running %d\n", schedp.sched_priority);
>    pthread_mutex_lock(glob_mutex);
> -  printf("Thread %d at start pthread pol %d pri %d - Got global lock\n", 
> pthr->priority,
> +  printf("Thread %d at start pthread pol %d pri %d - Got global lock\n", 
> schedp.sched_priority,
>          policy, schedp.sched_priority);
> 
>    /* we just use the mutex as something to slow things down */
> @@ -189,7 +170,7 @@ void* func_rt(void* arg)
>      if (i%100 == 0) {
>        sched_getparam(tid, &schedp);
>        policy = sched_getscheduler(tid);
> -      printf("Thread %d(%d) loop %d pthread pol %d pri %d\n", tid, 
> pthr->priority, i,
> +      printf("Thread %d(%d) loop %d pthread pol %d pri %d\n", tid, 
> schedp.sched_priority, i,
>          policy, schedp.sched_priority);
>        fflush(NULL);
>      }
> @@ -199,7 +180,7 @@ void* func_rt(void* arg)
>        pthread_mutex_unlock(&(pthr->mutex));
>      }
>      if ((pthr->flags & THREAD_STOP) != 0 ) {
> -      printf("Stopping thread %d\n", pthr->priority);
> +      printf("Stopping thread %d\n", schedp.sched_priority);
>        break;
>      }
>    }
> @@ -207,15 +188,16 @@ void* func_rt(void* arg)
>    return NULL;
>  }
> 
> -void* func_noise(void* arg)
> +void* func_noise()
>  {
> -  Thread* pthr = (Thread*)arg;
> +  Thread thr,* pthr = &thr;  
>    int rc, i, j, policy, tid = gettid();
>    struct sched_param schedp;
>    cpu_set_t mask;
>    CPU_ZERO(&mask);
>    CPU_SET(0, &mask);
> 
> +  pthr->pthread = pthread_self();
>    rc = sched_setaffinity(0, sizeof(mask), &mask);
>    if (rc < 0) {
>       printf("Thread %d: Can't set affinity: %d %s\n", tid, rc, strerror(rc));
> @@ -223,18 +205,18 @@ void* func_noise(void* arg)
>    }
>    rc = sched_getaffinity(0, sizeof(mask), &mask);
> 
> -  printf("Noise Thread started %d on CPU %ld\n", pthr->priority, 
> (long)mask.__bits);
> +  printf("Noise Thread started %d on CPU %ld\n", schedp.sched_priority, 
> (long)mask.__bits);
>    pthread_getschedparam(pthr->pthread, &policy, &schedp);
> 
>    pthread_barrier_wait(&barrier);
> 
> -  printf("Noise Thread running %d\n", pthr->priority);
> +  printf("Noise Thread running %d\n", schedp.sched_priority);
> 
>    for (i=0;i<10000;i++) {
>      if (i%100 == 0) {
>        sched_getparam(tid, &schedp);
>        policy = sched_getscheduler(tid);
> -      printf("Noise Thread %d(%d) loop %d pthread pol %d pri %d\n", tid, 
> pthr->priority, i,
> +      printf("Noise Thread %d(%d) loop %d pthread pol %d pri %d\n", tid, 
> schedp.sched_priority, i,
>          policy, schedp.sched_priority);
>        fflush(NULL);
>      }
> @@ -251,75 +233,6 @@ void* func_noise(void* arg)
>    return NULL;
>  }
> 
> -int startThread(Thread* thrd)
> -{
> -  struct sched_param schedp;
> -  pthread_condattr_t condattr;
> -  int retc, policy, inherit;
> -
> -  printf("Start thread priority %d\n", thrd->priority);
> -  if (pthread_attr_init(&(thrd->attr)) != 0) {
> -    printf("Attr init failed");
> -    exit(2);
> -  }
> -  thrd->flags = 0;
> -  memset(&schedp, 0, sizeof(schedp));
> -  schedp.sched_priority = thrd->priority;
> -  policy = thrd->policy;
> -
> -  if (pthread_attr_setschedpolicy(&(thrd->attr), policy) != 0) {
> -    printf("Can't set policy %d\n", policy);
> -  }
> -  if (pthread_attr_getschedpolicy(&(thrd->attr), &policy) != 0) {
> -    printf("Can't get policy\n");
> -  } else {
> -    printf("Policy in attribs is %d\n", policy);
> -  }
> -  if (pthread_attr_setschedparam(&(thrd->attr), &schedp) != 0) {
> -    printf("Can't set params");
> -  }
> -  if (pthread_attr_getschedparam(&(thrd->attr), &schedp) != 0) {
> -    printf("Can't get params");
> -  } else {
> -    printf("Priority in attribs is %d\n", schedp.sched_priority);
> -  }
> -  if (pthread_attr_setinheritsched(&(thrd->attr), PTHREAD_EXPLICIT_SCHED) != 
> 0) {
> -    printf("Can't set inheritsched\n");
> -  }
> -  if (pthread_attr_getinheritsched(&(thrd->attr), &inherit) != 0) {
> -    printf("Can't get inheritsched\n");
> -  } else {
> -    printf("inherit sched in attribs is %d\n", inherit);
> -  }
> -  if ((retc = pthread_mutex_init(&(thrd->mutex), NULL)) != 0) {
> -    printf("Failed to init mutex: %d\n", retc);
> -  }
> -  if (pthread_condattr_init(&condattr) != 0) {
> -    printf("Failed to init condattr\n");
> -  }
> -  if (pthread_cond_init(&(thrd->cond), &condattr) != 0) {
> -    printf("Failed to init cond\n");
> -  }
> -  retc = pthread_create(&(thrd->pthread),&(thrd->attr), thrd->func, thrd);
> -  printf("Create returns %d\n\n", retc);
> -  return retc;
> -}
> -
> -void stopThread(Thread* thr)
> -{
> -  thr->flags += THREAD_STOP;
> -  joinThread(thr);
> -}
> -
> -void joinThread(Thread* thr)
> -{
> -  void* ret = NULL;
> -  if (pthread_join(thr->pthread, &ret) != 0) {
> -    printf("Join failed\n");
> -  }
> -  printf("Join gave %p\n", ret);
> -}
> -
>  /*
>   * Test pthread creation at different thread priorities.
>   */
> @@ -347,12 +260,6 @@ int main(int argc, char* argv[]) {
>    }
>    retc = sched_getaffinity(0, sizeof(mask), &mask);
> 
> -  arg1.policy = SCHED_OTHER; arg1.priority = 0;  arg1.func = func_nonrt;
> -  arg2.policy = SCHED_RR;    arg2.priority = 20; arg2.func = func_rt;
> -  arg3.policy = SCHED_RR;    arg3.priority = 30; arg3.func = func_rt;
> -  arg4.policy = SCHED_RR;    arg4.priority = 40; arg4.func = func_rt;
> -  arg5.policy = SCHED_RR;    arg5.priority = 40; arg5.func = func_noise;
> -
>    for (i=0;i<argc;i++) {
>      if (strcmp(argv[i],"nopi") == 0) nopi = 1;
>    }
> @@ -387,23 +294,15 @@ int main(int argc, char* argv[]) {
>      exit(retc);
>    }
> 
> -  startThread(&arg1);
> -  startThread(&arg2);
> -  startThread(&arg3);
> -  startThread(&arg4);
> -  startThread(&arg5);
> -
>    sleep(10);
> -
> -  printf("Stopping threads\n");
> -  stopThread(&arg1);
> -  stopThread(&arg2);
> -  stopThread(&arg3);
> -  stopThread(&arg4);
> -  stopThread(&arg5);
> -
> -  printf("Thread counts %d %d %d %d %d\n",arg1.id, arg2.id,
> -      arg3.id, arg4.id, arg5.id);
> +  create_other_thread(func_nonrt, NULL);
> +  create_rr_thread(func_rt, NULL, 20);
> +  create_rr_thread(func_rt, NULL, 30);
> +  create_rr_thread(func_rt, NULL, 40);
> +  create_rr_thread(func_noise, NULL, 40);
> +
> +  printf("Joining threads\n");
> +  join_threads();
>    printf("Done\n");
> 
>    return 0;
> diff --git a/testcases/realtime/func/pi-tests/testpi-5.c 
> b/testcases/realtime/func/pi-tests/testpi-5.c
> index 4b62a96..e19cfb8 100644
> --- a/testcases/realtime/func/pi-tests/testpi-5.c
> +++ b/testcases/realtime/func/pi-tests/testpi-5.c
> @@ -41,10 +41,10 @@
>  #include <pthread.h>
>  #include <string.h>
>  #include <unistd.h>
> -
> +#include <librttest.h>
>  pthread_mutex_t  child_mutex;
> 
> -void* child_thread (void* arg)
> +void* child_thread ()
>  {
>      int ret;
> 
> @@ -68,7 +68,6 @@ void* child_thread (void* arg)
> 
>  int do_test(int argc, char ** argv)
>  {
> -    pthread_t thread;
>      pthread_mutexattr_t mutexattr;
>      int retc, protocol;
> 
> @@ -87,10 +86,9 @@ int do_test(int argc, char ** argv)
>        printf("Failed to init mutex: %d\n", retc);
>      }
> 
> -    pthread_create (&thread, NULL, child_thread, NULL);
> -
> -    pthread_join(thread, NULL);
> +    create_other_thread(child_thread, NULL);
> 
> +    join_threads();
>      return 0;
>  }
> 
> diff --git a/testcases/realtime/func/pi-tests/testpi-6.c 
> b/testcases/realtime/func/pi-tests/testpi-6.c
> index c835932..064902a 100644
> --- a/testcases/realtime/func/pi-tests/testpi-6.c
> +++ b/testcases/realtime/func/pi-tests/testpi-6.c
> @@ -41,10 +41,10 @@
>  #include <pthread.h>
>  #include <string.h>
>  #include <unistd.h>
> -
> +#include <librttest.h>
>  pthread_mutex_t  child_mutex;
> 
> -void* child_thread (void* arg)
> +void* child_thread ()
>  {
>      int ret;
> 
> @@ -68,7 +68,6 @@ void* child_thread (void* arg)
> 
>  int do_test(int argc, char **argv)
>  {
> -    pthread_t thread;
>      pthread_mutexattr_t mutexattr;
>      int retc, robust;
> 
> @@ -87,9 +86,9 @@ int do_test(int argc, char **argv)
>        printf("Failed to init mutex: %d\n", retc);
>      }
> 
> -    pthread_create (&thread, NULL, child_thread, NULL);
> +    create_other_thread (child_thread, NULL);
> 
> -    pthread_join(thread, NULL);
> +    join_threads();
> 
>      return 0;
>  }
> 
> -- 
> Cheers,
> Chirag Jog

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Ltp-list mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ltp-list

Reply via email to