/* [<][>][^][v][top][bottom][index][help] */
1 /****************************************************************************
2 * Copyright (C) 2006-2008 by Jason Ansel, Kapil Arya, and Gene Cooperman *
3 * jansel@csail.mit.edu, kapil@ccs.neu.edu, gene@ccs.neu.edu *
4 * *
5 * This file is part of the dmtcp/src module of DMTCP (DMTCP:dmtcp/src). *
6 * *
7 * DMTCP:dmtcp/src is free software: you can redistribute it and/or *
8 * modify it under the terms of the GNU Lesser General Public License as *
9 * published by the Free Software Foundation, either version 3 of the *
10 * License, or (at your option) any later version. *
11 * *
12 * DMTCP:dmtcp/src is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU Lesser General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU Lesser General Public *
18 * License along with DMTCP:dmtcp/src. If not, see *
19 * <http://www.gnu.org/licenses/>. *
20 ****************************************************************************/
21
22 #pragma once
23 #ifndef CONNECTIONLIST_H
24 #define CONNECTIONLIST_H
25
26 #include <pthread.h>
27 #include "dmtcpalloc.h"
28 #include "protectedfds.h"
29 #include "connection.h"
30 #include "jserialize.h"
31 #include "jalloc.h"
32
33 namespace dmtcp
34 {
35 class ConnectionList
36 {
37 public:
38 #ifdef JALIB_ALLOCATOR
39 static void* operator new(size_t nbytes, void* p) { return p; }
40 static void* operator new(size_t nbytes) { JALLOC_HELPER_NEW(nbytes); }
41 static void operator delete(void* p) { JALLOC_HELPER_DELETE(p); }
42 #endif
43 typedef map<ConnectionIdentifier, Connection*>::iterator iterator;
44
45 ConnectionList() {
46 numIncomingCons = 0;
47 JASSERT(pthread_mutex_init(&_lock, NULL) == 0);}
48 virtual ~ConnectionList();
49
50 void resetOnFork();
51 void deleteStaleConnections();
52
53 void add(int fd, Connection* c);
54 void erase(iterator i);
55 void erase(ConnectionIdentifier& key);
56 Connection *getConnection(const ConnectionIdentifier &id);
57 Connection *getConnection(int fd);
58 void processClose(int fd);
59 void processDup(int oldfd, int newfd);
60 void list();
61 void serialize(jalib::JBinarySerializer& o);
62
63 void eventHook(DmtcpEvent_t event, DmtcpEventData_t *data);
64 virtual void scanForPreExisting() {}
65 virtual void preLockSaveOptions();
66 virtual void preCkptFdLeaderElection();
67 virtual void drain();
68 virtual void preCkpt();
69 virtual void postRestart();
70 virtual void registerNSData(bool isRestart) {}
71 virtual void sendQueries(bool isRestart) {}
72 virtual void refill(bool isRestart);
73 virtual void resume(bool isRestart);
74
75 void registerIncomingCons();
76 void determineOutgoingCons();
77 void sendReceiveMissingFds();
78 virtual int protectedFd() = 0;
79
80 protected:
81 virtual Connection *createDummyConnection(int type) = 0;
82 iterator begin() { return _connections.begin(); }
83 iterator end() { return _connections.end(); }
84
85 private:
86 void processCloseWork(int fd);
87 void _lock_tbl() {
88 JASSERT(_real_pthread_mutex_lock(&_lock) == 0) (JASSERT_ERRNO);
89 }
90 void _unlock_tbl() {
91 JASSERT(_real_pthread_mutex_unlock(&_lock) == 0) (JASSERT_ERRNO);
92 }
93
94 pthread_mutex_t _lock;
95 typedef map<ConnectionIdentifier, Connection*> ConnectionMapT;
96 ConnectionMapT _connections;
97
98 typedef map<int, Connection*> FdToConMapT;
99 FdToConMapT _fdToCon;
100
101 size_t numIncomingCons;
102 };
103 }
104 #endif