*** xfunc.sgml 2006-10-31 12:52:01.000000000 -0800 --- xfunc.sgml.new 2006-10-31 12:51:51.000000000 -0800 *************** *** 2909,2912 **** --- 2909,2960 ---- + + Shared Memory and LWLocks in C-Language Functions + + + Add-ins may reserve LWLocks and an allocation of shared memory on server + startup. The add-in's shared library must be preloaded, by specifying + it in + shared-preload-libraries, + and the shared memory must be reserved by calling: + + void RequestAddinShmemSpace(int size) + + from your _PG_init function. + + + LWLocks are reserved by calling: + + void RequestAddinLWLocks(int n) + + from _PG_init. + + + To avoid possible race-conditions, each backend should use the LWLock + AddinShmemInitLock when connecting to and intializing + its allocation of shared memory, as shown here: + + + static mystruct *ptr = NULL; + + if (!ptr) + { + bool found; + + LWLockAcquire(AddinShmemInitLock, LW_EXCLUSIVE); + ptr = ShmemInitStruct("my struct name", size, &found); + if (!ptr) + elog(ERROR, "out of shared memory"); + if (!found) + { + initialize contents of shmem area; + acquire any requested LWLocks using: + ptr->mylockid = LWLockAssign(); + } + LWLockRelease(AddinShmemInitLock); + } + + +