关闭

利用创建临时文件实现共享存储锁

标签: 存储nullfilefpsystemc
454人阅读 评论(0) 收藏 举报
分类:

实现的是读写锁,很垃圾,但是也保留着,以后鄙视自己用。

#include "bson_rw.h"

//return true if count file exist,
bool CheckReadLock(const char *filename)
{
	assert(filename != NULL);
	int len = strlen(filename);
	char *tmpname = (char*) malloc(len+13);
	strcpy(tmpname,filename);
	strcat(tmpname,".rlock.count");

	FILE *fp = fopen(tmpname, "r");
	if(fp == NULL)
	{
		return false;
	}
	fclose(fp);
	return true;
}
int FetchReadLockNum(const char *filename)
{
	assert(filename != NULL);
	int len = strlen(filename);
	char *tmpname = (char*) malloc(len+13);
	strcpy(tmpname,filename);
	strcat(tmpname,".rlock.count");

	FILE *fp = fopen(tmpname, "r");
	if(fp == NULL)
	{
		free(tmpname);
		return 0;
	}

	int num = 0;
	char c;
	while((c = fgetc(fp)) != EOF)
	{
		num++;
	}
	fclose(fp);
	
	return num;
}
void UlinkFile(const char *filename)
{
	assert(filename != NULL);
	int len = strlen(filename);
	char *tmpname = (char*) malloc(len+13);
	strcpy(tmpname,filename);
	strcat(tmpname,".rlock.count");

	unlink(tmpname);
}
void AddReadLockCount(const char *filename)
{
	assert(filename != NULL);
	int len = strlen(filename);
	char *tmpname = (char*) malloc(len+13);
	strcpy(tmpname,filename);
	strcat(tmpname,".rlock.count");

	FILE *fp = fopen(tmpname,"a");
	char c = 'a';
	fprintf(fp, "%c",c);
	fclose(fp);
	free(tmpname);
}
void DelReadLockCount(const char *filename)
{
	assert(filename != NULL);
	int len = strlen(filename);
	char *tmpname = (char*) malloc(len+13);
	strcpy(tmpname,filename);
	strcat(tmpname,".rlock.count");

	FILE *fp = fopen(tmpname, "r");
	if(fp == NULL)
	{
		free(tmpname);
		return ;
	}

	int num = 0;
	char c;
	while((c = fgetc(fp)) != EOF)
	{
		num++;
	}
	fclose(fp);

	if(num == 0)
	{
		unlink(tmpname);
	}
	else
	{
		FILE *fp_w = fopen(tmpname,"w");
		char *tmpstr = (char*)malloc(num);
		memset(tmpstr,'a',num);
		*(tmpstr+num-1) = '\0';
		fprintf(fp_w,"%s",tmpstr);
		fclose(fp_w);
	}
	free(tmpname);
}

#ifdef WIN32

HANDLE lock_file_w(const char *filename)
{
	assert(filename != NULL);
	int len = strlen(filename);
	char *tmpname = (char*) malloc(len+7);
	strcpy(tmpname,filename);
	strcat(tmpname,".wlock");

	HANDLE handle = CreateFileA(tmpname,
		0,
		FILE_SHARE_READ,
		NULL,
		CREATE_NEW ,
		FILE_ATTRIBUTE_SYSTEM ,
		NULL);

	if(handle == INVALID_HANDLE_VALUE)
	{
		free(tmpname);
		return INVALID_HANDLE_VALUE;
	}
	free(tmpname);
	return handle;
}

void unlock_file_w(const char *filename, HANDLE handle)
{
	assert(filename != NULL);
	int len = strlen(filename);
	char *tmpname = (char*) malloc(len+7);
	strcpy(tmpname,filename);
	strcat(tmpname,".wlock");

	CloseHandle(handle);
	unlink(tmpname);
	free(tmpname);
}

int lock_test_w(const char *name)
{
	assert(name != NULL);
	int len = strlen(name);
	char *tmpname = (char*) malloc(len+7);
	strcpy(tmpname,name);
	strcat(tmpname,".wlock");

	HANDLE handle = CreateFileA(tmpname,
		0,
		FILE_SHARE_READ,
		NULL,
		CREATE_NEW ,
		FILE_ATTRIBUTE_SYSTEM ,
		NULL);

	if(handle == INVALID_HANDLE_VALUE)
	{
		free(tmpname);
		CloseHandle(handle);
		return -FILE_ERR_ALREADY_LOCK;
	}

	CloseHandle(handle);
	unlink(tmpname);
	free(tmpname);
	return 0;
}

