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

Optional monitors #3547

Closed
wants to merge 3 commits into from
Closed

Optional monitors #3547

wants to merge 3 commits into from

Conversation

yglukhov
Copy link
Contributor

This PR introduces optional monitors feature.
The whole idea is that Object doesn't contain __monitor field anymore.
TypeInfo_Class will hold a monitor offset, if a class has one (by marking class declaration with @monitor attribute), or 0.
Monitor lookup is done with a hash map, if monitorOffset is 0.
The hash map is protected by a primitive RW spin lock.
Monitor finalization will only lookup for monitors, if a monitor was allocated at least once for the type of object being finalized.

druntime counterpart: dlang/druntime#789
discussion: http://forum.dlang.org/thread/xpliectmvwrwthamquke@forum.dlang.org

@bearophile
Copy link

This needs a review, to avoid rotting like other pull requests.

@ghost
Copy link

ghost commented May 22, 2014

This needs a review, to avoid rotting like other pull requests.

Very insightful comment.

Anywho:

  • Please remove tabs, use spaces for indentation and keep the indentation consistent. Thanks!
  • I think the current code is probably checking for @("monitor") instead of @monitor. There should be an enum monitor; declaration in Druntime that can be used as a UDA since just using @("monitor") could mean arbitrarily anything in user-code.
  • There are no test-cases.

@ghost
Copy link

ghost commented May 22, 2014

I suppose we'll need some kind of __hasMonitor trait to avoid build failures in Druntime, unless someone has a better idea?

@yglukhov
Copy link
Contributor Author

@AndrejMitrovic, __traits(hasMember, "__monitor") should work.

@ghost
Copy link

ghost commented May 22, 2014

Yeah, good point. :)

@yglukhov
Copy link
Contributor Author

I think the current code is probably checking for @("monitor") instead of @monitor. There should be an enum monitor; declaration in Druntime that can be used as a UDA since just using @("monitor") could mean arbitrarily anything in user-code.

Well, my changes to druntime include adding @monitor attrs to some classes. Otherwise the code would not compile there =).

There are no test-cases.

I'm not sure if it needs to be covered explicitly. This is a pretty basic feature, that almost any code should cover it. However i would welcome any good test snippets for that.

@dnadlinger
Copy link
Member

Wouldn't the way the code is currently implemented trigger on all structs called monitor? The name is general enough that this might cause problems. For example, I currently have an UDA struct called monitor in a little scientific simulation project of mine (it enables tracking/display of chosen quantities while the simulation is running).

Also, yes, this absolutely needs a test case that checks if the attribute actually causes the monitor field to be emitted. Otherwise, we'll end up with a silent performance regression (and inadvertent ABI change) at some point or another. A simple test could consist of verifying classInstanceSize for a class declaration with and without the monitor field.

@yglukhov
Copy link
Contributor Author

Wouldn't the way the code is currently implemented trigger on all structs called monitor? The name is general enough that this might cause problems. For example, I currently have an UDA struct called monitor in a little scientific simulation project of mine (it enables tracking/display of chosen quantities while the simulation is running).

You're right. It works exactly as you're saying. But i'm not sure here how to fix that. One way would be using some trickier keyword instead of "monitor".

Also, yes, this absolutely needs a test case that checks if the attribute actually causes the monitor field to be emitted. Otherwise, we'll end up with a silent performance regression (and inadvertent ABI change) at some point or another. A simple test could consist of verifying classInstanceSize for a class declaration with and without the monitor field.

Done in druntime.

@schancel
Copy link

I don't quite understand what the benefit of this is over just having a Monitor type? Presumably it allows you to Signal or Wait on any class decorated with @monitor? Why is that much better than having a library Monitor type?

@yglukhov
Copy link
Contributor Author

@schancel the monitors as they are currently implemented allow to lock/unlock an instance, but not signal/wait. And we already have such type - Mutex =). This PR is aimed is to remove a hidden __monitor field from the Object class, without breaking old behavior.

@yglukhov
Copy link
Contributor Author

Rebased.

@ghost ghost added the Needs Approval label Sep 8, 2014
@DmitryOlshansky
Copy link
Member

@andralex @WalterBright Please state your view on this subject (again) and let's inact it.

@MartinNowak
Copy link
Member

I'm not really happy with the druntime hash table stuff, but I would totally support a deprecation from implicit monitors to an explicit @(Object.Monitor) UDA.

  1. recognize monitor UDA and deprecate synchronizing on classes without it
  2. templatize all druntime functions that expect Objects to have a monitor
    and dreprecate using them without the UDA
  3. wait 6 month
  4. stop emitting hidden __monitor for classes without the UDA

This would also deprecate the synchronized (typeid(Object)) anti-pattern.
Adding struct monitor; to object.d for a more concise @monitor would be possible but might break code.

@andralex
Copy link
Member

I'm in favor of this. The current backward-compatible approach is the way to go, though I agree with @MartinNowak in the long run we may envision complete deprecation.

Please rebase and let's push this through. Thanks!

@DmitryOlshansky
Copy link
Member

Half a year later w/o merging this patch the idea still looks sexy to me.
@MartinNowak any plans to move on this in any of 2 competing plans?

@andralex
Copy link
Member

Well what's going on?

@andralex
Copy link
Member

"All checks have failed"

@MartinNowak
Copy link
Member

Half a year later w/o merging this patch the idea still looks sexy to me.
@MartinNowak any plans to move on this in any of 2 competing plans?

Yes, I'm still opposed to adding a global hash and the outlined plan still makes sense.
Furthermore having monitor support on all classes creates ownership/attribute issue for the monitor (MartinNowak/phobos@8cf0ec2#diff-4e008aedb3026d4a84f58323e53bf017R4883).
I don't have the capacity to pull this story, but starting by adding an @(Object.Monitor) UDA and recognizing that in the compiler should be fairly trivial (e.g. look at the objective-c changes).

@ibuclaw
Copy link
Member

ibuclaw commented Jan 24, 2016

recognize monitor UDA and deprecate synchronizing on classes without it

Let's proceed with stage 1 for when 2.071 opens then?

Do we have available documentation / rationale on the deprecated features page? Or a DIP?

@MartinNowak
Copy link
Member

Do we have available documentation / rationale on the deprecated features page? Or a DIP?

Sure, a small entry on http://dlang.org/deprecate.html would be nice, a DIP would be overkill IMHO.

@yglukhov yglukhov closed this Feb 18, 2017
ibuclaw pushed a commit to ibuclaw/dmd that referenced this pull request Jul 10, 2022
Cirrus CI: Add FreeBSD 11 pipeline

Signed-off-by: Razvan Nitu <RazvanN7@users.noreply.github.com>
Merged-on-behalf-of: Razvan Nitu <RazvanN7@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
8 participants