OpenCores
URL https://opencores.org/ocsvn/openrisc_me/openrisc_me/trunk

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [gnu-src/] [gdb-6.8/] [gdb/] [testsuite/] [gdb.threads/] [tls.c] - Diff between revs 24 and 157

Only display areas with differences | Details | Blame | View Log

Rev 24 Rev 157
/* BeginSourceFile tls.c
/* BeginSourceFile tls.c
 
 
  This file creates and deletes threads. It uses thread local storage
  This file creates and deletes threads. It uses thread local storage
  variables too. */
  variables too. */
 
 
#include <unistd.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdio.h>
#include <assert.h>
#include <assert.h>
#include <pthread.h>
#include <pthread.h>
#include <semaphore.h>
#include <semaphore.h>
#include <errno.h>
#include <errno.h>
 
 
#define N_THREADS 3
#define N_THREADS 3
 
 
/* Uncomment to turn on debugging output */
/* Uncomment to turn on debugging output */
/*#define START_DEBUG*/
/*#define START_DEBUG*/
 
 
/* Thread-local storage.  */
/* Thread-local storage.  */
__thread int a_thread_local;
__thread int a_thread_local;
__thread int another_thread_local;
__thread int another_thread_local;
 
 
/* Global variable just for info addr in gdb.  */
/* Global variable just for info addr in gdb.  */
int a_global;
int a_global;
 
 
/* Print the results of thread-local storage.  */
/* Print the results of thread-local storage.  */
int thread_local_val[ N_THREADS ];
int thread_local_val[ N_THREADS ];
int another_thread_local_val[ N_THREADS ];
int another_thread_local_val[ N_THREADS ];
 
 
/* Semaphores to make sure the threads are alive when we print the TLS
/* Semaphores to make sure the threads are alive when we print the TLS
   variables from gdb.  */
   variables from gdb.  */
sem_t tell_main, tell_thread;
sem_t tell_main, tell_thread;
 
 
 
 
void print_error ()
void print_error ()
{
{
  switch (errno)
  switch (errno)
  {
  {
    case EAGAIN:
    case EAGAIN:
      fprintf (stderr, "EAGAIN\n");
      fprintf (stderr, "EAGAIN\n");
      break;
      break;
    case EINTR:
    case EINTR:
      fprintf (stderr, "EINTR\n");
      fprintf (stderr, "EINTR\n");
      break;
      break;
    case EINVAL:
    case EINVAL:
      fprintf (stderr, "EINVAL\n");
      fprintf (stderr, "EINVAL\n");
      break;
      break;
    case ENOSYS:
    case ENOSYS:
      fprintf (stderr, "ENOSYS\n");
      fprintf (stderr, "ENOSYS\n");
      break;
      break;
    case ENOENT:
    case ENOENT:
      fprintf (stderr, "ENOENT\n");
      fprintf (stderr, "ENOENT\n");
      break;
      break;
    case EDEADLK:
    case EDEADLK:
      fprintf (stderr, "EDEADLK\n");
      fprintf (stderr, "EDEADLK\n");
      break;
      break;
    default:
    default:
      fprintf (stderr, "Unknown error\n");
      fprintf (stderr, "Unknown error\n");
      break;
      break;
   }
   }
}
}
 
 
/* Routine for each thread to run, does nothing.  */
/* Routine for each thread to run, does nothing.  */
void *spin( vp )
void *spin( vp )
    void * vp;
    void * vp;
{
{
    int me = (long) vp;
    int me = (long) vp;
    int i;
    int i;
 
 
    /* Use a_global. */
    /* Use a_global. */
    a_global++;
    a_global++;
 
 
    a_thread_local = 0;
    a_thread_local = 0;
    another_thread_local = me;
    another_thread_local = me;
    for( i = 0; i <= me; i++ ) {
    for( i = 0; i <= me; i++ ) {
        a_thread_local += i;
        a_thread_local += i;
    }
    }
 
 
    another_thread_local_val[me] = another_thread_local;
    another_thread_local_val[me] = another_thread_local;
    thread_local_val[ me ] = a_thread_local; /* here we know tls value */
    thread_local_val[ me ] = a_thread_local; /* here we know tls value */
 
 
    if (sem_post (&tell_main) == -1)
    if (sem_post (&tell_main) == -1)
     {
     {
        fprintf (stderr, "th %d post on sem tell_main failed\n", me);
        fprintf (stderr, "th %d post on sem tell_main failed\n", me);
        print_error ();
        print_error ();
        return;
        return;
     }
     }
#ifdef START_DEBUG
#ifdef START_DEBUG
    fprintf (stderr, "th %d post on tell main\n", me);
    fprintf (stderr, "th %d post on tell main\n", me);
#endif
#endif
 
 
    while (1)
    while (1)
      {
      {
#ifdef START_DEBUG
#ifdef START_DEBUG
        fprintf (stderr, "th %d start wait on tell_thread\n", me);
        fprintf (stderr, "th %d start wait on tell_thread\n", me);
#endif
#endif
        if (sem_wait (&tell_thread) == 0)
        if (sem_wait (&tell_thread) == 0)
          break;
          break;
 
 
        if (errno == EINTR)
        if (errno == EINTR)
          {
          {
#ifdef START_DEBUG
#ifdef START_DEBUG
            fprintf (stderr, "th %d wait tell_thread got EINTR, rewaiting\n", me);
            fprintf (stderr, "th %d wait tell_thread got EINTR, rewaiting\n", me);
#endif
#endif
            continue;
            continue;
          }
          }
        else
        else
          {
          {
            fprintf (stderr, "th %d wait on sem tell_thread failed\n", me);
            fprintf (stderr, "th %d wait on sem tell_thread failed\n", me);
            print_error ();
            print_error ();
            return;
            return;
         }
         }
      }
      }
 
 
#ifdef START_DEBUG
#ifdef START_DEBUG
      fprintf (stderr, "th %d Wait on tell_thread\n", me);
      fprintf (stderr, "th %d Wait on tell_thread\n", me);
#endif
#endif
 
 
}
}
 
 
void
void
do_pass()
do_pass()
{
{
    int i;
    int i;
    pthread_t t[ N_THREADS ];
    pthread_t t[ N_THREADS ];
    int err;
    int err;
 
 
    for( i = 0; i < N_THREADS; i++)
    for( i = 0; i < N_THREADS; i++)
    {
    {
        thread_local_val[i] = 0;
        thread_local_val[i] = 0;
        another_thread_local_val[i] = 0;
        another_thread_local_val[i] = 0;
    }
    }
 
 
    if (sem_init (&tell_main, 0, 0) == -1)
    if (sem_init (&tell_main, 0, 0) == -1)
    {
    {
      fprintf (stderr, "tell_main semaphore init failed\n");
      fprintf (stderr, "tell_main semaphore init failed\n");
      return;
      return;
    }
    }
 
 
    if (sem_init (&tell_thread, 0, 0) == -1)
    if (sem_init (&tell_thread, 0, 0) == -1)
    {
    {
      fprintf (stderr, "tell_thread semaphore init failed\n");
      fprintf (stderr, "tell_thread semaphore init failed\n");
      return;
      return;
    }
    }
 
 
    /* Start N_THREADS threads, then join them so that they are terminated.  */
    /* Start N_THREADS threads, then join them so that they are terminated.  */
    for( i = 0; i < N_THREADS; i++ )
    for( i = 0; i < N_THREADS; i++ )
     {
     {
        err = pthread_create( &t[i], NULL, spin, (void *) (long) i );
        err = pthread_create( &t[i], NULL, spin, (void *) (long) i );
        if( err != 0 ) {
        if( err != 0 ) {
            fprintf(stderr, "Error in thread %d create\n", i );
            fprintf(stderr, "Error in thread %d create\n", i );
        }
        }
     }
     }
 
 
    for( i = 0; i < N_THREADS; i++ )
    for( i = 0; i < N_THREADS; i++ )
      {
      {
        while (1)
        while (1)
          {
          {
#ifdef START_DEBUG
#ifdef START_DEBUG
            fprintf (stderr, "main %d start wait on tell_main\n", i);
            fprintf (stderr, "main %d start wait on tell_main\n", i);
#endif
#endif
            if (sem_wait (&tell_main) == 0)
            if (sem_wait (&tell_main) == 0)
              break;
              break;
 
 
            if (errno == EINTR)
            if (errno == EINTR)
              {
              {
#ifdef START_DEBUG
#ifdef START_DEBUG
                fprintf (stderr, "main %d wait tell_main got EINTR, rewaiting\n", i);
                fprintf (stderr, "main %d wait tell_main got EINTR, rewaiting\n", i);
#endif
#endif
                continue;
                continue;
              }
              }
            else
            else
              {
              {
                fprintf (stderr, "main %d wait on sem tell_main failed\n", i);
                fprintf (stderr, "main %d wait on sem tell_main failed\n", i);
                print_error ();
                print_error ();
                return;
                return;
              }
              }
            }
            }
       }
       }
 
 
#ifdef START_DEBUG
#ifdef START_DEBUG
    fprintf (stderr, "main done waiting on tell_main\n");
    fprintf (stderr, "main done waiting on tell_main\n");
#endif
#endif
 
 
    i = 10;  /* Here all threads should be still alive. */
    i = 10;  /* Here all threads should be still alive. */
 
 
    for( i = 0; i < N_THREADS; i++ )
    for( i = 0; i < N_THREADS; i++ )
     {
     {
       if (sem_post (&tell_thread) == -1)
       if (sem_post (&tell_thread) == -1)
        {
        {
           fprintf (stderr, "main %d post on sem tell_thread failed\n", i);
           fprintf (stderr, "main %d post on sem tell_thread failed\n", i);
           print_error ();
           print_error ();
           return;
           return;
        }
        }
#ifdef START_DEBUG
#ifdef START_DEBUG
      fprintf (stderr, "main %d post on tell_thread\n", i);
      fprintf (stderr, "main %d post on tell_thread\n", i);
#endif
#endif
     }
     }
 
 
    for( i = 0; i < N_THREADS; i++ )
    for( i = 0; i < N_THREADS; i++ )
     {
     {
        err = pthread_join(t[i], NULL );
        err = pthread_join(t[i], NULL );
        if( err != 0 )
        if( err != 0 )
         {
         {
           fprintf (stderr, "error in thread %d join\n", i );
           fprintf (stderr, "error in thread %d join\n", i );
         }
         }
    }
    }
 
 
    i = 10;  /* Null line for setting bpts on. */
    i = 10;  /* Null line for setting bpts on. */
 
 
}
}
 
 
int
int
main()
main()
{
{
   do_pass ();
   do_pass ();
 
 
   return 0;  /* Set breakpoint here before exit. */
   return 0;  /* Set breakpoint here before exit. */
}
}
 
 
/* EndSourceFile */
/* EndSourceFile */
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.