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

Generated by: LCOV version 1.14