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

Commit236b6bc

Browse files
committed
Simplify Windows implementation of latches. There's no need to keep a
dynamic pool of event handles, we can permanently assign one for eachshared latch. Thanks to that, we no longer need a separate shared memoryblock for latches, and we don't need to know in advance how many sharedlatches there is, so you no longer need to remember to updateNumSharedLatches when you introduce a new latch to the system.
1 parent1eab7a5 commit236b6bc

File tree

4 files changed

+39
-153
lines changed

4 files changed

+39
-153
lines changed

‎src/backend/port/unix_latch.c

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@
7777
* Portions Copyright (c) 1994, Regents of the University of California
7878
*
7979
* IDENTIFICATION
80-
* $PostgreSQL: pgsql/src/backend/port/unix_latch.c,v 1.3 2010/09/13 18:01:20 heikki Exp $
80+
* $PostgreSQL: pgsql/src/backend/port/unix_latch.c,v 1.4 2010/09/15 10:06:21 heikki Exp $
8181
*
8282
*-------------------------------------------------------------------------
8383
*/
@@ -129,8 +129,10 @@ InitLatch(volatile Latch *latch)
129129
* is initially owned by no-one, use OwnLatch to associate it with the
130130
* current process.
131131
*
132-
* NB: When you introduce a new shared latch, you must increase the shared
133-
* latch count in NumSharedLatches in win32_latch.c!
132+
* InitSharedLatch needs to be called in postmaster before forking child
133+
* processes, usually right after allocating the shared memory block
134+
* containing the latch with ShmemInitStruct. The Unix implementation
135+
* doesn't actually require that, but the Windows one does.
134136
*/
135137
void
136138
InitSharedLatch(volatileLatch*latch)
@@ -322,29 +324,6 @@ ResetLatch(volatile Latch *latch)
322324
latch->is_set= false;
323325
}
324326

325-
/*
326-
* LatchShmemSize
327-
*Compute space needed for latch's shared memory
328-
*
329-
* Not needed for Unix implementation.
330-
*/
331-
Size
332-
LatchShmemSize(void)
333-
{
334-
return0;
335-
}
336-
337-
/*
338-
* LatchShmemInit
339-
*Allocate and initialize shared memory needed for latches
340-
*
341-
* Not needed for Unix implementation.
342-
*/
343-
void
344-
LatchShmemInit(void)
345-
{
346-
}
347-
348327
/*
349328
* SetLatch uses SIGUSR1 to wake up the process waiting on the latch. Wake
350329
* up WaitLatch.

‎src/backend/port/win32_latch.c

Lines changed: 31 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@
33
* win32_latch.c
44
* Windows implementation of latches.
55
*
6-
* The Windows implementation uses Windows events. See unix_latch.c for
7-
* information on usage.
6+
* See unix_latch.c for information on usage.
7+
*
8+
* The Windows implementation uses Windows events that are inherited by
9+
* all postmaster child processes.
810
*
911
* Portions Copyright (c) 1996-2010, PostgreSQL Global Development Group
1012
* Portions Copyright (c) 1994, Regents of the University of California
1113
*
1214
* IDENTIFICATION
13-
* $PostgreSQL: pgsql/src/backend/port/win32_latch.c,v 1.1 2010/09/11 15:48:04 heikki Exp $
15+
* $PostgreSQL: pgsql/src/backend/port/win32_latch.c,v 1.2 2010/09/15 10:06:21 heikki Exp $
1416
*
1517
*-------------------------------------------------------------------------
1618
*/
@@ -24,83 +26,60 @@
2426
#include"replication/walsender.h"
2527
#include"storage/latch.h"
2628
#include"storage/shmem.h"
27-
#include"storage/spin.h"
28-
29-
/*
30-
* Shared latches are implemented with Windows events that are shared by
31-
* all postmaster child processes. At postmaster startup we create enough
32-
* Event objects, and mark them as inheritable so that they are accessible
33-
* in child processes. The handles are stored in sharedHandles.
34-
*/
35-
typedefstruct
36-
{
37-
slock_tmutex;/* protects all the other fields */
3829

39-
intmaxhandles;/* number of shared handles created */
40-
intnfreehandles;/* number of free handles in array */
41-
HANDLEhandles[1];/* free handles, variable length */
42-
}SharedEventHandles;
43-
44-
staticSharedEventHandles*sharedHandles;
4530

