|
33 | 33 | /** Library Dependencies **/ |
34 | 34 | use NxSys\Core\ExtensibleSystemClasses as CoreEsc; |
35 | 35 |
|
| 36 | +use parallel\Runtime as Thread_Runtime; |
| 37 | +use parallel\Channel as Thread_Channel; |
| 38 | + |
36 | 39 | //.... |
37 | 40 | use SplQueue; |
38 | | -use Thread; |
39 | | -use Stackable; |
40 | 41 | use Exception; |
41 | 42 | use Throwable; |
| 43 | +use Closure; |
| 44 | + |
| 45 | +const DEFAULT_CHANNEL_CAPACITY = 1024; |
42 | 46 |
|
43 | 47 |
|
44 | 48 | /** |
|
50 | 54 | * @author Chris R. Feamster <cfeamster@f2developments.com> |
51 | 55 | */ |
52 | 56 | // abstract class BaseJob extends CoreEsc\pthreads\Thread implements IJob |
53 | | -abstract class BaseJob extends Thread implements IJob |
| 57 | +class BaseJob implements IJob |
54 | 58 | { |
| 59 | + const DEFAULT_CHANNEL_CAPACITY = DEFAULT_CHANNEL_CAPACITY; |
| 60 | + |
55 | 61 | protected $aLocalConstants = []; |
| 62 | + public $hThreadRuntime; |
56 | 63 | public function __construct() |
57 | 64 | { |
58 | | - $this->aInData=new SplQueue ; |
59 | | - $this->aOutData=new SplQueue ; |
| 65 | + |
| 66 | + // $this->aInData=new CoreEsc\spl\SplQueue ; |
| 67 | + // $this->aOutData=new CoreEsc\spl\SplQueue ; |
| 68 | + $this->aInData=new Thread_Channel(DEFAULT_CHANNEL_CAPACITY); |
| 69 | + $this->aOutData=new Thread_Channel(DEFAULT_CHANNEL_CAPACITY); |
| 70 | + $this->setRunMethod(); |
| 71 | + |
| 72 | + } |
| 73 | + |
| 74 | + public function setRunMethod(string $sMethodName = 'run') |
| 75 | + { |
| 76 | + if (!method_exists($this, $sMethodName)) |
| 77 | + { |
| 78 | + throw new InvalidArgumentException($sMethodName." is a not a valid method on ".__CLASS__); |
| 79 | + } |
| 80 | + $this->sRunMethodName=$sMethodName; |
| 81 | + } |
| 82 | + |
| 83 | + public function setRuntime(Thread_Runtime $hRuntime = null): ?Thread_Runtime |
| 84 | + { |
| 85 | + return $this->hThreadRuntime=$hRuntime; |
| 86 | + } |
| 87 | + |
| 88 | + public function start(int $iLegacyOptions=0, array $aThreadArguments=[]) |
| 89 | + { |
| 90 | + if (!$this->hThreadRuntime) |
| 91 | + { |
| 92 | + $this->hThreadRuntime=new Thread_Runtime(__DIR__.'\..\..\vendor\autoload.php'); |
| 93 | + } |
| 94 | + $hScopedBooter=Closure::fromcallable([$this, 'bootThread']); |
| 95 | + //$hScopedBooter = $hScopedBooter->bindTo($this); |
| 96 | + $this->hThreadState=$this->hThreadRuntime->run($hScopedBooter, [new $this, [$this->aInData, $this->aOutData]]);//, [($this), $aThreadArguments]); |
| 97 | + return $this->hThreadState; #"Future" |
| 98 | + } |
| 99 | + |
| 100 | + public function isRunning(): bool |
| 101 | + { |
| 102 | + return $this->hThreadState->cancelled() || $this->hThreadState->done(); |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * @see parallel\Future::value |
| 107 | + */ |
| 108 | + public function resolveJobToValue() |
| 109 | + { |
| 110 | + return $this->hThreadState->value(); |
60 | 111 | } |
61 | 112 |
|
| 113 | + protected function bootThread($oJob, array $aThreadArguments=[]) //: mixed |
| 114 | + { |
| 115 | + //require_once __DIR__.'\..\..\vendor\autoload.php'; |
| 116 | + // $oJob = unserialize($oJob); |
| 117 | + $oJob->initConstants(); |
| 118 | + //channel shenanigans |
| 119 | + return false?:call_user_func_array([$oJob, $oJob->sRunMethodName], $aThreadArguments); |
| 120 | + } |
| 121 | + |
| 122 | + public function run() |
| 123 | + { |
| 124 | + $sGoal="I am!"; |
| 125 | + return $sGoal; |
| 126 | + } |
| 127 | + |
| 128 | + //protected abstract function run(); |
| 129 | + |
| 130 | + //--- |
| 131 | + |
62 | 132 | public function setupConstants(array $aConstants): void |
63 | 133 | { |
64 | 134 | foreach ($aConstants as $name) |
|
0 commit comments