HANDLE lock_file_r(const char *filename)
{
	assert(filename != NULL);
	AddReadLockCount(filename);

	int len = strlen(filename);
	char *tmpname = (char*) malloc(len+7);
	strcpy(tmpname,filename);
	strcat(tmpname,".rlock");
	
	HANDLE handle = CreateFileA(tmpname,
		0,
		FILE_SHARE_READ,
		NULL,


		CREATE_NEW ,
		FILE_ATTRIBUTE_SYSTEM ,
		NULL);

	if(handle == INVALID_HANDLE_VALUE)
	{
		free(tmpname);
		return INVALID_HANDLE_VALUE;
	}
	free(tmpname);
	return handle;
}

void unlock_file_r(const char *filename, HANDLE handle)
{
	assert(filename != NULL);
	int len = strlen(filename);
	char *tmpname = (char*) malloc(len+7);
	strcpy(tmpname,filename);
	strcat(tmpname,".rlock");

	CloseHandle(handle);
	unlink(tmpname);
	free(tmpname);
}

int lock_test_r(const char *name)
{
	assert(name != NULL);
	int len = strlen(name);
	char *tmpname = (char*) malloc(len+7);
	strcpy(tmpname,name);
	strcat(tmpname,".rlock");

	HANDLE handle = CreateFileA(tmpname,
		0,
		FILE_SHARE_READ,
		NULL,
		CREATE_NEW ,
		FILE_ATTRIBUTE_SYSTEM ,
		NULL);

	if(handle == INVALID_HANDLE_VALUE)
	{
		free(tmpname);
		CloseHandle(handle);
		return -FILE_ERR_ALREADY_LOCK;
	}

	CloseHandle(handle);
	unlink(tmpname);
	free(tmpname);
	return 0;
}

#else

int lock_file_w(const char *filename)
{
	assert(filename != NULL);
	int len = strlen(filename);
	char *tmpname = (char*) malloc(len+7);
	strcpy(tmpname,filename);
	strcat(tmpname,".wlock");

	int fd = open(tmpname, O_CREAT | O_EXCL);
	if(fd == -1)
	{
		free(tmpname);
		return -1;
	}
	free(tmpname);
	return fd;	
}

void unlock_file_w(const char *filename, int fd)
{
	assert(filename != NULL);
	
	int len = strlen(filename);
	char *tmpname = (char*) malloc(len+7);
	strcpy(tmpname,filename);
	strcat(tmpname,".wlock");

	close(fd);
	unlink(tmpname);
	free(tmpname);
}

int lock_test_w(const char *name)
{
	assert(name != NULL);
	int len = strlen(name);
	char *tmpname = (char*) malloc(len+7);
	strcpy(tmpname,name);
	strcat(tmpname,".wlock");

	int fd = open(tmpname,  O_CREAT | O_EXCL);
	if(fd == -1)
	{
		close(fd);
		free(tmpname);
		return FILE_ERR_ALREADY_LOCK;
	}
	
	close(fd);
	unlink(tmpname);
	free(tmpname);
	return 0;
}

int lock_file_r(const char *filename)
{
	assert(filename != NULL);
	AddReadLockCount(filename);

	int len = strlen(filename);
	char *tmpname = (char*) malloc(len+7);
	strcpy(tmpname,filename);
	strcat(tmpname,".rlock");

	int fd = open(tmpname, O_CREAT | O_EXCL);
	if(fd == -1)
	{
		free(tmpname);
		return -1;
	}
	free(tmpname);
	return fd;	
}
void unlock_file_r(const char *filename, int fd)
{
	assert(filename != NULL);
	int len = strlen(filename);
	char *tmpname = (char*) malloc(len+7);
	strcpy(tmpname,filename);
	strcat(tmpname,".rlock");

	close(fd);
	unlink(tmpname);
	free(tmpname);
}
int lock_test_r(const char *name)
{
	assert(name != NULL);
	int len = strlen(name);
	char *tmpname = (char*) malloc(len+7);
	strcpy(tmpname,name);
	strcat(tmpname,".rlock");

	int fd = open(tmpname,  O_CREAT | O_EXCL);
	if(fd == -1)
	{
		close(fd);
		free(tmpname);
		return FILE_ERR_ALREADY_LOCK;
	}

	close(fd);
	unlink(tmpname);
	free(tmpname);
	return 0;
}

#endif


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:724235次
    • 积分:7828
    • 等级:
    • 排名:第2646名
    • 原创:130篇
    • 转载:109篇
    • 译文:0篇
    • 评论:64条
    最新评论