PostgreSQL Source Code git master
Loading...
Searching...
No Matches
worker.c File Reference
#include "postgres.h"
#include "miscadmin.h"
#include "storage/ipc.h"
#include "storage/procarray.h"
#include "storage/shm_mq.h"
#include "storage/shm_toc.h"
#include "tcop/tcopprot.h"
#include "test_shm_mq.h"
Include dependency graph for worker.c:

Go to the source code of this file.

Functions

static void attach_to_queues (dsm_segment *seg, shm_toc *toc, int myworkernumber, shm_mq_handle **inqhp, shm_mq_handle **outqhp)
 
static void copy_messages (shm_mq_handle *inqh, shm_mq_handle *outqh)
 
void test_shm_mq_main (Datum main_arg)
 

Function Documentation

◆ attach_to_queues()

static void attach_to_queues ( dsm_segment seg,
shm_toc toc,
int  myworkernumber,
shm_mq_handle **  inqhp,
shm_mq_handle **  outqhp 
)
static

Definition at line 148 of file worker.c.

150{
151 shm_mq *inq;
152 shm_mq *outq;
153
154 inq = shm_toc_lookup(toc, myworkernumber, false);
156 *inqhp = shm_mq_attach(inq, seg, NULL);
157 outq = shm_toc_lookup(toc, myworkernumber + 1, false);
159 *outqhp = shm_mq_attach(outq, seg, NULL);
160}
static int fb(int x)
void shm_mq_set_sender(shm_mq *mq, PGPROC *proc)
Definition shm_mq.c:224
void shm_mq_set_receiver(shm_mq *mq, PGPROC *proc)
Definition shm_mq.c:206
shm_mq_handle * shm_mq_attach(shm_mq *mq, dsm_segment *seg, BackgroundWorkerHandle *handle)
Definition shm_mq.c:290
void * shm_toc_lookup(shm_toc *toc, uint64 key, bool noError)
Definition shm_toc.c:232
PGPROC * MyProc
Definition proc.c:67

References fb(), MyProc, shm_mq_attach(), shm_mq_set_receiver(), shm_mq_set_sender(), and shm_toc_lookup().

Referenced by test_shm_mq_main().

◆ copy_messages()

static void copy_messages ( shm_mq_handle inqh,
shm_mq_handle outqh 
)
static

Definition at line 170 of file worker.c.

171{
172 Size len;
173 void *data;
174 shm_mq_result res;
175
176 for (;;)
177 {
178 /* Notice any interrupts that have occurred. */
180
181 /* Receive a message. */
182 res = shm_mq_receive(inqh, &len, &data, false);
183 if (res != SHM_MQ_SUCCESS)
184 break;
185
186 /* Send it back out. */
187 res = shm_mq_send(outqh, len, data, false, true);
188 if (res != SHM_MQ_SUCCESS)
189 break;
190 }
191}
size_t Size
Definition c.h:631
#define CHECK_FOR_INTERRUPTS()
Definition miscadmin.h:123
const void size_t len
const void * data
shm_mq_result shm_mq_receive(shm_mq_handle *mqh, Size *nbytesp, void **datap, bool nowait)
Definition shm_mq.c:572
shm_mq_result shm_mq_send(shm_mq_handle *mqh, Size nbytes, const void *data, bool nowait, bool force_flush)
Definition shm_mq.c:329
shm_mq_result
Definition shm_mq.h:37
@ SHM_MQ_SUCCESS
Definition shm_mq.h:38

References CHECK_FOR_INTERRUPTS, data, fb(), len, shm_mq_receive(), shm_mq_send(), and SHM_MQ_SUCCESS.

Referenced by test_shm_mq_main().

◆ test_shm_mq_main()

void test_shm_mq_main ( Datum  main_arg)

Definition at line 47 of file worker.c.

48{
49 dsm_segment *seg;
50 shm_toc *toc;
53 volatile test_shm_mq_header *hdr;
56
57 /* Unblock signals. The standard signal handlers are OK for us. */
59
60 /*
61 * Connect to the dynamic shared memory segment.
62 *
63 * The backend that registered this worker passed us the ID of a shared
64 * memory segment to which we must attach for further instructions. Once
65 * we've mapped the segment in our address space, attach to the table of
66 * contents so we can locate the various data structures we'll need to
67 * find within the segment.
68 *
69 * Note: at this point, we have not created any ResourceOwner in this
70 * process. This will result in our DSM mapping surviving until process
71 * exit, which is fine. If there were a ResourceOwner, it would acquire
72 * ownership of the mapping, but we have no need for that.
73 */
75 if (seg == NULL)
78 errmsg("unable to map dynamic shared memory segment")));
80 if (toc == NULL)
83 errmsg("bad magic number in dynamic shared memory segment")));
84
85 /*
86 * Acquire a worker number.
87 *
88 * By convention, the process registering this background worker should
89 * have stored the control structure at key 0. We look up that key to
90 * find it. Our worker number gives our identity: there may be just one
91 * worker involved in this parallel operation, or there may be many.
92 */
93 hdr = shm_toc_lookup(toc, 0, false);
100 errmsg("too many message queue testing workers already")));
101
102 /*
103 * Attach to the appropriate message queues.
104 */
106
107 /*
108 * Indicate that we're fully initialized and ready to begin the main part
109 * of the parallel operation.
110 *
111 * Once we signal that we're ready, the user backend is entitled to assume
112 * that our on_dsm_detach callbacks will fire before we disconnect from
113 * the shared memory segment and exit. Generally, that means we must have
114 * attached to all relevant dynamic shared memory data structures by now.
115 */
116 SpinLockAcquire(&hdr->mutex);
117 ++hdr->workers_ready;
118 SpinLockRelease(&hdr->mutex);
120 if (registrant == NULL)
121 {
122 elog(DEBUG1, "registrant backend has exited prematurely");
123 proc_exit(1);
124 }
125 SetLatch(&registrant->procLatch);
126
127 /* Do the work. */
129
130 /*
131 * We're done. For cleanliness, explicitly detach from the shared memory
132 * segment (that would happen anyway during process exit, though).
133 */
134 dsm_detach(seg);
135 proc_exit(1);
136}
void BackgroundWorkerUnblockSignals(void)
Definition bgworker.c:933
void dsm_detach(dsm_segment *seg)
Definition dsm.c:803
void * dsm_segment_address(dsm_segment *seg)
Definition dsm.c:1095
dsm_segment * dsm_attach(dsm_handle h)
Definition dsm.c:665
int errcode(int sqlerrcode)
Definition elog.c:864
int errmsg(const char *fmt,...)
Definition elog.c:1081
#define DEBUG1
Definition elog.h:30
#define ERROR
Definition elog.h:39
#define elog(elevel,...)
Definition elog.h:226
#define ereport(elevel,...)
Definition elog.h:150
void proc_exit(int code)
Definition ipc.c:105
void SetLatch(Latch *latch)
Definition latch.c:290
static uint32 DatumGetUInt32(Datum X)
Definition postgres.h:232
BackgroundWorker * MyBgworkerEntry
Definition postmaster.c:200
PGPROC * BackendPidGetProc(int pid)
Definition procarray.c:3160
shm_toc * shm_toc_attach(uint64 magic, void *address)
Definition shm_toc.c:64
#define SpinLockRelease(lock)
Definition spin.h:57
#define SpinLockAcquire(lock)
Definition spin.h:55
pid_t bgw_notify_pid
Definition bgworker.h:107
Definition proc.h:176
static void attach_to_queues(dsm_segment *seg, shm_toc *toc, int myworkernumber, shm_mq_handle **inqhp, shm_mq_handle **outqhp)
Definition worker.c:148
static void copy_messages(shm_mq_handle *inqh, shm_mq_handle *outqh)
Definition worker.c:170
#define PG_TEST_SHM_MQ_MAGIC
Definition test_shm_mq.h:22

References attach_to_queues(), BackendPidGetProc(), BackgroundWorkerUnblockSignals(), BackgroundWorker::bgw_notify_pid, copy_messages(), DatumGetUInt32(), DEBUG1, dsm_attach(), dsm_detach(), dsm_segment_address(), elog, ereport, errcode(), errmsg(), ERROR, fb(), test_shm_mq_header::mutex, MyBgworkerEntry, PG_TEST_SHM_MQ_MAGIC, proc_exit(), SetLatch(), shm_toc_attach(), shm_toc_lookup(), SpinLockAcquire, SpinLockRelease, test_shm_mq_header::workers_attached, test_shm_mq_header::workers_ready, and test_shm_mq_header::workers_total.