The online racing simulator
#1 - PoVo
Timers with smaller interval than 1 second?
Hi,

Is it possible to create timers in PRISM that have smaller intervals than 1 second?

For example I want to create a 30ms timer.

Using 0.03 as the interval doesn't work, the timer still runs with a 1 second interval.

Thanks.
You should be able to go down to microsecond increments. I'll look into it, but I don't have an ETA on any new updates.
#3 - PoVo
After reading the main PRISM php file, I checked the while loop and it seems that the while loop itself takes 1 second to process so I cannot use a timer that is a smaller interval.

Checking the difference between microtime at the start and end of the while loop shows that it takes 1 second.

I made 2 while loops (1 separate for the timers) and then realised both of them wouldn't run because one has to end before the next one begins...

I guess a solution would be to run it on a new thread which isn't possible on PHP afaik?
I had looked at how prism handles its timers previously, back when I was implementing something similar for pyinsim. Looking at the code in the PHPInSimMod.php updateSelectTimeout method I think I see what might be the cause of the bug:


<?php 
$sleep 
1;
$uSleep NULL;

$sleepTime NULL;
foreach (
$this->plugins->getPlugins() as $plugin => $object)
{
    
$timeout $object->executeTimers();

    if (
$timeout $sleepTime)
        
$sleepTime $timeout;
}
?>

It seems that the expression $timeout < $sleepTime is comparing an int with a null, which will always evaluate false in PHP. I wasn't sure so I checked it on CodePad and my thinking appears to be correct.

Next in the same method there is this code:


<?php 
# If there are no timers set or the next timeout is more then a second away, set the Sleep to 1 & uSleep to NULL.
if ($sleepTime == NULL OR $sleepTime >= 1)
{    
# Must have a max delay of a second, otherwise there is no connection maintenance done.
    
$sleep 1;
    
$uSleep NULL;
}
?>

It checks if the sleepTime is NULL, which it always is, then assigns the sleep variable a value of one second, which is what accounts for the constant one second stream_select timeout interval.

I think to fix the bug you would need to change the first problematic expression to something like the following:


<?php 
if ($sleepTime == NULL || $timeout $sleepTime)
    
$sleepTime $timeout;
?>

Which would/should/might fix the issue. Of course I might be completely wrong.
I'm sure PRISM's main loop could be fixed up so it's faster. Edit: See DarkTimes' post above.

However, on the threads subject:
There are no threads in PHP, but you could come up with a hack using pcntl_fork - if you're on a POSIX compliant platform with pcntl enabled - or if you're on windows you could emulate it if you have the com stuff compiled into PHP (probably). It would be tricky to come up with something that works on both POSIX and Windows without needing to be programmed differently.

The main issue you'd have is that you'd need to communicate between your main process and your child process. If it's POSIX only you could use shared memory. If you want it cross platform PRISM would need another socket for communicating with the child processes and your child process would need to pass stuff to the PRISM process to send to LFS (unless you were using UDP, instead of TCP to talk to LFS).
Thank you DarkTimes.

Now, regarding threads, I'm like the rest of you eagerly awaiting threading support within PHP. Once they have it, we will have it here to with PRISM.
#7 - PoVo
Thanks DarkTimes for that fix.

I've set my time-out value to 30ms (0.03) which is the smallest interval I'll be using for my plugin.

I've tried it with a 0.01 value and it also seems to run fine, so everything seems good!

Dygear, what value will you be using in the next PRISM release (IF you use it)?
Hundredth of a second.
DarkTimes, do you have a GitHub account? I'd like to give you credit for the change in GitHub's history, but you have to submit the change.

FGED GREDG RDFGDR GSFDG