On Mon, Aug 10, 2009 at 19:33, Magnus Hagander<mag...@hagander.net> wrote:
> On Mon, Aug 10, 2009 at 16:58, Tom Lane<t...@sss.pgh.pa.us> wrote:
>> Magnus Hagander <mag...@hagander.net> writes:
>>> On Mon, Aug 10, 2009 at 16:10, Tom Lane<t...@sss.pgh.pa.us> wrote:
>>>> 8.2 as well, no?
>>
>>> 8.2 has a different shmem implementation - the one that emulates sysv
>>> shmem. The patch will need to be changed around for that, and I
>>> haven't looked at that. It may be worthwhile to do that, but it's a
>>> separate patch, so let's get it out in 8.3 and 8.4 first.
>>
>> If it's at all hard to do, I could see deprecating 8.2 for Windows
>> instead.
>
> I haven't looked at how much work it would be at all yet. So let's do
> that before we decide to deprecate anything. As mentioned downthread,
> 8.2 is a very widespread release, and we really want to avoid
> deprecating it.

Here's an attempt at a backport to 8.2. I haven't examined it  in
detail, but it passes "make check" on mingw.

Comments?

-- 
 Magnus Hagander
 Me: http://www.hagander.net/
 Work: http://www.redpill-linpro.com/
*** a/src/backend/port/sysv_shmem.c
--- b/src/backend/port/sysv_shmem.c
***************
*** 49,54 **** typedef int IpcMemoryId;		/* shared memory ID returned by shmget(2) */
--- 49,57 ----
  
  unsigned long UsedShmemSegID = 0;
  void	   *UsedShmemSegAddr = NULL;
+ #ifdef WIN32
+ Size		UsedShmemSegSize = 0;
+ #endif
  
  static void *InternalIpcMemoryCreate(IpcMemoryKey memKey, Size size);
  static void IpcMemoryDetach(int status, Datum shmaddr);
***************
*** 403,408 **** PGSharedMemoryCreate(Size size, bool makePrivate, int port)
--- 406,412 ----
  
  	/* Save info for possible future use */
  	UsedShmemSegAddr = memAddress;
+ 	UsedShmemSegSize = size;
  	UsedShmemSegID = (unsigned long) NextShmemSegID;
  
  	return hdr;
*** a/src/backend/port/win32/shmem.c
--- b/src/backend/port/win32/shmem.c
***************
*** 12,19 ****
--- 12,22 ----
   */
  
  #include "postgres.h"
+ #include "miscadmin.h"
  
  static DWORD s_segsize = 0;
+ extern void *UsedShmemSegAddr;
+ extern Size UsedShmemSegSize;
  
  /* Detach from a shared mem area based on its address */
  int
