Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit12968cf

Browse files
committed
Add flags argument to dsm_create.
Right now, there's only one flag, DSM_CREATE_NULL_IF_MAXSEGMENTS,which suppresses the error that would normally be thrown when themaximum number of segments already exists, instead returning NULL.It might be useful to add more flags in the future, such as one toignore allocation errors, but I haven't done that here.
1 parent5f286c0 commit12968cf

File tree

3 files changed

+33
-4
lines changed

3 files changed

+33
-4
lines changed

‎src/backend/storage/ipc/dsm.c

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -454,9 +454,9 @@ dsm_set_control_handle(dsm_handle h)
454454
* Create a new dynamic shared memory segment.
455455
*/
456456
dsm_segment*
457-
dsm_create(Sizesize)
457+
dsm_create(Sizesize,intflags)
458458
{
459-
dsm_segment*seg=dsm_create_descriptor();
459+
dsm_segment*seg;
460460
uint32i;
461461
uint32nitems;
462462

@@ -466,6 +466,21 @@ dsm_create(Size size)
466466
if (!dsm_init_done)
467467
dsm_backend_startup();
468468

469+
/*
470+
* If we've been instructed to return NULL when it's not possible to
471+
* register another segment, check whether we seem to be at the limit.
472+
* This allows us to avoid the overhead of creating a new segment only to
473+
* immediately destroy it again. Since we don't take the lock here, the
474+
* value we read might be slightly stale, but the remote possibility of
475+
* an unnecessary failure here shouldn't trouble anyone too much.
476+
*/
477+
if ((flags&DSM_CREATE_NULL_IF_MAXSEGMENTS)!=0
478+
&&dsm_control->nitems >=dsm_control->maxitems)
479+
returnNULL;
480+
481+
/* Create a new segment descriptor. */
482+
seg=dsm_create_descriptor();
483+
469484
/* Loop until we find an unused segment identifier. */
470485
for (;;)
471486
{
@@ -496,9 +511,21 @@ dsm_create(Size size)
496511

497512
/* Verify that we can support an additional mapping. */
498513
if (nitems >=dsm_control->maxitems)
514+
{
515+
if ((flags&DSM_CREATE_NULL_IF_MAXSEGMENTS)!=0)
516+
{
517+
dsm_impl_op(DSM_OP_DESTROY,seg->handle,0,&seg->impl_private,
518+
&seg->mapped_address,&seg->mapped_size,WARNING);
519+
if (seg->resowner!=NULL)
520+
ResourceOwnerForgetDSM(seg->resowner,seg);
521+
dlist_delete(&seg->node);
522+
pfree(seg);
523+
returnNULL;
524+
}
499525
ereport(ERROR,
500526
(errcode(ERRCODE_INSUFFICIENT_RESOURCES),
501527
errmsg("too many dynamic shared memory segments")));
528+
}
502529

503530
/* Enter the handle into a new array slot. */
504531
dsm_control->item[nitems].handle=seg->handle;

‎src/include/storage/dsm.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
typedefstructdsm_segmentdsm_segment;
1919

20+
#defineDSM_CREATE_NULL_IF_MAXSEGMENTS0x0001
21+
2022
/* Startup and shutdown functions. */
2123
structPGShmemHeader;/* avoid including pg_shmem.h */
2224
externvoiddsm_cleanup_using_control_segment(dsm_handleold_control_handle);
@@ -29,7 +31,7 @@ extern void dsm_set_control_handle(dsm_handle h);
2931
#endif
3032

3133
/* Functions that create, update, or remove mappings. */
32-
externdsm_segment*dsm_create(Sizesize);
34+
externdsm_segment*dsm_create(Sizesize,intflags);
3335
externdsm_segment*dsm_attach(dsm_handleh);
3436
externvoid*dsm_resize(dsm_segment*seg,Sizesize);
3537
externvoid*dsm_remap(dsm_segment*seg);

‎src/test/modules/test_shm_mq/setup.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ setup_dynamic_shared_memory(int64 queue_size, int nworkers,
125125
segsize=shm_toc_estimate(&e);
126126

127127
/* Create the shared memory segment and establish a table of contents. */
128-
seg=dsm_create(shm_toc_estimate(&e));
128+
seg=dsm_create(shm_toc_estimate(&e),0);
129129
toc=shm_toc_create(PG_TEST_SHM_MQ_MAGIC,dsm_segment_address(seg),
130130
segsize);
131131

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp