[lldb-dev] Process spawning and shutdown

Greg Clayton gclayton at apple.com
Wed Sep 17 17:15:27 PDT 2014


> On Sep 17, 2014, at 3:47 PM, Zachary Turner <zturner at google.com> wrote:
> 
> 
> 
> On Wed, Sep 17, 2014 at 3:11 PM, Greg Clayton <gclayton at apple.com> wrote:
> 
> > On Sep 17, 2014, at 1:20 PM, Zachary Turner <zturner at google.com> wrote:
> >
> > +Jim, since he looked at my previous HostThread patch and some others, and may have additional context.
> >
> > Just to be explicit, are you saying that I should not refactor any of this code, or that it's fine as long as the Apple stuff remains functionally equivalent?
> 
> I was out of of the office during the last patch for the threads. I know Jim did work with you on that one. A few things from the last patch that made me worry about a Process equivalent:
> 
> class HostThread
> {
>   public:
>     HostThread();
>     HostThread(lldb::thread_t thread);
> 
>     Error Join(lldb::thread_result_t *result);
>     Error Cancel();
>     void Reset();
>     lldb::thread_t Release();
> 
>     void SetState(ThreadState state);
>     ThreadState GetState() const;
>     HostNativeThreadBase &GetNativeThread();
>     const HostNativeThreadBase &GetNativeThread() const;
>     lldb::thread_result_t GetResult() const;
> 
>     bool EqualsThread(lldb::thread_t thread) const;
> 
>   private:
>     std::shared_ptr<HostNativeThreadBase> m_native_thread;
> };
> 
> Why is there a separate HostNativeThreadBase class? Why not just make have each host implement the calls required in HostThread in their own respective HostThread.cpp class? Now we just indirect all calls through another class when there really isn't a valid reason to do so IMHO.
> 
> Sorry, long story incoming.
> 
> Something like that was actually my first choice, and how I originally implemented it.  There was HostThreadWindows, HostThreadPosix, HostThreadMac, etc.  And then HostThread was typedefed to the appropriate implementation.  We actually debated at length about whether it was the best approach, because people didn't like the typedef.  Two alternative approaches were:
> 
> 1) Use inheritance, make HostThreadBase provide only the generic operations, derived implementations can provide additional methods if they like
> 2) Make a single HostThread.h file with the interface, make multiple HostThread.cpp files each which implement the methods in HostThread.h differently.
> 
> I argued against 1 because I think it leads to some ugly code (for example, copy becomes clone, requires storing by pointer, requires null checking, requires factory-based creation, etc).  We never really discussed 2, but I think the same arguments that people had against my original option with the typedef would have applied to #2 as well.

