Skip to content

Commit 35166fd

Browse files
committed
Fix --disable-spinlocks in 9.2 and 9.3 branches.
My back-patch of the 9.4-era commit 44cd47c into 9.2 and 9.3 fixed HPPA builds as expected, but it broke --disable-spinlocks builds, because the dummy spinlock is initialized before the underlying semaphore infrastructure is alive. In 9.4 and up this works because of commit daa7527, which decoupled initialization of an slock_t variable from access to the actual system semaphore object. The best solution seems to be to back-port that patch, which should be a net win anyway because it improves the usability of --disable-spinlocks builds in the older branches; and it's been out long enough now to not be worrisome from a stability perspective.
1 parent 0a32768 commit 35166fd

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
VariableCache ShmemVariableCache;
502502
Backend *ShmemBackendArray;
503+
#ifndef HAVE_SPINLOCKS
504+
PGSemaphore SpinlockSemaArray;
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+
#ifndef HAVE_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+
#ifndef HAVE_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+
#ifndef HAVE_SPINLOCKS
124+
{
125+
PGSemaphore spinsemas;
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+
#ifndef HAVE_SPINLOCKS
33+
PGSemaphore SpinlockSemaArray;
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+
return SpinlockSemas() * sizeof(PGSemaphoreData);
44+
}
45+
3146
#ifdef HAVE_SPINLOCKS
3247

3348
/*
@@ -51,21 +66,20 @@ SpinlockSemas(void)
5166
int
5267
SpinlockSemas(void)
5368
{
54-
int nsemas;
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-
return nsemas;
69+
return NUM_SPINLOCK_SEMAPHORES;
70+
}
71+
72+
/*
73+
* Initialize semaphores.
74+
*/
75+
extern void
76+
SpinlockSemaInit(PGSemaphore spinsemas)
77+
{
78+
int i;
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(volatile slock_t *lock)
7791
{
78-
PGSemaphoreCreate((PGSemaphore) lock);
92+
static int counter = 0;
93+
94+
*lock = (++counter) % NUM_SPINLOCK_SEMAPHORES;
7995
}
8096

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

87103
bool
@@ -96,7 +112,7 @@ int
96112
tas_sema(volatile slock_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
#define NUM_USER_DEFINED_LWLOCKS 4
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+
#define NUM_SPINLOCK_SEMAPHORES 1024
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
#ifndef S_LOCK_H
9595
#define S_LOCK_H
9696

97-
#include "storage/pg_sema.h"
98-
9997
#ifdef HAVE_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-
typedef PGSemaphoreData slock_t;
1032+
typedef int slock_t;
10361033

10371034
extern bool s_lock_free_sema(volatile slock_t *lock);
10381035
extern void s_unlock_sema(volatile slock_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
#define SPIN_H
5858

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

6164

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

7073

7174
extern int SpinlockSemas(void);
75+
extern Size SpinlockSemaSize(void);
76+
77+
#ifndef HAVE_SPINLOCKS
78+
extern void SpinlockSemaInit(PGSemaphore);
79+
extern PGSemaphore SpinlockSemaArray;
80+
#endif
7281

7382
#endif /* SPIN_H */

0 commit comments

Comments
 (0)