チェンジセット 3371

差分発生行の前後
無視リスト:
コミット日時:
2010/02/02 19:30:09 (2 年前)
コミッタ:
yossy
ログメッセージ:

Thread: 割り込みハンドラがすぐに実行されるように修正 (see #104, #117)

ファイル:

凡例:

変更無し
追加
削除
更新
コピー
移動
  • as3/Thread/trunk/src/org/libspark/thread/Thread.as

    r2106 r3371  
    798798                                // state を切り替える 
    799799                                _state = _runningState; 
    800                                 // 割り込みハンドラがあれば実行関数を割り込みハンドラに設定 
     800                                 
     801                                // 割り込みハンドラがあれば 
    801802                                if (_interruptedHandler != null) { 
     803                                         
     804                                        // 実行関数を割り込みハンドラに設定 
    802805                                        _runHandler = _interruptedHandler; 
     806                                         
     807                                        // 入れ子になる場合があるのでカレントスレッドを保存 
     808                                        var current:Thread = _currentThread; 
     809                                         
     810                                        try { 
     811                                                // そしてすぐ実行してみる 
     812                                                internalExecute(null, this); 
     813                                        } 
     814                                        finally { 
     815                                                // カレントスレッドを復元 
     816                                                _currentThread = current; 
     817                                        } 
    803818                                } 
    804819                                else { 
  • as3/Thread/trunk/tests/org/libspark/thread/InterruptionTest.as

    r641 r3371  
    108108                        { 
    109109                                assertEquals('run interrupt runInterrupted interrupt runInterrupted2 finalize ', Static.log); 
     110                        }, 1000)); 
     111                         
     112                        t.start(); 
     113                } 
     114                 
     115                /** 
     116                 * イベントの発生前に割り込みを掛けた場合に割り込みハンドラが実行され、その後のイベントを受け取らないかどうか 
     117                 *  
     118                 * @see http://www.libspark.org/ticket/117 
     119                 */ 
     120                test function interruptionBeforeEvent():void 
     121                { 
     122                        Static.log = ''; 
     123                         
     124                        var t:TesterThread = new TesterThread(new InterruptionBeforeEventTestThread()); 
     125                         
     126                        t.addEventListener(Event.COMPLETE, async(function(e:Event):void 
     127                        { 
     128                                assertEquals('run EventWait::run interrupt EventWait::eventInterrupted dispatch EventWait::finalize dispatch2 finalize ', Static.log); 
     129                        }, 1000)); 
     130                         
     131                        t.start(); 
     132                } 
     133                 
     134                /** 
     135                 * イベントの発生後に割り込みを掛けた場合に割り込みハンドラが実行されるかどうか 
     136                 *  
     137                 * @see http://www.libspark.org/ticket/117 
     138                 */ 
     139                test function interruptionAfterEvent():void 
     140                { 
     141                        Static.log = ''; 
     142                         
     143                        var t:TesterThread = new TesterThread(new InterruptionAfterEventTestThread()); 
     144                         
     145                        t.addEventListener(Event.COMPLETE, async(function(e:Event):void 
     146                        { 
     147                                assertEquals('run EventWait::run dispatch EventWait::eventHandler EventWait::run interrupt EventWait::eventInterrupted EventWait::finalize dispatch2 finalize ', Static.log); 
     148                        }, 1000)); 
     149                         
     150                        t.start(); 
     151                } 
     152                 
     153                /** 
     154                 * 親子関係のあるスレッドの親スレッドに割り込んだ際に子スレッドの割り込みが実行され、その後のイベントを受け取らないかどうか 
     155                 *  
     156                 * @see http://www.libspark.org/ticket/117 
     157                 * @see http://www.libspark.org/ticket/104 
     158                 */ 
     159                test function childInterruption():void 
     160                { 
     161                        Static.log = ''; 
     162                         
     163                        var t:TesterThread = new TesterThread(new ChildInterruptionTestThread()); 
     164                         
     165                        t.addEventListener(Event.COMPLETE, async(function(e:Event):void 
     166                        { 
     167                                assertEquals('run EventWait::run EventWait::run interrupt EventWait::eventInterrupted EventWait::eventInterrupted dispatch EventWait::finalize EventWait::finalize dispatch2 finalize ', Static.log); 
    110168                        }, 1000)); 
    111169                         
     
    122180import org.libspark.thread.Thread; 
    123181import org.libspark.thread.ThreadState; 
     182import org.libspark.thread.utils.ParallelExecutor; 
    124183 
    125184class Static 
     
    289348        } 
    290349} 
     350 
     351class EventWaitThread extends Thread 
     352{ 
     353        public var dispatcher:IEventDispatcher; 
     354         
     355        override protected function run():void  
     356        { 
     357                Static.log += 'EventWait::run '; 
     358                 
     359                event(dispatcher, 'event', eventHandler); 
     360                interrupted(eventInterrupted); 
     361        } 
     362         
     363        private function eventHandler(e:Event):void 
     364        { 
     365                Static.log += 'EventWait::eventHandler '; 
     366                 
     367                run(); 
     368        } 
     369         
     370        private function eventInterrupted():void 
     371        { 
     372                Static.log += 'EventWait::eventInterrupted '; 
     373        } 
     374         
     375        override protected function finalize():void  
     376        { 
     377                Static.log += 'EventWait::finalize '; 
     378        } 
     379} 
     380 
     381class InterruptionBeforeEventTestThread extends Thread 
     382{ 
     383        private var _dispatcher:IEventDispatcher = new EventDispatcher(); 
     384        private var _thread:EventWaitThread; 
     385        private var _frame:uint = 0; 
     386         
     387        override protected function run():void  
     388        { 
     389                Static.log += 'run '; 
     390                 
     391                _thread = new EventWaitThread(); 
     392                _thread.dispatcher = _dispatcher; 
     393                _thread.start(); 
     394                 
     395                next(process); 
     396        } 
     397         
     398        private function process():void 
     399        { 
     400                ++_frame; 
     401                 
     402                if (_frame == 1) { 
     403                        Static.log += 'interrupt '; 
     404                        _thread.interrupt(); 
     405                        Static.log += 'dispatch '; 
     406                        _dispatcher.dispatchEvent(new Event('event')); 
     407                        next(process); 
     408                } 
     409                if (_frame == 2) { 
     410                        Static.log += 'dispatch2 '; 
     411                        _dispatcher.dispatchEvent(new Event('event')); 
     412                } 
     413        } 
     414         
     415        override protected function finalize():void  
     416        { 
     417                Static.log += 'finalize '; 
     418        } 
     419} 
     420 
     421class InterruptionAfterEventTestThread extends Thread 
     422{ 
     423        private var _dispatcher:IEventDispatcher = new EventDispatcher(); 
     424        private var _thread:EventWaitThread; 
     425        private var _frame:uint = 0; 
     426         
     427        override protected function run():void  
     428        { 
     429                Static.log += 'run '; 
     430                 
     431                _thread = new EventWaitThread(); 
     432                _thread.dispatcher = _dispatcher; 
     433                _thread.start(); 
     434                 
     435                next(process); 
     436        } 
     437         
     438        private function process():void 
     439        { 
     440                ++_frame; 
     441                 
     442                if (_frame == 1) { 
     443                        Static.log += 'dispatch '; 
     444                        _dispatcher.dispatchEvent(new Event('event')); 
     445                        Static.log += 'interrupt '; 
     446                        _thread.interrupt(); 
     447                        next(process); 
     448                } 
     449                if (_frame == 2) { 
     450                        Static.log += 'dispatch2 '; 
     451                        _dispatcher.dispatchEvent(new Event('event')); 
     452                } 
     453        } 
     454         
     455        override protected function finalize():void  
     456        { 
     457                Static.log += 'finalize '; 
     458        } 
     459} 
     460 
     461class ChildInterruptionTestThread extends Thread 
     462{ 
     463        private var _dispatcher:IEventDispatcher = new EventDispatcher(); 
     464        private var _thread:ParallelExecutor; 
     465        private var _frame:uint = 0; 
     466         
     467        override protected function run():void  
     468        { 
     469                Static.log += 'run '; 
     470                 
     471                var t1:EventWaitThread = new EventWaitThread(); 
     472                t1.dispatcher = _dispatcher; 
     473                 
     474                var t2:EventWaitThread = new EventWaitThread(); 
     475                t2.dispatcher = _dispatcher; 
     476                 
     477                _thread = new ParallelExecutor(); 
     478                _thread.addThread(t1); 
     479                _thread.addThread(t2); 
     480                _thread.start(); 
     481                 
     482                next(process); 
     483        } 
     484         
     485        private function process():void 
     486        { 
     487                ++_frame; 
     488                 
     489                if (_frame == 1) { 
     490                        next(process); 
     491                } 
     492                if (_frame == 2) { 
     493                        Static.log += 'interrupt '; 
     494                        _thread.interrupt(); 
     495                        Static.log += 'dispatch '; 
     496                        _dispatcher.dispatchEvent(new Event('event')); 
     497                        next(process); 
     498                } 
     499                if (_frame == 3) { 
     500                        Static.log += 'dispatch2 '; 
     501                        _dispatcher.dispatchEvent(new Event('event')); 
     502                } 
     503        } 
     504         
     505        override protected function finalize():void  
     506        { 
     507                Static.log += 'finalize '; 
     508        } 
     509}