#include <mpi.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/file.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdarg.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/sem.h>
#include <semaphore.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <errno.h>

struct flock lock =
        {
                .l_type         = F_WRLCK,
                .l_whence       = SEEK_SET,
                .l_start        = 0,
                .l_len          = 1024,
                .l_pid          = 0,
        };


int main(int argc, char **argv)
{
        char *filename = "/mnt/lustre/flock_torture";
        int numloops = 50;
        int sleeptime = 10;
	int rank;
	int fd = -1;
	int n;
	int error;
	int rc;
	char c;

        error = MPI_Init(&argc, &argv);
        if (error != MPI_SUCCESS) {
                printf("MPI_Init failed: %d\n", error);
		MPI_Abort(MPI_COMM_WORLD, -1);
	}

        /* Parse command line options */
        while ((c = getopt(argc, argv, "f:n:s:")) != EOF) {
                switch (c) {
                case 'f':
                        filename = optarg;
                        break;
                case 'n':
                        numloops = strtoul(optarg, NULL, 0);
                        break;
		case 's':
			sleeptime = strtoul(optarg, NULL, 0);
                }
        }

	MPI_Comm_rank(MPI_COMM_WORLD, &rank);

        for (n = 0; n < numloops; n++) {

		/* Rank 0 opens a file and gets the lock right away */
		if (rank == 0) {
			fd = open(filename, O_WRONLY|O_CREAT|O_TRUNC, 0666);
			if (fd < 0) {
				printf("open() returned %s\n",
					strerror(errno));
				MPI_Abort(MPI_COMM_WORLD, -1);
			}

			lock.l_type = F_WRLCK;
			rc = fcntl(fd, F_SETLKW, lock);
			if (rc < 0) {
				printf("fcntl() returned %s\n",
					strerror(errno));
				MPI_Abort(MPI_COMM_WORLD, -1);
			}
		}
		/* Everybody arrives here instantly */
		MPI_Barrier(MPI_COMM_WORLD);

		/* Step 2: */
		if (rank == 0) {
			/* Rank 0 is sleeping... */
			sleep(sleeptime);
			/* Then unlocking */
			lock.l_type = F_UNLCK;
			rc = fcntl(fd, F_SETLKW, lock);
			if (rc < 0) {
				printf("fcntl() returned %s\n",
					strerror(errno));
				MPI_Abort(MPI_COMM_WORLD, -1);
			}
		} else {
			/* While everybody else is racing for the same lock */
			fd = open(filename, O_RDWR);
			if (fd < 0) {
				printf("open() returned %s\n",
					strerror(errno));
				MPI_Abort(MPI_COMM_WORLD, -1);
			}
			lock.l_type = F_WRLCK;
			rc = fcntl(fd, F_SETLKW, lock);
                        if (rc < 0) {
                                printf("fcntl() returned %s\n",
                                        strerror(errno));
                                MPI_Abort(MPI_COMM_WORLD, -1);
                        }
			lock.l_type = F_UNLCK;
			rc = fcntl(fd, F_SETLKW, lock);
                        if (rc < 0) {
                                printf("fcntl() returned %s\n",
                                        strerror(errno));
                                MPI_Abort(MPI_COMM_WORLD, -1);
			}
		}
		close(fd);
		/* Threads arrive here as they complete their lock acquisition.
		 * With rank0 arriving first */
		MPI_Barrier(MPI_COMM_WORLD);
		if (rank == 0)
			printf("Iteration %d of %d complete\n", n, numloops);
	}

	/* And finally we are done */
	MPI_Finalize();

	return 0;
}

