Skip to contentSkip to conversation

A linuxthreads bug on mips?

DISCUSSION H . J . Lu 3 participants 4 messages Jan 26, 2002 – Jan 30, 2002 MIPS

lore
Share & tools
lore

H . J . Lu

author
#0 lore 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;
}

#1 lore raw
Quoted 4 lines · H . J . Lu

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.,

I ran the program on the PC and a MIPS le NEC vr5432. It worked fine on both machines.

I do not run the pthread library on the SGI site because it dead locks. I rebuilt the pthread lib natively on my MIPS box and use that lib in place of the lib provided in the RPM on the SGI site.
(I suspect it is a cross compilation problem rather than a problem in the pthread lib.)

I believe others have also posted about deadlocks with the pthread lib on the SGI site.

Scott

#2 lore raw
In reply to H . J . Lu
Quoted 3 lines · H . J . Lu

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?

Only if you assume fair scheduling which is not necessarily the case. Assume the put() function has to stop because the buffer is full. The get() function now reads. It can read everything. Calling pthread_cond_signal() does not mean that the put() function gets running. Instead get() keeps running and exhausts the input buffer and then gets in the

    while (b->writepos == b->readpos)
    {
      pthread_cond_wait (&b->notempty, &b->lock);
    }

loop where it can wake up immediately. put() never is required to be runnable.

Therefore your revised code is not acceptable although it probably will almost always work.

Signature · 3 lines
---------------.                          ,-.   1325 Chesapeake Terrace
Ulrich Drepper  \    ,-------------------'   \  Sunnyvale, CA 94089 USA
Red Hat          `--' drepper at redhat.com   `------------------------
author
#3 lore raw
Quoted 10 lines · Ulrich Drepper
Quoted 3 lines · H . J . Lu

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?

Therefore your revised code is not acceptable although it probably will almost always work.

It seems that the usage of ll/sc in glibc is wrong when they are used to implement the compare/set operations. With this patch, the testcase works now on mips when glibc is compiled with -mips2. Could someone please double check my changes? If they are really correct, I will verify other ll/sc usages in glibc.

Thanks.

H.J.
-----.
2002-01-29 H.J. Lu <[email protected]>

* sysdeps/mips/pt-machine.h (testandset): Return 1 when failed to set.
(__compare_and_swap): Return 0 when failed to compare or swap.

sysdeps/mips/pt-machine.h renamed from sysdeps/mips/pt-machine.h.llsc +11 -19
@@ -42,16 +42,12 @@ testandset (int *spinlock)
 
   __asm__ __volatile__
     ("/* Inline spinlock test & set */\n\t"
-     "1:\n\t"
      "ll	%0,%3\n\t"
-     ".set	push\n\t"
-     ".set	noreorder\n\t"
-     "bnez	%0,2f\n\t"
-     " li	%1,1\n\t"
-     ".set	pop\n\t"
+     "bnez	%0,1f\n\t"
+     "li	%1,1\n\t"
      "sc	%1,%2\n\t"
-     "beqz	%1,1b\n"
-     "2:\n\t"
+     "sltu	%0,%1,1\n"
+     "1:\n\t"
      "/* End spinlock test & set */"
      : "=&r" (ret), "=&r" (temp), "=m" (*spinlock)
      : "m" (*spinlock)
@@ -84,22 +80,18 @@ register char * stack_pointer __asm__ ("
 PT_EI int
 __compare_and_swap (long int *p, long int oldval, long int newval)
 {
-  long int ret;
+  long int ret, temp;
 
   __asm__ __volatile__
     ("/* Inline compare & swap */\n\t"
+     "move	%0,$0\n\t"
+     "ll	%1,%5\n\t"
+     "bne	%1,%3,1f\n\t"
+     "move	%0,%4\n\t"
+     "sc	%0,%2\n"
      "1:\n\t"
-     "ll	%0,%4\n\t"
-     ".set	push\n"
-     ".set	noreorder\n\t"
-     "bne	%0,%2,2f\n\t"
-     " move	%0,%3\n\t"
-     ".set	pop\n\t"
-     "sc	%0,%1\n\t"
-     "beqz	%0,1b\n"
-     "2:\n\t"
      "/* End compare & swap */"
-     : "=&r" (ret), "=m" (*p)
+     : "=&r" (ret), "=&r" (temp), "=m" (*p)
      : "r" (oldval), "r" (newval), "m" (*p)
      : "memory");