PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
win32_sema.c File Reference
#include "postgres.h"
#include "miscadmin.h"
#include "storage/ipc.h"
#include "storage/pg_sema.h"
Include dependency graph for win32_sema.c:

Go to the source code of this file.

Functions

static void ReleaseSemaphores (int code, Datum arg)
 
Size PGSemaphoreShmemSize (int maxSemas)
 
void PGReserveSemaphores (int maxSemas, int port)
 
PGSemaphore PGSemaphoreCreate (void)
 
void PGSemaphoreReset (PGSemaphore sema)
 
void PGSemaphoreLock (PGSemaphore sema)
 
void PGSemaphoreUnlock (PGSemaphore sema)
 
bool PGSemaphoreTryLock (PGSemaphore sema)
 

Variables

static HANDLE * mySemSet
 
static int numSems
 
static int maxSems
 

Function Documentation

void PGReserveSemaphores ( int  maxSemas,
int  port 
)

Definition at line 47 of file win32_sema.c.

References elog, malloc, maxSems, mySemSet, nextSemKey, NULL, numSems, on_shmem_exit(), PANIC, PGSemaphoreShmemSize(), ReleaseSemaphores(), and ShmemAllocUnlocked().

48 {
49  mySemSet = (HANDLE *) malloc(maxSemas * sizeof(HANDLE));
50  if (mySemSet == NULL)
51  elog(PANIC, "out of memory");
52  numSems = 0;
53  maxSems = maxSemas;
54 
56 }
static int numSems
Definition: win32_sema.c:21
#define PANIC
Definition: elog.h:53
static int maxSems
Definition: win32_sema.c:22
#define malloc(a)
Definition: header.h:50
void on_shmem_exit(pg_on_exit_callback function, Datum arg)
Definition: ipc.c:348
static HANDLE * mySemSet
Definition: win32_sema.c:20
static void ReleaseSemaphores(int code, Datum arg)
Definition: win32_sema.c:64
#define NULL
Definition: c.h:229
#define elog
Definition: elog.h:219
PGSemaphore PGSemaphoreCreate ( void  )

Definition at line 79 of file win32_sema.c.

References Assert, elog, ereport, errmsg(), IsUnderPostmaster, maxSems, mySemSet, NULL, numSems, PANIC, PG_SEM_REF, PosixSemaphoreCreate(), and TRUE.

80 {
81  HANDLE cur_handle;
82  SECURITY_ATTRIBUTES sec_attrs;
83 
84  /* Can't do this in a backend, because static state is postmaster's */
86 
87  if (numSems >= maxSems)
88  elog(PANIC, "too many semaphores created");
89 
90  ZeroMemory(&sec_attrs, sizeof(sec_attrs));
91  sec_attrs.nLength = sizeof(sec_attrs);
92  sec_attrs.lpSecurityDescriptor = NULL;
93  sec_attrs.bInheritHandle = TRUE;
94 
95  /* We don't need a named semaphore */
96  cur_handle = CreateSemaphore(&sec_attrs, 1, 32767, NULL);
97  if (cur_handle)
98  {
99  /* Successfully done */
100  mySemSet[numSems++] = cur_handle;
101  }
102  else
103  ereport(PANIC,
104  (errmsg("could not create semaphore: error code %lu",
105  GetLastError())));
106 
107  return (PGSemaphore) cur_handle;
108 }
static int numSems
Definition: win32_sema.c:21
#define PANIC
Definition: elog.h:53
static int maxSems
Definition: win32_sema.c:22
bool IsUnderPostmaster
Definition: globals.c:100
static HANDLE * mySemSet
Definition: win32_sema.c:20
#define ereport(elevel, rest)
Definition: elog.h:122
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define TRUE
Definition: c.h:217
#define elog
Definition: elog.h:219
void PGSemaphoreLock ( PGSemaphore  sema)

Definition at line 133 of file win32_sema.c.

References CHECK_FOR_INTERRUPTS, EINTR, elog, ereport, errmsg(), FALSE, FATAL, PG_SEM_REF, pgwin32_dispatch_queued_signals(), pgwin32_signal_event, and TRUE.

