xwords/xwords4/relay/tpool.cpp

379 lines
10 KiB
C++
Raw Normal View History

2005-03-31 04:11:51 +02:00
/* -*-mode: C; fill-column: 78; c-basic-offset: 4; -*- */
/*
2009-07-30 14:54:17 +02:00
* Copyright 2005-2009 by Eric House (xwords@eehouse.org). All rights
* reserved.
2005-03-31 04:11:51 +02:00
*
* 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 2
* 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, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <assert.h>
2006-03-21 05:05:33 +01:00
#include <stdio.h>
#include <unistd.h>
2005-03-31 04:11:51 +02:00
#include <pthread.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/poll.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
2005-03-31 04:11:51 +02:00
#include "tpool.h"
#include "xwrelay_priv.h"
#include "xwrelay.h"
#include "timermgr.h"
2005-03-31 04:11:51 +02:00
#include "mlock.h"
XWThreadPool* XWThreadPool::g_instance = NULL;
/* static */ XWThreadPool*
XWThreadPool::GetTPool()
{
XWThreadPool* me = g_instance;
if ( me == NULL ) {
me = new XWThreadPool();
g_instance = me;
}
return me;
}
XWThreadPool::XWThreadPool()
2009-07-13 04:58:16 +02:00
: m_timeToDie(false)
2005-10-23 17:49:48 +02:00
, m_nThreads(0)
2005-03-31 04:11:51 +02:00
{
2005-04-20 14:06:25 +02:00
pthread_rwlock_init( &m_activeSocketsRWLock, NULL );
2005-03-31 04:11:51 +02:00
pthread_mutex_init ( &m_queueMutex, NULL );
pthread_cond_init( &m_queueCondVar, NULL );
int fd[2];
if ( pipe( fd ) ) {
logf( XW_LOGERROR, "pipe failed" );
2005-03-31 04:11:51 +02:00
}
m_pipeRead = fd[0];
m_pipeWrite = fd[1];
logf( XW_LOGINFO, "pipes: m_pipeRead: %d; m_pipeWrite: %d",
m_pipeRead, m_pipeWrite );
2005-03-31 04:11:51 +02:00
}
2005-10-23 17:49:48 +02:00
XWThreadPool::~XWThreadPool()
{
pthread_cond_destroy( &m_queueCondVar );
pthread_rwlock_destroy( &m_activeSocketsRWLock );
pthread_mutex_destroy ( &m_queueMutex );
} /* ~XWThreadPool */
2005-03-31 04:11:51 +02:00
void
XWThreadPool::Setup( int nThreads, packet_func pFunc )
{
m_nThreads = nThreads;
m_pFunc = pFunc;
pthread_t thread;
int ii;
for ( ii = 0; ii < nThreads; ++ii ) {
2005-03-31 04:11:51 +02:00
int result = pthread_create( &thread, NULL, tpool_main, this );
assert( result == 0 );
2005-10-23 17:49:48 +02:00
pthread_detach( thread );
2005-03-31 04:11:51 +02:00
}
int result = pthread_create( &thread, NULL, listener_main, this );
assert( result == 0 );
}
2005-10-23 17:49:48 +02:00
void
XWThreadPool::Stop()
{
2009-07-13 04:58:16 +02:00
m_timeToDie = true;
2005-10-23 17:49:48 +02:00
int ii;
for ( ii = 0; ii < m_nThreads; ++ii ) {
2005-10-23 17:49:48 +02:00
enqueue( 0 );
}
interrupt_poll();
}
2005-03-31 04:11:51 +02:00
void
XWThreadPool::AddSocket( int socket )
{
logf( XW_LOGINFO, "%s(%d)", __func__, socket );
2005-03-31 04:11:51 +02:00
{
2005-04-20 14:06:25 +02:00
RWWriteLock ml( &m_activeSocketsRWLock );
2005-03-31 04:11:51 +02:00
m_activeSockets.push_back( socket );
}
interrupt_poll();
}
bool
2005-03-31 04:11:51 +02:00
XWThreadPool::RemoveSocket( int socket )
{
bool found = false;
2005-03-31 04:11:51 +02:00
{
2005-04-20 14:06:25 +02:00
RWWriteLock ml( &m_activeSocketsRWLock );
2005-03-31 04:11:51 +02:00
vector<int>::iterator iter = m_activeSockets.begin();
while ( iter != m_activeSockets.end() ) {
if ( *iter == socket ) {
m_activeSockets.erase( iter );
found = true;
2005-04-20 14:06:25 +02:00
break;
}
++iter;
}
}
return found;
} /* RemoveSocket */
2005-04-20 14:06:25 +02:00
void
XWThreadPool::CloseSocket( int socket )
{
/* bool do_interrupt = false; */
2005-04-20 14:06:25 +02:00
if ( !RemoveSocket( socket ) ) {
RWWriteLock rwl( &m_activeSocketsRWLock );
deque<int>::iterator iter = m_queue.begin();
while ( iter != m_queue.end() ) {
if ( *iter == socket ) {
m_queue.erase( iter );
/* do_interrupt = true; */
2005-03-31 04:11:51 +02:00
break;
}
++iter;
}
}
logf( XW_LOGINFO, "CLOSING socket %d", socket );
close( socket );
/* if ( do_interrupt ) { */
/* We always need to interrupt the poll because the socket we're closing
will be in the list being listened to. That or we need to drop sockets
that have been removed on some other thread while the poll call's
blocking.*/
interrupt_poll();
/* } */
2005-03-31 04:11:51 +02:00
}
bool
2005-03-31 04:11:51 +02:00
XWThreadPool::get_process_packet( int socket )
{
bool success = false;
2005-03-31 04:11:51 +02:00
short packetSize;
assert( sizeof(packetSize) == 2 );
ssize_t nRead = recv( socket, &packetSize,
sizeof(packetSize), MSG_WAITALL );
if ( nRead != 2 ) {
killSocket( socket, "nRead != 2" );
} else {
packetSize = ntohs( packetSize );
if ( packetSize < 0 || packetSize > MAX_MSG_LEN ) {
killSocket( socket, "packetSize wrong" );
} else {
unsigned char buf[MAX_MSG_LEN];
nRead = recv( socket, buf, packetSize, MSG_WAITALL );
if ( nRead != packetSize ) {
killSocket( socket, "nRead != packetSize" );
} else {
logf( XW_LOGINFO, "read %d bytes", nRead );
logf( XW_LOGINFO, "calling m_pFunc" );
success = (*m_pFunc)( buf, packetSize, socket );
}
}
2005-03-31 04:11:51 +02:00
}
return success;
2005-03-31 04:11:51 +02:00
} /* get_process_packet */
/* static */ void*
XWThreadPool::tpool_main( void* closure )
{
XWThreadPool* me = (XWThreadPool*)closure;
2005-04-08 16:28:04 +02:00
return me->real_tpool_main();
2005-03-31 04:11:51 +02:00
}
2005-04-08 16:28:04 +02:00
void*
2005-03-31 04:11:51 +02:00
XWThreadPool::real_tpool_main()
{
2005-10-23 17:49:48 +02:00
logf( XW_LOGINFO, "tpool worker thread starting" );
2005-03-31 04:11:51 +02:00
for ( ; ; ) {
pthread_mutex_lock( &m_queueMutex );
2005-10-23 17:49:48 +02:00
while ( !m_timeToDie && m_queue.size() == 0 ) {
2005-03-31 04:11:51 +02:00
pthread_cond_wait( &m_queueCondVar, &m_queueMutex );
}
2005-10-23 17:49:48 +02:00
if ( m_timeToDie ) {
break;
}
2005-03-31 04:11:51 +02:00
int socket = m_queue.front();
m_queue.pop_front();
pthread_mutex_unlock( &m_queueMutex );
logf( XW_LOGINFO, "worker thread got socket %d from queue", socket );
2005-03-31 04:11:51 +02:00
if ( get_process_packet( socket ) ) {
AddSocket( socket );
} /* else drop it: error */
}
2005-10-23 17:49:48 +02:00
logf( XW_LOGINFO, "tpool worker thread exiting" );
2005-04-08 16:28:04 +02:00
return NULL;
2005-03-31 04:11:51 +02:00
}
void
XWThreadPool::interrupt_poll()
{
logf( XW_LOGINFO, __func__ );
2005-03-31 04:11:51 +02:00
unsigned char byt = 0;
int nSent = write( m_pipeWrite, &byt, 1 );
if ( nSent != 1 ) {
logf( XW_LOGERROR, "errno = %s (%d)", strerror(errno), errno );
2005-03-31 04:11:51 +02:00
}
}
2005-04-08 16:28:04 +02:00
void*
2005-03-31 04:11:51 +02:00
XWThreadPool::real_listener()
{
int flags = POLLIN | POLLERR | POLLHUP;
TimerMgr* tmgr = TimerMgr::GetTimerMgr();
int nSocketsAllocd = 1;
struct pollfd* fds = (pollfd*)calloc( nSocketsAllocd, sizeof(fds[0]) );
2009-08-20 05:50:40 +02:00
#ifdef LOG_POLL
char* log = (char*)malloc( 4 * nSocketsAllocd );
2009-08-20 05:50:40 +02:00
#endif
2005-03-31 04:11:51 +02:00
for ( ; ; ) {
2005-04-20 14:06:25 +02:00
pthread_rwlock_rdlock( &m_activeSocketsRWLock );
2005-03-31 04:11:51 +02:00
int nSockets = m_activeSockets.size() + 1; /* for pipe */
2009-08-20 05:50:40 +02:00
#ifdef LOG_POLL
int logCapacity = 4 * nSockets;
int logLen = 0;
2009-08-20 05:50:40 +02:00
#endif
if ( nSockets > nSocketsAllocd ) {
fds = (struct pollfd*)realloc( fds, nSockets * sizeof(fds[0]) );
2009-08-20 05:50:40 +02:00
#ifdef LOG_POLL
log = (char*)realloc( log, nSockets * 4 );
2009-08-20 05:50:40 +02:00
#endif
nSocketsAllocd = nSockets;
}
struct pollfd* curfd = fds;
2005-03-31 04:11:51 +02:00
curfd->fd = m_pipeRead;
curfd->events = flags;
2009-08-20 05:50:40 +02:00
#ifdef LOG_POLL
logLen += snprintf( log+logLen, logCapacity - logLen, "%d,", curfd->fd );
2009-08-20 05:50:40 +02:00
#endif
2005-03-31 04:11:51 +02:00
++curfd;
vector<int>::iterator iter = m_activeSockets.begin();
while ( iter != m_activeSockets.end() ) {
2005-03-31 04:11:51 +02:00
curfd->fd = *iter++;
curfd->events = flags;
2009-08-20 05:50:40 +02:00
#ifdef LOG_POLL
if ( logCapacity > logLen ) {
logLen += snprintf( log+logLen, logCapacity - logLen, "%d,",
curfd->fd );
}
2009-08-20 05:50:40 +02:00
#endif
assert( curfd < fds + nSockets );
2005-03-31 04:11:51 +02:00
++curfd;
}
2005-04-20 14:06:25 +02:00
pthread_rwlock_unlock( &m_activeSocketsRWLock );
2005-03-31 04:11:51 +02:00
int nMillis = tmgr->GetPollTimeout();
2009-08-20 05:50:40 +02:00
#ifdef LOG_POLL
logf( XW_LOGINFO, "polling %s nmillis=%d", log, nMillis );
2009-08-20 05:50:40 +02:00
#endif
int nEvents = poll( fds, nSockets, nMillis );
logf( XW_LOGINFO, "back from poll: %d", nEvents );
2005-10-23 17:49:48 +02:00
if ( m_timeToDie ) {
break;
}
if ( nEvents == 0 ) {
tmgr->FireElapsedTimers();
} else if ( nEvents < 0 ) {
logf( XW_LOGERROR, "poll failed: errno: %s (%d)",
strerror(errno), errno );
}
2005-03-31 04:11:51 +02:00
if ( fds[0].revents != 0 ) {
logf( XW_LOGINFO, "poll interrupted" );
2005-03-31 04:11:51 +02:00
assert( fds[0].revents == POLLIN );
unsigned char byt;
read( fds[0].fd, &byt, 1 );
--nEvents;
}
if ( nEvents > 0 ) {
--nSockets;
curfd = &fds[1];
int ii;
for ( ii = 0; ii < nSockets && nEvents > 0; ++ii ) {
2005-03-31 04:11:51 +02:00
if ( curfd->revents != 0 ) {
int socket = curfd->fd;
if ( !RemoveSocket( socket ) ) {
/* no further processing if it's been removed while
we've been sleeping in poll */
2005-07-05 23:52:24 +02:00
--nEvents;
continue;
}
if ( 0 != (curfd->revents & (POLLIN | POLLPRI)) ) {
logf( XW_LOGINFO, "enqueuing %d", socket );
enqueue( socket );
} else {
logf( XW_LOGERROR, "odd revents: %x", curfd->revents );
killSocket( socket, "error/hup in poll()" );
}
2005-03-31 04:11:51 +02:00
--nEvents;
}
++curfd;
}
assert( nEvents == 0 );
}
}
2005-10-23 17:49:48 +02:00
logf( XW_LOGINFO, "real_listener returning" );
2005-04-08 16:28:04 +02:00
return NULL;
2005-03-31 04:11:51 +02:00
} /* real_listener */
/* static */ void*
XWThreadPool::listener_main( void* closure )
{
XWThreadPool* me = (XWThreadPool*)closure;
2005-04-08 16:28:04 +02:00
return me->real_listener();
2005-03-31 04:11:51 +02:00
}
void
XWThreadPool::enqueue( int socket )
{
MutexLock ml( &m_queueMutex );
m_queue.push_back( socket );
logf( XW_LOGINFO, "calling pthread_cond_signal" );
2005-03-31 04:11:51 +02:00
pthread_cond_signal( &m_queueCondVar );
/* implicit unlock */
}