thread.ts 4.5 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
A
tslint  
Alex Dima 已提交
5
'use strict';
E
Erich Gamma 已提交
6

A
Alex Dima 已提交
7
import {TPromise} from 'vs/base/common/winjs.base';
E
Erich Gamma 已提交
8 9 10 11 12
import descriptors = require('vs/platform/instantiation/common/descriptors');
import instantiation = require('vs/platform/instantiation/common/instantiation');

// --- thread service (web workers)

B
Benjamin Pasero 已提交
13
export const IThreadService = instantiation.createDecorator<IThreadService>('threadService');
E
Erich Gamma 已提交
14 15

export interface IThreadService {
B
Benjamin Pasero 已提交
16
	serviceId: instantiation.ServiceIdentifier<any>;
E
Erich Gamma 已提交
17 18 19 20 21 22 23

	// --- BEGIN deprecated methods
	isInMainThread: boolean;

	addStatusListener(listener: IThreadServiceStatusListener): void;
	removeStatusListener(listener: IThreadServiceStatusListener): void;

24 25
	OneWorker(obj: IThreadSynchronizableObject, methodName: string, target: Function, param: any[], affinity: ThreadAffinity): TPromise<any>;
	AllWorkers(obj: IThreadSynchronizableObject, methodName: string, target: Function, param: any[]): TPromise<any>;
E
Erich Gamma 已提交
26

27 28 29 30
	createInstance<T extends IThreadSynchronizableObject>(ctor: instantiation.INewConstructorSignature0<T>): T;
	createInstance<A1, T extends IThreadSynchronizableObject>(ctor: instantiation.INewConstructorSignature1<A1, T>, a1: A1): T;
	createInstance<A1, A2, T extends IThreadSynchronizableObject>(ctor: instantiation.INewConstructorSignature2<A1, A2, T>, a1: A1, a2: A2): T;
	createInstance<A1, A2, A3, T extends IThreadSynchronizableObject>(ctor: instantiation.INewConstructorSignature3<A1, A2, A3, T>, a1: A1, a2: A2, a3: A3): T;
E
Erich Gamma 已提交
31

32 33 34 35
	createInstance<T extends IThreadSynchronizableObject>(descriptor: descriptors.AsyncDescriptor0<T>): T;
	createInstance<A1, T extends IThreadSynchronizableObject>(descriptor: descriptors.AsyncDescriptor1<A1, T>, a1: A1): T;
	createInstance<A1, A2, T extends IThreadSynchronizableObject>(descriptor: descriptors.AsyncDescriptor2<A1, A2, T>, a1: A1, a2: A2): T;
	createInstance<A1, A2, A3, T extends IThreadSynchronizableObject>(descriptor: descriptors.AsyncDescriptor3<A1, A2, A3, T>, a1: A1, a2: A2, a3: A3): T;
E
Erich Gamma 已提交
36 37 38 39 40 41 42 43 44

	// --- END deprecated methods

	getRemotable<T>(ctor: instantiation.INewConstructorSignature0<T>): T;

	registerRemotableInstance(ctor: any, instance: any): void;
}

export class IRemotableCtorMap {
B
Benjamin Pasero 已提交
45
	[identifier: string]: Function;
E
Erich Gamma 已提交
46 47 48
}

export class IRemotableCtorAffinityMap {
B
Benjamin Pasero 已提交
49
	[identifier: string]: {
E
Erich Gamma 已提交
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
		ctor: Function;
		affinity: ThreadAffinity;
	};
}

export class Remotable {

	private static PROP_NAME = '$__REMOTABLE_ID';

	public static Registry = {
		MainContext: <IRemotableCtorMap>Object.create(null),
		PluginHostContext: <IRemotableCtorMap>Object.create(null),
		WorkerContext: <IRemotableCtorAffinityMap>Object.create(null),
	};

	public static getId(ctor: any): string {
		return (ctor[Remotable.PROP_NAME] || null);
	}

	public static MainContext(identifier: string) {
B
Benjamin Pasero 已提交
70
		return function(target: Function) {
E
Erich Gamma 已提交
71 72 73
			Remotable._ensureUnique(identifier);
			Remotable.Registry.MainContext[identifier] = target;
			target[Remotable.PROP_NAME] = identifier;
A
tslint  
Alex Dima 已提交
74
		};
E
Erich Gamma 已提交
75 76 77
	}

	public static PluginHostContext(identifier: string) {
B
Benjamin Pasero 已提交
78
		return function(target: Function) {
E
Erich Gamma 已提交
79 80 81
			Remotable._ensureUnique(identifier);
			Remotable.Registry.PluginHostContext[identifier] = target;
			target[Remotable.PROP_NAME] = identifier;
A
tslint  
Alex Dima 已提交
82
		};
E
Erich Gamma 已提交
83 84
	}

B
Benjamin Pasero 已提交
85 86
	public static WorkerContext(identifier: string, whichWorker: ThreadAffinity) {
		return function(target: Function) {
E
Erich Gamma 已提交
87 88 89 90 91 92
			Remotable._ensureUnique(identifier);
			Remotable.Registry.WorkerContext[identifier] = {
				ctor: target,
				affinity: whichWorker
			};
			target[Remotable.PROP_NAME] = identifier;
A
tslint  
Alex Dima 已提交
93
		};
E
Erich Gamma 已提交
94 95
	}

B
Benjamin Pasero 已提交
96
	private static _ensureUnique(identifier: string): void {
E
Erich Gamma 已提交
97 98 99 100 101 102
		if (Remotable.Registry.MainContext[identifier] || Remotable.Registry.PluginHostContext[identifier] || Remotable.Registry.WorkerContext[identifier]) {
			throw new Error('Duplicate Remotable identifier found');
		}
	}
}

103
export interface IThreadSynchronizableObject {
B
Benjamin Pasero 已提交
104
	getId(): string;
E
Erich Gamma 已提交
105

B
Benjamin Pasero 已提交
106
	creationDone?: () => void;
E
Erich Gamma 已提交
107

B
Benjamin Pasero 已提交
108
	asyncCtor?: () => TPromise<void>;
E
Erich Gamma 已提交
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
}

export enum ThreadAffinity {
	None = 0,
	Group1 = 1,
	Group2 = 2,
	Group3 = 3,
	Group4 = 4,
	Group5 = 5,
	Group6 = 6,
	Group7 = 7,
	Group8 = 8,
	Group9 = 9,
	All = 10
}

export interface IWorkerStatus {
B
Benjamin Pasero 已提交
126
	queueSize: number;
E
Erich Gamma 已提交
127 128 129 130 131 132 133
}

export interface IThreadServiceStatus {
	workers: IWorkerStatus[];
}

export interface IThreadServiceStatusListener {
B
Benjamin Pasero 已提交
134
	onThreadServiceStatus(status: IThreadServiceStatus): void;
E
Erich Gamma 已提交
135
}