4631
void
4732
InitLatch(volatileLatch*latch)
4833
{
49-
latch->event=CreateEvent(NULL, TRUE, FALSE,NULL);
50-
latch->is_shared= false;
5134
latch->is_set= false;
35+
latch->owner_pid=MyProcPid;
36+
latch->is_shared= false;
37+
38+
latch->event=CreateEvent(NULL, TRUE, FALSE,NULL);
39+
if (latch->event==NULL)
40+
elog(ERROR,"CreateEvent failed: error code %d", (int)GetLastError());
5241
}
5342

5443
void
5544
InitSharedLatch(volatileLatch*latch)
5645
{
57-
latch->is_shared= true;
46+
SECURITY_ATTRIBUTESsa;
47+
5848
latch->is_set= false;
59-
latch->event=NULL;
49+
latch->owner_pid=0;
50+
latch->is_shared= true;
51+
52+
/*
53+
* Set up security attributes to specify that the events are
54+
* inherited.
55+
*/
56+
ZeroMemory(&sa,sizeof(sa));
57+
sa.nLength=sizeof(sa);
58+
sa.bInheritHandle= TRUE;
59+
60+
latch->event=CreateEvent(&sa, TRUE, FALSE,NULL);
61+
if (latch->event==NULL)
62+
elog(ERROR,"CreateEvent failed: error code %d", (int)GetLastError());
6063
}
6164

6265
void
6366
OwnLatch(volatileLatch*latch)
6467
{
65-
HANDLEevent;
66-
6768
/* Sanity checks */
6869
Assert(latch->is_shared);
69-
if (latch->event!=0)
70+
if (latch->owner_pid!=0)
7071
elog(ERROR,"latch already owned");
7172

72-
/* Reserve an event handle from the shared handles array */
73-
SpinLockAcquire(&sharedHandles->mutex);
74-
if (sharedHandles->nfreehandles <=0)
75-
{
76-
SpinLockRelease(&sharedHandles->mutex);
77-
elog(ERROR,"out of shared event objects");
78-
}
79-
sharedHandles->nfreehandles--;
80-
event=sharedHandles->handles[sharedHandles->nfreehandles];
81-
SpinLockRelease(&sharedHandles->mutex);
82-
83-
latch->event=event;
73+
latch->owner_pid=MyProcPid;
8474
}
8575

8676
void
8777
DisownLatch(volatileLatch*latch)
8878
{
8979
Assert(latch->is_shared);
90-
Assert(latch->event!=NULL);
91-
92-
/* Put the event handle back to the pool */
93-
SpinLockAcquire(&sharedHandles->mutex);
94-
if (sharedHandles->nfreehandles >=sharedHandles->maxhandles)
95-
{
96-
SpinLockRelease(&sharedHandles->mutex);
97-
elog(PANIC,"too many free event handles");
98-
}
99-
sharedHandles->handles[sharedHandles->nfreehandles]=latch->event;
100-
sharedHandles->nfreehandles++;
101-
SpinLockRelease(&sharedHandles->mutex);
80+
Assert(latch->owner_pid==MyProcPid);
10281

103-
latch->event=NULL;
82+
latch->owner_pid=0;
10483
}
10584

