This is the mail archive of the cygwin-patches mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH] POSIX barrier implementation, take 2


Hi.

Here is a second take on the POSIX barrier API. This time it is tested
with the attached barrier.c file. Here is a change log.


Newlib:

	* libc/include/sys/features.h (_POSIX_BARRIERS): Define for Cygwin.
	* libc/include/sys/types.h (pthread_barrier_t)
	(pthread_barrierattr_t): Do not define for Cygwin.

Cygwin:

	* common.din (pthread_barrierattr_init)
	(pthread_barrierattr_setpshared, pthread_barrierattr_getpshared)
	(pthread_barrierattr_destroy, pthread_barrier_init)
	(pthread_barrier_destroy, pthread_barrier_wait): Export.
	* include/cygwin/types.h (pthread_barrierattr_t)
	(pthread_barrier_t): Declare.
	* include/pthread.h (PTHREAD_BARRIER_SERIAL_THREAD)
	(pthread_barrierattr_init, pthread_barrierattr_setpshared)
	(pthread_barrierattr_getpshared, pthread_barrierattr_destroy)
	(pthread_barrier_init, pthread_barrier_destroy)
	(pthread_barrier_wait): Declare.
	* cygwin/thread.h (PTHREAD_BARRIER_MAGIC)
	(PTHREAD_BARRIERATTR_MAGIC): Define.
	(class pthread_barrierattr, class pthread_barrier): Declare.
	* cygwin/thread.cc (delete_and_clear): New local helper function.
	(class pthread_barrierattr, class pthread_barrier): Implement.

-- 
VH
#include <pthread.h>
#include <stdlib.h>
#include <stddef.h>
#include <stdint.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>


pthread_barrier_t barrier;


#define ROUNDS 5

void *
thread_routine (void * arg)
{
  int myid = (ptrdiff_t)arg;

  for (int round = 0; round != ROUNDS; ++round)
    {
      printf ("Thread %d reporting for duty. Going to wait on barrier."
              " Round %d.\n", myid, round);
      int retval = pthread_barrier_wait (&barrier);
      if (retval == PTHREAD_BARRIER_SERIAL_THREAD)
        printf ("I, number %d, was the last thread to make it to the barrier."
                " Round %d.\n",
                myid, round);
      else if (retval != 0)
        printf ("I, number %d, got error %d on barrier. Round %d.\n",
                myid, retval, round);
      else
        printf ("I, number %d, got released by the barrier. Round %d.\n",
                myid, round);

    }

  return NULL;
}


#define COUNT 5

pthread_t threads[COUNT];


int
main ()
{
  pthread_barrierattr_t battr;
  int retval = pthread_barrierattr_init (&battr);
  if (retval != 0)
    {
      printf ("Failed to initialize barrier attribute: %d", retval);
      return retval;
    }

  retval = pthread_barrierattr_setpshared (&battr,
                                                PTHREAD_PROCESS_SHARED);
  if (retval != 0)
    {
      printf ("Failed to set PTHREAD_PROCESS_SHARED on barrier attribute:"
              " %d\n",
              retval);
      return retval;
    }

  retval = pthread_barrierattr_setpshared (&battr,
                                                PTHREAD_PROCESS_PRIVATE);
  if (retval != 0)
    {
      printf ("Failed to set PTHREAD_PROCESS_PRIVATE on barrier attribute:"
              " %d\n",
              retval);
      return retval;
    }

  retval = pthread_barrier_init (&barrier, &battr, COUNT);
  if (retval != 0)
    {
      printf ("Failed to init barrier: %d\n", retval);
      return retval;
    }

  retval = pthread_barrierattr_destroy (&battr);
  if (retval != 0)
    {
      printf ("Failed to destroy barrier attribute: %d\n", retval);
      return retval;
    }

  for (int i = 0; i != COUNT; ++i)
    {
      retval = pthread_create(&threads[i], NULL, thread_routine,
                              (void*)(ptrdiff_t)i);
      if (retval != 0)
        {
          printf ("Failed to create thread %d, error %d.\n", i, retval);
          return retval;
        }
    }

  for (int i = 0; i != COUNT; ++i)
    {
      retval = pthread_join (threads[i], NULL);
      if (retval != 0)
        {
          printf ("Failed to join thread %d, error %d.\n", i, retval);
          return retval;
        }
    }

  retval = pthread_barrier_destroy (&barrier);
  if (retval != 0)
    {
      printf ("Failed to destroy barrier: %d\n", retval);
      return retval;
    }

  return 0;
}

Attachment: barrier02.patch.txt
Description: Text document

Attachment: signature.asc
Description: OpenPGP digital signature


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]