#define TEMP_FAILURE_RETRY(expression) \
(__extension__\
({ long int __result;\
do __result = (long int)(expression);\
while(__result == -1L&& errno == EINTR);\
__result;})\
#endif
关于使用TEMP_FAILURE_RETRY
This macro evaluates expression once, and examines its value as type long int
. If the value equals -1
, that indicates a failure and errno
should be set to show what kind of failure. If it fails and reports error code EINTR
,TEMP_FAILURE_RETRY
evaluates it again, and over and over until the result is not a temporary failure.
从代码里看到,当while为真时,会不断执行表达式,例如,我们可以在我们的套接字里不断读取数据
int ret = TEMP_FAILURE_RETRY(::recv(sock,buffer,size,MSG_NOSIGNAL));
这样程序得一直读到数据返回,
使用MSG_NOSIGNAL
linux下当连接断开,还发数据的时候,会向系统发送一个异常消息,如果不作处理,系统会出 BrokePipe,程序会退出。为此,函数的最后一个参数可以设MSG_NOSIGNAL,禁止向系统发送异常消息。
使用errno
表达式中函数作为不可重入函数,执行过程中,被信号中断,包含下面两种情况
Interruption of System Calls and Library Functions by Signal Handlers
-
If a signal handler is invoked while a system call or library function call is blocked, then either:
the call is automatically restarted after the signal handler returns; or the call fails with the error EINTR.
GCC uses the __extension__ attribute when using the -ansi flag to avoid
warnings in headers with GCC extensions. This is mostly used in glibc
with function declartions using long long.