Solve the deadlock problem of nsdistributedlock process mutex (i)

Source: Internet
Author: User

In multi-process development under Mac, Nsdistributedlock is a very handy mutex solution, and it's a common way to use it:

/table>
 1 
2
3
4
5
6
7
8
 nsdistributedlock *lock = [[Nsdistributedlock] Alloc] Initwithpath:@ "/users/mac/desktop/lock.lock"]; 
while (![ Lock Trylock])
{
Sleep (1);

//do something
[lock unlock];

But in the actual use process, when executes to do something when the program exits, the program starts again after the Trylock can no longer succeed, into a deadlock state. This is a very hidden risk when using nsdistributedlock. The problem is how to automatically release the lock when the process exits.
There is an introduction to the record locking (recording lock) in advanced programming for the UNIX environment, which is also based on the file, but its unlocking condition just solves our problem because the record lock automatically releases the locks held when the process exits.

Fcntl is one of the best Unix-compatible record-locking implementations, with its interface described in Sys/fcntl.h, function prototypes are

1
int Fcnt1 (int cmd, ... /  * struct flock *flockptr */);

When the function is used to record a lock, CMD is f_getlk, f_setlk, or f_setlkw. The third parameter, called Flockptr, is a pointer to the flock structure.

 1 
2
3
4
5
6
7
 struct flock {
off_tl_start; /* Starting offset */
off_tl_len; /* len = 0 means until end of file */
pid_tl_pid; /* Lock owner */
shortl_type; /* Lock Type:read/write, etc. */
shortl_whence; /* Type of L_start */
};

Three command descriptions about the FCNTL function:
F_getlk gets the status of the file lock.
F_SETLK sets the state of the file lock. At this point the L_type value of the FLCOK structure must be f_rdlck, F_wrlck, or F_unlck. If the lock cannot be established, 1 is returned, and the error code is eacces or Eagain.
F_SETLKW the F_SETLK function is the same, but the lock cannot be established, the call waits until the lock action succeeds. If the signal is interrupted while waiting for the lock, it returns 1 immediately and the error code is EINTR.

Implement the Trylock in Nsdistributedlock with flock:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21st
22
23
- (BOOL) Trylock
{
int fd = open (path, o_creat| O_RDWR,0666);
if (FD <0)
{
NSLog (@ "[Err] Open fail:%s", Strerror (errno));
ReturnNO;
}

struct Flock lockinfo;
Lockinfo.l_type = F_wrlck;
Lockinfo.l_whence = Seek_set;
Lockinfo.l_start = 0;
Lockinfo.l_len = 0;

int re = Fcntl (fd, F_SETLK, &lockinfo);
if (re! = 0)
{
close (FD);
return no;
return yes;

/span>

Implement the unlock in Nsdistributedlock with flock:

1
2
3
4
5
6
7
8
9
10
11
- (void) _unlock
{
struct flock lockinfo;
Lockinfo.l_type = F_unlck;
Lockinfo .l_whence = Seek_set;
Lockinfo .l_start = 0;
Lockinfo .l_len = 0;
Fcntl (FD, F_SETLK, &lockinfo);
close (FD);

/span>

However, it is important to note that the record lock implemented by FCNTL is not a mutual exclusion in the process of multithreading, in order to solve these problems and fully implement all the methods and actual performance in Nsdistributedlock, here is my encapsulated Qmdistributedlock:
Download in station: Qmdistributedlock.zip

Solve the deadlock problem of nsdistributedlock process mutex (i)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.