134 {
135  HANDLE wh[2];
136  bool done = false;
137 
138  /*
139  * Note: pgwin32_signal_event should be first to ensure that it will be
140  * reported when multiple events are set. We want to guarantee that
141  * pending signals are serviced.
142  */
143  wh[0] = pgwin32_signal_event;
144  wh[1] = sema;
145 
146  /*
147  * As in other implementations of PGSemaphoreLock, we need to check for
148  * cancel/die interrupts each time through the loop. But here, there is
149  * no hidden magic about whether the syscall will internally service a
150  * signal --- we do that ourselves.
151  */
152  while (!done)
153  {
154  DWORD rc;
155 
157 
158  rc = WaitForMultipleObjectsEx(2, wh, FALSE, INFINITE, TRUE);
159  switch (rc)
160  {
161  case WAIT_OBJECT_0:
162  /* Signal event is set - we have a signal to deliver */
164  break;
165  case WAIT_OBJECT_0 + 1:
166  /* We got it! */
167  done = true;
168  break;
169  case WAIT_IO_COMPLETION:
170 
171  /*
172  * The system interrupted the wait to execute an I/O
173  * completion routine or asynchronous procedure call in this
174  * thread. PostgreSQL does not provoke either of these, but
175  * atypical loaded DLLs or even other processes might do so.
176  * Now, resume waiting.
177  */
178  break;
179  case WAIT_FAILED:
180  ereport(FATAL,
181  (errmsg("could not lock semaphore: error code %lu",
182  GetLastError())));
183  break;
184  default:
185  elog(FATAL, "unexpected return code from WaitForMultipleObjectsEx(): %lu", rc);
186  break;
187  }
188  }
189 }
HANDLE pgwin32_signal_event
Definition: signal.c:27
void pgwin32_dispatch_queued_signals(void)
Definition: signal.c:107
#define FALSE
Definition: c.h:221
#define FATAL
Definition: elog.h:52
#define ereport(elevel, rest)
Definition: elog.h:122
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define TRUE
Definition: c.h:217
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:97
#define elog
Definition: elog.h:219
void PGSemaphoreReset ( PGSemaphore  sema)

Definition at line 116 of file win32_sema.c.

References EAGAIN, EINTR, elog, FATAL, PG_SEM_REF, and PGSemaphoreTryLock().

117 {
118  /*
119  * There's no direct API for this in Win32, so we have to ratchet the
120  * semaphore down to 0 with repeated trylock's.
121  */
122  while (PGSemaphoreTryLock(sema))
123  /* loop */ ;
124 }
bool PGSemaphoreTryLock(PGSemaphore sema)
Definition: win32_sema.c:211
Size PGSemaphoreShmemSize ( int  maxSemas)

Definition at line 31 of file win32_sema.c.

References mul_size().

32 {
33  /* No shared memory needed on Windows */
34  return 0;
35 }
bool PGSemaphoreTryLock ( PGSemaphore  sema)

Definition at line 211 of file win32_sema.c.

References EAGAIN, EINTR, elog, ereport, errmsg(), FATAL, and PG_SEM_REF.

Referenced by PGSemaphoreReset().

212 {
213  DWORD ret;
214 
215  ret = WaitForSingleObject(sema, 0);
216 
217  if (ret == WAIT_OBJECT_0)
218  {
219  /* We got it! */
220  return true;
221  }
222  else if (ret == WAIT_TIMEOUT)
223  {
224  /* Can't get it */
225  errno = EAGAIN;
226  return false;
227  }
228 
229  /* Otherwise we are in trouble */
230  ereport(FATAL,
231  (errmsg("could not try-lock semaphore: error code %lu",
232  GetLastError())));
233 
234  /* keep compiler quiet */
235  return false;
236 }
#define EAGAIN
Definition: win32.h:283
#define FATAL
Definition: elog.h:52
#define ereport(elevel, rest)
Definition: elog.h:122
int errmsg(const char *fmt,...)
Definition: elog.c:797
void PGSemaphoreUnlock ( PGSemaphore  sema)

Definition at line 197 of file win32_sema.c.

References EINTR, elog, ereport, errmsg(), FATAL, NULL, and PG_SEM_REF.

198 {
199  if (!ReleaseSemaphore(sema, 1, NULL))
200  ereport(FATAL,
201  (errmsg("could not unlock semaphore: error code %lu",
202  GetLastError())));
203 }
#define FATAL
Definition: elog.h:52
#define ereport(elevel, rest)
Definition: elog.h:122
#define NULL
Definition: c.h:229
int errmsg(const char *fmt,...)
Definition: elog.c:797
static void ReleaseSemaphores ( int  code,
Datum  arg 
)
static

Definition at line 64 of file win32_sema.c.

References free, i, mySemSet, and numSems.

Referenced by PGReserveSemaphores().

65 {
66  int i;
67 
68  for (i = 0; i < numSems; i++)
69  CloseHandle(mySemSet[i]);
70  free(mySemSet);
71 }
static int numSems
Definition: win32_sema.c:21
static HANDLE * mySemSet
Definition: win32_sema.c:20
#define free(a)
Definition: header.h:65
int i

Variable Documentation

int maxSems
static

Definition at line 22 of file win32_sema.c.

Referenced by PGReserveSemaphores(), and PGSemaphoreCreate().

HANDLE* mySemSet
static

Definition at line 20 of file win32_sema.c.

Referenced by PGReserveSemaphores(), PGSemaphoreCreate(), and ReleaseSemaphores().

int numSems
static

Definition at line 21 of file win32_sema.c.

Referenced by PGReserveSemaphores(), PGSemaphoreCreate(), and ReleaseSemaphores().