LKML Archive on lore.kernel.org
help / color / mirror / Atom feed
* Problem with POSIX threads in latest kernel...
@ 2007-01-16  1:47 J.A. Magallón
       [not found] ` <20070122115630.a3d790f5.akpm@osdl.org>
  0 siblings, 1 reply; 2+ messages in thread
From: J.A. Magallón @ 2007-01-16  1:47 UTC (permalink / raw)
  To: Linux-Kernel, 

[-- Attachment #1: Type: text/plain, Size: 918 bytes --]

Hi...

I run the (almost) latest -mm kernel (2.6.20-rc3-mm1), and see some strange behaviour
with POSIX threads (glibc-2.4).
I have downgraded my test to a simple textboox example for a SMP-safe spool
queue, it's just a circular queue with a mutex and a condition variable for in
and out. I have seen the same structure in several places.

Well, it just sometimes gets blocked. GDB says its stuck in pthread_wait().
I could swear it worked on previous kernels. It works as is on IRIX.
I will try to build an older kernel to test.
I takes a second to block it with something like while :; tst; done.

Any ideas ?

--
J.A. Magallon <jamagallon()ono!com>     \               Software is like sex:
                                         \         It's better when it's free
Mandriva Linux release 2007.1 (Cooker) for i586
Linux 2.6.19-jam04 (gcc 4.1.2 20061110 (prerelease) (4.1.2-0.20061110.2mdv2007.1)) #0 SMP PREEMPT

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: tst.c --]
[-- Type: text/x-csrc; name=tst.c, Size: 1871 bytes --]

#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <pthread.h>

#define SIZE		16

int				jobs[SIZE];
int				in;
int				slots;
pthread_mutex_t	slots_mutex;
pthread_cond_t	slots_cond;
int				out;
int				items;
pthread_mutex_t	items_mutex;
pthread_cond_t	items_cond;

void put(int job);
void get(int* job);
void* prod(void* data);
void* cons(void* data);

int main(int argc,char** argv)
{
	pthread_t	prodid,consid;

	in = 0;
	slots = SIZE;
	pthread_mutex_init(&slots_mutex,0);
	pthread_cond_init(&slots_cond,0);
	out = 0;
	items = 0;
	pthread_mutex_init(&items_mutex,0);
	pthread_cond_init(&items_cond,0);

	pthread_setconcurrency(3);
	pthread_create(&prodid,0,prod,0);
	pthread_create(&consid,0,cons,0);

	pthread_join(prodid,0);
	pthread_join(consid,0);

	return 0;
}

void* prod(void* data)
{
	int	i;

	for (i=0; i<1000; i++)
	{
		if (!(i%100))
			printf("put %d\n",i);
		put(i);
	}
	put(-1);
	puts("prod done");

	return 0;
}

void* cons(void* data)
{
	int	i;
	do
	{
		get(&i);
		if (!(i%100))
			printf("got %d\n",i);
	}
	while (i>=0);
	puts("cons done");

	return 0;
}

void put(int job)
{
	pthread_mutex_lock(&slots_mutex);
		while (slots<=0)
			pthread_cond_wait(&slots_cond,&slots_mutex);
		jobs[in] = job;
		in++;
		in %= SIZE;
		slots--;
		items++;
	pthread_mutex_unlock(&slots_mutex);

	pthread_mutex_lock(&items_mutex);
		pthread_cond_signal(&items_cond);
	pthread_mutex_unlock(&items_mutex);
}

void get(int* job)
{
	pthread_mutex_lock(&items_mutex);
		while (items<=0)
			pthread_cond_wait(&items_cond,&items_mutex);
		*job = jobs[out];
		out++;
		out %= SIZE;
		items--;
		slots++;
	pthread_mutex_unlock(&items_mutex);

	pthread_mutex_lock(&slots_mutex);
		pthread_cond_signal(&slots_cond);
	pthread_mutex_unlock(&slots_mutex);
}


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

* Re: Problem with POSIX threads in latest kernel...
       [not found] ` <20070122115630.a3d790f5.akpm@osdl.org>
@ 2007-01-22 22:59   ` J.A. Magallón
  0 siblings, 0 replies; 2+ messages in thread
From: J.A. Magallón @ 2007-01-22 22:59 UTC (permalink / raw)
  To: Andrew Morton, Linux-Kernel, 

On Mon, 22 Jan 2007 11:56:30 -0800, Andrew Morton <akpm@osdl.org> wrote:

> > On Tue, 16 Jan 2007 02:47:10 +0100 "J.A. Magallón" <jamagallon@ono.com> wrote:
> > Hi...
> > 
> > I run the (almost) latest -mm kernel (2.6.20-rc3-mm1), and see some strange behaviour
> > with POSIX threads (glibc-2.4).
> > I have downgraded my test to a simple textboox example for a SMP-safe spool
> > queue, it's just a circular queue with a mutex and a condition variable for in
> > and out. I have seen the same structure in several places.
> > 
> > Well, it just sometimes gets blocked. GDB says its stuck in pthread_wait().
> > I could swear it worked on previous kernels. It works as is on IRIX.
> > I will try to build an older kernel to test.
> > I takes a second to block it with something like while :; tst; done.
> > 
> > Any ideas ?
> 
> Do I need to worry about this still?

Oops, no, sorry. It was buggy code that previously seemed to work.
Buggy code:

	pthread_mutex_lock(&slots_mutex);
		while (slots<=0)
			pthread_cond_wait(&slots_cond,&slots_mutex);
		slots--;
		items++;
	pthread_mutex_unlock(&slots_mutex);

	pthread_mutex_lock(&items_mutex);
		pthread_cond_signal(&items_cond);
	pthread_mutex_unlock(&items_mutex);

(buggy because it acceses items without locking. The same in the other
endpoint of the queue).

Correct code:

	pthread_mutex_lock(&slots_mutex);
		while (slots<=0)
			pthread_cond_wait(&slots_cond,&slots_mutex);
		slots--;
	pthread_mutex_unlock(&slots_mutex);

	pthread_mutex_lock(&items_mutex);
		items++;
		pthread_cond_signal(&items_cond);
	pthread_mutex_unlock(&items_mutex);

So don't worry. I was so busy I forgot to post the solution.

--
J.A. Magallon <jamagallon()ono!com>     \               Software is like sex:
                                         \         It's better when it's free
Mandriva Linux release 2007.1 (Cooker) for i586
Linux 2.6.19-jam04 (gcc 4.1.2 20061110 (prerelease) (4.1.2-0.20061110.2mdv2007.1)) #0 SMP PREEMPT

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

end of thread, other threads:[~2007-01-22 23:00 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-01-16  1:47 Problem with POSIX threads in latest kernel J.A. Magallón
     [not found] ` <20070122115630.a3d790f5.akpm@osdl.org>
2007-01-22 22:59   ` J.A. Magallón

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).