git » libfilo » master » tree

[master] / tests / test1.c

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

#include "libfilo.h"

#define LOCK(start, len, mode)	 			\
	do {						\
		printf("%lu: about to lock\n", pthread_self()); \
		filo_lock(&l, start, len, mode);	\
		printf("%lu: lock\t %d %d %d\n", pthread_self(), start, len, mode); \
	} while(0);

#define UNLOCK(start, len)	 			\
	do {						\
		printf("%lu: about to unlock\n", pthread_self()); \
		filo_unlock(&l, start, len);		\
		printf("%lu: unlock\t %d %d\n", pthread_self(), start, len); \
	} while(0);

#define LU_S(start, len, mode, sl)			\
	do {						\
		LOCK(start, len, mode);			\
		sleep(sl);				\
		UNLOCK(start, len);			\
	} while(0);

filock_t l;

void *lth1(void *unused) {
	LU_S(0, 50, FL_RD_MODE, 5);
	return NULL;
}

void *lth2(void *unused) {
	LU_S(51, 50, FL_RD_MODE, 7);
	return NULL;
}


void *lth3(void *unused) {
	sleep(1);
	printf("about to lock wr\n");
	filo_lock(&l, 10, 100, FL_WR_MODE);
	printf("got!\n");
	return NULL;
}

int main(void) {
	pthread_t t1, t2, t3;

	filo_init(&l);

	filo_lock(&l, 10, 100, FL_WR_MODE);
	filo_unlock(&l, 10, 100);

	filo_lock(&l, 0, 100, FL_WR_MODE);
	filo_unlock(&l, 10, 10);
	printf("aca vamos de nuevo\n");
	filo_lock(&l, 90, 50, FL_WR_MODE);
	filo_lock(&l, 10, 100, FL_RD_MODE);
	//filo_lock(&l, 10, 100, FL_WR_MODE);
	filo_unlock(&l, 10, 100);
	filo_unlock(&l, 0, 200);

	printf("done static\n\n");
	pthread_create(&t1, NULL, &lth1, NULL);
	pthread_create(&t2, NULL, &lth2, NULL);
	pthread_create(&t3, NULL, &lth3, NULL);

	pthread_join(t1, NULL);
	pthread_join(t2, NULL);
	pthread_join(t3, NULL);
	return 0;

}