mirror of
git://xwords.git.sourceforge.net/gitroot/xwords/xwords
synced 2025-02-05 20:45:49 +01:00
use rwlock instead of mutex
This commit is contained in:
parent
176e2bf68d
commit
7723fa5e19
1 changed files with 32 additions and 6 deletions
|
@ -46,7 +46,7 @@ XWThreadPool::GetTPool()
|
||||||
|
|
||||||
XWThreadPool::XWThreadPool()
|
XWThreadPool::XWThreadPool()
|
||||||
{
|
{
|
||||||
pthread_mutex_init ( &m_activeSocketsMutex, NULL );
|
pthread_rwlock_init( &m_activeSocketsRWLock, NULL );
|
||||||
pthread_mutex_init ( &m_queueMutex, NULL );
|
pthread_mutex_init ( &m_queueMutex, NULL );
|
||||||
|
|
||||||
pthread_cond_init( &m_queueCondVar, NULL );
|
pthread_cond_init( &m_queueCondVar, NULL );
|
||||||
|
@ -84,27 +84,53 @@ XWThreadPool::AddSocket( int socket )
|
||||||
{
|
{
|
||||||
logf( "AddSocket(%d)", socket );
|
logf( "AddSocket(%d)", socket );
|
||||||
{
|
{
|
||||||
MutexLock ml( &m_activeSocketsMutex );
|
RWWriteLock ml( &m_activeSocketsRWLock );
|
||||||
m_activeSockets.push_back( socket );
|
m_activeSockets.push_back( socket );
|
||||||
}
|
}
|
||||||
interrupt_poll();
|
interrupt_poll();
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
int
|
||||||
XWThreadPool::RemoveSocket( int socket )
|
XWThreadPool::RemoveSocket( int socket )
|
||||||
{
|
{
|
||||||
|
int found = 0;
|
||||||
{
|
{
|
||||||
MutexLock ml( &m_activeSocketsMutex );
|
RWWriteLock ml( &m_activeSocketsRWLock );
|
||||||
|
|
||||||
vector<int>::iterator iter = m_activeSockets.begin();
|
vector<int>::iterator iter = m_activeSockets.begin();
|
||||||
while ( iter != m_activeSockets.end() ) {
|
while ( iter != m_activeSockets.end() ) {
|
||||||
if ( *iter == socket ) {
|
if ( *iter == socket ) {
|
||||||
m_activeSockets.erase( iter );
|
m_activeSockets.erase( iter );
|
||||||
|
found = 1;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
++iter;
|
++iter;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return found;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void
|
||||||
|
XWThreadPool::CloseSocket( int socket )
|
||||||
|
{
|
||||||
|
int do_interrupt = 0;
|
||||||
|
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 = 1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
++iter;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
close( socket );
|
||||||
|
if ( do_interrupt ) {
|
||||||
|
interrupt_poll();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
|
@ -189,7 +215,7 @@ XWThreadPool::real_listener()
|
||||||
|
|
||||||
for ( ; ; ) {
|
for ( ; ; ) {
|
||||||
|
|
||||||
pthread_mutex_lock( &m_activeSocketsMutex );
|
pthread_rwlock_rdlock( &m_activeSocketsRWLock );
|
||||||
int nSockets = m_activeSockets.size() + 1; /* for pipe */
|
int nSockets = m_activeSockets.size() + 1; /* for pipe */
|
||||||
pollfd* fds = (pollfd*)malloc( sizeof(fds[0]) * nSockets );
|
pollfd* fds = (pollfd*)malloc( sizeof(fds[0]) * nSockets );
|
||||||
pollfd* curfd = fds;
|
pollfd* curfd = fds;
|
||||||
|
@ -209,7 +235,7 @@ XWThreadPool::real_listener()
|
||||||
len += sprintf( log+len, "%d,", curfd->fd );
|
len += sprintf( log+len, "%d,", curfd->fd );
|
||||||
++curfd;
|
++curfd;
|
||||||
}
|
}
|
||||||
pthread_mutex_unlock( &m_activeSocketsMutex );
|
pthread_rwlock_unlock( &m_activeSocketsRWLock );
|
||||||
|
|
||||||
logf( "calling poll on %s", log );
|
logf( "calling poll on %s", log );
|
||||||
int nEvents = poll( fds, nSockets, -1 ); /* -1: infinite timeout */
|
int nEvents = poll( fds, nSockets, -1 ); /* -1: infinite timeout */
|
||||||
|
|
Loading…
Add table
Reference in a new issue