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

cpp: consider thread_local isl::ctx object #14

Open
ftynse opened this issue Aug 4, 2017 · 11 comments
Open

cpp: consider thread_local isl::ctx object #14

ftynse opened this issue Aug 4, 2017 · 11 comments

Comments

@ftynse
Copy link
Member

ftynse commented Aug 4, 2017

Essentially, isl::ctx is used as a "global" state for thread-safety reasons. We may want to declare one thread_local isl::ctx and use it implicitly in all functions that require a context.

@aisoard
Copy link
Collaborator

aisoard commented Aug 4, 2017

How much clutter is remaining inside the isl_ctx one all object have been freed? If that does not create any memory leak that seems fine. Note that this means that an isl object from a thread cannot be transferred to an other thread.

@tobiasgrosser
Copy link
Member

We could add a method to isl::ctx called:

static isl::ctx getThreadDefaultCtx()

which is a singleton and then expand the code generator to generate two versions of each constructor. One with an isl_ctx argument and one which uses this method to get an isl::ctx.

We probably need one or two more methods alla freeThreadDefaultCtx and setThreadDefaultCtx

Tobias

@ftynse
Copy link
Member Author

ftynse commented Aug 6, 2017 via email

@tobiasgrosser
Copy link
Member

Indeed, maybe freeing the default context is not a good idea. We could e.g. assume there is always a single default context. Resetting (freeing and creating a new one) makes sense however, to check for memory leaks, e.g.. I just thought about it a little and wanted to share my thoughts as code. Feel free to advance them (or do something entirely different).

@aisoard
Copy link
Collaborator

aisoard commented Aug 6, 2017

The only safe way I see to transfer object between threads is to have a temporary context in which the source thread transfer the object. Transfer the temporary context. Then the destination thread transfer the object to it's context.

Quite cumbersome.

@aisoard
Copy link
Collaborator

aisoard commented Aug 6, 2017

If getThreadDefaultCtx reallocate a context if it has been freed, that should be fine, no? Swapping contexts is fine to me too.

Do we add a: isFromThreadDefaultCtx method to all objects too?

@tobiasgrosser
Copy link
Member

tobiasgrosser commented Aug 6, 2017

Swapping context would also be fine for me.

I would not add an extra method to each object. However, we can expose the get_ctx() methods and add a function isThreadDefaultCtx() to isl::ctx, I assume.

I am not sure we actually want to solve the inter thread transfer issue in this discussion. For me the default context is a way to handle the common case of single thread execution. I believe inter context transfer functionality should be rather orthogonal.

@ftynse
Copy link
Member Author

ftynse commented Aug 6, 2017

The inter-thread management comes into play as soon as we say the default context is thread_local. We can just make it globally shared, wrapped in a singleton and say it is not thread-safe.

Well, there is no way to move an object into a different context in the C API... The ugly way I would do it is to serialize the object to a string, pass a string between threads and reconstruct the object in the other side's context. The ugliness can be compensated by the fact that this would also work in a distributed environment.

Using a special context that could be passed between threads is not always an option since the objects allocated within one context cannot be used in a function with objects from a different context (maybe add an assert in C++ interface for this?).

For the same reason, implicitly reallocating the default context seems dangerous. You may end up mixing previously allocated objects with news ones from new context in function calls, which is forbidden.

@tobiasgrosser tobiasgrosser changed the title Consider thread_local isl::ctx object cpp: consider thread_local isl::ctx object Aug 7, 2017
@aisoard
Copy link
Collaborator

aisoard commented Aug 9, 2017

There was discussions with Sven about the fact that C string serialization does not always produce valid output that can be parsed back (if you name any id: "lb" or "min" or "exists"). Also, it loses any "user" data and associated destructor.

As for using objects from different contexts, this will be a problem indeed, and we could either check that does not happen (with an assert or an actual test), or have automatic context transfer (but I think that is not possible even if we had a way to transfer objects between contexts).

That being said, I don't think there is a risk with implicitly reallocating the default context (when it is non existent anymore) as there cannot be any remaining objects from any previously freed context still alive.

@ftynse
Copy link
Member Author

ftynse commented Aug 9, 2017 via email

@aisoard
Copy link
Collaborator

aisoard commented Aug 9, 2017

This probably means isl needs a full parser instead of what it uses now.

I am not sure that would help (but would probably clean-up the parser). This is the printing stage which is faulty, it should en-quote "conflicting" identifiers.

So the library user should know when the default context was
reallocated, which kind of contradicts the idea of default context
making life easier. IMHO, if they want movable objects, they should
just explicitly create a movable context.

So, would you prefer the default context to be non-movable (but still reset-able)?

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

No branches or pull requests

3 participants