All of lore.kernel.org
 help / color / mirror / Atom feed
* A linuxthreads bug on mips?
@ 2002-01-26  7:45 H . J . Lu
  2002-01-28 15:22 ` Scott A McConnell
  2002-01-29  8:54 ` Ulrich Drepper
  0 siblings, 2 replies; 5+ messages in thread
From: H . J . Lu @ 2002-01-26  7:45 UTC (permalink / raw)
  To: GNU C Library, linux-mips

Here is a modified ex2.c which only uses one conditional variable. It
works fine on x86. But it leads to dead lock on mips where both
producer and consumer are suspended. Is this testcase correct?


H.J.
----
/* The classic producer-consumer example.
   Illustrates mutexes and conditions.
   All integers between 0 and 9999 should be printed exactly twice,
   once to the right of the arrow and once to the left. */

#include <stdio.h>
#include "pthread.h"

#define BUFFER_SIZE 16

#define thread_cycles 10
#define thread_pairs 10
#define iters 10000

/* Circular buffer of integers. */

struct prodcons
{
  int buffer[BUFFER_SIZE];	/* the actual data */
  pthread_mutex_t lock;		/* mutex ensuring exclusive access to buffer */
  int readpos, writepos;	/* positions for reading and writing */
  pthread_cond_t cond;	/* signaled when buffer is not empty nor full */
};

/* Initialize a buffer */
static void
init (struct prodcons *b)
{
  pthread_mutex_init (&b->lock, NULL);
  pthread_cond_init (&b->cond, NULL);
  b->readpos = 0;
  b->writepos = 0;
}

/* Store an integer in the buffer */
static void
put (struct prodcons *b, int data)
{
  pthread_mutex_lock (&b->lock);
  /* Wait until buffer is not full */
  while ((b->writepos + 1) % BUFFER_SIZE == b->readpos)
    {
      pthread_cond_wait (&b->cond, &b->lock);
      /* pthread_cond_wait reacquired b->lock before returning */
    }
  /* Write the data and advance write pointer */
  b->buffer[b->writepos] = data;
  b->writepos++;
  if (b->writepos >= BUFFER_SIZE)
    b->writepos = 0;
  /* Signal that the buffer is now not empty */
  pthread_cond_signal (&b->cond);
  pthread_mutex_unlock (&b->lock);
}

/* Read and remove an integer from the buffer */
static int
get (struct prodcons *b)
{
  int data;
  pthread_mutex_lock (&b->lock);
  /* Wait until buffer is not empty */
  while (b->writepos == b->readpos)
    {
      pthread_cond_wait (&b->cond, &b->lock);
    }
  /* Read the data and advance read pointer */
  data = b->buffer[b->readpos];
  b->readpos++;
  if (b->readpos >= BUFFER_SIZE)
    b->readpos = 0;
  /* Signal that the buffer is now not full */
  pthread_cond_signal (&b->cond);
  pthread_mutex_unlock (&b->lock);
  return data;
}

/* A test program: one thread inserts integers from 1 to 10000,
   the other reads them and prints them. */

#define OVER (-1)

static void *
producer (void *data)
{
  struct prodcons *buffer = (struct prodcons *) data;
  int n;
  for (n = 0; n < 10000; n++)
    {
#if 0
      printf ("%d --->\n", n);
#endif
      put (buffer, n);
    }
  put (buffer, OVER);
  return NULL;
}

static void *
consumer (void *data)
{
  struct prodcons *buffer = (struct prodcons *) data;
  int d;
  while (1)
    {
      d = get (buffer);
      if (d == OVER)
	break;
#if 0
      printf ("---> %d\n", d);
#endif
    }
  return NULL;
}

struct prodcons buffer [thread_pairs];

int
main (void)
{
  pthread_t prod[thread_pairs];
  pthread_t cons[thread_pairs];
  int i, j;

  for (i = 0; i < thread_pairs; i++)
    init (&buffer [i]);

  for (j = 0; j < thread_cycles; j++)
    {
      for (i = 0; i < thread_pairs; i++)
	{
	  pthread_create (&prod[i], NULL, producer, (void *) &(buffer [i]));
	  pthread_create (&cons[i], NULL, consumer, (void *) &(buffer [i]));
	}

      for (i = 0; i < thread_pairs; i++)
	{
	  pthread_join (prod[i], NULL);
	  pthread_join (cons[i], NULL);
	}
    }

  return 0;
}

^ permalink raw reply	[flat|nested] 5+ messages in thread
* Re: A linuxthreads bug on mips?
@ 2002-01-26 16:15 Justin Carlson
  0 siblings, 0 replies; 5+ messages in thread
From: Justin Carlson @ 2002-01-26 16:15 UTC (permalink / raw)
  To: hjl; +Cc: libc-alpha, linux-mips

On Sat, 2002-01-26 at 02:45, H . J . Lu wrote:
> Here is a modified ex2.c which only uses one conditional variable. It
> works fine on x86. But it leads to dead lock on mips where both
> producer and consumer are suspended. Is this testcase correct?
> 

I stared at it for a while and see nothing wrong with it.  Well, ok, 
iters is #define'd but never used.  But somehow I doubt that's the root
of your problems.  ;)

-Justin

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2002-01-30  3:41 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2002-01-26  7:45 A linuxthreads bug on mips? H . J . Lu
2002-01-28 15:22 ` Scott A McConnell
2002-01-29  8:54 ` Ulrich Drepper
2002-01-30  2:41   ` H . J . Lu
  -- strict thread matches above, loose matches on Subject: below --
2002-01-26 16:15 Justin Carlson

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.