4848#endif
4949
5050#include "miscadmin.h"
51+ #include "portability/instr_time.h"
5152#include "postmaster/postmaster.h"
5253#include "storage/latch.h"
5354#include "storage/pmsignal.h"
@@ -176,12 +177,8 @@ DisownLatch(volatile Latch *latch)
176177 * function returns immediately.
177178 *
178179 * The 'timeout' is given in milliseconds. It must be >= 0 if WL_TIMEOUT flag
179- * is given. On some platforms, signals do not interrupt the wait, or even
180- * cause the timeout to be restarted, so beware that the function can sleep
181- * for several times longer than the requested timeout. However, this
182- * difficulty is not so great as it seems, because the signal handlers for any
183- * signals that the caller should respond to ought to be programmed to end the
184- * wait by calling SetLatch. Ideally, the timeout parameter is vestigial.
180+ * is given. Note that some extra overhead is incurred when WL_TIMEOUT is
181+ * given, so avoid using a timeout if possible.
185182 *
186183 * The latch must be owned by the current process, ie. it must be a
187184 * backend-local latch initialized with InitLatch, or a shared latch
@@ -211,13 +208,16 @@ WaitLatchOrSocket(volatile Latch *latch, int wakeEvents, pgsocket sock,
211208{
212209 int result = 0 ;
213210 int rc ;
211+ instr_time start_time ,
212+ cur_time ;
213+ long cur_timeout ;
214214
215215#ifdef HAVE_POLL
216216 struct pollfd pfds [3 ];
217217 int nfds ;
218218#else
219219 struct timeval tv ,
220- * tvp = NULL ;
220+ * tvp ;
221221 fd_set input_mask ;
222222 fd_set output_mask ;
223223 int hifd ;
@@ -234,21 +234,30 @@ WaitLatchOrSocket(volatile Latch *latch, int wakeEvents, pgsocket sock,
234234 if ((wakeEvents & WL_LATCH_SET ) && latch -> owner_pid != MyProcPid )
235235 elog (ERROR , "cannot wait on a latch owned by another process" );
236236
237- /* Initialize timeout */
237+ /*
238+ * Initialize timeout if requested. We must record the current time so
239+ * that we can determine the remaining timeout if the poll() or select()
240+ * is interrupted. (On some platforms, select() will update the contents
241+ * of "tv" for us, but unfortunately we can't rely on that.)
242+ */
238243 if (wakeEvents & WL_TIMEOUT )
239244 {
245+ INSTR_TIME_SET_CURRENT (start_time );
240246 Assert (timeout >= 0 );
247+ cur_timeout = timeout ;
248+
241249#ifndef HAVE_POLL
242- tv .tv_sec = timeout / 1000L ;
243- tv .tv_usec = (timeout % 1000L ) * 1000L ;
250+ tv .tv_sec = cur_timeout / 1000L ;
251+ tv .tv_usec = (cur_timeout % 1000L ) * 1000L ;
244252 tvp = & tv ;
245253#endif
246254 }
247255 else
248256 {
249- #ifdef HAVE_POLL
250- /* make sure poll() agrees there is no timeout */
251- timeout = -1 ;
257+ cur_timeout = -1 ;
258+
259+ #ifndef HAVE_POLL
260+ tvp = NULL ;
252261#endif
253262 }
254263
@@ -311,54 +320,62 @@ WaitLatchOrSocket(volatile Latch *latch, int wakeEvents, pgsocket sock,
311320 }
312321
313322 /* Sleep */
314- rc = poll (pfds , nfds , (int ) timeout );
323+ rc = poll (pfds , nfds , (int ) cur_timeout );
315324
316325 /* Check return code */
317326 if (rc < 0 )
318327 {
319- if (errno == EINTR )
320- continue ;
321- waiting = false;
322- ereport (ERROR ,
323- (errcode_for_socket_access (),
324- errmsg ("poll() failed: %m" )));
328+ /* EINTR is okay, otherwise complain */
329+ if (errno != EINTR )
330+ {
331+ waiting = false;
332+ ereport (ERROR ,
333+ (errcode_for_socket_access (),
334+ errmsg ("poll() failed: %m" )));
335+ }
325336 }
326- if (rc == 0 && ( wakeEvents & WL_TIMEOUT ) )
337+ else if (rc == 0 )
327338 {
328339 /* timeout exceeded */
329- result |= WL_TIMEOUT ;
340+ if (wakeEvents & WL_TIMEOUT )
341+ result |= WL_TIMEOUT ;
330342 }
331- if ((wakeEvents & WL_SOCKET_READABLE ) &&
332- (pfds [0 ].revents & (POLLIN | POLLHUP | POLLERR | POLLNVAL )))
343+ else
333344 {
334- /* data available in socket, or EOF/error condition */
335- result |= WL_SOCKET_READABLE ;
336- }
337- if ((wakeEvents & WL_SOCKET_WRITEABLE ) &&
338- (pfds [0 ].revents & POLLOUT ))
339- {
340- result |= WL_SOCKET_WRITEABLE ;
341- }
345+ /* at least one event occurred, so check revents values */
346+ if ((wakeEvents & WL_SOCKET_READABLE ) &&
347+ (pfds [0 ].revents & (POLLIN | POLLHUP | POLLERR | POLLNVAL )))
348+ {
349+ /* data available in socket, or EOF/error condition */
350+ result |= WL_SOCKET_READABLE ;
351+ }
352+ if ((wakeEvents & WL_SOCKET_WRITEABLE ) &&
353+ (pfds [0 ].revents & POLLOUT ))
354+ {
355+ result |= WL_SOCKET_WRITEABLE ;
356+ }
342357
343- /*
344- * We expect a POLLHUP when the remote end is closed, but because we
345- * don't expect the pipe to become readable or to have any errors
346- * either, treat those as postmaster death, too.
347- */
348- if ((wakeEvents & WL_POSTMASTER_DEATH ) &&
349- (pfds [nfds - 1 ].revents & (POLLHUP | POLLIN | POLLERR | POLLNVAL )))
350- {
351358 /*
352- * According to the select(2) man page on Linux, select(2) may
353- * spuriously return and report a file descriptor as readable,
354- * when it's not; and presumably so can poll(2). It's not clear
355- * that the relevant cases would ever apply to the postmaster
356- * pipe, but since the consequences of falsely returning
357- * WL_POSTMASTER_DEATH could be pretty unpleasant, we take the
358- * trouble to positively verify EOF with PostmasterIsAlive().
359+ * We expect a POLLHUP when the remote end is closed, but because
360+ * we don't expect the pipe to become readable or to have any
361+ * errors either, treat those cases as postmaster death, too.
359362 */
360- if (!PostmasterIsAlive ())
361- result |= WL_POSTMASTER_DEATH ;
363+ if ((wakeEvents & WL_POSTMASTER_DEATH ) &&
364+ (pfds [nfds - 1 ].revents & (POLLHUP | POLLIN | POLLERR | POLLNVAL )))
365+ {
366+ /*
367+ * According to the select(2) man page on Linux, select(2) may
368+ * spuriously return and report a file descriptor as readable,
369+ * when it's not; and presumably so can poll(2). It's not
370+ * clear that the relevant cases would ever apply to the
371+ * postmaster pipe, but since the consequences of falsely
372+ * returning WL_POSTMASTER_DEATH could be pretty unpleasant,
373+ * we take the trouble to positively verify EOF with
374+ * PostmasterIsAlive().
375+ */
376+ if (!PostmasterIsAlive ())
377+ result |= WL_POSTMASTER_DEATH ;
378+ }
362379 }
363380#else /* !HAVE_POLL */
364381
@@ -395,43 +412,66 @@ WaitLatchOrSocket(volatile Latch *latch, int wakeEvents, pgsocket sock,
395412 /* Check return code */
396413 if (rc < 0 )
397414 {
398- if (errno == EINTR )
399- continue ;
400- waiting = false;
401- ereport (ERROR ,
402- (errcode_for_socket_access (),
403- errmsg ("select() failed: %m" )));
415+ /* EINTR is okay, otherwise complain */
416+ if (errno != EINTR )
417+ {
418+ waiting = false;
419+ ereport (ERROR ,
420+ (errcode_for_socket_access (),
421+ errmsg ("select() failed: %m" )));
422+ }
404423 }
405- if (rc == 0 && ( wakeEvents & WL_TIMEOUT ) )
424+ else if (rc == 0 )
406425 {
407426 /* timeout exceeded */
408- result |= WL_TIMEOUT ;
409- }
410- if ((wakeEvents & WL_SOCKET_READABLE ) && FD_ISSET (sock , & input_mask ))
411- {
412- /* data available in socket, or EOF */
413- result |= WL_SOCKET_READABLE ;
427+ if (wakeEvents & WL_TIMEOUT )
428+ result |= WL_TIMEOUT ;
414429 }
415- if (( wakeEvents & WL_SOCKET_WRITEABLE ) && FD_ISSET ( sock , & output_mask ))
430+ else
416431 {
417- result |= WL_SOCKET_WRITEABLE ;
418- }
419- if ((wakeEvents & WL_POSTMASTER_DEATH ) &&
432+ /* at least one event occurred, so check masks */
433+ if ((wakeEvents & WL_SOCKET_READABLE ) && FD_ISSET (sock , & input_mask ))
434+ {
435+ /* data available in socket, or EOF */
436+ result |= WL_SOCKET_READABLE ;
437+ }
438+ if ((wakeEvents & WL_SOCKET_WRITEABLE ) && FD_ISSET (sock , & output_mask ))
439+ {
440+ result |= WL_SOCKET_WRITEABLE ;
441+ }
442+ if ((wakeEvents & WL_POSTMASTER_DEATH ) &&
420443 FD_ISSET (postmaster_alive_fds [POSTMASTER_FD_WATCH ], & input_mask ))
421- {
422- /*
423- * According to the select(2) man page on Linux, select(2) may
424- * spuriously return and report a file descriptor as readable,
425- * when it's not; and presumably so can poll(2). It's not clear
426- * that the relevant cases would ever apply to the postmaster
427- * pipe, but since the consequences of falsely returning
428- * WL_POSTMASTER_DEATH could be pretty unpleasant, we take the
429- * trouble to positively verify EOF with PostmasterIsAlive().
430- */
431- if (!PostmasterIsAlive ())
432- result |= WL_POSTMASTER_DEATH ;
444+ {
445+ /*
446+ * According to the select(2) man page on Linux, select(2) may
447+ * spuriously return and report a file descriptor as readable,
448+ * when it's not; and presumably so can poll(2). It's not
449+ * clear that the relevant cases would ever apply to the
450+ * postmaster pipe, but since the consequences of falsely
451+ * returning WL_POSTMASTER_DEATH could be pretty unpleasant,
452+ * we take the trouble to positively verify EOF with
453+ * PostmasterIsAlive().
454+ */
455+ if (!PostmasterIsAlive ())
456+ result |= WL_POSTMASTER_DEATH ;
457+ }
433458 }
434459#endif /* HAVE_POLL */
460+
461+ /* If we're not done, update cur_timeout for next iteration */
462+ if (result == 0 && cur_timeout >= 0 )
463+ {
464+ INSTR_TIME_SET_CURRENT (cur_time );
465+ INSTR_TIME_SUBTRACT (cur_time , start_time );
466+ cur_timeout = timeout - (long ) INSTR_TIME_GET_MILLISEC (cur_time );
467+ if (cur_timeout < 0 )
468+ cur_timeout = 0 ;
469+
470+ #ifndef HAVE_POLL
471+ tv .tv_sec = cur_timeout / 1000L ;
472+ tv .tv_usec = (cur_timeout % 1000L ) * 1000L ;
473+ #endif
474+ }
435475 } while (result == 0 );
436476 waiting = false;
437477
0 commit comments