1 /*-------------------------------------------------------------------------
4 * Implement PGSemaphores using POSIX semaphore facilities
6 * We prefer the unnamed style of POSIX semaphore (the kind made with
7 * sem_init). We can cope with the kind made with sem_open, however.
10 * Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
11 * Portions Copyright (c) 1994, Regents of the University of California
14 * $PostgreSQL: pgsql/src/backend/port/posix_sema.c,v 1.21 2009/01/01 17:23:46 momjian Exp $
16 *-------------------------------------------------------------------------
24 #include "miscadmin.h"
25 #include "storage/ipc.h"
26 #include "storage/pg_sema.h"
29 #ifdef USE_NAMED_POSIX_SEMAPHORES
30 /* PGSemaphore is pointer to pointer to sem_t */
31 #define PG_SEM_REF(x) (*(x))
33 /* PGSemaphore is pointer to sem_t */
34 #define PG_SEM_REF(x) (x)
38 #define IPCProtection (0600) /* access/modify by user only */
40 static sem_t **mySemPointers; /* keep track of created semaphores */
41 static int numSems; /* number of semas acquired so far */
42 static int maxSems; /* allocated size of mySemaPointers array */
43 static int nextSemKey; /* next name to try */
46 static void ReleaseSemaphores(int status, Datum arg);
49 #ifdef USE_NAMED_POSIX_SEMAPHORES
52 * PosixSemaphoreCreate
54 * Attempt to create a new named semaphore.
56 * If we fail with a failure code other than collision-with-existing-sema,
57 * print out an error and abort. Other types of errors suggest nonrecoverable
61 PosixSemaphoreCreate(void)
69 semKey = nextSemKey++;
71 snprintf(semname, sizeof(semname), "/pgsql-%d", semKey);
73 mySem = sem_open(semname, O_CREAT | O_EXCL,
74 (mode_t) IPCProtection, (unsigned) 1);
77 if (mySem != (sem_t *) SEM_FAILED)
80 if (mySem != (sem_t *) (-1))
84 /* Loop if error indicates a collision */
85 if (errno == EEXIST || errno == EACCES || errno == EINTR)
89 * Else complain and abort
91 elog(FATAL, "sem_open(\"%s\") failed: %m", semname);
95 * Unlink the semaphore immediately, so it can't be accessed externally.
96 * This also ensures that it will go away if we crash.
102 #else /* !USE_NAMED_POSIX_SEMAPHORES */
105 * PosixSemaphoreCreate
107 * Attempt to create a new unnamed semaphore.
110 PosixSemaphoreCreate(sem_t * sem)
112 if (sem_init(sem, 1, 1) < 0)
113 elog(FATAL, "sem_init failed: %m");
115 #endif /* USE_NAMED_POSIX_SEMAPHORES */
119 * PosixSemaphoreKill - removes a semaphore
122 PosixSemaphoreKill(sem_t * sem)
124 #ifdef USE_NAMED_POSIX_SEMAPHORES
125 /* Got to use sem_close for named semaphores */
126 if (sem_close(sem) < 0)
127 elog(LOG, "sem_close failed: %m");
129 /* Got to use sem_destroy for unnamed semaphores */
130 if (sem_destroy(sem) < 0)
131 elog(LOG, "sem_destroy failed: %m");
137 * PGReserveSemaphores --- initialize semaphore support
139 * This is called during postmaster start or shared memory reinitialization.
140 * It should do whatever is needed to be able to support up to maxSemas
141 * subsequent PGSemaphoreCreate calls. Also, if any system resources
142 * are acquired here or in PGSemaphoreCreate, register an on_shmem_exit
143 * callback to release them.
145 * The port number is passed for possible use as a key (for Posix, we use
146 * it to generate the starting semaphore name). In a standalone backend,
147 * zero will be passed.
149 * In the Posix implementation, we acquire semaphores on-demand; the
150 * maxSemas parameter is just used to size the array that keeps track of
151 * acquired semas for subsequent releasing.
154 PGReserveSemaphores(int maxSemas, int port)
156 mySemPointers = (sem_t **) malloc(maxSemas * sizeof(sem_t *));
157 if (mySemPointers == NULL)
158 elog(PANIC, "out of memory");
161 nextSemKey = port * 1000;
163 on_shmem_exit(ReleaseSemaphores, 0);
167 * Release semaphores at shutdown or shmem reinitialization
169 * (called as an on_shmem_exit callback, hence funny argument list)
172 ReleaseSemaphores(int status, Datum arg)
176 for (i = 0; i < numSems; i++)
177 PosixSemaphoreKill(mySemPointers[i]);
184 * Initialize a PGSemaphore structure to represent a sema with count 1
187 PGSemaphoreCreate(PGSemaphore sema)
191 /* Can't do this in a backend, because static state is postmaster's */
192 Assert(!IsUnderPostmaster);
194 if (numSems >= maxSems)
195 elog(PANIC, "too many semaphores created");
197 #ifdef USE_NAMED_POSIX_SEMAPHORES
198 *sema = newsem = PosixSemaphoreCreate();
200 PosixSemaphoreCreate(sema);
204 /* Remember new sema for ReleaseSemaphores */
205 mySemPointers[numSems++] = newsem;
211 * Reset a previously-initialized PGSemaphore to have count 0
214 PGSemaphoreReset(PGSemaphore sema)
217 * There's no direct API for this in POSIX, so we have to ratchet the
218 * semaphore down to 0 with repeated trywait's.
222 if (sem_trywait(PG_SEM_REF(sema)) < 0)
224 if (errno == EAGAIN || errno == EDEADLK)
225 break; /* got it down to 0 */
227 continue; /* can this happen? */
228 elog(FATAL, "sem_trywait failed: %m");
236 * Lock a semaphore (decrement count), blocking if count would be < 0
239 PGSemaphoreLock(PGSemaphore sema, bool interruptOK)
244 * See notes in sysv_sema.c's implementation of PGSemaphoreLock.
245 * Just as that code does for semop(), we handle both the case where
246 * sem_wait() returns errno == EINTR after a signal, and the case
247 * where it just keeps waiting.
251 ImmediateInterruptOK = interruptOK;
252 CHECK_FOR_INTERRUPTS();
253 errStatus = sem_wait(PG_SEM_REF(sema));
254 ImmediateInterruptOK = false;
255 } while (errStatus < 0 && errno == EINTR);
258 elog(FATAL, "sem_wait failed: %m");
264 * Unlock a semaphore (increment count)
267 PGSemaphoreUnlock(PGSemaphore sema)
272 * Note: if errStatus is -1 and errno == EINTR then it means we returned
273 * from the operation prematurely because we were sent a signal. So we
274 * try and unlock the semaphore again. Not clear this can really happen,
275 * but might as well cope.
279 errStatus = sem_post(PG_SEM_REF(sema));
280 } while (errStatus < 0 && errno == EINTR);
283 elog(FATAL, "sem_post failed: %m");
289 * Lock a semaphore only if able to do so without blocking
292 PGSemaphoreTryLock(PGSemaphore sema)
297 * Note: if errStatus is -1 and errno == EINTR then it means we returned
298 * from the operation prematurely because we were sent a signal. So we
299 * try and lock the semaphore again.
303 errStatus = sem_trywait(PG_SEM_REF(sema));
304 } while (errStatus < 0 && errno == EINTR);
308 if (errno == EAGAIN || errno == EDEADLK)
309 return false; /* failed to lock it */
310 /* Otherwise we got trouble */
311 elog(FATAL, "sem_trywait failed: %m");