Skip to content
This repository has been archived by the owner on Dec 16, 2019. It is now read-only.

WIP: New Concurrent class #906

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

Conversation

sirsnyder
Copy link
Collaborator

@sirsnyder sirsnyder commented Oct 18, 2018

Introduction of Concurrent class. This new mutable class features property visibility, a thread-local annotation(@thread_local), ArrayAccess callback support and proper __get/__set/__isset/__unset handling.

Additionally this PR fixes #861, however with a BC break. Copying of static properties is confined to only default values and not assigned values.

dktapps and others added 9 commits April 24, 2018 19:56
I haven't been able to find the root cause of this issue yet, but it stems from pthreads' handling of copied static arrays.
When pthreads copies a static array, modifying the array on the parent thread triggers heap corruption and other nasties for reasons I am yet to fully understand.

The test case in the PR demonstrates the bug. It needs to be noted that:
- the crash does not occur unless the `PTHREADS_INHERIT_CLASSES` flag is enabled (hence, it is to do with the way static arrays are being handled)
- the crash occurs in GC when removing members of the array (`Test::$array = [];` works, also `unset(Test::$array[0]);`)
- the crash does not occur if the static array is populated with scalar values or arrays, only objects.
@dktapps
Copy link
Contributor

dktapps commented Oct 20, 2018

I'm not sure I understand the deal with thread-locals. Why is it necessary to confine them to a new class? What if I want thread locals without the performance hit involved with mutability? The additional runtime checks appear to be very minor, so I don't see why they should be relegated to a separate class.

I think it would be better if the statics fix could be handled separately from the Concurrent implementation so that can be integrated independently.

@@ -138,6 +145,10 @@ ZEND_END_MODULE_GLOBALS(pthreads)
#define PTHREADS_PG(ls, v) PTHREADS_FETCH_CTX(ls, core_globals_id, php_core_globals*, v)
#define PTHREADS_EG_ALL(ls) PTHREADS_FETCH_ALL(ls, executor_globals_id, zend_executor_globals*)

#define PTHREADS_ACC_THREADLOCAL 0x10
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there any way this could be automatically assigned to an unused flag? I'm just wondering about possible future collisions with ZEND_ACC flags which could be difficult to debug.

Copy link
Collaborator Author

@sirsnyder sirsnyder Oct 27, 2018

Choose a reason for hiding this comment

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

Yep, it's not unproblematic. But if this PoC works, I'll look for an as properties unused ZEND_ACC_* flag that we can reuse. Automatic assignment won't work :-/

@@ -102,13 +103,19 @@ extern zend_class_entry *pthreads_socket_entry;
(Z_TYPE_P(o) == IS_OBJECT && instanceof_function(Z_OBJCE_P(o), pthreads_volatile_entry))
#endif

#ifndef IS_PTHREADS_CONCURRENT
#define IS_PTHREADS_CONCURRENT(o) \
(Z_TYPE_P(o) == IS_OBJECT && instanceof_function(Z_OBJCE_P(o), pthreads_concurrent_entry))
Copy link
Contributor

Choose a reason for hiding this comment

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

Perhaps it would be better to make Concurrent extends Volatile instead of adding boilerplate checks for rhis.

@tpunt
Copy link
Collaborator

tpunt commented Oct 20, 2018

I'm not so sure I like introducing a new class for this. Is the performance overhead that much, since this is only done at copy time? I get that it introduces a little extra overhead, but given that it has the potential to save on some serialisation overhead (which is forcibly incurred on every instance property currently), the potential speed improvements of TLS could negate this. Maybe some benchmarks would help to see this more clearly.

Also, the name "Concurrent" isn't particularly good, since it is pretty generic. It could refer to any concurrency primitive (thread, process, actor, communicating sequential process, green thread, etc).

I'm glad to see an implementation of this, though :)

@sirsnyder sirsnyder mentioned this pull request Oct 27, 2018
@sirsnyder sirsnyder mentioned this pull request Dec 30, 2018
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.

None yet

3 participants