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

Commit35166fd

Browse files
committed
Fix --disable-spinlocks in 9.2 and 9.3 branches.
My back-patch of the 9.4-era commit44cd47c into 9.2 and 9.3 fixedHPPA builds as expected, but it broke --disable-spinlocks builds, becausethe dummy spinlock is initialized before the underlying semaphoreinfrastructure is alive. In 9.4 and up this works because of commitdaa7527, which decoupled initialization of an slock_t variablefrom access to the actual system semaphore object. The best solutionseems to be to back-port that patch, which should be a net win anywaybecause it improves the usability of --disable-spinlocks builds in theolder branches; and it's been out long enough now to not be worrisomefrom a stability perspective.
1 parent0a32768 commit35166fd

File tree

7 files changed

+80
-25
lines changed

7 files changed

+80
-25
lines changed

‎src/backend/postmaster/postmaster.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,9 @@ typedef struct
500500
slock_t*ShmemLock;
501501
VariableCacheShmemVariableCache;
502502
Backend*ShmemBackendArray;
503+
#ifndefHAVE_SPINLOCKS
504+
PGSemaphoreSpinlockSemaArray;
505+
#endif
503506
LWLock*LWLockArray;
504507
slock_t*ProcStructLock;
505508
PROC_HDR*ProcGlobal;
@@ -6050,6 +6053,9 @@ save_backend_variables(BackendParameters *param, Port *port,
60506053
param->ShmemVariableCache=ShmemVariableCache;
60516054
param->ShmemBackendArray=ShmemBackendArray;
60526055

6056+
#ifndefHAVE_SPINLOCKS
6057+
param->SpinlockSemaArray=SpinlockSemaArray;
6058+
#endif
60536059
param->LWLockArray=LWLockArray;
60546060
param->ProcStructLock=ProcStructLock;
60556061
param->ProcGlobal=ProcGlobal;
@@ -6278,6 +6284,9 @@ restore_backend_variables(BackendParameters *param, Port *port)
62786284
ShmemVariableCache=param->ShmemVariableCache;
62796285
ShmemBackendArray=param->ShmemBackendArray;
62806286

6287+
#ifndefHAVE_SPINLOCKS
6288+
SpinlockSemaArray=param->SpinlockSemaArray;
6289+
#endif
62816290
LWLockArray=param->LWLockArray;
62826291
ProcStructLock=param->ProcStructLock;
62836292
ProcGlobal=param->ProcGlobal;

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ CreateSharedMemoryAndSemaphores(bool makePrivate, int port)
103103
* need to be so careful during the actual allocation phase.
104104
*/
105105
size=100000;
106+
size=add_size(size,SpinlockSemaSize());
106107
size=add_size(size,hash_estimate_size(SHMEM_INDEX_SIZE,
107108
sizeof(ShmemIndexEnt)));
108109
size=add_size(size,BufferShmemSize());

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

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,24 @@ InitShmemAllocation(void)
116116
Assert(shmhdr!=NULL);
117117

118118
/*
119-
* Initialize the spinlock used by ShmemAlloc. We have to do the space
120-
* allocation the hard way, since obviously ShmemAlloc can't be called
121-
* yet.
119+
* If spinlocks are disabled, initialize emulation layer. We have to do
120+
* the space allocation the hard way, since obviously ShmemAlloc can't be
121+
* called yet.
122+
*/
123+
#ifndefHAVE_SPINLOCKS
124+
{
125+
PGSemaphorespinsemas;
126+
127+
spinsemas= (PGSemaphore) (((char*)shmhdr)+shmhdr->freeoffset);
128+
shmhdr->freeoffset+=MAXALIGN(SpinlockSemaSize());
129+
SpinlockSemaInit(spinsemas);
130+
Assert(shmhdr->freeoffset <=shmhdr->totalsize);
131+
}
132+
#endif
133+
134+
/*
135+
* Initialize the spinlock used by ShmemAlloc; we have to do this the hard
136+
* way, too, for the same reasons as above.
122137
*/
123138
ShmemLock= (slock_t*) (((char*)shmhdr)+shmhdr->freeoffset);
124139
shmhdr->freeoffset+=MAXALIGN(sizeof(slock_t));

‎src/backend/storage/lmgr/spin.c

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,24 @@
2525
#include"miscadmin.h"
2626
#include"replication/walsender.h"
2727
#include"storage/lwlock.h"
28+
#include"storage/pg_sema.h"
2829
#include"storage/spin.h"
2930

3031

32+
#ifndefHAVE_SPINLOCKS
33+
PGSemaphoreSpinlockSemaArray;
34+
#endif
35+
36+
/*
37+
* Report the amount of shared memory needed to store semaphores for spinlock
38+
* support.
39+
*/
40+
Size
41+
SpinlockSemaSize(void)
42+
{
43+
returnSpinlockSemas()*sizeof(PGSemaphoreData);
44+
}
45+
3146
#ifdefHAVE_SPINLOCKS
3247

3348
/*
@@ -51,21 +66,20 @@ SpinlockSemas(void)
5166
int
5267
SpinlockSemas(void)
5368
{
54-
intnsemas;
55-
56-
/*
57-
* It would be cleaner to distribute this logic into the affected modules,
58-
* similar to the way shmem space estimation is handled.
59-
*
60-
* For now, though, there are few enough users of spinlocks that we just
61-
* keep the knowledge here.
62-
*/
63-
nsemas=NumLWLocks();/* one for each lwlock */
64-
nsemas+=NBuffers;/* one for each buffer header */
65-
nsemas+=max_wal_senders;/* one for each wal sender process */
66-
nsemas+=30;/* plus a bunch for other small-scale use */
67-
68-
returnnsemas;
69+
returnNUM_SPINLOCK_SEMAPHORES;
70+
}
71+
72+
/*
73+
* Initialize semaphores.
74+
*/
75+
externvoid
76+
SpinlockSemaInit(PGSemaphorespinsemas)
77+
{
78+
inti;
79+
80+
for (i=0;i<NUM_SPINLOCK_SEMAPHORES;++i)
81+
PGSemaphoreCreate(&spinsemas[i]);
82+
SpinlockSemaArray=spinsemas;
6983
}
7084

7185
/*
@@ -75,13 +89,15 @@ SpinlockSemas(void)
7589
void
7690
s_init_lock_sema(volatileslock_t*lock)
7791
{
78-
PGSemaphoreCreate((PGSemaphore)lock);
92+
staticintcounter=0;
93+
94+
*lock= (++counter) %NUM_SPINLOCK_SEMAPHORES;
7995
}
8096

8197
void
8298
s_unlock_sema(volatileslock_t*lock)
8399
{
84-
PGSemaphoreUnlock((PGSemaphore)lock);
100+
PGSemaphoreUnlock(&SpinlockSemaArray[*lock]);
85101
}
86102

87103
bool
@@ -96,7 +112,7 @@ int
96112
tas_sema(volatileslock_t*lock)
97113
{
98114
/* Note that TAS macros return 0 if *success* */
99-
return !PGSemaphoreTryLock((PGSemaphore)lock);
115+
return !PGSemaphoreTryLock(&SpinlockSemaArray[*lock]);
100116
}
101117

102118
#endif/* !HAVE_SPINLOCKS */

‎src/include/pg_config_manual.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@
5656
*/
5757
#defineNUM_USER_DEFINED_LWLOCKS4
5858

59+
/*
60+
* When we don't have native spinlocks, we use semaphores to simulate them.
61+
* Decreasing this value reduces consumption of OS resources; increasing it
62+
* may improve performance, but supplying a real spinlock implementation is
63+
* probably far better.
64+
*/
65+
#defineNUM_SPINLOCK_SEMAPHORES1024
66+
5967
/*
6068
* Define this if you want to allow the lo_import and lo_export SQL
6169
* functions to be executed by ordinary users. By default these

‎src/include/storage/s_lock.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,8 @@
9494
#ifndefS_LOCK_H
9595
#defineS_LOCK_H
9696

97-
#include"storage/pg_sema.h"
98-
9997
#ifdefHAVE_SPINLOCKS/* skip spinlocks if requested */
10098

101-
10299
#if defined(__GNUC__)|| defined(__INTEL_COMPILER)
103100
/*************************************************************************
104101
* All the gcc inlines
@@ -1032,7 +1029,7 @@ spin_delay(void)
10321029
* to fall foul of kernel limits on number of semaphores, so don't use this
10331030
* unless you must! The subroutines appear in spin.c.
10341031
*/
1035-
typedefPGSemaphoreDataslock_t;
1032+
typedefintslock_t;
10361033

10371034
externbools_lock_free_sema(volatileslock_t*lock);
10381035
externvoids_unlock_sema(volatileslock_t*lock);

‎src/include/storage/spin.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@
5757
#defineSPIN_H
5858

5959
#include"storage/s_lock.h"
60+
#ifndefHAVE_SPINLOCKS
61+
#include"storage/pg_sema.h"
62+
#endif
6063

6164

6265
#defineSpinLockInit(lock)S_INIT_LOCK(lock)
@@ -69,5 +72,11 @@
6972

7073

7174
externintSpinlockSemas(void);
75+
externSizeSpinlockSemaSize(void);
76+
77+
#ifndefHAVE_SPINLOCKS
78+
externvoidSpinlockSemaInit(PGSemaphore);
79+
externPGSemaphoreSpinlockSemaArray;
80+
#endif
7281

7382
#endif/* SPIN_H */

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp