1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-30 11:03:19 +03:00

Split WaitEventSet functions to separate source file

latch.c now only contains the Latch related functions, which build on
the WaitEventSet abstraction. Most of the platform-dependent stuff is
now in waiteventset.c.

Reviewed-by: Andres Freund <andres@anarazel.de>
Discussion: https://www.postgresql.org/message-id/8a507fb6-df28-49d3-81a5-ede180d7f0fb@iki.fi
This commit is contained in:
Heikki Linnakangas
2025-03-06 01:26:16 +02:00
parent 84e5b2f07a
commit 393e0d2314
9 changed files with 2154 additions and 2060 deletions

View File

@ -42,7 +42,7 @@ pqinitmask(void)
{
sigemptyset(&UnBlockSig);
/* Note: InitializeLatchSupport() modifies UnBlockSig. */
/* Note: InitializeWaitEventSupport() modifies UnBlockSig. */
/* First set all signals, then clear some. */
sigfillset(&BlockSig);

View File

@ -548,7 +548,7 @@ PostmasterMain(int argc, char *argv[])
pqsignal(SIGCHLD, handle_pm_child_exit_signal);
/* This may configure SIGURG, depending on platform. */
InitializeLatchSupport();
InitializeWaitEventSupport();
InitProcessLocalLatch();
/*

View File

@ -25,6 +25,7 @@ OBJS = \
signalfuncs.o \
sinval.o \
sinvaladt.o \
standby.o
standby.o \
waiteventset.o
include $(top_srcdir)/src/backend/common.mk

File diff suppressed because it is too large Load Diff

View File

@ -18,5 +18,6 @@ backend_sources += files(
'sinval.c',
'sinvaladt.c',
'standby.c',
'waiteventset.c',
)

File diff suppressed because it is too large Load Diff

View File

@ -127,7 +127,7 @@ InitPostmasterChild(void)
#endif
/* Initialize process-local latch support */
InitializeLatchSupport();
InitializeWaitEventSupport();
InitProcessLocalLatch();
InitializeLatchWaitSet();
@ -188,7 +188,7 @@ InitStandaloneProcess(const char *argv0)
InitProcessGlobals();
/* Initialize process-local latch support */
InitializeLatchSupport();
InitializeWaitEventSupport();
InitProcessLocalLatch();
InitializeLatchWaitSet();

View File

@ -84,10 +84,11 @@
* use of any generic handler.
*
*
* WaitEventSets allow to wait for latches being set and additional events -
* postmaster dying and socket readiness of several sockets currently - at the
* same time. On many platforms using a long lived event set is more
* efficient than using WaitLatch or WaitLatchOrSocket.
* See also WaitEventSets in waiteventset.h. They allow to wait for latches
* being set and additional events - postmaster dying and socket readiness of
* several sockets currently - at the same time. On many platforms using a
* long lived event set is more efficient than using WaitLatch or
* WaitLatchOrSocket.
*
*
* Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
@ -102,7 +103,7 @@
#include <signal.h>
#include "utils/resowner.h"
#include "storage/waiteventset.h" /* for WL_* arguments to WaitLatch */
/*
* Latch structure should be treated as opaque and only accessed through
@ -120,53 +121,9 @@ typedef struct Latch
#endif
} Latch;
/*
* Bitmasks for events that may wake-up WaitLatch(), WaitLatchOrSocket(), or
* WaitEventSetWait().
*/
#define WL_LATCH_SET (1 << 0)
#define WL_SOCKET_READABLE (1 << 1)
#define WL_SOCKET_WRITEABLE (1 << 2)
#define WL_TIMEOUT (1 << 3) /* not for WaitEventSetWait() */
#define WL_POSTMASTER_DEATH (1 << 4)
#define WL_EXIT_ON_PM_DEATH (1 << 5)
#ifdef WIN32
#define WL_SOCKET_CONNECTED (1 << 6)
#else
/* avoid having to deal with case on platforms not requiring it */
#define WL_SOCKET_CONNECTED WL_SOCKET_WRITEABLE
#endif
#define WL_SOCKET_CLOSED (1 << 7)
#ifdef WIN32
#define WL_SOCKET_ACCEPT (1 << 8)
#else
/* avoid having to deal with case on platforms not requiring it */
#define WL_SOCKET_ACCEPT WL_SOCKET_READABLE
#endif
#define WL_SOCKET_MASK (WL_SOCKET_READABLE | \
WL_SOCKET_WRITEABLE | \
WL_SOCKET_CONNECTED | \
WL_SOCKET_ACCEPT | \
WL_SOCKET_CLOSED)
typedef struct WaitEvent
{
int pos; /* position in the event data structure */
uint32 events; /* triggered events */
pgsocket fd; /* socket fd associated with event */
void *user_data; /* pointer provided in AddWaitEventToSet */
#ifdef WIN32
bool reset; /* Is reset of the event required? */
#endif
} WaitEvent;
/* forward declaration to avoid exposing latch.c implementation details */
typedef struct WaitEventSet WaitEventSet;
/*
* prototypes for functions in latch.c
*/
extern void InitializeLatchSupport(void);
extern void InitLatch(Latch *latch);
extern void InitSharedLatch(Latch *latch);
extern void OwnLatch(Latch *latch);
@ -174,22 +131,10 @@ extern void DisownLatch(Latch *latch);
extern void SetLatch(Latch *latch);
extern void ResetLatch(Latch *latch);
extern WaitEventSet *CreateWaitEventSet(ResourceOwner resowner, int nevents);
extern void FreeWaitEventSet(WaitEventSet *set);
extern void FreeWaitEventSetAfterFork(WaitEventSet *set);
extern int AddWaitEventToSet(WaitEventSet *set, uint32 events, pgsocket fd,
Latch *latch, void *user_data);
extern void ModifyWaitEvent(WaitEventSet *set, int pos, uint32 events, Latch *latch);
extern int WaitEventSetWait(WaitEventSet *set, long timeout,
WaitEvent *occurred_events, int nevents,
uint32 wait_event_info);
extern int WaitLatch(Latch *latch, int wakeEvents, long timeout,
uint32 wait_event_info);
extern int WaitLatchOrSocket(Latch *latch, int wakeEvents,
pgsocket sock, long timeout, uint32 wait_event_info);
extern void InitializeLatchWaitSet(void);
extern int GetNumRegisteredWaitEvents(WaitEventSet *set);
extern bool WaitEventSetCanReportClosed(void);
#endif /* LATCH_H */

View File

@ -0,0 +1,97 @@
/*-------------------------------------------------------------------------
*
* waiteventset.h
* ppoll() / pselect() like interface for waiting for events
*
* WaitEventSets allow to wait for latches being set and additional events -
* postmaster dying and socket readiness of several sockets currently - at the
* same time. On many platforms using a long lived event set is more
* efficient than using WaitLatch or WaitLatchOrSocket.
*
* WaitEventSetWait includes a provision for timeouts (which should be avoided
* when possible, as they incur extra overhead) and a provision for postmaster
* child processes to wake up immediately on postmaster death. See
* storage/ipc/waiteventset.c for detailed specifications for the exported
* functions.
*
*
* Portions Copyright (c) 1996-2024, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
* src/include/storage/waiteventset.h
*
*-------------------------------------------------------------------------
*/
#ifndef WAITEVENTSET_H
#define WAITEVENTSET_H
#include "utils/resowner.h"
/*
* Bitmasks for events that may wake-up WaitLatch(), WaitLatchOrSocket(), or
* WaitEventSetWait().
*/
#define WL_LATCH_SET (1 << 0)
#define WL_SOCKET_READABLE (1 << 1)
#define WL_SOCKET_WRITEABLE (1 << 2)
#define WL_TIMEOUT (1 << 3) /* not for WaitEventSetWait() */
#define WL_POSTMASTER_DEATH (1 << 4)
#define WL_EXIT_ON_PM_DEATH (1 << 5)
#ifdef WIN32
#define WL_SOCKET_CONNECTED (1 << 6)
#else
/* avoid having to deal with case on platforms not requiring it */
#define WL_SOCKET_CONNECTED WL_SOCKET_WRITEABLE
#endif
#define WL_SOCKET_CLOSED (1 << 7)
#ifdef WIN32
#define WL_SOCKET_ACCEPT (1 << 8)
#else
/* avoid having to deal with case on platforms not requiring it */
#define WL_SOCKET_ACCEPT WL_SOCKET_READABLE
#endif
#define WL_SOCKET_MASK (WL_SOCKET_READABLE | \
WL_SOCKET_WRITEABLE | \
WL_SOCKET_CONNECTED | \
WL_SOCKET_ACCEPT | \
WL_SOCKET_CLOSED)
typedef struct WaitEvent
{
int pos; /* position in the event data structure */
uint32 events; /* triggered events */
pgsocket fd; /* socket fd associated with event */
void *user_data; /* pointer provided in AddWaitEventToSet */
#ifdef WIN32
bool reset; /* Is reset of the event required? */
#endif
} WaitEvent;
/* forward declarations to avoid exposing waiteventset.c implementation details */
typedef struct WaitEventSet WaitEventSet;
typedef struct Latch Latch;
/*
* prototypes for functions in waiteventset.c
*/
extern void InitializeWaitEventSupport(void);
extern WaitEventSet *CreateWaitEventSet(ResourceOwner resowner, int nevents);
extern void FreeWaitEventSet(WaitEventSet *set);
extern void FreeWaitEventSetAfterFork(WaitEventSet *set);
extern int AddWaitEventToSet(WaitEventSet *set, uint32 events, pgsocket fd,
Latch *latch, void *user_data);
extern void ModifyWaitEvent(WaitEventSet *set, int pos, uint32 events, Latch *latch);
extern int WaitEventSetWait(WaitEventSet *set, long timeout,
WaitEvent *occurred_events, int nevents,
uint32 wait_event_info);
extern int GetNumRegisteredWaitEvents(WaitEventSet *set);
extern bool WaitEventSetCanReportClosed(void);
#ifndef WIN32
extern void WakeupMyProc(void);
extern void WakeupOtherProc(int pid);
#endif
#endif /* WAITEVENTSET_H */