Browse Source

Merge pull request #306 from herby/forkpool-cleaning

super initialize, small cleaning, Queue class comment
Nicolas Petton 12 years ago
parent
commit
096dd5e9d6

+ 1 - 0
js/Kernel-Collections.deploy.js

@@ -3499,6 +3499,7 @@ smalltalk.method({
 selector: "initialize",
 fn: function (){
 var self=this;
+smalltalk.send(self,"_initialize",[],smalltalk.Object);
 self["@read"]=[];
 self["@readIndex"]=(1);
 self["@write"]=smalltalk.send((smalltalk.OrderedCollection || OrderedCollection),"_new",[]);

+ 4 - 2
js/Kernel-Collections.js

@@ -4623,6 +4623,7 @@ smalltalk.Set);
 
 
 smalltalk.addClass('Queue', smalltalk.Object, ['read', 'readIndex', 'write'], 'Kernel-Collections');
+smalltalk.Queue.comment="I am a one-sided Queue.\x0a\x0aI use two OrderedCollections inside,\x0a`read` is at the front, is not modified and only read using `readIndex`.\x0a`write` is at the back and is appended new items.\x0aWhen `read` is exhausted, `write` is promoted to `read` and new `write` is created.\x0a\x0aAs a consequence, no data moving is done by me; write appending may do data moving\x0awhen growing `write`, but this is left to engine to implement as good as it chooses to."
 smalltalk.addMethod(
 "_back_",
 smalltalk.method({
@@ -4711,13 +4712,14 @@ selector: "initialize",
 category: 'initialization',
 fn: function (){
 var self=this;
+smalltalk.send(self,"_initialize",[],smalltalk.Object);
 self["@read"]=[];
 self["@readIndex"]=(1);
 self["@write"]=smalltalk.send((smalltalk.OrderedCollection || OrderedCollection),"_new",[]);
 return self},
 args: [],
-source: "initialize\x0a\x09read := #().\x0a    readIndex := 1.\x0a    write := OrderedCollection new",
-messageSends: ["new"],
+source: "initialize\x0a\x09super initialize.\x0a\x09read := #().\x0a    readIndex := 1.\x0a    write := OrderedCollection new",
+messageSends: ["initialize", "new"],
 referencedClasses: ["OrderedCollection"]
 }),
 smalltalk.Queue);

+ 19 - 6
js/Kernel-Methods.deploy.js

@@ -496,13 +496,25 @@ smalltalk.method({
 selector: "initialize",
 fn: function (){
 var self=this;
-var $1;
-var sentinel;
+smalltalk.send(self,"_initialize",[],smalltalk.Object);
 self["@poolSize"]=(0);
 self["@maxPoolSize"]=smalltalk.send(smalltalk.send(self,"_class",[]),"_defaultMaxPoolSize",[]);
 self["@queue"]=smalltalk.send((smalltalk.Queue || Queue),"_new",[]);
+self["@worker"]=smalltalk.send(self,"_makeWorker",[]);
+return self}
+}),
+smalltalk.ForkPool);
+
+smalltalk.addMethod(
+"_makeWorker",
+smalltalk.method({
+selector: "makeWorker",
+fn: function (){
+var self=this;
+var $2,$1;
+var sentinel;
 sentinel=smalltalk.send((smalltalk.Object || Object),"_new",[]);
-self["@worker"]=(function(){
+$1=(function(){
 var block;
 self["@poolSize"]=smalltalk.send(self["@poolSize"],"__minus",[(1)]);
 self["@poolSize"];
@@ -510,8 +522,8 @@ block=smalltalk.send(self["@queue"],"_frontIfAbsent_",[(function(){
 return sentinel;
 })]);
 block;
-$1=smalltalk.send(block,"__eq_eq",[sentinel]);
-if(! smalltalk.assert($1)){
+$2=smalltalk.send(block,"__eq_eq",[sentinel]);
+if(! smalltalk.assert($2)){
 return smalltalk.send((function(){
 return smalltalk.send(block,"_value",[]);
 }),"_ensure_",[(function(){
@@ -519,7 +531,8 @@ return smalltalk.send(self,"_addWorker",[]);
 })]);
 };
 });
-return self}
+return $1;
+}
 }),
 smalltalk.ForkPool);
 

+ 27 - 9
js/Kernel-Methods.js

@@ -684,13 +684,30 @@ selector: "initialize",
 category: 'initialization',
 fn: function (){
 var self=this;
-var $1;
-var sentinel;
+smalltalk.send(self,"_initialize",[],smalltalk.Object);
 self["@poolSize"]=(0);
 self["@maxPoolSize"]=smalltalk.send(smalltalk.send(self,"_class",[]),"_defaultMaxPoolSize",[]);
 self["@queue"]=smalltalk.send((smalltalk.Queue || Queue),"_new",[]);
+self["@worker"]=smalltalk.send(self,"_makeWorker",[]);
+return self},
+args: [],
+source: "initialize\x0a    super initialize.\x0a\x09poolSize := 0.\x0a    maxPoolSize := self class defaultMaxPoolSize.\x0a    queue := Queue new.\x0a    worker := self makeWorker",
+messageSends: ["initialize", "defaultMaxPoolSize", "class", "new", "makeWorker"],
+referencedClasses: ["Queue"]
+}),
+smalltalk.ForkPool);
+
+smalltalk.addMethod(
+"_makeWorker",
+smalltalk.method({
+selector: "makeWorker",
+category: 'initialization',
+fn: function (){
+var self=this;
+var $2,$1;
+var sentinel;
 sentinel=smalltalk.send((smalltalk.Object || Object),"_new",[]);
-self["@worker"]=(function(){
+$1=(function(){
 var block;
 self["@poolSize"]=smalltalk.send(self["@poolSize"],"__minus",[(1)]);
 self["@poolSize"];
@@ -698,8 +715,8 @@ block=smalltalk.send(self["@queue"],"_frontIfAbsent_",[(function(){
 return sentinel;
 })]);
 block;
-$1=smalltalk.send(block,"__eq_eq",[sentinel]);
-if(! smalltalk.assert($1)){
+$2=smalltalk.send(block,"__eq_eq",[sentinel]);
+if(! smalltalk.assert($2)){
 return smalltalk.send((function(){
 return smalltalk.send(block,"_value",[]);
 }),"_ensure_",[(function(){
@@ -707,11 +724,12 @@ return smalltalk.send(self,"_addWorker",[]);
 })]);
 };
 });
-return self},
+return $1;
+},
 args: [],
-source: "initialize\x0a\x09| sentinel |\x0a\x09poolSize := 0.\x0a    maxPoolSize := self class defaultMaxPoolSize.\x0a    queue := Queue new.\x0a    sentinel := Object new.\x0a    worker := [\x0a\x09\x09| block |\x0a        poolSize := poolSize - 1.\x0a\x09\x09block := queue frontIfAbsent: [ sentinel ].\x0a        block == sentinel ifFalse: [\x0a        \x09[ block value ] ensure: [ self addWorker ]]].",
-messageSends: ["defaultMaxPoolSize", "class", "new", "-", "frontIfAbsent:", "ifFalse:", "ensure:", "addWorker", "value", "=="],
-referencedClasses: ["Queue", "Object"]
+source: "makeWorker\x0a\x09| sentinel |\x0a    sentinel := Object new.\x0a    ^[ | block |\x0a        poolSize := poolSize - 1.\x0a\x09\x09block := queue frontIfAbsent: [ sentinel ].\x0a        block == sentinel ifFalse: [\x0a        \x09[ block value ] ensure: [ self addWorker ]]]",
+messageSends: ["new", "-", "frontIfAbsent:", "ifFalse:", "ensure:", "addWorker", "value", "=="],
+referencedClasses: ["Object"]
 }),
 smalltalk.ForkPool);
 

+ 11 - 0
st/Kernel-Collections.st

@@ -1490,6 +1490,16 @@ includes: anObject
 Object subclass: #Queue
 	instanceVariableNames: 'read readIndex write'
 	package: 'Kernel-Collections'!
+!Queue commentStamp!
+I am a one-sided Queue.
+
+I use two OrderedCollections inside,
+`read` is at the front, is not modified and only read using `readIndex`.
+`write` is at the back and is appended new items.
+When `read` is exhausted, `write` is promoted to `read` and new `write` is created.
+
+As a consequence, no data moving is done by me; write appending may do data moving
+when growing `write`, but this is left to engine to implement as good as it chooses to.!
 
 !Queue methodsFor: 'accessing'!
 
@@ -1519,6 +1529,7 @@ frontIfAbsent: aBlock
 !Queue methodsFor: 'initialization'!
 
 initialize
+	super initialize.
 	read := #().
     readIndex := 1.
     write := OrderedCollection new

+ 8 - 4
st/Kernel-Methods.st

@@ -231,17 +231,21 @@ fork: aBlock
 !ForkPool methodsFor: 'initialization'!
 
 initialize
-	| sentinel |
+    super initialize.
 	poolSize := 0.
     maxPoolSize := self class defaultMaxPoolSize.
     queue := Queue new.
+    worker := self makeWorker
+!
+
+makeWorker
+	| sentinel |
     sentinel := Object new.
-    worker := [
-		| block |
+    ^[ | block |
         poolSize := poolSize - 1.
 		block := queue frontIfAbsent: [ sentinel ].
         block == sentinel ifFalse: [
-        	[ block value ] ensure: [ self addWorker ]]].
+        	[ block value ] ensure: [ self addWorker ]]]
 ! !
 
 ForkPool class instanceVariableNames: 'default'!