1 /* Function CreateMutex: Create a mutex using file locking. */
2 int CreateMutex(const char *path_name)
4 return open(path_name, O_EXCL|O_CREAT);
6 /* Function UnlockMutex: unlock a file. */
7 int FindMutex(const char *path_name)
9 return open(path_name, O_RDWR);
11 /* Function LockMutex: lock mutex using file locking. */
14 struct flock lock; /* file lock structure */
15 lock.l_type = F_WRLCK; /* set type: read or write */
16 lock.l_whence = SEEK_SET; /* start from the beginning of the file */
17 lock.l_start = 0; /* set the start of the locked region */
18 lock.l_len = 0; /* set the length of the locked region */
20 return fcntl(fd, F_SETLKW, &lock);
22 /* Function UnlockMutex: unlock a file. */
23 int UnlockMutex(int fd)
25 struct flock lock; /* file lock structure */
26 lock.l_type = F_UNLCK; /* set type: unlock */
27 lock.l_whence = SEEK_SET; /* start from the beginning of the file */
28 lock.l_start = 0; /* set the start of the locked region */
29 lock.l_len = 0; /* set the length of the locked region */
31 return fcntl(fd, F_SETLK, &lock);
33 /* Function RemoveMutex: remove a mutex (unlinking the lock file). */
34 int RemoveMutex(const char *path_name)
36 return unlink(path_name);
38 /* Function ReadMutex: read a mutex status. */
42 struct flock lock; /* file lock structure */
43 lock.l_type = F_WRLCK; /* set type: unlock */
44 lock.l_whence = SEEK_SET; /* start from the beginning of the file */
45 lock.l_start = 0; /* set the start of the locked region */
46 lock.l_len = 0; /* set the length of the locked region */
48 if ( (res = fcntl(fd, F_GETLK, &lock)) ) {