| 1 |
////////////////////////////////////////////////////////////////////////////// |
| 2 |
////////////////////////////////////////////////////////////////////////////// |
| 3 |
// |
| 4 |
// copyright : (C) 2008 by Eran Ifrah |
| 5 |
// file name : worker_thread.cpp |
| 6 |
// |
| 7 |
// ------------------------------------------------------------------------- |
| 8 |
// A |
| 9 |
// _____ _ _ _ _ |
| 10 |
// / __ \ | | | | (_) | |
| 11 |
// | / \/ ___ __| | ___| | _| |_ ___ |
| 12 |
// | | / _ \ / _ |/ _ \ | | | __/ _ ) |
| 13 |
// | \__/\ (_) | (_| | __/ |___| | || __/ |
| 14 |
// \____/\___/ \__,_|\___\_____/_|\__\___| |
| 15 |
// |
| 16 |
// F i l e |
| 17 |
// |
| 18 |
// This program is free software; you can redistribute it and/or modify |
| 19 |
// it under the terms of the GNU General Public License as published by |
| 20 |
// the Free Software Foundation; either version 2 of the License, or |
| 21 |
// (at your option) any later version. |
| 22 |
// |
| 23 |
////////////////////////////////////////////////////////////////////////////// |
| 24 |
////////////////////////////////////////////////////////////////////////////// |
| 25 |
#include "asyncprocess.h" |
| 26 |
#include "processreaderthread.h" |
| 27 |
|
| 28 |
wxDEFINE_EVENT(wxEVT_ASYNC_PROCESS_OUTPUT, clProcessEvent); |
| 29 |
wxDEFINE_EVENT(wxEVT_ASYNC_PROCESS_STDERR, clProcessEvent); |
| 30 |
wxDEFINE_EVENT(wxEVT_ASYNC_PROCESS_TERMINATED, clProcessEvent); |
| 31 |
|
| 32 |
#if defined(__WXGTK__) || defined(__WXMAC__) |
| 33 |
#include <sys/wait.h> |
| 34 |
#endif |
| 35 |
|
| 36 |
ProcessReaderThread::ProcessReaderThread() |
| 37 |
: wxThread(wxTHREAD_JOINABLE) |
| 38 |
, m_notifiedWindow(NULL) |
| 39 |
, m_process(NULL) |
| 40 |
{ |
| 41 |
m_suspend.store(false); |
| 42 |
m_is_suspended.store(false); |
| 43 |
} |
| 44 |
|
| 45 |
ProcessReaderThread::~ProcessReaderThread() { m_notifiedWindow = NULL; } |
| 46 |
|
| 47 |
void* ProcessReaderThread::Entry() |
| 48 |
{ |
| 49 |
while(true) { |
| 50 |
// Did we get a request to terminate? |
| 51 |
if(TestDestroy()) { |
| 52 |
break; |
| 53 |
} |
| 54 |
|
| 55 |
if(m_suspend.load() && (m_is_suspended.load() == false)) { |
| 56 |
// request to suspend thread, but the status is not suspended |
| 57 |
m_is_suspended.store(true); |
| 58 |
} else if(m_is_suspended.load() && m_suspend.load() == false) { |
| 59 |
// request to resume thread but the status is suspended |
| 60 |
m_is_suspended.store(false); |
| 61 |
} |
| 62 |
|
| 63 |
if(m_is_suspended.load()) { |
| 64 |
wxThread::Sleep(5); |
| 65 |
continue; |
| 66 |
} |
| 67 |
|
| 68 |
if(m_process) { |
| 69 |
wxString buff; |
| 70 |
wxString buffErr; |
| 71 |
if(m_process->IsRedirect()) { |
| 72 |
if(m_process->Read(buff, buffErr)) { |
| 73 |
if(!buff.IsEmpty() || !buffErr.IsEmpty()) { |
| 74 |
// If we got a callback object, use it |
| 75 |
if(m_process && m_process->GetCallback()) { |
| 76 |
m_process->GetCallback()->CallAfter(&IProcessCallback::OnProcessOutput, buff); |
| 77 |
|
| 78 |
} else { |
| 79 |
// We fire an event per data (stderr/stdout) |
| 80 |
if(!buff.IsEmpty() && m_notifiedWindow) { |
| 81 |
// fallback to the event system |
| 82 |
// we got some data, send event to parent |
| 83 |
clProcessEvent e(wxEVT_ASYNC_PROCESS_OUTPUT); |
| 84 |
wxString& b = const_cast<wxString&>(e.GetOutput()); |
| 85 |
b.swap(buff); |
| 86 |
e.SetProcess(m_process); |
| 87 |
m_notifiedWindow->AddPendingEvent(e); |
| 88 |
} |
| 89 |
if(!buffErr.IsEmpty() && m_notifiedWindow) { |
| 90 |
// we got some data, send event to parent |
| 91 |
clProcessEvent e(wxEVT_ASYNC_PROCESS_STDERR); |
| 92 |
wxString& b = const_cast<wxString&>(e.GetOutput()); |
| 93 |
b.swap(buffErr); |
| 94 |
e.SetProcess(m_process); |
| 95 |
m_notifiedWindow->AddPendingEvent(e); |
| 96 |
} |
| 97 |
} |
| 98 |
} |
| 99 |
} else { |
| 100 |
|
| 101 |
// Process terminated, exit |
| 102 |
// If we got a callback object, use it |
| 103 |
NotifyTerminated(); |
| 104 |
break; |
| 105 |
} |
| 106 |
} else { |
| 107 |
// Check if the process is alive |
| 108 |
if(!m_process->IsAlive()) { |
| 109 |
// Notify about termination |
| 110 |
NotifyTerminated(); |
| 111 |
break; |
| 112 |
} else { |
| 113 |
wxThread::Sleep(5); |
| 114 |
} |
| 115 |
} |
| 116 |
} else { |
| 117 |
// No process?? |
| 118 |
break; |
| 119 |
} |
| 120 |
} |
| 121 |
m_process = NULL; |
| 122 |
return NULL; |
| 123 |
} |
| 124 |
|
| 125 |
void ProcessReaderThread::Stop() |
| 126 |
{ |
| 127 |
// Notify the thread to exit and |
| 128 |
// wait for it |
| 129 |
if(IsAlive()) { |
| 130 |
Delete(NULL, wxTHREAD_WAIT_BLOCK); |
| 131 |
} else { |
| 132 |
Wait(wxTHREAD_WAIT_BLOCK); |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
void ProcessReaderThread::Start(int priority) |
| 137 |
{ |
| 138 |
Create(); |
| 139 |
SetPriority(priority); |
| 140 |
Run(); |
| 141 |
} |
| 142 |
|
| 143 |
void ProcessReaderThread::NotifyTerminated() |
| 144 |
{ |
| 145 |
// Process terminated, exit |
| 146 |
// If we got a callback object, use it |
| 147 |
if(m_process && m_process->GetCallback()) { |
| 148 |
m_process->GetCallback()->CallAfter(&IProcessCallback::OnProcessTerminated); |
| 149 |
|
| 150 |
} else { |
| 151 |
// fallback to the event system |
| 152 |
clProcessEvent e(wxEVT_ASYNC_PROCESS_TERMINATED); |
| 153 |
e.SetProcess(m_process); |
| 154 |
if(m_notifiedWindow) { |
| 155 |
m_notifiedWindow->AddPendingEvent(e); |
| 156 |
} |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
void ProcessReaderThread::Suspend() |
| 161 |
{ |
| 162 |
m_suspend.store(true); |
| 163 |
// wait until we make sure that the reader thread is suspended |
| 164 |
while(true) { |
| 165 |
if(m_is_suspended.load()) { |
| 166 |
break; |
| 167 |
} |
| 168 |
wxThread::Sleep(1); |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
void ProcessReaderThread::Resume() |
| 173 |
{ |
| 174 |
m_suspend.store(false); |
| 175 |
// wait until we make sure that the reader thread is resumed |
| 176 |
while(true) { |
| 177 |
if(!m_is_suspended.load()) { |
| 178 |
break; |
| 179 |
} |
| 180 |
wxThread::Sleep(1); |
| 181 |
} |
| 182 |
} |