LCOV - code coverage report
Current view: top level - src/backend/port - pg_sema.c (source / functions) Coverage Total Hit
Test: PostgreSQL 20devel Lines: 70.9 % 55 39
Test Date: 2026-11-26 20:16:09 Functions: 90.0 % 10 9
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 23.1 % 52 12

             Branch data     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
      62                 :      176314 : PosixSemaphoreCreate(sem_t *sem)
      63                 :             : {
      64         [ -  + ]:      176314 :     if (sem_init(sem, 1, 1) < 0)
      65         [ #  # ]:           0 :         elog(FATAL, "sem_init failed: %m");
      66                 :      176314 : }
      67                 :             : 
      68                 :             : 
      69                 :             : /*
      70                 :             :  * PosixSemaphoreKill   - removes a semaphore
      71                 :             :  */
      72                 :             : static void
      73                 :      176314 : PosixSemaphoreKill(sem_t *sem)
      74                 :             : {
      75         [ -  + ]:      176314 :     if (sem_destroy(sem) < 0)
      76         [ #  # ]:           0 :         elog(LOG, "sem_destroy failed: %m");
      77                 :      176314 : }
      78                 :             : 
      79                 :             : 
      80                 :             : /*
      81                 :             :  * Request shared memory needed for semaphores
      82                 :             :  */
      83                 :             : void
      84                 :        1363 : PGSemaphoreShmemRequest(int maxSemas)
      85                 :             : {
      86                 :             :     /* Need a PGSemaphoreData per semaphore */
      87                 :        1363 :     ShmemRequestStruct(.name = "Semaphores",
      88                 :             :                        .size = mul_size(maxSemas, sizeof(PGSemaphoreData)),
      89                 :             :                        .ptr = (void **) &sharedSemas,
      90                 :             :         );
      91                 :        1363 : }
      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
     110                 :        1359 : PGSemaphoreInit(int maxSemas)
     111                 :             : {
     112                 :        1359 :     numSems = 0;
     113                 :        1359 :     maxSems = maxSemas;
     114                 :             : 
     115                 :        1359 :     on_shmem_exit(ReleaseSemaphores, 0);
     116                 :        1359 : }
     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                 :        1359 : ReleaseSemaphores(int status, Datum arg)
     125                 :             : {
     126         [ +  + ]:      177673 :     for (int i = 0; i < numSems; i++)
     127                 :      176314 :         PosixSemaphoreKill(PG_SEM_REF(sharedSemas + i));
     128                 :        1359 : }
     129                 :             : 
     130                 :             : /*
     131                 :             :  * PGSemaphoreCreate
     132                 :             :  *
     133                 :             :  * Allocate a PGSemaphore structure with initial count 1
     134                 :             :  */
     135                 :             : PGSemaphore
     136                 :      176314 : 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 */
     142                 :             :     Assert(!IsUnderPostmaster);
     143                 :             : 
     144         [ -  + ]:      176314 :     if (numSems >= maxSems)
     145         [ #  # ]:           0 :         elog(PANIC, "too many semaphores created");
     146                 :             : 
     147                 :      176314 :     sema = &sharedSemas[numSems];
     148                 :      176314 :     newsem = PG_SEM_REF(sema);
     149                 :      176314 :     PosixSemaphoreCreate(newsem);
     150                 :             : 
     151                 :      176314 :     numSems++;
     152                 :             : 
     153                 :      176314 :     return sema;
     154                 :             : }
     155                 :             : 
     156                 :             : /*
     157                 :             :  * PGSemaphoreReset
     158                 :             :  *
     159                 :             :  * Reset a previously-initialized PGSemaphore to have count 0
     160                 :             :  */
     161                 :             : void
     162                 :       26828 : 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         [ +  + ]:       43351 :         if (sem_trywait(PG_SEM_REF(sema)) < 0)
     171                 :             :         {
     172   [ -  +  -  - ]:       26828 :             if (errno == EAGAIN || errno == EDEADLK)
     173                 :             :                 break;          /* got it down to 0 */
     174         [ #  # ]:           0 :             if (errno == EINTR)
     175                 :           0 :                 continue;       /* can this happen? */
     176         [ #  # ]:           0 :             elog(FATAL, "sem_trywait failed: %m");
     177                 :             :         }
     178                 :             :     }
     179                 :       26828 : }
     180                 :             : 
     181                 :             : /*
     182                 :             :  * PGSemaphoreLock
     183                 :             :  *
     184                 :             :  * Lock a semaphore (decrement count), blocking if count would be < 0
     185                 :             :  */
     186                 :             : void
     187                 :     5511532 : PGSemaphoreLock(PGSemaphore sema)
     188                 :             : {
     189                 :             :     int         errStatus;
     190                 :             : 
     191                 :             :     /* See notes in sysv_sema.c's implementation of PGSemaphoreLock. */
     192                 :             :     do
     193                 :             :     {
     194                 :     5511532 :         errStatus = sem_wait(PG_SEM_REF(sema));
     195   [ -  +  -  - ]:     5511532 :     } while (errStatus < 0 && errno == EINTR);
     196                 :             : 
     197         [ -  + ]:     5511532 :     if (errStatus < 0)
     198         [ #  # ]:           0 :         elog(FATAL, "sem_wait failed: %m");
     199                 :     5511532 : }
     200                 :             : 
     201                 :             : /*
     202                 :             :  * PGSemaphoreUnlock
     203                 :             :  *
     204                 :             :  * Unlock a semaphore (increment count)
     205                 :             :  */
     206                 :             : void
     207                 :     5511613 : 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                 :     5511613 :         errStatus = sem_post(PG_SEM_REF(sema));
     220   [ -  +  -  - ]:     5511613 :     } while (errStatus < 0 && errno == EINTR);
     221                 :             : 
     222         [ -  + ]:     5511613 :     if (errStatus < 0)
     223         [ #  # ]:           0 :         elog(FATAL, "sem_post failed: %m");
     224                 :     5511613 : }
     225                 :             : 
     226                 :             : /*
     227                 :             :  * PGSemaphoreTryLock
     228                 :             :  *
     229                 :             :  * Lock a semaphore only if able to do so without blocking
     230                 :             :  */
     231                 :             : bool
     232                 :           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 */
     251         [ #  # ]:           0 :         elog(FATAL, "sem_trywait failed: %m");
     252                 :             :     }
     253                 :             : 
     254                 :           0 :     return true;
     255                 :             : }
        

Generated by: LCOV version 2.0-1