LCOV - code coverage report
Current view: top level - root/contrail/src/contrail-common/io - server_manager.h (source / functions) Hit Total Coverage
Test: OpenSDN C/C++ coverage (all TARGET_SET jobs) Lines: 12 12 100.0 %
Date: 2026-11-09 02:10:49 Functions: 7 7 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2014 Juniper Networks, Inc. All rights reserved.
       3             :  * Copyright Contributors to the OpenSDN project
       4             :  */
       5             : 
       6             : #ifndef SRC_IO_SERVER_MANAGER_H_
       7             : #define SRC_IO_SERVER_MANAGER_H_
       8             : 
       9             : #include <set>
      10             : #include <mutex>
      11             : 
      12             : //
      13             : // ServerManager is the place holder for all the TcpServer and UdpServer
      14             : // objects instantiated in the life time of a process
      15             : //
      16             : // TcpServer and UdpServer objects are help in ServerSet until all the cleanup
      17             : // is complete and only then should they be deleted via DeleteServer() API
      18             : //
      19             : // Since TcpServer and UdpServer bjects are also held by boost::asio routines,
      20             : // they are protected using intrusive pointers
      21             : //
      22             : // This is similar to how TcpSession objects are managed via TcpSessionPtr
      23             : //
      24             : template <typename ServerType, typename ServerPtrType>
      25             : class ServerManager {
      26             : public:
      27             :     //
      28             :     // Add a server object to the data base, by creating an intrusive reference
      29             :     //
      30       17569 :     static void AddServer(ServerType *server) {
      31       17569 :         std::scoped_lock lock(mutex_);
      32       17569 :         server_ref_.insert(ServerPtrType(server));
      33       17569 :     }
      34             :     //
      35             :     // Delete a server object from the data base, by removing the intrusive
      36             :     // reference. If any other objects has a reference to this server such as
      37             :     // boost::asio, the server object deletion is automatically deferred
      38             :     //
      39       17461 :     static void DeleteServer(ServerType *server) {
      40       17461 :         std::scoped_lock lock(mutex_);
      41       17461 :         server_ref_.erase(ServerPtrType(server));
      42       17461 :     }
      43             :     //
      44             :     // Return number of Servers.
      45             :     // Used in tests to ensure there are no leaks.
      46             :     //
      47        1243 :     size_t GetServerCount() {
      48        1243 :         return server_ref_.size();
      49             :     }
      50             : 
      51             : private:
      52             :     struct ServerPtrCmp {
      53      105747 :         bool operator()(const ServerPtrType &lhs,
      54             :                         const ServerPtrType &rhs) const {
      55      105747 :             return lhs.get() < rhs.get();
      56             :         }
      57             :     };
      58             :     typedef std::set<ServerPtrType, ServerPtrCmp> ServerSet;
      59             : 
      60             :     static std::mutex mutex_;
      61             :     static ServerSet server_ref_;
      62             : };
      63             : 
      64             : template <typename ServerType, typename ServerPtrType>
      65             : typename ServerManager<ServerType, ServerPtrType>::ServerSet
      66             :     ServerManager<ServerType, ServerPtrType>::server_ref_;
      67             : 
      68             : template <typename ServerType, typename ServerPtrType>
      69             : std::mutex ServerManager<ServerType, ServerPtrType>::mutex_;
      70             : 
      71             : #endif  // SRC_IO_SERVER_MANAGER_H_

Generated by: LCOV version 1.14