PostgreSQL Source Code  git master
pthread_barrier_wait.c File Reference
#include "c.h"
#include "port/pg_pthread.h"
Include dependency graph for pthread_barrier_wait.c:

Go to the source code of this file.

Functions

int pthread_barrier_init (pthread_barrier_t *barrier, const void *attr, int count)
 
int pthread_barrier_wait (pthread_barrier_t *barrier)
 
int pthread_barrier_destroy (pthread_barrier_t *barrier)
 

Function Documentation

◆ pthread_barrier_destroy()

int pthread_barrier_destroy ( pthread_barrier_t barrier)

Definition at line 72 of file pthread_barrier_wait.c.

References pg_pthread_barrier::cond, and pg_pthread_barrier::mutex.

73 {
74  pthread_cond_destroy(&barrier->cond);
75  pthread_mutex_destroy(&barrier->mutex);
76  return 0;
77 }
pthread_mutex_t mutex
Definition: pg_pthread.h:29
pthread_cond_t cond
Definition: pg_pthread.h:30

◆ pthread_barrier_init()

int pthread_barrier_init ( pthread_barrier_t barrier,
const void *  attr,
int  count 
)

Definition at line 19 of file pthread_barrier_wait.c.

References pg_pthread_barrier::arrived, pg_pthread_barrier::cond, pg_pthread_barrier::count, error(), pg_pthread_barrier::mutex, pthread_mutex_init(), and pg_pthread_barrier::sense.

20 {
21  int error;
22 
23  barrier->sense = false;
24  barrier->count = count;
25  barrier->arrived = 0;
26  if ((error = pthread_cond_init(&barrier->cond, NULL)) != 0)
27  return error;
28  if ((error = pthread_mutex_init(&barrier->mutex, NULL)) != 0)
29  {
30  pthread_cond_destroy(&barrier->cond);
31  return error;
32  }
33 
34  return 0;
35 }
static void error(void)
Definition: sql-dyntest.c:147
int pthread_mutex_init(pthread_mutex_t *mp, void *attr)
Definition: pthread-win32.c:35
pthread_mutex_t mutex
Definition: pg_pthread.h:29
pthread_cond_t cond
Definition: pg_pthread.h:30

◆ pthread_barrier_wait()

int pthread_barrier_wait ( pthread_barrier_t barrier)

Definition at line 38 of file pthread_barrier_wait.c.

References pg_pthread_barrier::arrived, Assert, pg_pthread_barrier::cond, pg_pthread_barrier::count, pg_pthread_barrier::mutex, PTHREAD_BARRIER_SERIAL_THREAD, pthread_mutex_lock(), pthread_mutex_unlock(), and pg_pthread_barrier::sense.

39 {
40  bool initial_sense;
41 
42  pthread_mutex_lock(&barrier->mutex);
43 
44  /* We have arrived at the barrier. */
45  barrier->arrived++;
46  Assert(barrier->arrived <= barrier->count);
47 
48  /* If we were the last to arrive, release the others and return. */
49  if (barrier->arrived == barrier->count)
50  {
51  barrier->arrived = 0;
52  barrier->sense = !barrier->sense;
53  pthread_mutex_unlock(&barrier->mutex);
54  pthread_cond_broadcast(&barrier->cond);
55 
57  }
58 
59  /* Wait for someone else to flip the sense. */
60  initial_sense = barrier->sense;
61  do
62  {
63  pthread_cond_wait(&barrier->cond, &barrier->mutex);
64  } while (barrier->sense == initial_sense);
65 
66  pthread_mutex_unlock(&barrier->mutex);
67 
68  return 0;
69 }
int pthread_mutex_lock(pthread_mutex_t *mp)
Definition: pthread-win32.c:45
pthread_mutex_t mutex
Definition: pg_pthread.h:29
pthread_cond_t cond
Definition: pg_pthread.h:30
int pthread_mutex_unlock(pthread_mutex_t *mp)
Definition: pthread-win32.c:54
#define Assert(condition)
Definition: c.h:804
#define PTHREAD_BARRIER_SERIAL_THREAD
Definition: pg_pthread.h:21