Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.
/ druntime Public archive

Decouple Stack Context#2797

Closed
baziotis wants to merge 13 commits intodlang:masterfrom
baziotis:thread_ctx
Closed

Decouple Stack Context#2797
baziotis wants to merge 13 commits intodlang:masterfrom
baziotis:thread_ctx

Conversation

@baziotis
Copy link
Contributor

Continuing from #2689

This PR decouples Context (now StackContext) from Thread / Fiber in the form of a super class for both.
Also, it adds a GlobalStackContext for commonly used global data.

@thewilsonator I did it kind of quickly, tell me what you think. I did not follow your diff precisely as some things seemed not necessary. One example is push/popContext etc. which seem to be relevant only to Thread.
Also, you seem to want a LockGuardedBlock for which I found no use and so it's not added yet.
Finally, it may be good to alias GlobalStackContext as it is used very much and it is a little verbose.

@dlang-bot
Copy link
Contributor

Thanks for your pull request and interest in making D better, @baziotis! We are looking forward to reviewing it, and you should be hearing from a maintainer soon.
Please verify that your PR follows this checklist:

  • My PR is fully covered with tests (you can see the coverage diff by visiting the details link of the codecov check)
  • My PR is as minimal as possible (smaller, focused PRs are easier to review than big ones)
  • I have provided a detailed rationale explaining my changes
  • New or modified functions have Ddoc comments (with Params: and Returns:)

Please see CONTRIBUTING.md for more information.


If you have addressed all reviews or aren't sure how to proceed, don't hesitate to ping us with a simple comment.

Bugzilla references

Your PR doesn't reference any Bugzilla issue.

If your PR contains non-trivial changes, please reference a Bugzilla issue or create a manual changelog.

Testing this PR locally

If you don't have a local development environment setup, you can use Digger to test this PR:

dub fetch digger
dub run digger -- build "master + druntime#2797"

@thewilsonator
Copy link
Contributor

One example is push/popContext etc. which seem to be relevant only to Thread.

Yes, but the operations only require the data members of StackContextExecutor. Also osthread.d is (still) a massive file anything that can logically be removed from it is a good thing.

Also, you seem to want a LockGuardedBlock for which I found no use and so it's not added yet.

I think I was trying to factorise the horrible mess of globals somewhat. See the low level thread stuff at the bottom of osthread.d, although tbh it might be better to split that off completely in a separate PR and refactoring it there (making a struct LowLevelThread { ThreadID tid; } with the free functions as methods and the locks as static members. Be sure to ping Rainer for that one.

Finally, it may be good to alias GlobalStackContext as it is used very much and it is a little verbose.

I don't think so, as its kind of the point to make it obvious that the variable being accessed is global (and therefore synchronisation may be required, and should be suspicious if absent).

@baziotis
Copy link
Contributor Author

baziotis commented Sep 15, 2019

One example is push/popContext etc. which seem to be relevant only to Thread.

Yes, but the operations only require the data members of StackContextExecutor.

Yes, but if we put them in StackContextExecutor, then it seems like Fiber needs them, while
it doesn't seem to. If that's ok, I'll put them there.

Also osthread.d is (still) a massive file anything that can logically be removed from it is a good thing.

Yes, I'll see if I have something when the tests pass. Which btw, fail currently. If you happen to have any suggestion, in this unittest calling the derived.call or composed.call (and even having everything else commented out), yields a seg fault. GDB shows that it tries to call m_dg or m_fn and they're null - here.
Which is weird if you consider that to reach this point (because of the switch) , m_call has
to be set to Call.DG, which is set in reset(), which should set m_dg as well.

So.. the hard part on these refactorings is that something can break with 0 chances of finding why. :p

Also, you seem to want a LockGuardedBlock for which I found no use and so it's not added yet.

I think I was trying to factorise the horrible mess of globals somewhat. See the low level thread stuff at the bottom of osthread.d, although tbh it might be better to split that off completely in a separate PR and refactoring it there (making a struct LowLevelThread { ThreadID tid; } with the free functions as methods and the locks as static members. Be sure to ping Rainer for that one.

Ok great, interesting.

Finally, it may be good to alias GlobalStackContext as it is used very much and it is a little verbose.

I don't think so, as its kind of the point to make it obvious that the variable being accessed is global (and therefore synchronisation may be required, and should be suspicious if absent).

Ok, clear.

-- Edit --
Fixed it (at least what I was referring to).

Due to changing the number of fields in Fiber (and moving them in a base class),
the indexing on .tupleof had to change.
@thewilsonator
Copy link
Contributor

Yes, but if we put them in StackContextExecutor, then it seems like Fiber needs them, while
it doesn't seem to.

Even if Fiber doesn't need it, having the stuff related solely to StackContext management all in one place is good practice.

If that's ok, I'll put them there.

Please do.


// Thread / Fiber entry point. Invokes the function or delegate passed on
// construction (if any).
final void run()
Copy link
Contributor

@thewilsonator thewilsonator Sep 15, 2019

Choose a reason for hiding this comment

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

note that somehow this has triggered

../.dub/packages/gtk-d-3.3.1/gtk-d/src/glib/Spawn.d(204,15): Error: function `glib.Spawn.Spawn.ReadFile.run` cannot override `final` function `core.thread.context.StackContextExecutor.run`
ae:sys-net-test 0.0.2444: building configuration "ae-sys-net-test-test-library"...
--
  | sys/file.d(2020,8): Error: function `ae.sys.file.writeFileAsync.Writer.run` cannot override `final` function `core.thread.context.StackContextExecutor.run`

in build kite, why I'm not sure.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I can't find the source of either of these files but the error most probably is something like here. Note that the private was not there before. I put it to fix an identical error (following the example of other unittests).
So, the point is that this is user code and it's not a problem of Fiber's code.
But this may lead to breaking change in existing code.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah its probably a visibility/protection issue, you can ask on the forums for an explanation if you can't find a solution. (Alternately you could call it something else that won't cause a collision)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Well, the problem is that a final function cannot be overriden, unless it is also private.
Previously: run was member of Fiber but it was private so overriding in e.g. DerivedFiber was ok.
Now: run is a member of StackContextExecutor and it can't be private as it has to be used by Fiber. Because of that, overriding it causes a problem.

I think of a couple solutions to that, of which the simplest is the one you said - chaning the name.

Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm, perhaps doing #2797 (comment) and having it as the opCall in this PR would be worth doing then. Saves having to come up with a name that won't collide. Or just move run back to Fiber for the time being and do that when you address #2797 (comment) in a future PR. Up to you.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, I'll see if there are any simple things left and do that.

*/
class Fiber

import core.thread.context : StackContext, StackContextExecutor;
Copy link
Contributor

Choose a reason for hiding this comment

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

You have multiple import of the same module just collapse them into one, and the public import for Rethrow just do alias Rethrow = .Rethow; (or alias Rethrow = core.thread.context.Rethrow; if that doesn't work)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, the imports are a little bad at the moment, along with the versioning. The last one is good thought, thanks.

@baziotis
Copy link
Contributor Author

baziotis commented Oct 7, 2019

This was supposed to be a follow-up to #2821,
continuing the core.thread refactoring by decoupling the stack context.
I don't have time at the moment to continue it, but I'm leaving this for reference.

@baziotis baziotis closed this Oct 7, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants