Jim Carlson sez:
>> Are calls to suspend and resume ref counted? iow, if i do this in
>> pseudo-code:
>
>No.  It just does a SIGSTOP and SIGCONT for suspend and resume.  The
>first resume after N>=1 suspends will cause the task to restart.
>
>> is that the way it works for pthreads?
>
>Pthreads doesn't really have suspend/resume.  Just SIGSTOP and
>SIGCONT.

I agree with you, but it even gets worse than that. Let me elaborate.
The signal handling under LinuxThreads is different from the signal
handling under POSIX threads for one. And, as far as I can tell, the
signal handling is cooperative, and the current thread implementation
does not cooperative signal handling. The code for suspend and resume
is show here:


void
linuxThread::
Suspend()
{
    m_suspendMutex->Acquire(WAIT_FOREVER);
    if (!m_suspended) {
   pthread_kill(m_threadHandle, SIGSTOP);
   m_suspended = true;
    }
    m_suspendMutex->Release();
}

void
linuxThread::
Resume()
{
    m_suspendMutex->Acquire(WAIT_FOREVER);
    if (m_suspended) {
   pthread_kill(m_threadHandle, SIGCONT);
   m_suspended = false;
    }
    m_suspendMutex->Release();
}  

So, I wrote this little test program, to see how well the
internals of Suspend and Resume work:

#include <stdio.h>
#include <pthread.h>
#include <sys/types.h>
#include <signal.h>
#include <time.h>

void tprint(char *string)
{
     time_t t;

     time(&t);
     printf("%s%s\n", ctime(&t), string);
}

void *worker_thread(void *pArg)
{
     tprint("Worker thread start, sleeping 5 secs\n");
     sleep(5);

     tprint("Back from sleep, worker thread exiting...\n");

     return NULL;
}

int main(int argc, char *pargv[])
{
     pthread_t t;

     t = pthread_create(&t, NULL, worker_thread, NULL);

     tprint("Main thread starting\n");
     sleep(1);

     tprint("Suspending thread...\n");
     pthread_kill(t, SIGSTOP);

     sleep(10);
     tprint("Exit...\n");

     return 0;
} 

The output of this program is:

Mon Sep 20 11:15:45 1999
Main thread starting

Mon Sep 20 11:15:45 1999
Worker thread start, sleeping 5 secs

Mon Sep 20 11:15:46 1999
Suspending thread...

Mon Sep 20 11:15:50 1999
Back from sleep, worker thread exiting...

Mon Sep 20 11:15:56 1999
Exit...  

As you can see, the worker thread never gets interrupted or slowed
down. This is exactly what would happend with the current Thread.cpp
class for linux.

Conclusion: We should not use Suspend() and Resume(). As a matter of
fact it would be best for us to remove these calls from the classes
altogether.



--ruaok         Freezerburn! All else is only icing. -- Soul Coughing

Robert Kaye -- [EMAIL PROTECTED]  http://moon.eorbit.net/~robert

Reply via email to