* 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 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
1 sibling, 0 replies; 5+ messages in thread
From: Scott A McConnell @ 2002-01-28 15:22 UTC (permalink / raw)
To: H . J . Lu; +Cc: GNU C Library, linux-mips
"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?
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
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: A linuxthreads bug on mips?
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
1 sibling, 1 reply; 5+ messages in thread
From: Ulrich Drepper @ 2002-01-29 8:54 UTC (permalink / raw)
To: H . J . Lu; +Cc: GNU C Library, linux-mips
"H . J . Lu" <hjl@lucon.org> writes:
> 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.
--
---------------. ,-. 1325 Chesapeake Terrace
Ulrich Drepper \ ,-------------------' \ Sunnyvale, CA 94089 USA
Red Hat `--' drepper at redhat.com `------------------------
^ permalink raw reply [flat|nested] 5+ messages in thread* Re: A linuxthreads bug on mips?
2002-01-29 8:54 ` Ulrich Drepper
@ 2002-01-30 2:41 ` H . J . Lu
0 siblings, 0 replies; 5+ messages in thread
From: H . J . Lu @ 2002-01-30 2:41 UTC (permalink / raw)
To: Ulrich Drepper; +Cc: GNU C Library, linux-mips
On Tue, Jan 29, 2002 at 12:54:41AM -0800, Ulrich Drepper wrote:
> "H . J . Lu" <hjl@lucon.org> writes:
>
> > 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 <hjl@gnu.org>
* sysdeps/mips/pt-machine.h (testandset): Return 1 when failed
to set.
(__compare_and_swap): Return 0 when failed to compare or swap.
--- linuxthreads/sysdeps/mips/pt-machine.h.llsc Mon Jan 28 18:02:45 2002
+++ linuxthreads/sysdeps/mips/pt-machine.h Tue Jan 29 16:59:46 2002
@@ -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");
^ 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.