///////////////////////////////////////////////////////////////////////////////
-// Name: socket.cpp
-// Purpose: Socket handler classes
-// Authors: Guilhem Lavaux (completely rewritten from a basic API of Andrew
-// Davidson(1995) in wxWeb)
-// Created: April 1997
-// Updated: April 1999
+// Name: sckint.cpp
+// Purpose: Socket internal classes
+// Authors: Guilhem Lavaux
+// Created: April 1999
+// Updated:
// Copyright: (C) 1999, 1998, 1997, Guilhem Lavaux
// RCS_ID: $Id$
// License: see wxWindows license
#if wxUSE_THREADS
SocketWaiter::SocketWaiter(wxSocketBase *socket,
wxSocketInternal *internal)
- : m_socket(socket), m_internal(internal), m_fd(internal->GetFD())
+ : wxThread(),
+ m_socket(socket), m_internal(internal), m_fd(internal->GetFD())
{
}
// Else, no error => there is something to be read else
// we've lost the connection.
- if (ret > 0)
+ if (ret > 0) {
m_socket->OnRequest(wxSocketBase::EVT_READ);
- else {
+ } else {
m_socket->OnRequest(wxSocketBase::EVT_LOST);
Exit(NULL);
}
FD_ZERO(&sockrd_set);
FD_ZERO(&sockwr_set);
-
+
if ((m_socket->NeededReq() & READ_MASK) != 0)
FD_SET(m_fd, &sockrd_set);
if ((m_socket->NeededReq() & WRITE_MASK) != 0)
FD_SET(m_fd, &sockwr_set);
m_internal->AcquireFD();
- ret = select(FD_SETSIZE, &sockrd_set, &sockwr_set, NULL, &tv);
+ ret = select(m_fd+1, &sockrd_set, &sockwr_set, NULL, &tv);
m_internal->ReleaseFD();
if (FD_ISSET(m_fd, &sockrd_set))
SocketRequester::SocketRequester(wxSocketBase *socket,
wxSocketInternal *internal)
- : m_socket(socket), m_internal(internal), m_fd(internal->GetFD())
+ : wxThread(),
+ m_socket(socket), m_internal(internal), m_fd(internal->GetFD())
{
}
FD_SET(m_fd, &sockwr_set);
m_internal->AcquireFD();
- ret = select(FD_SETSIZE, &sockrd_set, &sockwr_set, NULL, &tv);
+ ret = select(m_fd+1, &sockrd_set, &sockwr_set, NULL, &tv);
m_internal->ReleaseFD();
return (ret != 0);
int ret;
size_t len;
+ if (!WaitFor(wxSocketBase::REQ_WRITE, req->timeout)) {
+ m_internal->EndRequest(req);
+ return;
+ }
+
m_internal->AcquireFD();
ret = send(m_fd, req->buffer, req->size, 0);
m_internal->ReleaseFD();
{
SockRequest *req;
+ m_internal->m_request_locker.Lock();
while (1) {
// Wait for a new request or a destroy message.
req = m_internal->WaitForReq();
m_internal->m_end_requester.Lock();
- if (TestDestroy() || req == NULL) {
+ if (req == NULL) {
m_internal->m_invalid_requester = TRUE;
m_internal->m_end_requester.Unlock();
+ m_internal->m_request_locker.Unlock();
return NULL;
}
m_internal->m_end_requester.Unlock();
m_thread_requester = NULL;
m_thread_waiter = NULL;
m_invalid_requester = TRUE;
- m_request_locker.Lock();
#endif
}
wxASSERT(m_thread_requester == NULL);
StopWaiter();
wxASSERT(m_thread_waiter == NULL);
- m_request_locker.Unlock();
#endif
}
node = m_requests.First();
if (node == NULL) {
- m_socket_cond.Wait(m_request_locker, 1, 0);
+ m_socket_cond.Wait(m_request_locker, 10, 0);
node = m_requests.First();
if (node == NULL)
#if wxUSE_THREADS
wxThreadError err;
- wxASSERT(m_thread_requester == NULL);
+ wxASSERT(m_thread_requester == NULL || m_invalid_requester);
m_end_requester.Lock();
if (m_invalid_requester) {
- delete m_thread_requester;
+ if (m_thread_requester != NULL)
+ delete m_thread_requester;
+ m_invalid_requester = FALSE;
+ }
+ m_end_requester.Unlock();
- m_thread_requester = new SocketRequester(m_socket, this);
- m_thread_requester->m_fd = m_socket->m_fd;
+ m_thread_requester = new SocketRequester(m_socket, this);
- err = m_thread_requester->Create();
- wxASSERT(err == wxTHREAD_NO_ERROR);
+ err = m_thread_requester->Create();
+ wxASSERT(err == wxTHREAD_NO_ERROR);
- err = m_thread_requester->Run();
- wxASSERT(err == wxTHREAD_NO_ERROR);
+ err = m_thread_requester->Run();
+ wxASSERT(err == wxTHREAD_NO_ERROR);
- m_invalid_requester = FALSE;
- }
- m_end_requester.Unlock();
#endif
}
void wxSocketInternal::StopRequester()
{
#if wxUSE_THREADS
+ m_end_requester.Lock();
if (m_invalid_requester) {
+ m_end_requester.Unlock();
delete m_thread_requester;
m_thread_requester = NULL;
m_invalid_requester = FALSE;
return;
}
+ m_end_requester.Unlock();
wxASSERT(m_thread_requester != NULL);
m_socket_locker.Lock();
// Send a signal to the requester.
- if (m_requests.Number() == 0)
- m_socket_cond.Signal();
+ m_socket_cond.Signal();
m_socket_locker.Unlock();
return;
m_thread_waiter = new SocketWaiter(m_socket, this);
- m_thread_waiter->m_fd = m_socket->m_fd;
err = m_thread_waiter->Create();
wxASSERT(err == wxTHREAD_NO_ERROR);
void wxSocketInternal::QueueRequest(SockRequest *request, bool async)
{
#if wxUSE_THREADS
+/*
+ if (m_invalid_requester)
+ ResumeRequester();
+*/
+ m_thread_requester = new SocketRequester(m_socket, this);
+
+/*
if (async) {
- if (m_thread_requester == NULL)
- ResumeRequester();
m_request_locker.Lock();
request->done = FALSE;
}
}
} else {
+*/
m_request_locker.Lock();
if ((request->type & wxSocketBase::REQ_WAIT) != 0) {
}
request->done = TRUE;
m_request_locker.Unlock();
- }
+// }
+ delete m_thread_requester;
+ m_thread_requester = NULL;
#endif
}
}
#if HAVE_THREAD_CLEANUP_FUNCTIONS
// Install the cleanup handler.
- pthread_cleanup_push(wxThreadInternal::PthreadCleanup, ptr);
+// pthread_cleanup_push(wxThreadInternal::PthreadCleanup, ptr);
#endif
// wait for the condition to be signaled from Run()
// mutex state: currently locked by the thread which created us
pthread->m_cond.Wait(pthread->m_mutex);
-
// mutex state: locked again on exit of Wait()
// call the main entry
status = thread->Entry();
#if HAVE_THREAD_CLEANUP_FUNCTIONS
- pthread_cleanup_pop(FALSE);
+// pthread_cleanup_pop(FALSE);
#endif
// terminate the thread
// unlocked in the very end
m_mutex.Lock();
+ // this mutex is used by wxThreadInternal::Wait() and by
+ // wxThreadInternal::SignalExit(). We don't use m_mutex because of a
+ // possible deadlock in either Wait() or SignalExit().
m_end_mutex.Lock();
// this mutex is used in Pause()/Resume() and is also locked all the time
// note that m_mutex will be unlocked by the thread which waits for our
// termination
+
+ // m_end_mutex can be unlocked here.
m_end_mutex.Unlock();
}
if ( wxThread::IsMain() )
wxMutexGuiLeave();
- printf("Entering wait ...\n");
// entering Wait() releases the mutex thus allowing SignalExit() to acquire
// it and to signal us its termination
m_cond.Wait(m_end_mutex);
- printf("Exiting wait ...\n");
// mutex is still in the locked state - relocked on exit from Wait(), so
// unlock it - we don't need it any more, the thread has already terminated
m_end_mutex.Unlock();
+ // After that, we wait for the real end of the other thread.
+ pthread_join(GetId(), NULL);
+
// reacquire GUI mutex
if ( wxThread::IsMain() )
wxMutexGuiEnter();
void wxThreadInternal::SignalExit()
{
- printf("SignalExit\n");
// GL: Unlock mutexSuspend here.
m_mutexSuspend.Unlock();
// as mutex is currently locked, this will block until some other thread
// (normally the same which created this one) unlocks it by entering Wait()
m_end_mutex.Lock();
- printf("Mutex acquired\n");
// wake up all the threads waiting for our termination
m_cond.Broadcast();
// after this call mutex will be finally unlocked
m_end_mutex.Unlock();
- printf("Mutex unacquired\n");
}
void wxThreadInternal::Pause()
{
m_critsect.Enter();
wxThreadState state = p_internal->GetState();
- m_critsect.Leave();
// ask the thread to stop
p_internal->SetCancelFlag();
+ m_critsect.Leave();
+
switch ( state )
{
case STATE_NEW:
{
// first call user-level clean up code
OnExit();
- printf(" ... OnExit()\n");
// next wake up the threads waiting for us (OTOH, this function won't return
// until someone waited for us!)
p_internal->SignalExit();
- printf(" ... SignalExit()\n");
p_internal->SetState(STATE_EXITED);
- printf(" ... SetState()\n");
// delete both C++ thread object and terminate the OS thread object
// GL: This is very ugly and buggy ...
// delete this;
- printf(" ... Exit\n");
pthread_exit(status);
}