OpenCores
URL https://opencores.org/ocsvn/openrisc_2011-10-31/openrisc_2011-10-31/trunk

Subversion Repositories openrisc_2011-10-31

[/] [openrisc/] [tags/] [gnu-src/] [newlib-1.18.0/] [newlib-1.18.0-or32-1.0rc1/] [newlib/] [libc/] [sys/] [linux/] [linuxthreads/] [tst-cancel.c] - Diff between revs 207 and 345

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

Rev 207 Rev 345
/* Tests for cancelation handling.  */
/* Tests for cancelation handling.  */
 
 
#include <pthread.h>
#include <pthread.h>
#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>
#include <string.h>
#include <string.h>
#include <unistd.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/stat.h>
 
 
int fd;
int fd;
 
 
pthread_barrier_t bar;
pthread_barrier_t bar;
 
 
 
 
static void
static void
cleanup (void *arg)
cleanup (void *arg)
{
{
  int nr = (int) (long int) arg;
  int nr = (int) (long int) arg;
  char s[30];
  char s[30];
  char *cp = stpcpy (s, "cleanup ");
  char *cp = stpcpy (s, "cleanup ");
  *cp++ = '0' + nr;
  *cp++ = '0' + nr;
  *cp++ = '\n';
  *cp++ = '\n';
  __libc_lseek (fd, 0, SEEK_END);
  __libc_lseek (fd, 0, SEEK_END);
  __libc_write (fd, s, cp - s);
  __libc_write (fd, s, cp - s);
}
}
 
 
 
 
static void *
static void *
t1 (void *arg)
t1 (void *arg)
{
{
  pthread_cleanup_push (cleanup, (void *) (long int) 1);
  pthread_cleanup_push (cleanup, (void *) (long int) 1);
  return NULL;
  return NULL;
  pthread_cleanup_pop (0);
  pthread_cleanup_pop (0);
}
}
 
 
 
 
static void
static void
inner (int a)
inner (int a)
{
{
  pthread_cleanup_push (cleanup, (void *) (long int) a);
  pthread_cleanup_push (cleanup, (void *) (long int) a);
  if (a)
  if (a)
    return;
    return;
  pthread_cleanup_pop (0);
  pthread_cleanup_pop (0);
}
}
 
 
 
 
static void *
static void *
t2 (void *arg)
t2 (void *arg)
{
{
  pthread_cleanup_push (cleanup, (void *) (long int) 2);
  pthread_cleanup_push (cleanup, (void *) (long int) 2);
  inner ((int) (long int) arg);
  inner ((int) (long int) arg);
  return NULL;
  return NULL;
  pthread_cleanup_pop (0);
  pthread_cleanup_pop (0);
}
}
 
 
 
 
/* This does not work yet.  */
/* This does not work yet.  */
volatile int cleanupokcnt;
volatile int cleanupokcnt;
 
 
static void
static void
cleanupok (void *arg)
cleanupok (void *arg)
{
{
  ++cleanupokcnt;
  ++cleanupokcnt;
}
}
 
 
 
 
static void *
static void *
t3 (void *arg)
t3 (void *arg)
{
{
  pthread_cleanup_push (cleanupok, (void *) (long int) 4);
  pthread_cleanup_push (cleanupok, (void *) (long int) 4);
  inner ((int) (long int) arg);
  inner ((int) (long int) arg);
  pthread_exit (NULL);
  pthread_exit (NULL);
  pthread_cleanup_pop (0);
  pthread_cleanup_pop (0);
}
}
 
 
 
 
static void
static void
innerok (int a)
innerok (int a)
{
{
  pthread_cleanup_push (cleanupok, (void *) (long int) a);
  pthread_cleanup_push (cleanupok, (void *) (long int) a);
  pthread_exit (NULL);
  pthread_exit (NULL);
  pthread_cleanup_pop (0);
  pthread_cleanup_pop (0);
}
}
 
 
 
 
static void *
static void *
t4 (void *arg)
t4 (void *arg)
{
{
  pthread_cleanup_push (cleanupok, (void *) (long int) 6);
  pthread_cleanup_push (cleanupok, (void *) (long int) 6);
  innerok ((int) (long int) arg);
  innerok ((int) (long int) arg);
  pthread_cleanup_pop (0);
  pthread_cleanup_pop (0);
  return NULL;
  return NULL;
}
}
 
 
 
 
int
int
main (int argc, char *argv[])
main (int argc, char *argv[])
{
{
  pthread_t td;
  pthread_t td;
  int err;
  int err;
  char *tmp;
  char *tmp;
  const char *prefix;
  const char *prefix;
  const char template[] = "thtstXXXXXX";
  const char template[] = "thtstXXXXXX";
  struct stat64 st;
  struct stat64 st;
  int result = 0;
  int result = 0;
 
 
  prefix = argc > 1 ? argv[1] : "";
  prefix = argc > 1 ? argv[1] : "";
  tmp = (char *) alloca (strlen (prefix) + sizeof template);
  tmp = (char *) alloca (strlen (prefix) + sizeof template);
  strcpy (stpcpy (tmp, prefix), template);
  strcpy (stpcpy (tmp, prefix), template);
 
 
  fd = mkstemp (tmp);
  fd = mkstemp (tmp);
  if (fd == -1)
  if (fd == -1)
    {
    {
      printf ("cannot create temporary file: %m");
      printf ("cannot create temporary file: %m");
      exit (1);
      exit (1);
    }
    }
  unlink (tmp);
  unlink (tmp);
 
 
  err = pthread_barrier_init (&bar, NULL, 2);
  err = pthread_barrier_init (&bar, NULL, 2);
  if (err != 0 )
  if (err != 0 )
    {
    {
      printf ("cannot create barrier: %s\n", strerror (err));
      printf ("cannot create barrier: %s\n", strerror (err));
      exit (1);
      exit (1);
    }
    }
 
 
#ifdef NOT_YET
#ifdef NOT_YET
  err = pthread_create (&td, NULL, t1, NULL);
  err = pthread_create (&td, NULL, t1, NULL);
  if (err != 0)
  if (err != 0)
    {
    {
      printf ("cannot create thread t1: %s\n", strerror (err));
      printf ("cannot create thread t1: %s\n", strerror (err));
      exit (1);
      exit (1);
    }
    }
 
 
  err = pthread_join (td, NULL);
  err = pthread_join (td, NULL);
  if (err != 0)
  if (err != 0)
    {
    {
      printf ("cannot join thread: %s\n", strerror (err));
      printf ("cannot join thread: %s\n", strerror (err));
      exit (1);
      exit (1);
    }
    }
 
 
  err = pthread_create (&td, NULL, t2, (void *) 3);
  err = pthread_create (&td, NULL, t2, (void *) 3);
  if (err != 0)
  if (err != 0)
    {
    {
      printf ("cannot create thread t2: %s\n", strerror (err));
      printf ("cannot create thread t2: %s\n", strerror (err));
      exit (1);
      exit (1);
    }
    }
 
 
  err = pthread_join (td, NULL);
  err = pthread_join (td, NULL);
  if (err != 0)
  if (err != 0)
    {
    {
      printf ("cannot join thread: %s\n", strerror (err));
      printf ("cannot join thread: %s\n", strerror (err));
      exit (1);
      exit (1);
    }
    }
 
 
  err = pthread_create (&td, NULL, t3, (void *) 5);
  err = pthread_create (&td, NULL, t3, (void *) 5);
  if (err != 0)
  if (err != 0)
    {
    {
      printf ("cannot create thread t3: %s\n", strerror (err));
      printf ("cannot create thread t3: %s\n", strerror (err));
      exit (1);
      exit (1);
    }
    }
 
 
  err = pthread_join (td, NULL);
  err = pthread_join (td, NULL);
  if (err != 0)
  if (err != 0)
    {
    {
      printf ("cannot join thread: %s\n", strerror (err));
      printf ("cannot join thread: %s\n", strerror (err));
      exit (1);
      exit (1);
    }
    }
#endif
#endif
 
 
  err = pthread_create (&td, NULL, t4, (void *) 7);
  err = pthread_create (&td, NULL, t4, (void *) 7);
  if (err != 0)
  if (err != 0)
    {
    {
      printf ("cannot create thread t3: %s\n", strerror (err));
      printf ("cannot create thread t3: %s\n", strerror (err));
      exit (1);
      exit (1);
    }
    }
 
 
  err = pthread_join (td, NULL);
  err = pthread_join (td, NULL);
  if (err != 0)
  if (err != 0)
    {
    {
      printf ("cannot join thread: %s\n", strerror (err));
      printf ("cannot join thread: %s\n", strerror (err));
      exit (1);
      exit (1);
    }
    }
 
 
  if (fstat64 (fd, &st) < 0)
  if (fstat64 (fd, &st) < 0)
    {
    {
      printf ("cannot stat temporary file: %m\n");
      printf ("cannot stat temporary file: %m\n");
      result = 1;
      result = 1;
    }
    }
  else if (st.st_size != 0)
  else if (st.st_size != 0)
    {
    {
      char buf[512];
      char buf[512];
      puts ("some cleanup handlers ran:");
      puts ("some cleanup handlers ran:");
      fflush (stdout);
      fflush (stdout);
      __lseek (fd, 0, SEEK_SET);
      __lseek (fd, 0, SEEK_SET);
      while (1)
      while (1)
        {
        {
          ssize_t n = read (fd, buf, sizeof buf);
          ssize_t n = read (fd, buf, sizeof buf);
          if (n <= 0)
          if (n <= 0)
            break;
            break;
          write (STDOUT_FILENO, buf, n);
          write (STDOUT_FILENO, buf, n);
        }
        }
      result = 1;
      result = 1;
    }
    }
 
 
  // if (cleanupokcnt != 3)  will be three once t3 runs
  // if (cleanupokcnt != 3)  will be three once t3 runs
  if (cleanupokcnt != 2)
  if (cleanupokcnt != 2)
    {
    {
      printf ("cleanupokcnt = %d\n", cleanupokcnt);
      printf ("cleanupokcnt = %d\n", cleanupokcnt);
      result = 1;
      result = 1;
    }
    }
 
 
  return result;
  return result;
}
}
 
 

powered by: WebSVN 2.1.0

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