Difficulty
At my current level of skill and understanding, this feature would be impossible for me to implement on my own. It is just an introductory look at what the other sections will visually look like when they are also finished. I will not be implementing this feature.
Introduction
Multi-threading should not be allowed at the user level, but allow for sandbox threading. The PHP Engine should keep track of the creation of threads, protect against deadlocking, and control synchronization. This will abstract away the complexity and have PHP do all of the heavy lifting as well as protect the developers from themselves and harming the process the thread is in.
There are some architectures, both CPU and OS, that do not allow threading. There should be a fall back implemented in the virtual machine that still enables an environment functionally similar to threading. The fall back can be implemented like the ticks in the current PHP Engine or it could use a stack approach similar to operating systems.
The Engine could also track how efficient the system is and how well it would perform if it was single threaded. There should be a mix of allowing the developers to test and optimize on their own and tracking internally to help protect against slow scripts caused by wrong usage of this feature.
There should be internal reflection for threading to allow for tracking, overriding, and debugging. The reflection will track the time a thread takes, which threads and variables it is synchronized with, and how many times faults occurred. There should be functions for overriding whether or not the thread is still multi-threaded or if it creates an interprocess, or if it is instead acts like a single thread, when processed.
The multi-threading feature wouldn’t be useful, if the process shutdown and destroyed everything at execution. The feature would only be enabled for PHP environments that are already multi-tasked to keep the script from taking longer than it should.
Virtual Machine: Timing the Best Method
During runtime, the virtual machine can keep track of the time the script takes and switch between single threaded and multiple threads to get statistics on which would be the best method overall. After it figures which is the best method for the user, it can either log what it finds, allow for reflection to get the number, and then decide which is the best method to take. It would still work like the user expects, but allow for the virtual machine to decide which is the best method for the user.
This would add additional memory and processing overhead, so it could be disabled by those who already think that their script is optimized enough already. This adds another level of complexity to the feature and could introduce difficult to find and fix bugs. It should also be allowed to be overwritten by the user for those who know for a fact that the system doesn’t work that well for their script. The system won’t be able to work perfectly 100% of the time for every script.
This additional feature would be extremely useful for JIT or opcode cached environments where the script is stored in memory for extended periods of time. Keeping track of timing of various environments would allow for the system to rewrite itself to optimize the speed for the user. The abstraction would be such that the user wouldn’t be able to tell, except when the system fully realizes and optimizes itself and the script.
Keywords
The following sections will introduce additional keywords and syntax that will require that the flex and bison skeletons be updated to allow for this feature. This section will not be updated and will not be found here.
Different Methods for Creating Threads
The following parts will detail the various parts of this feature and will be split up to allow for better understanding on what the feature will involve.
-
Anonymous
- Does not allow for returning to main thread or calling from the main thread.
- Runs along side the main thread.
-
Named
- Does not run unless called from the main process.
- Can return data to the calling thread.
-
Shell
- Allows for shell commands to be processed in another thread or process.
- Allows for returning to the calling thread.
- Allows for both anonymous and named shells.
- It would replace functionality, most of the shell extensions, when implemented.
-
Interprocess
- Replaces the current method of the Process Control extension with an easier to setup and create method.
- Also will allow for connecting to other processes for passing data.
Possibly Related Posts:
- What is the Obituary Story
- Hashing Out Obituary Book Details
- English IV: Untitled Satire
- In Response to JIT Compilation
- PHP Opcode Series