I can see your point. If we thing about option 2 where we use different HostThread.cpp for each platform where only one is compiled on each host. This would require that all typedefs for the lldb::host::thread_t and such be done correctly and each platform would then be required to only use those defines and all systems would need to use the same instance variables. This may or may not work (MacOSX has 3 kinds of thread IDs we could actually cache for a thread. So the current approach allows for this and is probably the cleanest. I pull my reservations on the current approach..

> But it probably helps to take a step back.  From a high level perspective and ignoring any implementation details, what I'm trying to accomplish with this and other refactors is to create a strong separation between generic code and platform specific code.  I want it to be actually difficult to call into platform-specific code from generic code.  With the current design of Host, it's extremely easy.  If you need a method, you add it to Host, and do something like this:
> 
> #if defined(MY_PLATFORM)
> void Host::MyMethod()
> {
>     // Implementation
> }
> #else
> void Host::MyMethod()
> {
>     // Nothing
> }
> #endif
> 
> This creates a problem in that a platform-specific operation is exposed through a supposedly generic interface.  
> 
> The main argument against my original implementation with the typedef was that while it solves the problem somewhat (If you have HostThreadMacOSX::FooBar() but don't have HostThreadWindows::FooBar(), then writing HostThread::FooBar() on MacOSX will compile locally but break the buildbots, instead of silently passing and potentially introducing a bug in other platforms where the method was stubbed out), it could be made better by just never, for any platform, exposing a method that is not generic enough to actually work on every platform.  This was the basis for Jim arguing for #1, and have everything use the base interface.  This way it becomes very difficult to call a platform specific method.  You have to include a platform specific header and cast to a platform specific type.  
> 
> We arrived at the existing solution as sort of a compromise.  The syntax is still nice, it's easily copyable just like an lldb::thread_t, and it doesn't expose any method unless that method can be implemented on every platform.

I agree and understand the logic and organization now. Thanks for the info and background.

> 
>  
> 
> Why is there a SetState() and GetState() here? We set this to running when the thread handle is valid, even though it has no bearing on if the thread is actually running or suspended? Can we remove this?
> I can probably remove it.  I provided it because it is possible to construct a HostThread with an arbitrary lldb::thread_t, and while in most cases it's safe to assume that the initial state is running, you could do some platform specific stuff with the handle before constructing it, and the class would be confused about the state.

I would like to see the state removed and have some accessors that can determine if a thread handle is valid. Maybe removing Get/SetState() and replacing with "bool IsValid() const" or "bool HandleIsValid() const" or adding a test operator to class (operator bool() const).
> 
> Everything else is ok.
> 
> The fallout from this was now any code that used to just check if a thread was valid, are now doing:
> 
> bool
> Communication::JoinReadThread (Error *error_ptr)
> {
>     if (m_read_thread.GetState() != eThreadStateRunning)
>         return true;
> 
>     Error error = m_read_thread.Join(nullptr);
>     m_read_thread.Reset();
>     return error.Success();
> }
> 
> 
> Since this is now a class, we should probably just do:
> 
> 
> bool
> Communication::JoinReadThread (Error *error_ptr)
> {
>     Error error = m_read_thread.Join(nullptr);
>     m_read_thread.Reset();
>     return error.Success();
> }
> 
> And have join return success if the thread handle wasn't valid. The other reason I don't like the state in the HostThread is that it seems to indicate that this thread is actually running. I could have already exited, but if you ask the HostThread its state it will tell you "running" when it really isn't. Tracking when a thread goes away can get tricky so it would be hard to keep this state up to date. Can we change it back to a "bool IsValid() const" that returns true if the thread handle is valid?
>  
> Thinking about this, I kind of agree with you.  In fixing up the code to use HostThread, I actually didn't notice anywhere where we care about anything other than whether or not the thread is running.  So the other states don't seem to be that useful in practice.  But is IsValid() really what we want to check?  We really want to check if the thread is running.  We own the thread routine because it's the ThreadCreateTrampoline.  Can't we detect this by just having the HostThread look at the value of a shared variable?

We can add support to HostThread for threads we start via the HostThread interface. For others we might need to still be able to interact with another thread, but it might not have the abilities that the other threads do have. Is this is the case we might want:

bool HandleIsValid() const;

and:

bool ThreadIsRunning() const;

We might not be able to tell if a thread is running if we didn't launch the thread through the host layer though, so I am not sure if we would return true or false for that...

> 
>  
> I am not sure if you were thinking of adding more to this class. But this class isn't really designed to represent a thread from another process, so it isn't very useful in that respect. I seem to remember Jim saying that you thought that this class might be able to be reused in for other threads in other processes, and then backed back to your design, so there might just be stuff left over?
>  
> I was convinced the other way on that point, and now think that this class should not be used to represent a thread running in another process.  The requirements and interface are too different.

Agreed.

> 
>  
> 
> This is the main reason for my concern with the next patch.
> 
> After we reach  an agreement about the HostThread patch, I can go into more detail about my plan for HostProcess and how it would be integrated.


Sounds good. I propose:

1 - remove Get/SetState()
2 - add "bool ThreadIsRunning()" to the interface and replace previous uses of "host_thread.GetState() == eStateRunning" or "host_thread.GetState() == eStateStopped" with the new call where needed.
3 - add "bool HandleIsValid() const;" only if needed by someone that is checking if a thread handle is valid where they don't care if the thread is running, or if we have cases where we give a thread handle to a HostThread class but didn't launch it...

Let me know what you think. Thanks for the response and let me know when you have an approach for HostProcess.

Greg



More information about the lldb-dev mailing list