Commit cc8d9a29 authored by Edward Thomson's avatar Edward Thomson

win32: introduce `do_with_retries` macro

Provide a macro that will allow us to run a function with posix-like
return values multiple times in a retry loop, with an optional cleanup
function called between invocations.
parent dcaa9099
......@@ -53,6 +53,7 @@ typedef enum {
GIT_PASSTHROUGH = -30, /**< Internal only */
GIT_ITEROVER = -31, /**< Signals end of iteration with iterator */
GIT_RETRY = -32, /**< Internal only */
} git_error_code;
/**
......
......@@ -157,6 +157,27 @@ GIT_INLINE(void) set_errno(void)
}
}
GIT_INLINE(bool) last_error_retryable(void)
{
int os_error = GetLastError();
return (os_error == ERROR_SHARING_VIOLATION ||
os_error == ERROR_ACCESS_DENIED);
}
#define do_with_retries(fn, cleanup) \
do { \
int __tries, __ret; \
for (__tries = 0; __tries < 10; __tries++) { \
if (__tries && (__ret = (cleanup)) != 0) \
return __ret; \
if ((__ret = (fn)) != GIT_RETRY) \
return __ret; \
Sleep(5); \
} \
return -1; \
} while (0) \
/**
* Truncate or extend file.
*
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment