All of lore.kernel.org
 help / color / mirror / Atom feed
From: "H . J . Lu" <hjl@lucon.org>
To: GNU C Library <libc-alpha@sources.redhat.com>, linux-mips@oss.sgi.com
Subject: A linuxthreads bug on mips?
Date: Fri, 25 Jan 2002 23:45:42 -0800	[thread overview]
Message-ID: <20020125234542.A31028@lucon.org> (raw)

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;
}

             reply	other threads:[~2002-01-26  8:46 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-01-26  7:45 H . J . Lu [this message]
2002-01-28 15:22 ` A linuxthreads bug on mips? 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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20020125234542.A31028@lucon.org \
    --to=hjl@lucon.org \
    --cc=libc-alpha@sources.redhat.com \
    --cc=linux-mips@oss.sgi.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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.