git-2.22.0/lockfile.c
/*
 * Try locking path, retrying with quadratic backoff for at least
 * timeout_ms milliseconds. If timeout_ms is 0, try locking the file
 * exactly once. If timeout_ms is -1, try indefinitely.
 */
Try locking path, retrying with quadratic backoff for at least
timeout_ms milliseconds. If timeout_ms is 0, try locking the file
exactly once. If timeout_ms is -1, try indefinitely.
Try locking path retrying with quadratic backoff for at least timeout_ms milliseconds . If timeout_ms is 0 try locking the file exactly once . If timeout_ms is - 1 try indefinitely .
static int lock_file_timeout(struct lock_file *lk, const char *path,
			     int flags, long timeout_ms)
{
	int n = 1;
	int multiplier = 1;
	long remaining_ms = 0;
	static int random_initialized = 0;
	if (timeout_ms == 0)
		return lock_file(lk, path, flags);
	if (!random_initialized) {
		srand((unsigned int)getpid());
		random_initialized = 1;
	}
	if (timeout_ms > 0)
		remaining_ms = timeout_ms;
	while (1) {
		long backoff_ms, wait_ms;
		int fd;
		fd = lock_file(lk, path, flags);
		if (fd >= 0)
			return fd; 
		else if (errno != EEXIST)
			return -1; 
		else if (timeout_ms > 0 && remaining_ms <= 0)
			return -1; 
		backoff_ms = multiplier * INITIAL_BACKOFF_MS;
		wait_ms = (750 + rand() % 500) * backoff_ms / 1000;
		sleep_millisec(wait_ms);
		remaining_ms -= wait_ms;
		multiplier += 2*n + 1;
		if (multiplier > BACKOFF_MAX_MULTIPLIER)
			multiplier = BACKOFF_MAX_MULTIPLIER;
		else
			n++;
	}
}
static int lock_file_timeout ( struct lock_file * lk , const char * path , int flags , long timeout_ms ) { int n = 1 ; int multiplier = 1 ; long remaining_ms = 0 ; static int random_initialized = 0 ; if ( timeout_ms == 0 ) return lock_file ( lk , path , flags ) ; if ( ! random_initialized ) { srand ( ( unsigned int ) getpid ( ) ) ; random_initialized = 1 ; } if ( timeout_ms > 0 ) remaining_ms = timeout_ms ; while ( 1 ) { long backoff_ms , wait_ms ; int fd ; fd = lock_file ( lk , path , flags ) ; if ( fd >= 0 ) return fd ; else if ( errno != EEXIST ) return -1 ; else if ( timeout_ms > 0 && remaining_ms <= 0 ) return -1 ; backoff_ms = multiplier * INITIAL_BACKOFF_MS ; wait_ms = ( 750 + rand ( ) % 500 ) * backoff_ms / 1000 ; sleep_millisec ( wait_ms ) ; remaining_ms -= wait_ms ; multiplier += 2 * n + 1 ; if ( multiplier > BACKOFF_MAX_MULTIPLIER ) multiplier = BACKOFF_MAX_MULTIPLIER ; else n ++ ; } }
Try locking path, retrying with quadratic backoff for at least
 timeout_ms milliseconds.
Try locking path retrying with quadratic backoff for at least timeout_ms milliseconds .
/* success */ /* failure other than lock held */ /* failure due to timeout */ /* back off for between 0.75*backoff_ms and 1.25*backoff_ms */ /* Recursion: (n+1)^2 = n^2 + 2n + 1 */