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 74 of file pthread_barrier_wait.c.

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

75 {
76  pthread_cond_destroy(&barrier->cond);
77  pthread_mutex_destroy(&barrier->mutex);
78  return 0;
79 }
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, pg_pthread_barrier::mutex, pthread_mutex_init(), and pg_pthread_barrier::sense.

20 {
21  barrier->sense = false;
22  barrier->count = count;
23  barrier->arrived = 0;
24  if (pthread_cond_init(&barrier->cond, NULL) < 0)
25  return -1;
26  if (pthread_mutex_init(&barrier->mutex, NULL) < 0)
27  {
28  int save_errno = errno;
29 
30  pthread_cond_destroy(&barrier->cond);
31  errno = save_errno;
32 
33  return -1;
34  }
35 
36  return 0;
37 }
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 40 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.

41 {
42  bool initial_sense;
43 
44  pthread_mutex_lock(&barrier->mutex);
45 
46  /* We have arrived at the barrier. */
47  barrier->arrived++;
48  Assert(barrier->arrived <= barrier->count);
49 
50  /* If we were the last to arrive, release the others and return. */
51  if (barrier->arrived == barrier->count)
52  {
53  barrier->arrived = 0;
54  barrier->sense = !barrier->sense;
55  pthread_mutex_unlock(&barrier->mutex);
56  pthread_cond_broadcast(&barrier->cond);
57 
59  }
60 
61  /* Wait for someone else to flip the sense. */
62  initial_sense = barrier->sense;
63  do
64  {
65  pthread_cond_wait(&barrier->cond, &barrier->mutex);
66  } while (barrier->sense == initial_sense);
67 
68  pthread_mutex_unlock(&barrier->mutex);
69 
70  return 0;
71 }
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