Make Select a singleton

It's going to manipulate process-global signal state, so multiple
instances do not make sense.
This commit is contained in:
Keegan McAllister
2012-05-04 23:37:07 -04:00
committed by Keith Winstein
parent bb651581a7
commit 768d4ce797
9 changed files with 45 additions and 8 deletions
+1 -1
View File
@@ -29,7 +29,7 @@ termemu_LDADD = ../terminal/libmoshterminal.a ../util/libmoshutil.a ../statesync
ntester_SOURCES = ntester.cc ntester_SOURCES = ntester.cc
ntester_CPPFLAGS = -I$(srcdir)/../util -I$(srcdir)/../statesync -I$(srcdir)/../terminal -I$(srcdir)/../network -I$(srcdir)/../crypto -I../protobufs $(protobuf_CFLAGS) ntester_CPPFLAGS = -I$(srcdir)/../util -I$(srcdir)/../statesync -I$(srcdir)/../terminal -I$(srcdir)/../network -I$(srcdir)/../crypto -I../protobufs $(protobuf_CFLAGS)
ntester_LDADD = ../statesync/libmoshstatesync.a ../terminal/libmoshterminal.a ../network/libmoshnetwork.a ../crypto/libmoshcrypto.a ../protobufs/libmoshprotos.a -lutil -lm $(protobuf_LIBS) ntester_LDADD = ../statesync/libmoshstatesync.a ../terminal/libmoshterminal.a ../network/libmoshnetwork.a ../crypto/libmoshcrypto.a ../protobufs/libmoshprotos.a ../util/libmoshutil.a -lutil -lm $(protobuf_LIBS)
benchmark_SOURCES = benchmark.cc benchmark_SOURCES = benchmark.cc
benchmark_CPPFLAGS = -I$(srcdir)/../util -I$(srcdir)/../statesync -I$(srcdir)/../terminal -I../protobufs -I$(srcdir)/../frontend -I$(srcdir)/../crypto -I$(srcdir)/../network $(STDDJB_CPPFLAGS) $(protobuf_CFLAGS) benchmark_CPPFLAGS = -I$(srcdir)/../util -I$(srcdir)/../statesync -I$(srcdir)/../terminal -I../protobufs -I$(srcdir)/../frontend -I$(srcdir)/../crypto -I$(srcdir)/../network $(STDDJB_CPPFLAGS) $(protobuf_CFLAGS)
+2 -2
View File
@@ -58,7 +58,7 @@ int main( int argc, char *argv[] )
fprintf( stderr, "Port bound is %d, key is %s\n", n->port(), n->get_key().c_str() ); fprintf( stderr, "Port bound is %d, key is %s\n", n->port(), n->get_key().c_str() );
if ( server ) { if ( server ) {
Select sel; Select &sel = Select::get_instance();
sel.add_fd( n->fd() ); sel.add_fd( n->fd() );
uint64_t last_num = n->get_remote_state_num(); uint64_t last_num = n->get_remote_state_num();
while ( true ) { while ( true ) {
@@ -100,7 +100,7 @@ int main( int argc, char *argv[] )
exit( 1 ); exit( 1 );
} }
Select sel; Select &sel = Select::get_instance();
sel.add_fd( STDIN_FILENO ); sel.add_fd( STDIN_FILENO );
sel.add_fd( n->fd() ); sel.add_fd( n->fd() );
+1 -1
View File
@@ -122,7 +122,7 @@ void emulate_terminal( int fd )
{ {
Parser::UTF8Parser parser; Parser::UTF8Parser parser;
Select sel; Select &sel = Select::get_instance();
sel.add_fd( STDIN_FILENO ); sel.add_fd( STDIN_FILENO );
sel.add_fd( fd ); sel.add_fd( fd );
+1 -1
View File
@@ -227,7 +227,7 @@ void emulate_terminal( int fd )
/* open display */ /* open display */
Terminal::Display display( true ); /* use TERM to initialize */ Terminal::Display display( true ); /* use TERM to initialize */
Select sel; Select &sel = Select::get_instance();
sel.add_fd( STDIN_FILENO ); sel.add_fd( STDIN_FILENO );
sel.add_fd( fd ); sel.add_fd( fd );
sel.add_fd( signal_fd ); sel.add_fd( signal_fd );
+1 -1
View File
@@ -479,7 +479,7 @@ void serve( int host_fd, Terminal::Complete &terminal, ServerConnection &network
fatal_assert( sigfd_trap( SIGINT ) == 0 ); fatal_assert( sigfd_trap( SIGINT ) == 0 );
/* prepare to poll for events */ /* prepare to poll for events */
Select sel; Select &sel = Select::get_instance();
sel.add_fd( network.fd() ); sel.add_fd( network.fd() );
sel.add_fd( host_fd ); sel.add_fd( host_fd );
sel.add_fd( signal_fd ); sel.add_fd( signal_fd );
+1 -1
View File
@@ -299,7 +299,7 @@ void STMClient::main( void )
main_init(); main_init();
/* prepare to poll for events */ /* prepare to poll for events */
Select sel; Select &sel = Select::get_instance();
sel.add_fd( network->fd() ); sel.add_fd( network->fd() );
sel.add_fd( STDIN_FILENO ); sel.add_fd( STDIN_FILENO );
sel.add_fd( signal_fd ); sel.add_fd( signal_fd );
+1 -1
View File
@@ -2,7 +2,7 @@ AM_CXXFLAGS = $(WARNING_CXXFLAGS) $(PICKY_CXXFLAGS) $(HARDEN_CFLAGS) $(MISC_CXXF
noinst_LIBRARIES = libmoshutil.a noinst_LIBRARIES = libmoshutil.a
libmoshutil_a_SOURCES = locale_utils.cc locale_utils.h swrite.cc swrite.h dos_assert.h fatal_assert.h sigfd.h select.h libmoshutil_a_SOURCES = locale_utils.cc locale_utils.h swrite.cc swrite.h dos_assert.h fatal_assert.h sigfd.h select.h select.cc
if !USE_LIBSTDDJB if !USE_LIBSTDDJB
libmoshutil_a_SOURCES += sigfd.cc libmoshutil_a_SOURCES += sigfd.cc
+21
View File
@@ -0,0 +1,21 @@
/*
Mosh: the mobile shell
Copyright 2012 Keith Winstein
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "select.h"
Select *Select::instance = NULL;
+16
View File
@@ -33,6 +33,17 @@ static fd_set dummy_fd_set;
/* Convenience wrapper for select(2). */ /* Convenience wrapper for select(2). */
class Select { class Select {
public: public:
static Select &get_instance( void ) {
// NB: not thread-safe
if ( !instance ) {
instance = new Select;
}
return *instance;
}
private:
static Select *instance;
Select() Select()
: max_fd( -1 ) : max_fd( -1 )
@@ -47,6 +58,11 @@ public:
FD_ZERO( &error_fds ); FD_ZERO( &error_fds );
} }
/* not implemented */
Select( const Select & );
Select &operator=( const Select & );
public:
void add_fd( int fd ) void add_fd( int fd )
{ {
if ( fd > max_fd ) { if ( fd > max_fd ) {