LCOV - differential code coverage report
Current view: top level - src/include/storage - proclist.h (source / functions) Coverage Total Hit UBC CBC
Current: c70b6db34ffeab48beef1fb4ce61bcad3772b8dd vs 06473f5a344df8c9594ead90a609b86f6724cff8 Lines: 100.0 % 63 63 63
Current Date: 2025-09-06 07:49:51 +0900 Functions: 100.0 % 8 8 8
Baseline: lcov-20250906-005545-baseline Branches: 66.7 % 54 36 18 36
Baseline Date: 2025-09-05 08:21:35 +0100 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(360..) days: 100.0 % 63 63 63
Function coverage date bins:
(360..) days: 100.0 % 8 8 8
Branch coverage date bins:
(360..) days: 66.7 % 54 36 18 36

 Age         Owner                    Branch data    TLA  Line data    Source code
                                  1                 :                : /*-------------------------------------------------------------------------
                                  2                 :                :  *
                                  3                 :                :  * proclist.h
                                  4                 :                :  *      operations on doubly-linked lists of pgprocnos
                                  5                 :                :  *
                                  6                 :                :  * The interface is similar to dlist from ilist.h, but uses pgprocno instead
                                  7                 :                :  * of pointers.  This allows proclist_head to be mapped at different addresses
                                  8                 :                :  * in different backends.
                                  9                 :                :  *
                                 10                 :                :  * See proclist_types.h for the structs that these functions operate on.  They
                                 11                 :                :  * are separated to break a header dependency cycle with proc.h.
                                 12                 :                :  *
                                 13                 :                :  * Portions Copyright (c) 2016-2025, PostgreSQL Global Development Group
                                 14                 :                :  *
                                 15                 :                :  * IDENTIFICATION
                                 16                 :                :  *      src/include/storage/proclist.h
                                 17                 :                :  *-------------------------------------------------------------------------
                                 18                 :                :  */
                                 19                 :                : #ifndef PROCLIST_H
                                 20                 :                : #define PROCLIST_H
                                 21                 :                : 
                                 22                 :                : #include "storage/proc.h"
                                 23                 :                : #include "storage/proclist_types.h"
                                 24                 :                : 
                                 25                 :                : /*
                                 26                 :                :  * Initialize a proclist.
                                 27                 :                :  */
                                 28                 :                : static inline void
 3309 rhaas@postgresql.org       29                 :CBC    29728223 : proclist_init(proclist_head *list)
                                 30                 :                : {
  552 heikki.linnakangas@i       31                 :       29728223 :     list->head = list->tail = INVALID_PROC_NUMBER;
 3309 rhaas@postgresql.org       32                 :       29728223 : }
                                 33                 :                : 
                                 34                 :                : /*
                                 35                 :                :  * Is the list empty?
                                 36                 :                :  */
                                 37                 :                : static inline bool
  961 peter@eisentraut.org       38                 :        7312438 : proclist_is_empty(const proclist_head *list)
                                 39                 :                : {
  552 heikki.linnakangas@i       40                 :        7312438 :     return list->head == INVALID_PROC_NUMBER;
                                 41                 :                : }
                                 42                 :                : 
                                 43                 :                : /*
                                 44                 :                :  * Get a pointer to a proclist_node inside a given PGPROC, given a procno and
                                 45                 :                :  * the proclist_node field's offset within struct PGPROC.
                                 46                 :                :  */
                                 47                 :                : static inline proclist_node *
 3309 rhaas@postgresql.org       48                 :       10003029 : proclist_node_get(int procno, size_t node_offset)
                                 49                 :                : {
                                 50                 :       10003029 :     char       *entry = (char *) GetPGProcByNumber(procno);
                                 51                 :                : 
                                 52                 :       10003029 :     return (proclist_node *) (entry + node_offset);
                                 53                 :                : }
                                 54                 :                : 
                                 55                 :                : /*
                                 56                 :                :  * Insert a process at the beginning of a list.
                                 57                 :                :  */
                                 58                 :                : static inline void
                                 59                 :         122007 : proclist_push_head_offset(proclist_head *list, int procno, size_t node_offset)
                                 60                 :                : {
                                 61                 :         122007 :     proclist_node *node = proclist_node_get(procno, node_offset);
                                 62                 :                : 
 2798 tgl@sss.pgh.pa.us          63   [ +  -  -  + ]:         122007 :     Assert(node->next == 0 && node->prev == 0);
                                 64                 :                : 
  552 heikki.linnakangas@i       65         [ +  + ]:         122007 :     if (list->head == INVALID_PROC_NUMBER)
                                 66                 :                :     {
                                 67         [ -  + ]:          46982 :         Assert(list->tail == INVALID_PROC_NUMBER);
                                 68                 :          46982 :         node->next = node->prev = INVALID_PROC_NUMBER;
 3309 rhaas@postgresql.org       69                 :          46982 :         list->head = list->tail = procno;
                                 70                 :                :     }
                                 71                 :                :     else
                                 72                 :                :     {
  552 heikki.linnakangas@i       73         [ -  + ]:          75025 :         Assert(list->tail != INVALID_PROC_NUMBER);
 3210 rhaas@postgresql.org       74         [ -  + ]:          75025 :         Assert(list->head != procno);
                                 75         [ -  + ]:          75025 :         Assert(list->tail != procno);
 3309                            76                 :          75025 :         node->next = list->head;
                                 77                 :          75025 :         proclist_node_get(node->next, node_offset)->prev = procno;
  552 heikki.linnakangas@i       78                 :          75025 :         node->prev = INVALID_PROC_NUMBER;
 3309 rhaas@postgresql.org       79                 :          75025 :         list->head = procno;
                                 80                 :                :     }
                                 81                 :         122007 : }
                                 82                 :                : 
                                 83                 :                : /*
                                 84                 :                :  * Insert a process at the end of a list.
                                 85                 :                :  */
                                 86                 :                : static inline void
                                 87                 :         992972 : proclist_push_tail_offset(proclist_head *list, int procno, size_t node_offset)
                                 88                 :                : {
                                 89                 :         992972 :     proclist_node *node = proclist_node_get(procno, node_offset);
                                 90                 :                : 
 2798 tgl@sss.pgh.pa.us          91   [ +  -  -  + ]:         992972 :     Assert(node->next == 0 && node->prev == 0);
                                 92                 :                : 
  552 heikki.linnakangas@i       93         [ +  + ]:         992972 :     if (list->tail == INVALID_PROC_NUMBER)
                                 94                 :                :     {
                                 95         [ -  + ]:         884325 :         Assert(list->head == INVALID_PROC_NUMBER);
                                 96                 :         884325 :         node->next = node->prev = INVALID_PROC_NUMBER;
 3309 rhaas@postgresql.org       97                 :         884325 :         list->head = list->tail = procno;
                                 98                 :                :     }
                                 99                 :                :     else
                                100                 :                :     {
  552 heikki.linnakangas@i      101         [ -  + ]:         108647 :         Assert(list->head != INVALID_PROC_NUMBER);
 3210 rhaas@postgresql.org      102         [ -  + ]:         108647 :         Assert(list->head != procno);
                                103         [ -  + ]:         108647 :         Assert(list->tail != procno);
 3309                           104                 :         108647 :         node->prev = list->tail;
                                105                 :         108647 :         proclist_node_get(node->prev, node_offset)->next = procno;
  552 heikki.linnakangas@i      106                 :         108647 :         node->next = INVALID_PROC_NUMBER;
 3309 rhaas@postgresql.org      107                 :         108647 :         list->tail = procno;
                                108                 :                :     }
                                109                 :         992972 : }
                                110                 :                : 
                                111                 :                : /*
                                112                 :                :  * Delete a process from a list --- it must be in the list!
                                113                 :                :  */
                                114                 :                : static inline void
                                115                 :        1068430 : proclist_delete_offset(proclist_head *list, int procno, size_t node_offset)
                                116                 :                : {
                                117                 :        1068430 :     proclist_node *node = proclist_node_get(procno, node_offset);
                                118                 :                : 
 2798 tgl@sss.pgh.pa.us         119   [ +  +  -  + ]:        1068430 :     Assert(node->next != 0 || node->prev != 0);
                                120                 :                : 
  552 heikki.linnakangas@i      121         [ +  + ]:        1068430 :     if (node->prev == INVALID_PROC_NUMBER)
                                122                 :                :     {
 2798 tgl@sss.pgh.pa.us         123         [ -  + ]:        1067165 :         Assert(list->head == procno);
 3309 rhaas@postgresql.org      124                 :        1067165 :         list->head = node->next;
                                125                 :                :     }
                                126                 :                :     else
                                127                 :           1265 :         proclist_node_get(node->prev, node_offset)->next = node->next;
                                128                 :                : 
  552 heikki.linnakangas@i      129         [ +  + ]:        1068430 :     if (node->next == INVALID_PROC_NUMBER)
                                130                 :                :     {
 2798 tgl@sss.pgh.pa.us         131         [ -  + ]:         885795 :         Assert(list->tail == procno);
 3309 rhaas@postgresql.org      132                 :         885795 :         list->tail = node->prev;
                                133                 :                :     }
                                134                 :                :     else
                                135                 :         182635 :         proclist_node_get(node->next, node_offset)->prev = node->prev;
                                136                 :                : 
 2798 tgl@sss.pgh.pa.us         137                 :        1068430 :     node->next = node->prev = 0;
 3210 rhaas@postgresql.org      138                 :        1068430 : }
                                139                 :                : 
                                140                 :                : /*
                                141                 :                :  * Check if a process is currently in a list.  It must be known that the
                                142                 :                :  * process is not in any _other_ proclist that uses the same proclist_node,
                                143                 :                :  * so that the only possibilities are that it is in this list or none.
                                144                 :                :  */
                                145                 :                : static inline bool
  961 peter@eisentraut.org      146                 :        6936526 : proclist_contains_offset(const proclist_head *list, int procno,
                                147                 :                :                          size_t node_offset)
                                148                 :                : {
                                149                 :        6936526 :     const proclist_node *node = proclist_node_get(procno, node_offset);
                                150                 :                : 
                                151                 :                :     /* If it's not in any list, it's definitely not in this one. */
 3210 rhaas@postgresql.org      152   [ +  +  +  + ]:        6936526 :     if (node->prev == 0 && node->next == 0)
                                153                 :        6590858 :         return false;
                                154                 :                : 
                                155                 :                :     /*
                                156                 :                :      * It must, in fact, be in this list.  Ideally, in assert-enabled builds,
                                157                 :                :      * we'd verify that.  But since this function is typically used while
                                158                 :                :      * holding a spinlock, crawling the whole list is unacceptable.  However,
                                159                 :                :      * we can verify matters in O(1) time when the node is a list head or
                                160                 :                :      * tail, and that seems worth doing, since in practice that should often
                                161                 :                :      * be enough to catch mistakes.
                                162                 :                :      */
  552 heikki.linnakangas@i      163   [ +  +  -  + ]:         345668 :     Assert(node->prev != INVALID_PROC_NUMBER || list->head == procno);
                                164   [ +  +  -  + ]:         345668 :     Assert(node->next != INVALID_PROC_NUMBER || list->tail == procno);
                                165                 :                : 
 2798 tgl@sss.pgh.pa.us         166                 :         345668 :     return true;
                                167                 :                : }
                                168                 :                : 
                                169                 :                : /*
                                170                 :                :  * Remove and return the first process from a list (there must be one).
                                171                 :                :  */
                                172                 :                : static inline PGPROC *
 3210 rhaas@postgresql.org      173                 :         254496 : proclist_pop_head_node_offset(proclist_head *list, size_t node_offset)
                                174                 :                : {
                                175                 :                :     PGPROC     *proc;
                                176                 :                : 
                                177         [ -  + ]:         254496 :     Assert(!proclist_is_empty(list));
                                178                 :         254496 :     proc = GetPGProcByNumber(list->head);
                                179                 :         254496 :     proclist_delete_offset(list, list->head, node_offset);
                                180                 :         254496 :     return proc;
                                181                 :                : }
                                182                 :                : 
                                183                 :                : /*
                                184                 :                :  * Helper macros to avoid repetition of offsetof(PGPROC, <member>).
                                185                 :                :  * 'link_member' is the name of a proclist_node member in PGPROC.
                                186                 :                :  */
                                187                 :                : #define proclist_delete(list, procno, link_member) \
                                188                 :                :     proclist_delete_offset((list), (procno), offsetof(PGPROC, link_member))
                                189                 :                : #define proclist_push_head(list, procno, link_member) \
                                190                 :                :     proclist_push_head_offset((list), (procno), offsetof(PGPROC, link_member))
                                191                 :                : #define proclist_push_tail(list, procno, link_member) \
                                192                 :                :     proclist_push_tail_offset((list), (procno), offsetof(PGPROC, link_member))
                                193                 :                : #define proclist_pop_head_node(list, link_member) \
                                194                 :                :     proclist_pop_head_node_offset((list), offsetof(PGPROC, link_member))
                                195                 :                : #define proclist_contains(list, procno, link_member) \
                                196                 :                :     proclist_contains_offset((list), (procno), offsetof(PGPROC, link_member))
                                197                 :                : 
                                198                 :                : /*
                                199                 :                :  * Iterate through the list pointed at by 'lhead', storing the current
                                200                 :                :  * position in 'iter'.  'link_member' is the name of a proclist_node member in
                                201                 :                :  * PGPROC.  Access the current position with iter.cur.
                                202                 :                :  *
                                203                 :                :  * The only list modification allowed while iterating is deleting the current
                                204                 :                :  * node with proclist_delete(list, iter.cur, node_offset).
                                205                 :                :  */
                                206                 :                : #define proclist_foreach_modify(iter, lhead, link_member)                   \
                                207                 :                :     for (AssertVariableIsOfTypeMacro(iter, proclist_mutable_iter),          \
                                208                 :                :          AssertVariableIsOfTypeMacro(lhead, proclist_head *),               \
                                209                 :                :          (iter).cur = (lhead)->head,                                     \
                                210                 :                :          (iter).next = (iter).cur == INVALID_PROC_NUMBER ? INVALID_PROC_NUMBER :    \
                                211                 :                :              proclist_node_get((iter).cur,                                  \
                                212                 :                :                                offsetof(PGPROC, link_member))->next;     \
                                213                 :                :          (iter).cur != INVALID_PROC_NUMBER;                                 \
                                214                 :                :          (iter).cur = (iter).next,                                          \
                                215                 :                :          (iter).next = (iter).cur == INVALID_PROC_NUMBER ? INVALID_PROC_NUMBER :    \
                                216                 :                :              proclist_node_get((iter).cur,                                  \
                                217                 :                :                                offsetof(PGPROC, link_member))->next)
                                218                 :                : 
                                219                 :                : #endif                          /* PROCLIST_H */
        

Generated by: LCOV version 2.4-beta