Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use target connections as contexts. #465

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

setrofim
Copy link
Collaborator

Make it easier to use multiple connections to the target from the same thread.

@setrofim
Copy link
Collaborator Author

Cc @douglas-raillard-arm: this should be largely orthogonal to #450, I believe, but FYI, just in case.

doc/target.rst Outdated
Set the Target's connection for the current thread to the one specified
(typically, one that has previously been returned by the call to
``get_connection``). Returns the old connection to the current thread -- it
is up to the coller to keep track of it and restore it if they wish.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/s/coller/caller.

target.execute('ls') # uses conn rather than the default connection.
```

If the connection object is being uses withn another function, you do not need
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

uses->used

target.execute('ls') # uses conn rather than the default connection.
```

If the connection object is being uses withn another function, you do not need
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/s/withn/within


.. _multiple-connections:

Multipe Connections With One Target
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it worth adding a note somewhere that Gem5Connections and TelnetConnections do not support this?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. I'll add a note for Gem5Connection. TelnetConnection actually derives from SshConnection so it would be supported.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In which case for TelnetConnection, a call to the BaseConnection initi method will also need to be added.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, yes. Good catch. Fixed.

@@ -249,3 +249,32 @@ The only methods discussed below are those that will be overwritten by the
.. method:: _wait_for_boot(self)

Wait for the gem5 simulated system to have booted and finished the booting animation.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update the info on line 19 about a common base?

Add a method to set the connection for the current thread within the
target. This method returns the connection that was previously set.
@douglas-raillard-arm
Copy link
Collaborator

douglas-raillard-arm commented Jan 30, 2020

You might want to take the patches that fixes thread local variables to store the connection first, they somehow conflicts, especially on the set_connection() side that is already handled by tls_property out of the box:
d8f9fac
aa3b0e3

I can submit them in a separate PR if needed.

There is also a ConnectionBase class introduced:
d7908d1
But it should be straightforward to merge the features of both as they don't seem to overlap.

@setrofim
Copy link
Collaborator Author

There is also a ConnectionBase class introduced:
d7908d1
But it should be straightforward to merge the features of both as they don't seem to overlap.

Ah cool, I'll hold off on this then, until that is merged, and will then add the changes to that. One nit -- could you rename the file to connection.py (singular) to be consistent with the other files.

Add a base class for connection objects that implements the context
manager interface and keeps track of the connection's target (which gets
set by Target.get_connection). When used as a context manager, the connection
substitutes itself for the target's default connection for the thread
within the context using Target.set_connect(). The old connection is
restored upon exiting the context.
@douglas-raillard-arm
Copy link
Collaborator

I think this PR is stale as quite a lot of things has changed since then in that area, but it would be pretty easy to implement these days with something like that:

class Target:
     ...
     @contextlib.contextmanager
     def with_connection(self, conn):
        old = self.conn
        try:
            self.conn = conn
            yield
        finally:
            self.conn = old

self.conn is defined using devlib.utils.misc.tls_property that provides a thread-local value (and creates a value when used for the first time in a given thread). It supports assignment (__set__) and that assignment will be thread-local as well. The main issue with that implementation is that it will create the old connection if it does not exist yet, but that would be easy to fix by extending the tls_property API to optionally let it raise instead of creating a new instance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants