ipci.c 3.49 KB
Newer Older
1 2 3
/*-------------------------------------------------------------------------
 *
 * ipci.c--
4
 *	  POSTGRES inter-process communication initialization code.
5 6 7 8 9
 *
 * Copyright (c) 1994, Regents of the University of California
 *
 *
 * IDENTIFICATION
10
 *	  $Header: /cvsroot/pgsql/src/backend/storage/ipc/ipci.c,v 1.12 1998/06/25 14:24:34 momjian Exp $
11 12 13
 *
 *-------------------------------------------------------------------------
 */
Bruce Momjian's avatar
Bruce Momjian committed
14 15 16
#include <string.h>
#include <sys/types.h>

Marc G. Fournier's avatar
Marc G. Fournier committed
17
#include "postgres.h"
18 19 20 21 22 23 24 25

#include "storage/ipc.h"
#include "storage/multilev.h"
#include "storage/sinval.h"
#include "storage/bufmgr.h"
#include "storage/proc.h"
#include "storage/smgr.h"
#include "storage/lock.h"
26
#include "miscadmin.h"			/* for DebugLvl */
27 28 29

/*
 * SystemPortAddressCreateMemoryKey --
30
 *		Returns a memory key given a port address.
31 32 33 34
 */
IPCKey
SystemPortAddressCreateIPCKey(SystemPortAddress address)
{
35 36 37
	Assert(address < 32768);	/* XXX */

	return (SystemPortAddressGetIPCKey(address));
38 39 40 41
}

/*
 * CreateSharedMemoryAndSemaphores --
42
 *		Creates and initializes shared memory and semaphores.
43 44
 */
/**************************************************
45

46 47 48
  CreateSharedMemoryAndSemaphores
  is called exactly *ONCE* by the postmaster.
  It is *NEVER* called by the postgres backend
49

50 51 52 53
  0) destroy any existing semaphores for both buffer
  and lock managers.
  1) create the appropriate *SHARED* memory segments
  for the two resource managers.
54

55 56 57 58 59
  **************************************************/

void
CreateSharedMemoryAndSemaphores(IPCKey key)
{
60
	int			size;
61

62
#ifdef HAS_TEST_AND_SET
63 64 65 66 67
	/* ---------------
	 *	create shared memory for slocks
	 * --------------
	 */
	CreateAndInitSLockMemory(IPCKeyGetSLockSharedMemoryKey(key));
68
#endif
69 70 71 72 73 74 75
	/* ----------------
	 *	kill and create the buffer manager buffer pool (and semaphore)
	 * ----------------
	 */
	CreateSpinlocks(IPCKeyGetSpinLockSemaphoreKey(key));
	size = BufferShmemSize() + LockShmemSize();

76
#ifdef STABLE_MEMORY_STORAGE
77
	size += MMShmemSize();
78
#endif
79 80 81 82 83 84 85

	if (DebugLvl > 1)
	{
		fprintf(stderr, "binding ShmemCreate(key=%x, size=%d)\n",
				IPCKeyGetBufferMemoryKey(key), size);
	}
	ShmemCreate(IPCKeyGetBufferMemoryKey(key), size);
86
	ShmemBindingTableReset();
87 88 89 90 91 92 93 94
	InitShmem(key, size);
	InitBufferPool(key);

	/* ----------------
	 *	do the lock table stuff
	 * ----------------
	 */
	InitLocks();
95
	InitMultiLevelLockm();
96 97 98 99 100 101 102 103
	if (InitMultiLevelLockm() == INVALID_TABLEID)
		elog(FATAL, "Couldn't create the lock table");

	/* ----------------
	 *	do process table stuff
	 * ----------------
	 */
	InitProcGlobal(key);
104
	on_exitpg(ProcFreeAllSemaphores, NULL);
105 106

	CreateSharedInvalidationState(key);
107 108 109 110 111
}


/*
 * AttachSharedMemoryAndSemaphores --
112
 *		Attachs existant shared memory and semaphores.
113 114 115 116
 */
void
AttachSharedMemoryAndSemaphores(IPCKey key)
{
117
	int			size;
118 119 120 121 122 123 124 125 126 127 128

	/* ----------------
	 *	create rather than attach if using private key
	 * ----------------
	 */
	if (key == PrivateIPCKey)
	{
		CreateSharedMemoryAndSemaphores(key);
		return;
	}

129
#ifdef HAS_TEST_AND_SET
130 131 132 133 134
	/* ----------------
	 *	attach the slock shared memory
	 * ----------------
	 */
	AttachSLockMemory(IPCKeyGetSLockSharedMemoryKey(key));
135
#endif
136 137 138 139 140 141 142 143 144 145 146 147 148
	/* ----------------
	 *	attach the buffer manager buffer pool (and semaphore)
	 * ----------------
	 */
	size = BufferShmemSize() + LockShmemSize();
	InitShmem(key, size);
	InitBufferPool(key);

	/* ----------------
	 *	initialize lock table stuff
	 * ----------------
	 */
	InitLocks();
149
	if (InitMultiLevelLockm() == INVALID_TABLEID)
150 151 152
		elog(FATAL, "Couldn't attach to the lock table");

	AttachSharedInvalidationState(key);
153
}