Make command_reference links (eg, in apropos-command) work.
[conkeror.git] / modules / thread.js
blob21ddd83355869d780c69ba65a75d6ec814c5f629
2 var thread_manager = Cc["@mozilla.org/thread-manager;1"].getService();
4 function thread_callback(run_function) {
5     this.run_function = run_function;
7 thread_callback.prototype = {
8     QueryInterface: generate_QI(Ci.nsIRunnable),
9     run: function () {
10         this.run_function.call(null);
11     }
14 function call_in_thread(thread, func) {
15     thread.dispatch(new thread_callback(func), Ci.nsIEventTarget.DISPATCH_NORMAL);
18 function call_in_new_thread(func, success_cont, error_cont) {
19     var thread = thread_manager.newThread(0);
20     var current_thread = thread_manager.currentThread;
21     call_in_thread(thread, function () {
22             try {
23                 var result = func();
24                 call_in_thread(current_thread, function () {
25                         if (success_cont)
26                             success_cont(result);
27                         thread.shutdown();
28                     });
29             } catch (e) {
30                 call_in_thread(current_thread, function () {
31                         if (error_cont)
32                             error_cont(e);
33                         thread.shutdown();
34                     });
35             }
36         });
39 /* Coroutine interface */
40 function in_new_thread(f) {
41     var args = Array.prototype.splice.call(arguments, 1);
42     var cc = yield CONTINUATION;
43     var thread = thread_manager.newThread(0);
44     var current_thread = thread_manager.currentThread;
45     call_in_thread(thread, function () {
46         try {
47             var result = f.apply(null, args);
48             call_in_thread(current_thread, function () {
49                 thread.shutdown();
50                 cc(result);
51             });
52         } catch (e) {
53             call_in_thread(current_thread, function () {
54                 thread.shutdown();
55                 cc.throw(e);
56             });
57         }
58     });
59     var result = yield SUSPEND;
60     yield co_return(result);