1 /** 2 Copyright: 2018 Mark Fisher 3 4 License: 5 Permission is hereby granted, free of charge, to any person obtaining a copy of 6 this software and associated documentation files (the "Software"), to deal in 7 the Software without restriction, including without limitation the rights to 8 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 9 of the Software, and to permit persons to whom the Software is furnished to do 10 so, subject to the following conditions: 11 12 The above copyright notice and this permission notice shall be included in all 13 copies or substantial portions of the Software. 14 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 SOFTWARE. 22 **/ 23 module dxx.app.workbench; 24 25 private import std.exception; 26 27 /* private import hunt.cache; */ 28 29 private import dxx.util; 30 31 private import dxx.app.resource.workspace; 32 private import dxx.app.job; 33 private import dxx.app.platform; 34 35 /++ 36 The Workbench provides a framework for synchronizing job access to 37 platform resources. 38 39 Only one single workbench job can run over a particular 40 workbench. The `waitLock` method blocks until the previous job has 41 terminated. The `lock` method throws an exception if the previous job 42 has not terminated. 43 44 The idea is that we don't have to worry about serializing access to the 45 underlying resources (for example, the Workspace). We can create 46 instances of WorkbenchJob and execute these in a thread pool - the 47 workbench ensures that no conflicts occur. 48 +/ 49 interface Workbench { 50 static Workbench getCurrent() { 51 return resolveInjector!Workbench("app.workbench"); 52 } 53 //Workbench getWorkbench(); 54 void lock(shared(WorkbenchJob)) shared; 55 void unlock(shared(WorkbenchJob)) shared; 56 void waitLock(shared(WorkbenchJob)) shared; 57 bool isLocked() shared; 58 } 59 60 final class WorkbenchDefault : 61 SyncNotificationSource, Workbench { 62 /* UCache resourceCache; 63 this() { 64 resourceCache = UCache.CreateUCache(); 65 } */ 66 shared(WorkbenchJob)* currentJob; 67 shared 68 void lock(shared(WorkbenchJob) j) { 69 enforce(this.currentJob is null); 70 this.currentJob = &j; 71 } 72 shared 73 void unlock(shared(WorkbenchJob) j) { 74 enforce(this.currentJob == &j); 75 this.currentJob = null; 76 } 77 shared 78 void waitLock(shared(WorkbenchJob) j) { 79 while(currentJob !is null) { 80 JobBase.join(*currentJob); 81 } 82 lock(j); 83 } 84 shared 85 bool isLocked() { 86 return (currentJob is null); 87 } 88 } 89 90 class WorkbenchJob : PlatformJobBase { 91 override shared 92 void processPlatformJob() { 93 workbench.waitLock(this); 94 scope(exit)workbench.unlock(this); 95 processWorkbenchJob; 96 } 97 override 98 void setup() shared { 99 DXXPlatform.clearLocalCache; 100 super.setup; 101 } 102 abstract shared 103 void processWorkbenchJob(); 104 } 105 106 class WorkbenchJobDefault : WorkbenchJob { 107 Job job; 108 shared this(shared(Job) j) { 109 enforce(cast(WorkbenchJob)j is null); 110 this.job = j; 111 } 112 override shared 113 void processWorkbenchJob() { 114 job.execute; 115 } 116 }