Hi!
Code prototype follows. It uses mutexes and can be used with up to
page_size/sizeof(uint32) pairs. I've checked that it compiles fine and
works back to kernel 2.6.11 compiled in year 2005, which should be more
than enough.

It uses a regular file as a backing for the shared pages which makes it
even easier to replace the fifo based solution we have. All that is
needed is to map the same file created in the test temporary directory
just like the fifo does.

Please anone that uses LTP, check that the code compiles and works for
fine on all distributions you care for. Once that is checked, I will
write the new library functions based on this code and convert all the
test to the new interface.

-- 
Cyril Hrubis
chru...@suse.cz
#include <fcntl.h>
#include <stdint.h>
#include <unistd.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <sys/syscall.h>
#include <linux/futex.h>

#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>

typedef volatile uint32_t futex_t;

static futex_t *futexes;

static void finit(void)
{
	int fd;

	fd = open("/tmp/syncronization_futex_base_file", O_RDWR | O_CREAT, 0666);

	if (fd < 0) {
		fprintf(stderr, "open(): %s\n", strerror(errno));
		exit(1);
	}

	if (ftruncate(fd, getpagesize())) {
		fprintf(stderr, "ftruncate(): %s\n", strerror(errno));
		exit(1);
	}

	futexes = mmap(NULL, getpagesize(), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);

	if (futexes == MAP_FAILED) {
		fprintf(stderr, "mmap(): %s\n", strerror(errno));
		exit(1);
	}
}

static void fwait(unsigned int id, struct timespec *timeout)
{
	syscall(SYS_futex, &futexes[id], FUTEX_WAIT, futexes[id], timeout);
}

static void fwake(unsigned int id)
{
	int ret;

	do {
		ret = syscall(SYS_futex, &futexes[id], FUTEX_WAKE, INT_MAX, NULL);
		usleep(100);
	} while (ret == 0);
}

int main(void)
{
	int pid;

	finit();

	pid = fork();

	switch (pid) {
	case 0:
		fprintf(stderr, "Child calls wait\n");
		fwait(0, NULL);
		fprintf(stderr, "Child woken up\n");
	break;
	case -1:
		fprintf(stderr, "fork(): %s\n", strerror(errno));
	break;
	default:
		/* uncomment to change ordering */
		//usleep(100);
		fprintf(stderr, "Parent calls wake\n");
		fwake(0);
		fprintf(stderr, "Parent finishes\n");
	}

	return 0;
}
------------------------------------------------------------------------------
Dive into the World of Parallel Programming. The Go Parallel Website,
sponsored by Intel and developed in partnership with Slashdot Media, is your
hub for all things parallel software development, from weekly thought
leadership blogs to news, videos, case studies, tutorials and more. Take a
look and join the conversation now. http://goparallel.sourceforge.net/
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list

Reply via email to