***************
*** 29,34 **** shmdt(const void *shmaddr)
--- 32,44 ----
  void *
  shmat(int memId, void *shmaddr, int flag)
  {
+ 	/* Release the memory region reserved in the postmaster */
+ 	if (IsUnderPostmaster)
+ 	{
+ 		if (VirtualFree(shmaddr, 0, MEM_RELEASE) == 0)
+ 			elog(FATAL, "failed to release reserved memory region (addr=%p): %lu",
+ 				 shmaddr, GetLastError());
+ 	}
  	/* TODO -- shmat needs to count # attached to shared mem */
  	void	   *lpmem = MapViewOfFileEx((HANDLE) memId,
  										FILE_MAP_WRITE | FILE_MAP_READ,
***************
*** 128,130 **** shmget(int memKey, int size, int flag)
--- 138,190 ----
  
  	return (int) hmap;
  }
+ 
+ /*
+  * pgwin32_ReserveSharedMemoryRegion(hChild)
+  *
+  * Reserve the memory region that will be used for shared memory in a child
+  * process. It is called before the child process starts, to make sure the
+  * memory is available.
+  *
+  * Once the child starts, DLLs loading in different order or threads getting
+  * scheduled differently may allocate memory which can conflict with the
+  * address space we need for our shared memory. By reserving the shared
+  * memory region before the child starts, and freeing it only just before we
+  * attempt to get access to the shared memory forces these allocations to
+  * be given different address ranges that don't conflict.
+  *
+  * NOTE! This function executes in the postmaster, and should for this
+  * reason not use elog(FATAL) since that would take down the postmaster.
+  */
+ int
+ pgwin32_ReserveSharedMemoryRegion(HANDLE hChild)
+ {
+ 	void *address;
+ 
+ 	Assert(UsedShmemSegAddr != NULL);
+ 	Assert(UsedShmemSegSize != 0);
+ 
+ 	address = VirtualAllocEx(hChild, UsedShmemSegAddr, UsedShmemSegSize,
+ 								MEM_RESERVE, PAGE_READWRITE);
+ 	if (address == NULL) {
+ 		/* Don't use FATAL since we're running in the postmaster */
+ 		elog(LOG, "could not reserve shared memory region (addr=%p) for child %lu: %lu",
+ 			 UsedShmemSegAddr, hChild, GetLastError());
+ 		return false;
+ 	}
+ 	if (address != UsedShmemSegAddr)
+ 	{
+ 		/*
+ 		 * Should never happen - in theory if allocation granularity causes strange
+ 		 * effects it could, so check just in case.
+ 		 *
+ 		 * Don't use FATAL since we're running in the postmaster.
+ 		 */
+ 	    elog(LOG, "reserved shared memory region got incorrect address %p, expected %p",
+ 			 address, UsedShmemSegAddr);
+ 		VirtualFreeEx(hChild, address, 0, MEM_RELEASE);
+ 		return false;
+ 	}
+ 
+ 	return true;
+ }
*** a/src/backend/postmaster/postmaster.c
--- b/src/backend/postmaster/postmaster.c
***************
*** 3184,3190 **** internal_forkexec(int argc, char *argv[], Port *port)
  		return -1;				/* log made by save_backend_variables */
  	}
  
! 	/* Drop the shared memory that is now inherited to the backend */
  	if (!UnmapViewOfFile(param))
  		elog(LOG, "could not unmap view of backend parameter file: error code %d",
  			 (int) GetLastError());
--- 3184,3190 ----
  		return -1;				/* log made by save_backend_variables */
  	}
  
! 	/* Drop the parameter shared memory that is now inherited to the backend */
  	if (!UnmapViewOfFile(param))
  		elog(LOG, "could not unmap view of backend parameter file: error code %d",
  			 (int) GetLastError());
***************
*** 3193,3198 **** internal_forkexec(int argc, char *argv[], Port *port)
--- 3193,3217 ----
  			 (int) GetLastError());
  
  	/*
+ 	 * Reserve the memory region used by our main shared memory segment before we
+ 	 * resume the child process.
+ 	 */
+ 	if (!pgwin32_ReserveSharedMemoryRegion(pi.hProcess))
+ 	{
+ 		/*
+ 		 * Failed to reserve the memory, so terminate the newly created
+ 		 * process and give up.
+ 		 */
+ 		if (!TerminateProcess(pi.hProcess, 255))
+ 			ereport(ERROR,
+ 					(errmsg_internal("could not terminate process that failed to reserve memory: error code %d",
+ 									 (int) GetLastError())));
+ 		CloseHandle(pi.hProcess);
+ 		CloseHandle(pi.hThread);
+ 		return -1;			/* logging done made by pgwin32_ReserveSharedMemoryRegion() */
+ 	}
+ 
+ 	/*
  	 * Now that the backend variables are written out, we start the child
  	 * thread so it can start initializing while we set up the rest of the
  	 * parent state.
*** a/src/include/port/win32.h
--- b/src/include/port/win32.h
***************
*** 263,268 **** extern int	pgwin32_is_admin(void);
--- 263,271 ----
  extern int	pgwin32_is_service(void);
  #endif
  
+ /* in backend/port/win32/shmem.c */
+ extern int	pgwin32_ReserveSharedMemoryRegion(HANDLE);
+ 
  /* in port/win32error.c */
  extern void _dosmaperr(unsigned long);
  
-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

Reply via email to