Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
DMDesign
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Amanda Ghassaei
DMDesign
Commits
1d5a4109
Commit
1d5a4109
authored
10 years ago
by
Amanda Ghassaei
Browse files
Options
Downloads
Patches
Plain Diff
task are deallocated after they are finished
parent
82bfca72
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
js/main.js
+6
-2
6 additions, 2 deletions
js/main.js
js/persistentWorkers.js
+47
-20
47 additions, 20 deletions
js/persistentWorkers.js
with
53 additions
and
22 deletions
js/main.js
+
6
−
2
View file @
1d5a4109
...
...
@@ -7,12 +7,16 @@ $(function(){
three
=
Three
();
setupNavBar
();
workers
=
persistentWorkers
(
4
);
workers
=
persistentWorkers
(
8
);
workers
.
map
([
11
,
12
,
13
,
14
,
15
,
18
,
30
],
executable
,
{});
workers
.
map
([
11
,
12
],
executable
,
{}
,
incrCallback
);
function
executable
(){
return
arg
*
arg
;
}
function
incrCallback
(
result
){
console
.
log
(
result
);
}
});
This diff is collapsed.
Click to expand it.
js/persistentWorkers.js
+
47
−
20
View file @
1d5a4109
...
...
@@ -17,55 +17,82 @@ function persistentWorkers(numWorkers){
var
workerURL
=
makeBlobURL
(
URL
,
myWorker
);
for
(
var
i
=
0
;
i
<
numWorkers
;
i
++
){
var
worker
=
new
Worker
(
workerURL
);
worker
.
onmessage
=
workerCallback
;
worker
.
onmessage
=
onMessage
;
worker
.
postMessage
({
url
:
document
.
location
.
toString
()});
allWorkers
.
push
(
worker
);
}
URL
.
revokeObjectURL
(
workerURL
);
function
map
(
data
,
executable
,
env
){
function
map
(
data
,
executable
,
env
,
incrCallback
){
//save args in map queue
mapQueue
.
push
({
data
:
data
,
executable
:
executable
,
env
:
env
,
index
:
0
});
mapQueue
.
push
({
data
:
data
,
executable
:
executable
,
env
:
env
,
index
:
0
,
incrCallback
:
incrCallback
,
finished
:
false
,
activeThreads
:
0
});
for
(
var
i
=
0
;
i
<
allWorkers
.
length
;
i
++
){
allWorkers
[
i
].
postMessage
({
isWorking
:
true
});
//ask workers if they are busy
}
}
function
workerCallback
(
e
){
function
onMessage
(
e
){
if
(
e
.
data
.
result
)
console
.
log
(
e
.
data
.
result
);
//handle result first
if
(
e
.
data
.
result
)
{
//handle result first
if
(
e
.
data
.
isWorking
===
false
){
//get next work item off queue
//get current work item off queue
if
(
mapQueue
.
length
==
0
)
return
;
var
currentTask
=
mapQueue
[
0
];
var
currentIndex
=
currentTask
.
index
;
currentTask
.
index
=
currentTask
.
index
+
1
;
currentTask
.
activeThreads
--
;
//decrement active threads
//check that the index is not out of bounds
if
(
currentTask
.
data
.
length
<=
currentIndex
){
currentTask
.
incrCallback
(
e
.
data
.
result
);
//incremental callback
if
(
currentTask
.
finished
&&
currentTask
.
activeThreads
==
0
){
mapQueue
.
shift
();
//remove first element
e
.
data
.
result
=
null
;
//remove result so it doesn't get handled twice
workerCallback
(
e
);
//try again in case there is another item in the queue
return
;
console
.
log
(
"
end of task
"
);
}
}
var
nextTask
=
getNextTask
(
mapQueue
[
0
],
0
);
if
(
!
nextTask
)
return
;
var
currentIndex
=
nextTask
.
index
;
nextTask
.
index
++
;
//check that the index is not out of bounds
if
(
nextTask
.
data
.
length
<=
currentIndex
){
nextTask
.
finished
=
true
;
e
.
data
.
result
=
null
;
//remove result so it doesn't get handled twice
onMessage
(
e
);
//try again in case there is another item in the queue to start
return
;
}
if
(
e
.
data
.
isWorking
===
false
){
nextTask
.
activeThreads
++
;
e
.
target
.
postMessage
({
arg
:
currentTask
.
data
[
currentIndex
],
localEnv
:
currentTask
.
env
,
executable
:
currentTask
.
executable
.
toString
()});
arg
:
nextTask
.
data
[
currentIndex
],
localEnv
:
nextTask
.
env
,
executable
:
nextTask
.
executable
.
toString
()});
}
}
function
getNextTask
(
currentTask
,
index
){
if
(
!
currentTask
)
return
null
;
if
(
currentTask
.
finished
){
var
nextIndex
=
index
+
1
;
if
(
mapQueue
.
length
<=
nextIndex
)
return
null
;
return
getNextTask
(
mapQueue
[
nextIndex
]);
}
else
{
return
currentTask
;
}
}
function
incrementalCallback
(){
}
function
makeBlobURL
(
URL
,
someFunction
)
{
var
blob
=
new
Blob
([
"
(
"
+
someFunction
.
toString
()
+
"
)()
"
],
{
type
:
"
text/javascript
"
});
return
URL
.
createObjectURL
(
blob
);
}
return
{
map
:
map
}
//return all public methods and vars
return
{
map
:
map
}
;
//return all public methods and vars
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment