LCOV - differential code coverage report
Current view: top level - src/backend/port - posix_sema.c (source / functions) Coverage Total Hit UBC GNC CBC DCB
Current: 4b3c9325757dfd8f2312e9f1a8decf4431f344ae vs e87b024bfb8dd55e3049b1b6e6ab4e810cf1abc8 Lines: 71.4 % 56 40 16 1 39 1
Current Date: 2026-11-28 16:48:44 +0900 Functions: 90.0 % 10 9 1 1 8
Baseline: lcov-20261128-baseline Branches: 24.1 % 54 13 41 2 11
Baseline Date: 2026-11-28 16:48:48 +0900 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(7,30] days: 100.0 % 1 1 1
(30,360] days: 100.0 % 3 3 3
(360..) days: 69.2 % 52 36 16 36
Function coverage date bins:
(30,360] days: 100.0 % 2 2 2
(360..) days: 87.5 % 8 7 1 1 6
Branch coverage date bins:
(7,30] days: 100.0 % 2 2 2
(360..) days: 21.2 % 52 11 41 11

 Age         Owner                    Branch data    TLA  Line data    Source code
                                  1                 :                : /*-------------------------------------------------------------------------
                                  2                 :                :  *
                                  3                 :                :  * posix_sema.c
                                  4                 :                :  *    Implement PGSemaphores using POSIX semaphore facilities
                                  5                 :                :  *
                                  6                 :                :  * We use the unnamed style of POSIX semaphore (the kind made with
                                  7                 :                :  * sem_init).  We used to support the kind made with sem_open, but
                                  8                 :                :  * that has several disadvantages, and there are no longer any
                                  9                 :                :  * platforms where it is a preferable choice.
                                 10                 :                :  *
                                 11                 :                :  * In this implementation, typedef PGSemaphore is equivalent to "sem_t *".
                                 12                 :                :  * The sem_t structs live in an array in shared memory.
                                 13                 :                :  *
                                 14                 :                :  *
                                 15                 :                :  * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
                                 16                 :                :  * Portions Copyright (c) 1994, Regents of the University of California
                                 17                 :                :  *
                                 18                 :                :  * IDENTIFICATION
                                 19                 :                :  *    src/backend/port/posix_sema.c
                                 20                 :                :  *
                                 21                 :                :  *-------------------------------------------------------------------------
                                 22                 :                :  */
                                 23                 :                : #include "postgres.h"
                                 24                 :                : 
                                 25                 :                : #include <semaphore.h>
                                 26                 :                : 
                                 27                 :                : #include "miscadmin.h"
                                 28                 :                : #include "storage/ipc.h"
                                 29                 :                : #include "storage/pg_sema.h"
                                 30                 :                : #include "storage/shmem.h"
                                 31                 :                : 
                                 32                 :                : 
                                 33                 :                : typedef union SemTPadded
                                 34                 :                : {
                                 35                 :                :     sem_t       pgsem;
                                 36                 :                :     char        pad[PG_CACHE_LINE_SIZE];
                                 37                 :                : } SemTPadded;
                                 38                 :                : 
                                 39                 :                : /* typedef PGSemaphore is equivalent to pointer to sem_t */
                                 40                 :                : typedef struct PGSemaphoreData
                                 41                 :                : {
                                 42                 :                :     SemTPadded  sem_padded;
                                 43                 :                : } PGSemaphoreData;
                                 44                 :                : 
                                 45                 :                : #define PG_SEM_REF(x)   (&(x)->sem_padded.pgsem)
                                 46                 :                : 
                                 47                 :                : 
                                 48                 :                : static PGSemaphore sharedSemas; /* array of PGSemaphoreData in shared memory */
                                 49                 :                : static int  numSems;            /* number of semas acquired so far */
                                 50                 :                : static int  maxSems;            /* allocated size of above array */
                                 51                 :                : 
                                 52                 :                : 
                                 53                 :                : static void ReleaseSemaphores(int status, Datum arg);
                                 54                 :                : 
                                 55                 :                : 
                                 56                 :                : /*
                                 57                 :                :  * PosixSemaphoreCreate
                                 58                 :                :  *
                                 59                 :                :  * Attempt to create a new unnamed semaphore.
                                 60                 :                :  */
                                 61                 :                : static void
 3482 bruce@momjian.us           62                 :CBC      170678 : PosixSemaphoreCreate(sem_t *sem)
                                 63                 :                : {
 8973 tgl@sss.pgh.pa.us          64         [ -  + ]:         170678 :     if (sem_init(sem, 1, 1) < 0)
 8525 tgl@sss.pgh.pa.us          65         [ #  # ]:UBC           0 :         elog(FATAL, "sem_init failed: %m");
 8973 tgl@sss.pgh.pa.us          66                 :CBC      170678 : }
                                 67                 :                : 
                                 68                 :                : 
                                 69                 :                : /*
                                 70                 :                :  * PosixSemaphoreKill   - removes a semaphore
                                 71                 :                :  */
                                 72                 :                : static void
 3482 bruce@momjian.us           73                 :         170678 : PosixSemaphoreKill(sem_t *sem)
                                 74                 :                : {
 8973 tgl@sss.pgh.pa.us          75         [ -  + ]:         170678 :     if (sem_destroy(sem) < 0)
 8525 tgl@sss.pgh.pa.us          76         [ #  # ]:UBC           0 :         elog(LOG, "sem_destroy failed: %m");
 8973 tgl@sss.pgh.pa.us          77                 :CBC      170678 : }
                                 78                 :                : 
                                 79                 :                : 
                                 80                 :                : /*
                                 81                 :                :  * Request shared memory needed for semaphores
                                 82                 :                :  */
                                 83                 :                : void
  236 heikki.linnakangas@i       84                 :           1329 : PGSemaphoreShmemRequest(int maxSemas)
                                 85                 :                : {
                                 86                 :                :     /* Need a PGSemaphoreData per semaphore */
                                 87                 :           1329 :     ShmemRequestStruct(.name = "Semaphores",
                                 88                 :                :                        .size = mul_size(maxSemas, sizeof(PGSemaphoreData)),
                                 89                 :                :                        .ptr = (void **) &sharedSemas,
                                 90                 :                :         );
 3638 tgl@sss.pgh.pa.us          91                 :           1329 : }
                                 92                 :                : 
                                 93                 :                : /*
                                 94                 :                :  * PGSemaphoreInit --- initialize semaphore support
                                 95                 :                :  *
                                 96                 :                :  * This is called during postmaster start or shared memory reinitialization.
                                 97                 :                :  * It should do whatever is needed to be able to support up to maxSemas
                                 98                 :                :  * subsequent PGSemaphoreCreate calls.  Also, if any system resources
                                 99                 :                :  * are acquired here or in PGSemaphoreCreate, register an on_shmem_exit
                                100                 :                :  * callback to release them.
                                101                 :                :  *
                                102                 :                :  * In the Posix implementation, we acquire semaphores on-demand; the
                                103                 :                :  * maxSemas parameter is just used to size the arrays.  We keep track
                                104                 :                :  * of them using an array of PGSemaphoreData structs in shared memory.
                                105                 :                :  * (In the event that a failing backend scribbles on that array, the
                                106                 :                :  * postmaster's subsequent sem_destroy() calls might fail, but with
                                107                 :                :  * luck the consequences are no worse than some leaked resources.)
                                108                 :                :  */
                                109                 :                : void
  236 heikki.linnakangas@i      110                 :           1325 : PGSemaphoreInit(int maxSemas)
                                111                 :                : {
 8973 tgl@sss.pgh.pa.us         112                 :           1325 :     numSems = 0;
                                113                 :           1325 :     maxSems = maxSemas;
                                114                 :                : 
                                115                 :           1325 :     on_shmem_exit(ReleaseSemaphores, 0);
                                116                 :           1325 : }
                                117                 :                : 
                                118                 :                : /*
                                119                 :                :  * Release semaphores at shutdown or shmem reinitialization
                                120                 :                :  *
                                121                 :                :  * (called as an on_shmem_exit callback, hence funny argument list)
                                122                 :                :  */
                                123                 :                : static void
                                124                 :           1325 : ReleaseSemaphores(int status, Datum arg)
                                125                 :                : {
   15 tgl@sss.pgh.pa.us         126         [ +  + ]:GNC      172003 :     for (int i = 0; i < numSems; i++)
 3638 tgl@sss.pgh.pa.us         127                 :CBC      170678 :         PosixSemaphoreKill(PG_SEM_REF(sharedSemas + i));
 8973                           128                 :           1325 : }
                                129                 :                : 
                                130                 :                : /*
                                131                 :                :  * PGSemaphoreCreate
                                132                 :                :  *
                                133                 :                :  * Allocate a PGSemaphore structure with initial count 1
                                134                 :                :  */
                                135                 :                : PGSemaphore
 3638                           136                 :         170678 : PGSemaphoreCreate(void)
                                137                 :                : {
                                138                 :                :     PGSemaphore sema;
                                139                 :                :     sem_t      *newsem;
                                140                 :                : 
                                141                 :                :     /* Can't do this in a backend, because static state is postmaster's */
 8973                           142         [ -  + ]:         170678 :     Assert(!IsUnderPostmaster);
                                143                 :                : 
                                144         [ -  + ]:         170678 :     if (numSems >= maxSems)
 8530 tgl@sss.pgh.pa.us         145         [ #  # ]:UBC           0 :         elog(PANIC, "too many semaphores created");
                                146                 :                : 
 3638 tgl@sss.pgh.pa.us         147                 :CBC      170678 :     sema = &sharedSemas[numSems];
                                148                 :         170678 :     newsem = PG_SEM_REF(sema);
                                149                 :         170678 :     PosixSemaphoreCreate(newsem);
                                150                 :                : 
                                151                 :         170678 :     numSems++;
                                152                 :                : 
                                153                 :         170678 :     return sema;
                                154                 :                : }
                                155                 :                : 
                                156                 :                : /*
                                157                 :                :  * PGSemaphoreReset
                                158                 :                :  *
                                159                 :                :  * Reset a previously-initialized PGSemaphore to have count 0
                                160                 :                :  */
                                161                 :                : void
 8973                           162                 :          25031 : PGSemaphoreReset(PGSemaphore sema)
                                163                 :                : {
                                164                 :                :     /*
                                165                 :                :      * There's no direct API for this in POSIX, so we have to ratchet the
                                166                 :                :      * semaphore down to 0 with repeated trywait's.
                                167                 :                :      */
                                168                 :                :     for (;;)
                                169                 :                :     {
                                170         [ +  + ]:          41122 :         if (sem_trywait(PG_SEM_REF(sema)) < 0)
                                171                 :                :         {
                                172   [ -  +  -  - ]:          25031 :             if (errno == EAGAIN || errno == EDEADLK)
                                173                 :                :                 break;          /* got it down to 0 */
 8973 tgl@sss.pgh.pa.us         174         [ #  # ]:UBC           0 :             if (errno == EINTR)
                                175                 :              0 :                 continue;       /* can this happen? */
 8525                           176         [ #  # ]:              0 :             elog(FATAL, "sem_trywait failed: %m");
                                177                 :                :         }
                                178                 :                :     }
 8973 tgl@sss.pgh.pa.us         179                 :CBC       25031 : }
                                180                 :                : 
                                181                 :                : /*
                                182                 :                :  * PGSemaphoreLock
                                183                 :                :  *
                                184                 :                :  * Lock a semaphore (decrement count), blocking if count would be < 0
                                185                 :                :  */
                                186                 :                : void
 4316 andres@anarazel.de        187                 :         293270 : PGSemaphoreLock(PGSemaphore sema)
                                188                 :                : {
                                189                 :                :     int         errStatus;
                                190                 :                : 
                                191                 :                :     /* See notes in sysv_sema.c's implementation of PGSemaphoreLock. */
                                192                 :                :     do
                                193                 :                :     {
 8973 tgl@sss.pgh.pa.us         194                 :         293270 :         errStatus = sem_wait(PG_SEM_REF(sema));
                                195   [ -  +  -  - ]:         293270 :     } while (errStatus < 0 && errno == EINTR);
                                196                 :                : 
                                197         [ -  + ]:         293270 :     if (errStatus < 0)
 8525 tgl@sss.pgh.pa.us         198         [ #  # ]:UBC           0 :         elog(FATAL, "sem_wait failed: %m");
 8973 tgl@sss.pgh.pa.us         199                 :CBC      293270 : }
                                200                 :                : 
                                201                 :                : /*
                                202                 :                :  * PGSemaphoreUnlock
                                203                 :                :  *
                                204                 :                :  * Unlock a semaphore (increment count)
                                205                 :                :  */
                                206                 :                : void
                                207                 :         293339 : PGSemaphoreUnlock(PGSemaphore sema)
                                208                 :                : {
                                209                 :                :     int         errStatus;
                                210                 :                : 
                                211                 :                :     /*
                                212                 :                :      * Note: if errStatus is -1 and errno == EINTR then it means we returned
                                213                 :                :      * from the operation prematurely because we were sent a signal.  So we
                                214                 :                :      * try and unlock the semaphore again. Not clear this can really happen,
                                215                 :                :      * but might as well cope.
                                216                 :                :      */
                                217                 :                :     do
                                218                 :                :     {
                                219                 :         293339 :         errStatus = sem_post(PG_SEM_REF(sema));
                                220   [ -  +  -  - ]:         293339 :     } while (errStatus < 0 && errno == EINTR);
                                221                 :                : 
                                222         [ -  + ]:         293339 :     if (errStatus < 0)
 8525 tgl@sss.pgh.pa.us         223         [ #  # ]:UBC           0 :         elog(FATAL, "sem_post failed: %m");
 8973 tgl@sss.pgh.pa.us         224                 :CBC      293339 : }
                                225                 :                : 
                                226                 :                : /*
                                227                 :                :  * PGSemaphoreTryLock
                                228                 :                :  *
                                229                 :                :  * Lock a semaphore only if able to do so without blocking
                                230                 :                :  */
                                231                 :                : bool
 8973 tgl@sss.pgh.pa.us         232                 :UBC           0 : PGSemaphoreTryLock(PGSemaphore sema)
                                233                 :                : {
                                234                 :                :     int         errStatus;
                                235                 :                : 
                                236                 :                :     /*
                                237                 :                :      * Note: if errStatus is -1 and errno == EINTR then it means we returned
                                238                 :                :      * from the operation prematurely because we were sent a signal.  So we
                                239                 :                :      * try and lock the semaphore again.
                                240                 :                :      */
                                241                 :                :     do
                                242                 :                :     {
                                243                 :              0 :         errStatus = sem_trywait(PG_SEM_REF(sema));
                                244   [ #  #  #  # ]:              0 :     } while (errStatus < 0 && errno == EINTR);
                                245                 :                : 
                                246         [ #  # ]:              0 :     if (errStatus < 0)
                                247                 :                :     {
                                248   [ #  #  #  # ]:              0 :         if (errno == EAGAIN || errno == EDEADLK)
                                249                 :              0 :             return false;       /* failed to lock it */
                                250                 :                :         /* Otherwise we got trouble */
 8525                           251         [ #  # ]:              0 :         elog(FATAL, "sem_trywait failed: %m");
                                252                 :                :     }
                                253                 :                : 
 8973                           254                 :              0 :     return true;
                                255                 :                : }
        

Generated by: LCOV version 2.0-1