Created
March 3, 2016 20:09
-
-
Save zwetan/38afd4255d2e23112f9c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Usage: | |
public class Something | |
{ | |
private var _mutex:Mutex; | |
private var _commands:WorkerConnection; | |
public function say( message:String ):void | |
{ | |
_mutex.lock(); | |
trace( message ); | |
_mutex.unlock(); | |
} | |
public function helloworld( lang:String, from:uint ):void | |
{ | |
switch( lang ) | |
{ | |
case "fr": | |
trace( from + ": bonjour le monde" ); | |
break; | |
case "cn": | |
trace( from + ": ni hao shijie" ); | |
break; | |
case "en": | |
default: | |
trace( from + ": hello world" ); | |
} | |
} | |
// main | |
public function main():void | |
{ | |
_mutex = new Mutex(); | |
_commands = new WorkerConnection( WorkerConnection.getPrimordial(), _mutex ); | |
_commands.client = this; | |
var childID:uint = 0; | |
// main loop | |
while( true ) | |
{ | |
if( testCondition() ) | |
{ | |
var newChild:Worker = WorkerDomain.current.createWorkerFromPrimordial(); | |
newChild.setSharedProperty( "childID", childID ); | |
newChild.setSharedProperty( "mutex", _mutex ); | |
newChild.start(); | |
childID++; | |
} | |
_commands.fetch(); //at the end of loop | |
} | |
} | |
private function _randRange(minNum:Number, maxNum:Number):Number | |
{ | |
return (Math.floor(Math.random() * (maxNum - minNum + 1)) + minNum); | |
} | |
// child worker | |
public function background():void | |
{ | |
var child:Worker = Worker.current; | |
var ID:uint = child.getSharedProperty( "childID" ); | |
var mutex:Mutex = child.getSharedProperty( "mutex" ); // by reference | |
_mutex = mutex; | |
_commands = new WorkerConnection( WorkerConnection.getPrimordial(), _mutex ); | |
say( "Child Worker " + ID ); | |
say( "state " + child.state ); | |
say( "hello world" ); | |
var langs:Array = [ "fr", "cn", "en" ]; | |
var lang:String = langs[ _randRange(0,2) ]; | |
while( true ) | |
{ | |
if( someOtherCOndition ) | |
{ | |
say( "id: " + ID + " is alive and " + child.state ); | |
_commands.send( "helloworld", lang, ID ); | |
} | |
} | |
say( "child " + ID + " terminated" ); | |
child.terminate(); | |
} | |
} | |
*/ | |
package your.name.here | |
{ | |
import flash.system.Worker; | |
import flash.system.WorkerDomain; | |
import flash.concurrent.Mutex; | |
/** | |
* The WorkerConnection class is modelled after the LocalConnection class | |
* and allow to pass shared properties objects between Workers. | |
* | |
* There are three ways to add callback methods to a WorkerConnection object: | |
* <ul> | |
* <li> | |
* Subclass the WorkerConnection class and add methods. | |
* </li> | |
* <li> | |
* Set the <code>WorkerConnection.client</code> property | |
* to an object that implements the methods. | |
* </li> | |
* <li> | |
* Create a dynamic class that extends LocalConnection | |
* and dynamically attach methods. | |
* </li> | |
* </ul> | |
*/ | |
public class WorkerConnection | |
{ | |
public static function getPrimordial():Worker | |
{ | |
var current:Worker = Worker.current; | |
if( current.isPrimordial ) | |
{ | |
return current; | |
} | |
var wdomain:WorkerDomain = WorkerDomain.current; | |
var workers:Vector.<Worker> = wdomain.listWorkers(); | |
var worker:Worker; | |
for( var i:uint = 0; i < workers.length; i++ ) | |
{ | |
worker = workers[i]; | |
if( worker.isPrimordial ) | |
{ | |
return worker; | |
} | |
} | |
return null; | |
} | |
private const _NAME:String = "commands"; | |
private var _worker:Worker; | |
private var _mutex:Mutex; | |
private var _client:Object; | |
private var _commands:Array; | |
/** | |
* | |
*/ | |
public function WorkerConnection( worker:Worker, mutex:Mutex ) | |
{ | |
super(); | |
_worker = worker; | |
_mutex = mutex; | |
_client = this; | |
_reset(); | |
} | |
private function _reset():void | |
{ | |
_commands = []; | |
_worker.setSharedProperty( _NAME, _commands ); | |
} | |
public function get client():Object | |
{ | |
return _client; | |
} | |
public function set client( value:Object ):void | |
{ | |
_client = value; | |
} | |
public function send( methodName:String, ... arguments ):void | |
{ | |
var data:Array = [ methodName ]; | |
data = data.concat( arguments ); | |
_mutex.lock(); | |
_commands = _worker.getSharedProperty( _NAME ); | |
_commands.push( data ); | |
_worker.setSharedProperty( _NAME, _commands ); | |
_mutex.unlock(); | |
} | |
public function fetch():void | |
{ | |
var i:uint; | |
var command:String; | |
var data:Array; | |
var args:Array; | |
_mutex.lock(); | |
_commands = _worker.getSharedProperty( _NAME ); | |
for( i = 0; i < _commands.length; i++ ) | |
{ | |
data = _commands[i]; | |
if( data && (data.length > 0) ) | |
{ | |
command = data.shift(); | |
args = data; | |
if( _client[command] ) | |
{ | |
if( args && (args.length > 0) ) | |
{ | |
_client[command].apply( _client, args ); | |
} | |
else | |
{ | |
_client[command].apply( _client ); | |
} | |
} | |
} | |
} | |
_reset(); | |
_mutex.unlock(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment