diff options
| author | Robert Haas | 2013-12-18 17:57:20 +0000 |
|---|---|---|
| committer | Robert Haas | 2013-12-18 18:09:09 +0000 |
| commit | 001a573a2011d605f2a6e10aee9996de8581d099 (patch) | |
| tree | 5bec5fa0c42508d9d17ae324191f8c6d3ca7e05d /src/backend/storage/ipc/dsm.c | |
| parent | 613c6d26bd42dd8c2dd0664315be9551475b8864 (diff) | |
Allow on-detach callbacks for dynamic shared memory segments.
Just as backends must clean up their shared memory state (releasing
lwlocks, buffer pins, etc.) before exiting, they must also perform
any similar cleanups related to dynamic shared memory segments they
have mapped before unmapping those segments. So add a mechanism to
ensure that.
Existing on_shmem_exit hooks include both "user level" cleanup such
as transaction abort and removal of leftover temporary relations and
also "low level" cleanup that forcibly released leftover shared
memory resources. On-detach callbacks should run after the first
group but before the second group, so create a new before_shmem_exit
function for registering the early callbacks and keep on_shmem_exit
for the regular callbacks. (An earlier draft of this patch added an
additional argument to on_shmem_exit, but that had a much larger
footprint and probably a substantially higher risk of breaking third
party code for no real gain.)
Patch by me, reviewed by KaiGai Kohei and Andres Freund.
Diffstat (limited to 'src/backend/storage/ipc/dsm.c')
| -rw-r--r-- | src/backend/storage/ipc/dsm.c | 78 |
1 files changed, 72 insertions, 6 deletions
diff --git a/src/backend/storage/ipc/dsm.c b/src/backend/storage/ipc/dsm.c index a33700e7918..19eb87858be 100644 --- a/src/backend/storage/ipc/dsm.c +++ b/src/backend/storage/ipc/dsm.c @@ -58,6 +58,14 @@ #define INVALID_CONTROL_SLOT ((uint32) -1) +/* Backend-local tracking for on-detach callbacks. */ +typedef struct dsm_segment_detach_callback +{ + on_dsm_detach_callback function; + Datum arg; + slist_node node; +} dsm_segment_detach_callback; + /* Backend-local state for a dynamic shared memory segment. */ struct dsm_segment { @@ -68,6 +76,7 @@ struct dsm_segment void *impl_private; /* Implementation-specific private data. */ void *mapped_address; /* Mapping address, or NULL if unmapped. */ Size mapped_size; /* Size of our mapping. */ + slist_head on_detach; /* On-detach callbacks. */ }; /* Shared-memory state for a dynamic shared memory segment. */ @@ -91,7 +100,6 @@ static void dsm_cleanup_for_mmap(void); static bool dsm_read_state_file(dsm_handle *h); static void dsm_write_state_file(dsm_handle h); static void dsm_postmaster_shutdown(int code, Datum arg); -static void dsm_backend_shutdown(int code, Datum arg); static dsm_segment *dsm_create_descriptor(void); static bool dsm_control_segment_sane(dsm_control_header *control, Size mapped_size); @@ -556,9 +564,6 @@ dsm_backend_startup(void) } #endif - /* Arrange to detach segments on exit. */ - on_shmem_exit(dsm_backend_shutdown, 0); - dsm_init_done = true; } @@ -718,8 +723,8 @@ dsm_attach(dsm_handle h) /* * At backend shutdown time, detach any segments that are still attached. */ -static void -dsm_backend_shutdown(int code, Datum arg) +void +dsm_backend_shutdown(void) { while (!dlist_is_empty(&dsm_segment_list)) { @@ -775,6 +780,27 @@ void dsm_detach(dsm_segment *seg) { /* + * Invoke registered callbacks. Just in case one of those callbacks + * throws a further error that brings us back here, pop the callback + * before invoking it, to avoid infinite error recursion. + */ + while (!slist_is_empty(&seg->on_detach)) + { + slist_node *node; + dsm_segment_detach_callback *cb; + on_dsm_detach_callback function; + Datum arg; + + node = slist_pop_head_node(&seg->on_detach); + cb = slist_container(dsm_segment_detach_callback, node, node); + function = cb->function; + arg = cb->arg; + pfree(cb); + + function(seg, arg); + } + + /* * Try to remove the mapping, if one exists. Normally, there will be, * but maybe not, if we failed partway through a create or attach * operation. We remove the mapping before decrementing the reference @@ -916,6 +942,44 @@ dsm_segment_handle(dsm_segment *seg) } /* + * Register an on-detach callback for a dynamic shared memory segment. + */ +void +on_dsm_detach(dsm_segment *seg, on_dsm_detach_callback function, Datum arg) +{ + dsm_segment_detach_callback *cb; + + cb = MemoryContextAlloc(TopMemoryContext, + sizeof(dsm_segment_detach_callback)); + cb->function = function; + cb->arg = arg; + slist_push_head(&seg->on_detach, &cb->node); +} + +/* + * Unregister an on-detach callback for a dynamic shared memory segment. + */ +void +cancel_on_dsm_detach(dsm_segment *seg, on_dsm_detach_callback function, + Datum arg) +{ + slist_mutable_iter iter; + + slist_foreach_modify(iter, &seg->on_detach) + { + dsm_segment_detach_callback *cb; + + cb = slist_container(dsm_segment_detach_callback, node, iter.cur); + if (cb->function == function && cb->arg == arg) + { + slist_delete_current(&iter); + pfree(cb); + break; + } + } +} + +/* * Create a segment descriptor. */ static dsm_segment * @@ -937,6 +1001,8 @@ dsm_create_descriptor(void) seg->resowner = CurrentResourceOwner; ResourceOwnerRememberDSM(CurrentResourceOwner, seg); + slist_init(&seg->on_detach); + return seg; } |
