PostgreSQL Source Code  git master
barrier.h File Reference
Include dependency graph for barrier.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  Barrier
 

Typedefs

typedef struct Barrier Barrier
 

Functions

void BarrierInit (Barrier *barrier, int num_workers)
 
bool BarrierArriveAndWait (Barrier *barrier, uint32 wait_event_info)
 
bool BarrierArriveAndDetach (Barrier *barrier)
 
int BarrierAttach (Barrier *barrier)
 
bool BarrierDetach (Barrier *barrier)
 
int BarrierPhase (Barrier *barrier)
 
int BarrierParticipants (Barrier *barrier)
 

Typedef Documentation

◆ Barrier

typedef struct Barrier Barrier

Function Documentation

◆ BarrierArriveAndDetach()

bool BarrierArriveAndDetach ( Barrier barrier)

Definition at line 203 of file barrier.c.

References BarrierDetachImpl().

Referenced by ExecHashTableDetachBatch().

204 {
205  return BarrierDetachImpl(barrier, true);
206 }
static bool BarrierDetachImpl(Barrier *barrier, bool arrive)
Definition: barrier.c:278

◆ BarrierArriveAndWait()

bool BarrierArriveAndWait ( Barrier barrier,
uint32  wait_event_info 
)

Definition at line 125 of file barrier.c.

References Barrier::arrived, Assert, Barrier::condition_variable, ConditionVariableBroadcast(), ConditionVariableCancelSleep(), ConditionVariablePrepareToSleep(), ConditionVariableSleep(), Barrier::elected, Barrier::mutex, Barrier::participants, Barrier::phase, SpinLockAcquire, and SpinLockRelease.

Referenced by ExecHashJoinImpl(), ExecHashTableCreate(), ExecParallelHashIncreaseNumBatches(), ExecParallelHashIncreaseNumBuckets(), ExecParallelHashJoinNewBatch(), ExecParallelHashJoinSetUpBatches(), and MultiExecParallelHash().

126 {
127  bool release = false;
128  bool elected;
129  int start_phase;
130  int next_phase;
131 
132  SpinLockAcquire(&barrier->mutex);
133  start_phase = barrier->phase;
134  next_phase = start_phase + 1;
135  ++barrier->arrived;
136  if (barrier->arrived == barrier->participants)
137  {
138  release = true;
139  barrier->arrived = 0;
140  barrier->phase = next_phase;
141  barrier->elected = next_phase;
142  }
143  SpinLockRelease(&barrier->mutex);
144 
145  /*
146  * If we were the last expected participant to arrive, we can release our
147  * peers and return true to indicate that this backend has been elected to
148  * perform any serial work.
149  */
150  if (release)
151  {
153 
154  return true;
155  }
156 
157  /*
158  * Otherwise we have to wait for the last participant to arrive and
159  * advance the phase.
160  */
161  elected = false;
163  for (;;)
164  {
165  /*
166  * We know that phase must either be start_phase, indicating that we
167  * need to keep waiting, or next_phase, indicating that the last
168  * participant that we were waiting for has either arrived or detached
169  * so that the next phase has begun. The phase cannot advance any
170  * further than that without this backend's participation, because
171  * this backend is attached.
172  */
173  SpinLockAcquire(&barrier->mutex);
174  Assert(barrier->phase == start_phase || barrier->phase == next_phase);
175  release = barrier->phase == next_phase;
176  if (release && barrier->elected != next_phase)
177  {
178  /*
179  * Usually the backend that arrives last and releases the other
180  * backends is elected to return true (see above), so that it can
181  * begin processing serial work while it has a CPU timeslice.
182  * However, if the barrier advanced because someone detached, then
183  * one of the backends that is awoken will need to be elected.
184  */
185  barrier->elected = barrier->phase;
186  elected = true;
187  }
188  SpinLockRelease(&barrier->mutex);
189  if (release)
190  break;
191  ConditionVariableSleep(&barrier->condition_variable, wait_event_info);
192  }
194 
195  return elected;
196 }
int elected
Definition: barrier.h:32
void ConditionVariableBroadcast(ConditionVariable *cv)
int participants
Definition: barrier.h:29
void ConditionVariablePrepareToSleep(ConditionVariable *cv)
int phase
Definition: barrier.h:28
#define SpinLockAcquire(lock)
Definition: spin.h:62
void ConditionVariableCancelSleep(void)
#define SpinLockRelease(lock)
Definition: spin.h:64
ConditionVariable condition_variable
Definition: barrier.h:34
void ConditionVariableSleep(ConditionVariable *cv, uint32 wait_event_info)
#define Assert(condition)
Definition: c.h:733
slock_t mutex
Definition: barrier.h:27
int arrived
Definition: barrier.h:30

◆ BarrierAttach()

int BarrierAttach ( Barrier barrier)

Definition at line 214 of file barrier.c.

References Assert, Barrier::mutex, Barrier::participants, Barrier::phase, SpinLockAcquire, SpinLockRelease, and Barrier::static_party.

Referenced by ExecHashTableCreate(), ExecParallelHashJoinNewBatch(), ExecParallelHashJoinSetUpBatches(), and MultiExecParallelHash().

215 {
216  int phase;
217 
218  Assert(!barrier->static_party);
219 
220  SpinLockAcquire(&barrier->mutex);
221  ++barrier->participants;
222  phase = barrier->phase;
223  SpinLockRelease(&barrier->mutex);
224 
225  return phase;
226 }
bool static_party
Definition: barrier.h:33
int participants
Definition: barrier.h:29
int phase
Definition: barrier.h:28
#define SpinLockAcquire(lock)
Definition: spin.h:62
#define SpinLockRelease(lock)
Definition: spin.h:64
#define Assert(condition)
Definition: c.h:733
slock_t mutex
Definition: barrier.h:27

◆ BarrierDetach()

bool BarrierDetach ( Barrier barrier)

Definition at line 234 of file barrier.c.

References BarrierDetachImpl().

Referenced by ExecHashTableDetach(), ExecParallelHashJoinNewBatch(), ExecParallelHashJoinSetUpBatches(), and MultiExecParallelHash().

235 {
236  return BarrierDetachImpl(barrier, false);
237 }
static bool BarrierDetachImpl(Barrier *barrier, bool arrive)
Definition: barrier.c:278

◆ BarrierInit()

void BarrierInit ( Barrier barrier,
int  num_workers 
)

Definition at line 100 of file barrier.c.

References Barrier::arrived, Barrier::condition_variable, ConditionVariableInit(), Barrier::elected, Barrier::mutex, Barrier::participants, Barrier::phase, SpinLockInit, and Barrier::static_party.

Referenced by ExecHashJoinInitializeDSM(), ExecHashJoinReInitializeDSM(), and ExecParallelHashJoinSetUpBatches().

101 {
102  SpinLockInit(&barrier->mutex);
103  barrier->participants = participants;
104  barrier->arrived = 0;
105  barrier->phase = 0;
106  barrier->elected = 0;
107  barrier->static_party = participants > 0;
109 }
int elected
Definition: barrier.h:32
bool static_party
Definition: barrier.h:33
#define SpinLockInit(lock)
Definition: spin.h:60
int participants
Definition: barrier.h:29
int phase
Definition: barrier.h:28
void ConditionVariableInit(ConditionVariable *cv)
ConditionVariable condition_variable
Definition: barrier.h:34
slock_t mutex
Definition: barrier.h:27
int arrived
Definition: barrier.h:30

◆ BarrierParticipants()

int BarrierParticipants ( Barrier barrier)

Definition at line 259 of file barrier.c.

References Barrier::mutex, Barrier::participants, SpinLockAcquire, and SpinLockRelease.

260 {
261  int participants;
262 
263  SpinLockAcquire(&barrier->mutex);
264  participants = barrier->participants;
265  SpinLockRelease(&barrier->mutex);
266 
267  return participants;
268 }
int participants
Definition: barrier.h:29
#define SpinLockAcquire(lock)
Definition: spin.h:62
#define SpinLockRelease(lock)
Definition: spin.h:64
slock_t mutex
Definition: barrier.h:27

◆ BarrierPhase()

int BarrierPhase ( Barrier barrier)

Definition at line 243 of file barrier.c.

References Barrier::phase.

Referenced by ExecHashJoinImpl(), ExecHashTableCreate(), ExecHashTableDetachBatch(), ExecParallelHashIncreaseNumBatches(), ExecParallelHashIncreaseNumBuckets(), ExecParallelHashJoinNewBatch(), ExecParallelHashJoinSetUpBatches(), ExecParallelHashTableInsert(), ExecParallelHashTupleAlloc(), and MultiExecParallelHash().

244 {
245  /*
246  * It is OK to read barrier->phase without locking, because it can't
247  * change without us (we are attached to it), and we executed a memory
248  * barrier when we either attached or participated in changing it last
249  * time.
250  */
251  return barrier->phase;
252 }
int phase
Definition: barrier.h:28