PageRenderTime 28ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/scripts/jahova/OS/Cores/Threads/jahova.os.cores.thread.js

https://bitbucket.org/GTL/surveyjs-builder
JavaScript | 582 lines | 382 code | 111 blank | 89 comment | 14 complexity | 1dbae494397100928cdb4a2fbda777ed MD5 | raw file
  1. //JaHOVA OS : Cores : Thread
  2. // Dependent on JaHOVA OS : Cores
  3. /**h* InternalCores/Network
  4. * LINKS
  5. * |html <p><a href="http://jahovaos.com/JaHOVA/Documentation/Full/toc_index.html"> Table of Contents </a></p>
  6. *
  7. * NAME
  8. * JaHOVA OS Multithreading Core
  9. *
  10. * AUTHOR
  11. * Corey Clark
  12. * cclark@coreyclarkphd.com
  13. *
  14. * HISTORY
  15. * Created: 8/6/2011
  16. *
  17. *
  18. * DESCRIPTION
  19. * This Singleton holds the Internal JaHOVA OS Multithreading Core
  20. *
  21. * EXAMPLE
  22. * network = com.jahova.os.Instance().Cores.Instance().Multithreading.Instance();
  23. *
  24. **/
  25. /*
  26. Features
  27. Create Thread
  28. Delayed Execution - Need Execute Method
  29. Immediate Execution
  30. Static Type - loads a .js file
  31. Dynamic Type, creates a blob
  32. Conditional Execution
  33. Only executed after dependencies from other threads have been met
  34. Paused variable, increment for depencies, decrement when finsihed
  35. List of pending threads, calls paused variable of dependent thread
  36. Name assigned by user
  37. Send new inputs to thread
  38. Update Callback function of thread
  39. Create Thread Pool
  40. Set number of workers for given thread
  41. Delete Thread
  42. Set Max Threads
  43. */
  44. com.jahova.os.Instance().Cores.Instance().Threads = (function()
  45. {
  46. var pInstance;
  47. function constructor()
  48. {
  49. //
  50. //PRIVATE ATTRIBUTES
  51. //
  52. var NAME = "JaHOVA OS Internal API : Multithreading Core";
  53. var VERSION = "0v3";
  54. var PATH = "scripts/jahova/OS/Cores/jahova.os.cores.thread.js";
  55. var ID = null;
  56. var os = com.jahova.os.Instance();
  57. var utilities = com.jahova.utilities.Instance();
  58. //
  59. // API Specific Private Variables
  60. //
  61. //
  62. //PRIVATE METHODS
  63. //
  64. //
  65. //Private Classes
  66. //
  67. var CThread = function(Name, Type, Priority, Pool, MaxThreadCount){
  68. this.ID = os.threads.ThreadManager.NextID++;
  69. this.name = Name;
  70. this.type = Type;
  71. this.pool = Pool;
  72. this.frame = null;
  73. this.maxThreadCount = MaxThreadCount;
  74. this.priority = Priority;
  75. this.callbacks = os.resschmgr.Create.Map();
  76. }
  77. CThread.prototype.Suspend = function(input){
  78. var cmd = new CCommand();
  79. cmd.msgType = "SUSPENDTHREAD";
  80. //cmd.data = input;
  81. cmd.priority = 0;
  82. cmd.sendTime = (new Date()).getTime();
  83. cmd.osThreadID = this.ID;
  84. os.threads.ThreadManager.SendToController(cmd.Serialize());
  85. };
  86. CThread.prototype.Resume = function(input){
  87. var cmd = new CCommand();
  88. cmd.msgType = "RESUMETHREAD";
  89. //cmd.data = input;
  90. cmd.priority = 0;
  91. cmd.sendTime = (new Date()).getTime();
  92. cmd.osThreadID = this.ID;
  93. os.threads.ThreadManager.SendToController(cmd.Serialize());
  94. };
  95. CThread.prototype.GetSuspendCount = function(input){
  96. var cmd = new CCommand();
  97. cmd.msgType = "GETSUSPENDCOUNT";
  98. //cmd.data = input;
  99. cmd.priority = 0;
  100. cmd.sendTime = (new Date()).getTime();
  101. cmd.osThreadID = this.ID;
  102. os.threads.ThreadManager.SendToController(cmd.Serialize());
  103. };
  104. CThread.prototype.Execute = function(input){
  105. var cmd = new CCommand();
  106. cmd.msgType = "EXECUTETHREAD";
  107. cmd.data = input;
  108. cmd.priority = 0;
  109. cmd.sendTime = (new Date()).getTime();
  110. cmd.osThreadID = this.ID;
  111. os.threads.ThreadManager.SendToController(cmd.Serialize());
  112. };
  113. CThread.prototype.SetPriority = function(input){
  114. var cmd = new CCommand();
  115. cmd.msgType = "SETPRIORITY";
  116. cmd.data = input;
  117. cmd.priority = 0;
  118. cmd.sendTime = (new Date()).getTime();
  119. cmd.osThreadID = this.ID;
  120. os.threads.ThreadManager.SendToController(cmd.Serialize());
  121. };
  122. CThread.prototype.GetPriority = function(){
  123. var cmd = new CCommand();
  124. cmd.msgType = "GETPRIORITY";
  125. //cmd.data = input;
  126. cmd.priority = 0;
  127. cmd.sendTime = (new Date()).getTime();
  128. cmd.osThreadID = this.ID;
  129. os.threads.ThreadManager.SendToController(cmd.Serialize());
  130. };
  131. CThread.prototype.AddTask = function(input){
  132. var cmd = new CCommand();
  133. cmd.msgType = "ADDTASK";
  134. cmd.data = input;
  135. cmd.priority = 0;
  136. cmd.sendTime = (new Date()).getTime();
  137. cmd.osThreadID = this.ID;
  138. os.threads.ThreadManager.SendToController(cmd.Serialize());
  139. };
  140. CThread.prototype.GetPendingTask = function(){
  141. var cmd = new CCommand();
  142. cmd.msgType = "GETPENDINGTASK";
  143. //cmd.data = input;
  144. cmd.priority = 0;
  145. cmd.sendTime = (new Date()).getTime();
  146. cmd.osThreadID = this.ID;
  147. os.threads.ThreadManager.SendToController(cmd.Serialize());
  148. };
  149. CThread.prototype.Exit = function(){
  150. var cmd = new CCommand();
  151. cmd.msgType = "EXITTHREAD";
  152. //cmd.data = input;
  153. cmd.priority = 0;
  154. cmd.sendTime = (new Date()).getTime();
  155. cmd.osThreadID = this.ID;
  156. os.threads.ThreadManager.SendToController(cmd.Serialize());
  157. };
  158. CThread.prototype.SetMaxThreadCount = function(input){
  159. var cmd = new CCommand();
  160. cmd.msgType = "SETMAXTHREADCOUNT";
  161. cmd.data = input;
  162. cmd.priority = 0;
  163. cmd.sendTime = (new Date()).getTime();
  164. cmd.osThreadID = this.ID;
  165. os.threads.ThreadManager.SendToController(cmd.Serialize());
  166. };
  167. CThread.prototype.GetFunction = function(){
  168. var cmd = new CCommand();
  169. cmd.msgType = "GETFUNCTION";
  170. //cmd.data = input;
  171. cmd.priority = 0;
  172. cmd.sendTime = (new Date()).getTime();
  173. cmd.osThreadID = this.ID;
  174. os.threads.ThreadManager.SendToController(cmd.Serialize());
  175. };
  176. CThread.prototype.GetCurrentTask = function(){
  177. var cmd = new CCommand();
  178. cmd.msgType = "GETCURRENTTASK";
  179. //cmd.data = input;
  180. cmd.priority = 0;
  181. cmd.sendTime = (new Date()).getTime();
  182. cmd.osThreadID = this.ID;
  183. os.threads.ThreadManager.SendToController(cmd.Serialize());
  184. };
  185. CThread.prototype.GetFilename = function(){
  186. var cmd = new CCommand();
  187. cmd.msgType = "GETFILENAME";
  188. //cmd.data = input;
  189. cmd.priority = 0;
  190. cmd.sendTime = (new Date()).getTime();
  191. cmd.osThreadID = this.ID;
  192. os.threads.ThreadManager.SendToController(cmd.Serialize());
  193. };
  194. CThread.prototype.AddCallback = function(){
  195. };
  196. CThread.prototype.RemoveCallback = function(){
  197. };
  198. CThread.prototype.GetStatus = function(){
  199. var cmd = new CCommand();
  200. cmd.msgType = "GETTHREADSTATUS";
  201. //cmd.data = input;
  202. cmd.priority = 0;
  203. cmd.sendTime = (new Date()).getTime();
  204. cmd.osThreadID = this.ID;
  205. os.threads.ThreadManager.SendToController(cmd.Serialize());
  206. };
  207. var CCommand = function(){
  208. this.msgID = os.threads.ThreadManager.NextMessageID++;
  209. this.msgType;
  210. this.data;
  211. this.priority;
  212. this.sendTime;
  213. this.deliveryTime;
  214. this.osThreadID;
  215. this.cntrlThreadID;
  216. this.callbackCoreID;
  217. this.callbackFunctionID;
  218. };
  219. CCommand.prototype.Serialize = function(){
  220. return JSON.stringify(this);
  221. };
  222. CCommand.prototype.Parse = function(input){
  223. var temp = JSON.parse(input);
  224. this.msgID = temp.msgID;
  225. this.msgType = temp.msgType;
  226. this.data = temp.data;
  227. this.priority = temp.priority;
  228. this.sendTime = temp.sendTime;
  229. this.deliveryTime = temp.deliveryTime;
  230. this.osThreadID = temp.osThreadID;
  231. this.cntrlThreadID = temp.cntrlThreadID;
  232. this.callbackCoreID = temp.callbackCoreID;
  233. this.callbackFunctionID = temp.callbackFunctionID;
  234. };
  235. return{
  236. //PUBLIC ATTRIBUTES
  237. //PUBLIC PRIVILEDGE METHODS
  238. GetName: function(){
  239. return NAME;
  240. },
  241. GetVersion: function(){
  242. return VERSION;
  243. },
  244. GetPath: function(){
  245. return PATH;
  246. },
  247. GetID: function(){
  248. return ID;
  249. },
  250. Initialize: function(){
  251. //Initialize Thread Manager
  252. this.ThreadManager.Initialize();
  253. },
  254. //PUBLIC PRIVILEDGE OBJECTS
  255. ThreadManager: {
  256. NextID: 0,
  257. SharedWorkers: false,
  258. NextMessageID: 0,
  259. ThreadController: null,
  260. ThreadControllerPath: null,
  261. SWThreadControllerPath: null,
  262. ThreadPath:null,
  263. ThreadNameMap: null,
  264. ThreadIDMap: null,
  265. ThreadCallbacksMap: null,
  266. PendingCommandsMap: null,
  267. CommandFunctionsMap: null,
  268. Initialize: function(){
  269. os.console.AppendComment("Initializing Thread Core")
  270. //Create Maps
  271. os.console.AppendComment(" Creating Thread Maps");
  272. this.ThreadNameMap = os.resschmgr.Create.Map();
  273. this.ThreadIDMap = os.resschmgr.Create.Map();
  274. this.ThreadCallbacksMap = os.resschmgr.Create.Map();
  275. this.PendingCommandsMap = os.resschmgr.Create.Map();
  276. this.CommandFunctionsMap = os.resschmgr.Create.Map();
  277. //Set Controller Path
  278. this.ThreadControllerPath = "scripts/jahova/OS/Cores/Threads/jahova.os.cores.thread.controller.js";
  279. this.SWThreadControllerPath = "scripts/jahova/OS/Cores/Threads/jahova.os.cores.thread.swcontroller.js";
  280. os.console.AppendComment(" Path Set: " + this.ThreadControllerPath);
  281. //Set Dynamic Thread Path
  282. this.ThreadPath = 'scripts/jahova/OS/Cores/Threads/jahova.os.cores.thread.dynamic.js'
  283. os.console.AppendComment(" Set Dynamic Thread Path");
  284. //Testing for Web Worker Support
  285. if(window.Worker)
  286. {
  287. os.console.AppendComment(" Web Workers Supported: true");
  288. //Testing for Shared Worker capability
  289. if(window.SharedWorker)
  290. {
  291. this.SharedWorkers = true;
  292. }
  293. os.console.AppendComment(" Shared Workers Supported: " + this.SharedWorkers);
  294. }
  295. else
  296. {
  297. os.console.AppendComment(" Web Workers Supported: false");
  298. }
  299. },
  300. CreateThread: function(name, type, priority, suspend, callback, filename, input, pool, maxThreadCount){
  301. var thread = null
  302. //Verify name does not already exist
  303. if(!this.ThreadNameMap.get(name))
  304. {
  305. //Create Thread Object
  306. thread = new CThread(name, type, priority, pool, maxThreadCount);
  307. if(callback){thread.callbacks.put(callback, callback);};
  308. if(this.SharedWorkers)
  309. {
  310. thread.frame = document.createElement("iframe");
  311. thread.frame.style.display = "none";
  312. thread.frame.name = thread.name;
  313. thread.frame.id = thread.ID;
  314. document.body.appendChild(thread.frame);
  315. var script = document.createElement("script");
  316. script.type = "text/javascript";
  317. script.src = "scripts/jahova/OS/Cores/Threads/jahova.os.cores.thread.swdynamic.js";
  318. thread.frame.contentDocument.head.appendChild(script);
  319. }
  320. //Register with the Thread Manager Maps
  321. this.ThreadIDMap.put(thread.ID, thread);
  322. this.ThreadNameMap.put(thread.name, thread);
  323. // {id, name, type, priority, pooled, maxPooledThreads, suspend, filename, data{func: , input: }}
  324. var data ={
  325. id: thread.ID,
  326. name: thread.name,
  327. type: thread.type,
  328. priority: thread.priority,
  329. pooled: thread.pool,
  330. maxPooledThreads: thread.maxThreadCount,
  331. suspend: suspend,
  332. filename: filename,
  333. data:{
  334. func: filename,
  335. input: input
  336. }
  337. }
  338. //Send CCommand to Thread Controller to create Thread
  339. var cmd = new CCommand();
  340. cmd.msgType = "CreateThread";
  341. cmd.data = JSON.stringify(data);
  342. cmd.priority = priority;
  343. cmd.sendTime = (new Date()).getTime();
  344. cmd.deliveryTime;
  345. cmd.osThreadID = thread.ID;
  346. cmd.cntrlThreadID = null;
  347. cmd.callbackCoreID = callback;
  348. cmd.callbackFunctionID;
  349. this.SendToController(cmd.Serialize());
  350. }
  351. return thread;
  352. },
  353. ExitThread: function(){
  354. },
  355. GetFunctionBody: function(func){
  356. var start = func.indexOf("{") + 1;
  357. var end = func.lastIndexOf("}");
  358. var body = func.slice(start,end);
  359. return body;
  360. },
  361. GetThreadByName: function(name){
  362. return this.ThreadNameMap.get(name);
  363. },
  364. GetThreadByID: function(){
  365. },
  366. CreateThreadController: function(){
  367. if(this.ThreadController == null)
  368. {
  369. //os.console.AppendComment("Controller Doesnt Exist");
  370. if(window.Worker)
  371. {
  372. //os.console.AppendComment("Workers Exist");
  373. if(this.SharedWorkers)
  374. {
  375. //os.console.AppendComment("Creating Shared Worker");
  376. this.ThreadController = new SharedWorker(this.SWThreadControllerPath);
  377. this.ThreadController.port.start();
  378. this.ThreadController.port.onmessage = this.ProcessControllerRequest;
  379. var cmd = new CCommand();
  380. cmd.msgType = "Start";
  381. cmd.data = "Core Requesting Controller Initialize";
  382. }
  383. else
  384. {
  385. //os.console.AppendComment("Creating Nested Worker");
  386. this.ThreadController = new Worker(this.ThreadControllerPath);
  387. this.ThreadController.addEventListener('message',this.ProcessControllerRequest ,false);
  388. var cmd = new CCommand();
  389. cmd.msgType = "Start";
  390. cmd.data = "Core Requesting Controller Initialize";
  391. }
  392. this.SendToController(cmd.Serialize());
  393. }
  394. else{
  395. os.console.Comment("");
  396. os.console.Warning("\nCan not create Thread Controller \nYour Browser Does Not Support Web Workers");
  397. }
  398. }
  399. else
  400. {
  401. os.console.Comment("");
  402. os.console.Warning("Thread Controller already exist");
  403. }
  404. },
  405. SendToController: function(JSONcmd){
  406. if(this.SharedWorkers){os.threads.ThreadManager.ThreadController.port.postMessage(JSONcmd);}
  407. else{os.threads.ThreadManager.ThreadController.postMessage(JSONcmd);}
  408. },
  409. ProcessControllerRequest: function(e){
  410. //Parse incomming Command
  411. var cmd = new CCommand();
  412. cmd.Parse(e.data);
  413. var thread = os.threads.ThreadManager.ThreadIDMap.get(cmd.osThreadID);
  414. //Test to see if thread exist
  415. if(thread)
  416. {
  417. numOfCallbacks = thread.callbacks.size;
  418. if(numOfCallbacks > 0)
  419. {
  420. for(var i = 0; i++ < numOfCallbacks; thread.callbacks.next())
  421. {
  422. (thread.callbacks.value())(cmd);
  423. }
  424. }
  425. else
  426. {
  427. os.console.AppendComment("\nNo Callback Assigned For Thread");
  428. os.console.AppendComment("Message Type: " + cmd.msgType +"\nMessage Data: " + cmd.data);
  429. }
  430. }
  431. else
  432. {
  433. os.console.AppendComment("\nNo Thread Found to Handle Command Callback");
  434. os.console.AppendComment("Message Type: " + cmd.msgType +"\nMessage Data: " + cmd.data);
  435. }
  436. os.console.Comment("");
  437. },
  438. CreateSocket: function(){
  439. var cmd = new CCommand();
  440. cmd.msgType = "CREATESOCKET";
  441. //cmd.data = input;
  442. cmd.priority = 0;
  443. cmd.sendTime = (new Date()).getTime();
  444. cmd.osThreadID = this.ID;
  445. os.threads.ThreadManager.SendToController(cmd.Serialize());
  446. },
  447. CloseSocket: function(){
  448. var cmd = new CCommand();
  449. cmd.msgType = "CLOSESOCKET";
  450. //cmd.data = input;
  451. cmd.priority = 0;
  452. cmd.sendTime = (new Date()).getTime();
  453. cmd.osThreadID = this.ID;
  454. os.threads.ThreadManager.SendToController(cmd.Serialize());
  455. }
  456. }
  457. }
  458. }
  459. return {
  460. //OBJECT ACCESSOR
  461. Instance: function()
  462. {
  463. if(!pInstance)
  464. {
  465. //Instantiate if pInstance does not exist
  466. pInstance = constructor();
  467. }
  468. return pInstance;
  469. }
  470. }
  471. })();