PostgreSQL Source Code git master
Loading...
Searching...
No Matches
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)
 
void PGSemaphoreShmemRequest (int maxSemas)
 
void PGSemaphoreInit (int maxSemas)
 
PGSemaphore PGSemaphoreCreate (void)
 
void PGSemaphoreReset (PGSemaphore sema)
 
void PGSemaphoreLock (PGSemaphore sema)
 
void PGSemaphoreUnlock (PGSemaphore sema)
 
bool PGSemaphoreTryLock (PGSemaphore sema)
 

Variables

static HANDLEmySemSet
 
static int numSems
 
static int maxSems
 

Function Documentation

◆ PGSemaphoreCreate()

PGSemaphore PGSemaphoreCreate ( void  )

Definition at line 78 of file win32_sema.c.

79{
82
83 /* Can't do this in a backend, because static state is postmaster's */
85
86 if (numSems >= maxSems)
87 elog(PANIC, "too many semaphores created");
88
90 sec_attrs.nLength = sizeof(sec_attrs);
91 sec_attrs.lpSecurityDescriptor = NULL;
92 sec_attrs.bInheritHandle = TRUE;
93
94 /* We don't need a named semaphore */
96 if (cur_handle)
97 {
98 /* Successfully done */
100 }
101 else
103 (errmsg("could not create semaphore: error code %lu",
104 GetLastError())));
105
106 return (PGSemaphore) cur_handle;
107}
#define Assert(condition)
Definition c.h:943
#define PANIC
Definition elog.h:44
#define elog(elevel,...)
Definition elog.h:228
#define ereport(elevel,...)
Definition elog.h:152
bool IsUnderPostmaster
Definition globals.c:122
static char * errmsg
static int fb(int x)
static int maxSems
Definition win32_sema.c:22
static int numSems
Definition win32_sema.c:21
static HANDLE * mySemSet
Definition win32_sema.c:20

References Assert, elog, ereport, errmsg, fb(), IsUnderPostmaster, maxSems, mySemSet, numSems, and PANIC.

◆ PGSemaphoreInit()

void PGSemaphoreInit ( int  maxSemas)

Definition at line 46 of file win32_sema.c.

47{
48 mySemSet = (HANDLE *) malloc(maxSemas * sizeof(HANDLE));
49 if (mySemSet == NULL)
50 elog(PANIC, "out of memory");
51 numSems = 0;
53
55}
void on_shmem_exit(pg_on_exit_callback function, Datum arg)
Definition ipc.c:372
#define malloc(a)
static void ReleaseSemaphores(int code, Datum arg)
Definition win32_sema.c:63

References elog, fb(), malloc, maxSems, mySemSet, numSems, on_shmem_exit(), PANIC, and ReleaseSemaphores().

◆ PGSemaphoreLock()

void PGSemaphoreLock ( PGSemaphore  sema)

Definition at line 131 of file win32_sema.c.

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

References CHECK_FOR_INTERRUPTS, elog, ereport, errmsg, FATAL, fb(), pgwin32_dispatch_queued_signals(), and pgwin32_signal_event.

◆ PGSemaphoreReset()

void PGSemaphoreReset ( PGSemaphore  sema)

Definition at line 115 of file win32_sema.c.

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

References PGSemaphoreTryLock().

◆ PGSemaphoreShmemRequest()

void PGSemaphoreShmemRequest ( int  maxSemas)

Definition at line 31 of file win32_sema.c.

32{
33 /* No shared memory needed on Windows */
34}

◆ PGSemaphoreTryLock()

bool PGSemaphoreTryLock ( PGSemaphore  sema)

Definition at line 209 of file win32_sema.c.

210{
211 DWORD ret;
212
213 ret = WaitForSingleObject(sema, 0);
214
215 if (ret == WAIT_OBJECT_0)
216 {
217 /* We got it! */
218 return true;
219 }
220 else if (ret == WAIT_TIMEOUT)
221 {
222 /* Can't get it */
223 errno = EAGAIN;
224 return false;
225 }
226
227 /* Otherwise we are in trouble */
229 (errmsg("could not try-lock semaphore: error code %lu",
230 GetLastError())));
231
232 /* keep compiler quiet */
233 return false;
234}
#define EAGAIN
Definition win32_port.h:359

References EAGAIN, ereport, errmsg, FATAL, and fb().

Referenced by PGSemaphoreReset().

◆ PGSemaphoreUnlock()

void PGSemaphoreUnlock ( PGSemaphore  sema)

Definition at line 195 of file win32_sema.c.

196{
197 if (!ReleaseSemaphore(sema, 1, NULL))
199 (errmsg("could not unlock semaphore: error code %lu",
200 GetLastError())));
201}

References ereport, errmsg, FATAL, and fb().

◆ ReleaseSemaphores()

static void ReleaseSemaphores ( int  code,
Datum  arg 
)
static

Definition at line 63 of file win32_sema.c.

64{
65 int i;
66
67 for (i = 0; i < numSems; i++)
70}
int i
Definition isn.c:77
#define free(a)

References fb(), free, i, mySemSet, and numSems.

Referenced by PGSemaphoreInit().

Variable Documentation

◆ maxSems

int maxSems
static

Definition at line 22 of file win32_sema.c.

Referenced by PGSemaphoreCreate(), and PGSemaphoreInit().

◆ mySemSet

HANDLE* mySemSet
static

Definition at line 20 of file win32_sema.c.

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

◆ numSems

int numSems
static

Definition at line 21 of file win32_sema.c.

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