Changeset 359
- Timestamp:
- 06/09/08 10:10:53 (6 months ago)
- Files:
-
- trunk/tools/tools/base.d (modified) (1 diff)
- trunk/tools/tools/mersenne.d (modified) (1 diff)
- trunk/tools/tools/threadpool.d (modified) (5 diffs)
- trunk/tools/tools/threads.d (modified) (2 diffs)
- trunk/tools/tools/time.d (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/tools/tools/base.d
r349 r359 236 236 class AxiomaticException : Exception { this(char[] msg) { super("!!!!Basic axiom violated: "~msg); } } 237 237 238 void removeFrom(T )(ref T[] array, size_tindex) {238 void removeFrom(T, U)(ref T[] array, U index) { 239 239 if (index!<array.length) throw new Exception("remove: Index "~toString(index)~" not in [0.."~toString(array.length)~"] !"); 240 240 // this also ensures array length is >0 trunk/tools/tools/mersenne.d
r287 r359 60 60 } 61 61 62 private Mersenne deflt; 63 void seed(uint s) { deflt = new Mersenne; deflt.seed(s); } 64 uint rand() { if (!deflt) seed(23); return deflt(); } 62 import tools.threads; 63 private TLS!(Mersenne) deflt; 64 uint glob_seed = 23; 65 void seed(uint s) { auto res = new Mersenne; res.seed(s); deflt.set(res); seed = s; } 65 66 66 float randf() { if (!deflt) seed(23); return 1f * deflt() / (1f * typeof(deflt()).max); } 67 int i; 68 static this() { 69 Mersenne gen_mers() { auto res = new Mersenne; int k; synchronized k = i++; res.seed(glob_seed + k); return res; } 70 deflt = new typeof(deflt)(&gen_mers); 71 } 72 73 uint rand() { return deflt()(); } 74 75 float randf() { return rand() / (1f * typeof(rand()).max); } trunk/tools/tools/threadpool.d
r319 r359 4 4 import std.thread, tools.threads; 5 5 import tools.log, tools.functional; 6 7 Lock lock; // perpetually blocking 8 static this() { New(lock); lock.lock; } 9 void halt() { lock.lock; } 6 10 7 11 struct task { … … 18 22 bool finish=false; 19 23 MessageMultiChannel!(task, false, false) tasks; 20 void addTask(string name, proc c) { if (!finish) tasks.put(task(name, c)); } 24 task getTask() { if (finish) return task("ShutdownThreadsBusy", &shutdown_me); else return tasks.get(); } 25 void addTask(string name, proc c) { tasks.put(task(name, c)); } 26 Semaphore shutdown_counter; 27 void shutdown_me() { shutdown_counter.release; halt; } 28 void shutdown() { 29 shutdown_counter = new Semaphore; 30 finish = true; 31 auto me = Thread.getThis(); 32 foreach (thread; threads) if (thread !is me) addTask("ShutdownThreadsWakeUp", &shutdown_me); 33 foreach (thread; threads) if (thread !is me) shutdown_counter.acquire; 34 return; 35 } 21 36 class Future(T) { 22 37 bool done; MessageChannel!(bool) channel; … … 51 66 } 52 67 void addTask(proc c) { addTask("Unnamed task", c); } 53 void newThread() { auto thr=new poolthread(tasks); thr.mainthr=mainthr; thr.start; synchronized threads~=thr; } 68 void newThread() { 69 auto thr=new poolthread(tasks, &getTask); 70 thr.mainthr=mainthr; 71 threadnames[thr] = "Freshling PoolThread"; 72 thr.start; 73 synchronized threads~=thr; 74 } 54 75 Thread mainthr=null; 55 76 bool idle() { 56 77 if (tasks.active) { 57 auto task = tasks.get();78 auto task = getTask(); 58 79 try task(); 59 80 catch (TaskUnfinished tu) addTask(task.name, task.dg); … … 65 86 New(tasks); 66 87 mainthr=Thread.getThis; 67 while (count--) { auto thr=new poolthread(tasks); threadnames[thr] = "Freshling PoolThread"; thr.start; threads~=thr; }88 while (count--) newThread; 68 89 } 69 90 } … … 73 94 Thread mainthr=null; 74 95 MessageMultiChannel!(task, false, false) tasksource; 75 this(typeof(tasksource) ts) { mainthr=Thread.getThis; tasksource=ts; } 96 task delegate() getTask; // to allow overrides on finish=true 97 this(typeof(tasksource) ts, task delegate() dg) { mainthr=Thread.getThis; getTask = dg; tasksource=ts; } 76 98 ~this() { fail("POOLTHREAD DELETED. BAD."); } 77 99 int run() { 78 100 while (true) { 79 auto t= tasksource.get();101 auto t=getTask(); 80 102 threadnames[this] = t.name; 81 103 try (t()); trunk/tools/tools/threads.d
r306 r359 81 81 if (key == TLS_OUT_OF_INDEXES) throw new Exception("Out of indexes!"); 82 82 maker = m; 83 } 84 void set(Ref r) { 85 check("set:TlsSetValue", TlsSetValue(key, cast(void*) r)); 83 86 } 84 87 Ref ptr() { … … 196 199 maker = m; 197 200 } 201 void set(Ref r) { 202 check("set:pthread_setspecific", pthread_setspecific(key, cast(void*) r)); 203 } 198 204 Ref ptr() { 199 205 auto res = cast(Ref) cast(void*) pthread_getspecific(key); trunk/tools/tools/time.d
r336 r359 24 24 pragma(msg, "Is this correct? I'm not sure. Somebody on win32 verify or fix."); 25 25 } 26 double sec() { return clock()*1.0/clocks_per_sec; } 26 27 } else { 27 28 extern(C) { … … 37 38 return tv.tv_sec*1_000_000 + tv.tv_usec; 38 39 } 40 double sec() { 41 timeval tv = void; 42 gettimeofday(&tv, null); 43 auto res = tv.tv_sec * 1.0 + tv.tv_usec / 1_000_000.0; 44 return res; 45 } 39 46 } 40 47 } 41 42 float sec() { return µsec() / 1_000_000f; }43 48 44 49 /*struct SchedTask {
