[ELDK] Problem with pthread_cond_wait on 8xx?

Frank Svendsbøe frank.svendsboe at gmail.com
Tue Sep 22 17:55:17 CEST 2009


Hi,
I have a problem with high CPU load on 8xx when using pthread_cond_wait.
I'm using the glibc v2.6 that came with ELDK 4.2, compiled with NPTL
support. The same code compiled for x86 works fine (but then using
glibc 2.9). I'm running torvalds mainline kernel v2.6.26-rc2.

Can anyone here using ELDK 4.2 on 8xx or another PowerPC target try to compile
the program listed below and check the CPU load when running it? Or pinpoint
a problem with the code.

My current assumption is that the glibc pthread_cond_wait call is
implemented using
busywaiting/spinlocking on 8xx, or something is wrong in the kernel.

Best regards,
Frank Svendsbøe

/*
  A simple pthread_cond_wait example showing the
  problem with NPTL v2.6 on MPC8xx.

  The problem: 100% load when executing
  pthread_cond_wait.

  The same program compiled for x86, using NPTL
  v2.9 have approx. 0% load.

  - Frank

*/

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

struct event
{
	pthread_mutex_t lock;
	pthread_cond_t cond;
	volatile int count;
};

static void signal_handler (int sign);
static void event_init (struct event* p);
static void event_wait (struct event* p);
static void event_signal (struct event* p);


struct event event;


int main(void)
{
	event_init(&event);
	signal (SIGINT, &signal_handler);

	event_wait(&event);
	printf("Bye bye\n");

	return 0;
}


static void event_init (struct event* p)
{
	pthread_mutex_init(&p->lock, NULL);
	pthread_cond_init(&p->cond, NULL);
	p->count = 0;
}

/*
  Q: Does pthread_cond_wait spinlocks in
  the glibc/NPTL implementation on 8xx?

 */
static void event_wait (struct event* p)
{
	pthread_mutex_lock(&p->lock);
	while (p->count == 0)
		pthread_cond_wait(&p->cond, &p->lock);
	p->count = 0;
	pthread_mutex_unlock(&p->lock);
}

static void event_signal (struct event* p)
{
	pthread_mutex_lock(&p->lock);
	if (p->count == 0)
		pthread_cond_signal(&p->cond);
	p->count = 1;
	pthread_mutex_unlock(&p->lock);
}


static void signal_handler (int sign)
{
	event_signal(&event);
}


More information about the eldk mailing list