pthreads_001.phpt 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. --TEST--
  2. Test V8::executeString() : Pthreads test #1
  3. --SKIPIF--
  4. <?php
  5. require_once(dirname(__FILE__) . '/skipif.inc');
  6. if(!class_exists('Thread')) {
  7. die('skip pthreads extension required');
  8. }
  9. ?>
  10. --FILE--
  11. <?php
  12. class Workhorse extends Thread
  13. {
  14. protected $val;
  15. public function __construct($val)
  16. {
  17. $this->val = $val;
  18. }
  19. public function run()
  20. {
  21. $v8 = new V8Js();
  22. $v8->val = $this->val;
  23. $v8->sync_var_dump = function($value) {
  24. $this->synchronized(function($thread) use ($value) {
  25. while(!$thread->readyToPrint) {
  26. $thread->wait();
  27. }
  28. var_dump($value);
  29. $thread->notify();
  30. }, $this);
  31. };
  32. $v8->executeString('PHP.sync_var_dump(PHP.val);');
  33. }
  34. }
  35. $foo = new Workhorse('foo');
  36. $bar = new Workhorse('bar');
  37. $foo->start();
  38. $bar->start();
  39. $bar->synchronized(function($thread) {
  40. $thread->readyToPrint = true;
  41. $thread->notify();
  42. $thread->wait();
  43. }, $bar);
  44. $foo->synchronized(function($thread) {
  45. $thread->readyToPrint = true;
  46. $thread->notify();
  47. $thread->wait();
  48. }, $foo);
  49. $foo->join();
  50. $bar->join();
  51. ?>
  52. ===EOF===
  53. --EXPECT--
  54. string(3) "bar"
  55. string(3) "foo"
  56. ===EOF===