29 #include <semaphore.h>
41 #if defined(USE_NAMED_POSIX_SEMAPHORES) && defined(EXEC_BACKEND)
42 #error cannot use named POSIX semaphores with EXEC_BACKEND
57 #define PG_SEM_REF(x) (&(x)->sem_padded.pgsem)
59 #define IPCProtection (0600)
61 #ifdef USE_NAMED_POSIX_SEMAPHORES
62 static sem_t **mySemPointers;
74 #ifdef USE_NAMED_POSIX_SEMAPHORES
96 snprintf(semname,
sizeof(semname),
"/pgsql-%d", semKey);
98 mySem = sem_open(semname, O_CREAT | O_EXCL,
102 if (mySem != (sem_t *) SEM_FAILED)
105 if (mySem != (sem_t *) (-1))
110 if (errno == EEXIST || errno == EACCES || errno ==
EINTR)
116 elog(
FATAL,
"sem_open(\"%s\") failed: %m", semname);
137 if (sem_init(
sem, 1, 1) < 0)
149 #ifdef USE_NAMED_POSIX_SEMAPHORES
151 if (sem_close(
sem) < 0)
152 elog(
LOG,
"sem_close failed: %m");
155 if (sem_destroy(
sem) < 0)
156 elog(
LOG,
"sem_destroy failed: %m");
167 #ifdef USE_NAMED_POSIX_SEMAPHORES
209 errmsg(
"could not stat data directory \"%s\": %m",
212 #ifdef USE_NAMED_POSIX_SEMAPHORES
213 mySemPointers = (sem_t **)
malloc(maxSemas *
sizeof(sem_t *));
214 if (mySemPointers == NULL)
243 #ifdef USE_NAMED_POSIX_SEMAPHORES
249 #ifdef USE_UNNAMED_POSIX_SEMAPHORES
270 elog(
PANIC,
"too many semaphores created");
272 #ifdef USE_NAMED_POSIX_SEMAPHORES
275 mySemPointers[
numSems] = newsem;
304 if (errno ==
EAGAIN || errno == EDEADLK)
327 }
while (errStatus < 0 && errno ==
EINTR);
352 }
while (errStatus < 0 && errno ==
EINTR);
376 }
while (errStatus < 0 && errno ==
EINTR);
380 if (errno ==
EAGAIN || errno == EDEADLK)
#define Assert(condition)
int errcode_for_file_access(void)
int errmsg(const char *fmt,...)
#define ereport(elevel,...)
void on_shmem_exit(pg_on_exit_callback function, Datum arg)
#define PG_CACHE_LINE_SIZE
struct PGSemaphoreData * PGSemaphore
Size PGSemaphoreShmemSize(int maxSemas)
void PGSemaphoreUnlock(PGSemaphore sema)
union SemTPadded SemTPadded
static void PosixSemaphoreKill(sem_t *sem)
struct PGSemaphoreData PGSemaphoreData
void PGReserveSemaphores(int maxSemas)
void PGSemaphoreReset(PGSemaphore sema)
void PGSemaphoreLock(PGSemaphore sema)
bool PGSemaphoreTryLock(PGSemaphore sema)
static PGSemaphore sharedSemas
PGSemaphore PGSemaphoreCreate(void)
static void ReleaseSemaphores(int status, Datum arg)
static void PosixSemaphoreCreate(sem_t *sem)
Size mul_size(Size s1, Size s2)
void * ShmemAllocUnlocked(Size size)
char pad[PG_CACHE_LINE_SIZE]