10685
bool
@@ -217,68 +196,3 @@ ResetLatch(volatile Latch *latch)
217196
{
218197
latch->is_set= false;
219198
}
220-
221-
/*
222-
* Number of shared latches, used to allocate the right number of shared
223-
* Event handles at postmaster startup. You must update this if you
224-
* introduce a new shared latch!
225-
*/
226-
staticint
227-
NumSharedLatches(void)
228-
{
229-
intnumLatches=0;
230-
231-
/* Each walsender needs one latch */
232-
numLatches+=max_wal_senders;
233-
234-
returnnumLatches;
235-
}
236-
237-
/*
238-
* LatchShmemSize
239-
*Compute space needed for latch's shared memory
240-
*/
241-
Size
242-
LatchShmemSize(void)
243-
{
244-
return offsetof(SharedEventHandles,handles)+
245-
NumSharedLatches()*sizeof(HANDLE);
246-
}
247-
248-
/*
249-
* LatchShmemInit
250-
*Allocate and initialize shared memory needed for latches
251-
*/
252-
void
253-
LatchShmemInit(void)
254-
{
255-
Sizesize=LatchShmemSize();
256-
boolfound;
257-
258-
sharedHandles=ShmemInitStruct("SharedEventHandles",size,&found);
259-
260-
/* If we're first, initialize the struct and allocate handles */
261-
if (!found)
262-
{
263-
inti;
264-
SECURITY_ATTRIBUTESsa;
265-
266-
/*
267-
* Set up security attributes to specify that the events are
268-
* inherited.
269-
*/
270-
ZeroMemory(&sa,sizeof(sa));
271-
sa.nLength=sizeof(sa);
272-
sa.bInheritHandle= TRUE;
273-
274-
SpinLockInit(&sharedHandles->mutex);
275-
sharedHandles->maxhandles=NumSharedLatches();
276-
sharedHandles->nfreehandles=sharedHandles->maxhandles;
277-
for (i=0;i<sharedHandles->maxhandles;i++)
278-
{
279-
sharedHandles->handles[i]=CreateEvent(&sa, TRUE, FALSE,NULL);
280-
if (sharedHandles->handles[i]==NULL)
281-
elog(ERROR,"CreateEvent failed: error code %d", (int)GetLastError());
282-
}
283-
}
284-
}

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/storage/ipc/ipci.c,v 1.105 2010/09/11 15:48:04 heikki Exp $
11+
* $PostgreSQL: pgsql/src/backend/storage/ipc/ipci.c,v 1.106 2010/09/15 10:06:21 heikki Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -30,7 +30,6 @@
3030
#include"replication/walsender.h"
3131
#include"storage/bufmgr.h"
3232
#include"storage/ipc.h"
33-
#include"storage/latch.h"
3433
#include"storage/pg_shmem.h"
3534
#include"storage/pmsignal.h"
3635
#include"storage/procarray.h"
@@ -118,7 +117,6 @@ CreateSharedMemoryAndSemaphores(bool makePrivate, int port)
118117
size=add_size(size,SInvalShmemSize());
119118
size=add_size(size,PMSignalShmemSize());
120119
size=add_size(size,ProcSignalShmemSize());
121-
size=add_size(size,LatchShmemSize());
122120
size=add_size(size,BgWriterShmemSize());
123121
size=add_size(size,AutoVacuumShmemSize());
124122
size=add_size(size,WalSndShmemSize());
@@ -219,7 +217,6 @@ CreateSharedMemoryAndSemaphores(bool makePrivate, int port)
219217
*/
220218
PMSignalShmemInit();
221219
ProcSignalShmemInit();
222-
LatchShmemInit();
223220
BgWriterShmemInit();
224221
AutoVacuumShmemInit();
225222
WalSndShmemInit();

‎src/include/storage/latch.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2010, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $PostgreSQL: pgsql/src/include/storage/latch.h,v 1.1 2010/09/11 15:48:04 heikki Exp $
10+
* $PostgreSQL: pgsql/src/include/storage/latch.h,v 1.2 2010/09/15 10:06:21 heikki Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -25,9 +25,8 @@ typedef struct
2525
{
2626
sig_atomic_tis_set;
2727
boolis_shared;
28-
#ifndefWIN32
2928
intowner_pid;
30-
#else
29+
#ifdefWIN32
3130
HANDLEevent;
3231
#endif
3332
}Latch;
@@ -46,9 +45,6 @@ extern void SetLatch(volatile Latch *latch);
4645
externvoidResetLatch(volatileLatch*latch);
4746
#defineTestLatch(latch) (((volatile Latch *) latch)->is_set)
4847

49-
externSizeLatchShmemSize(void);
50-
externvoidLatchShmemInit(void);
51-
5248
/*
5349
* Unix implementation uses SIGUSR1 for inter-process signaling, Win32 doesn't
5450
* need this.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp