Changeset 359

Show
Ignore:
Timestamp:
06/09/08 10:10:53 (6 months ago)
Author:
FeepingCreature
Message:
  • Fixes for dglut
Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/tools/tools/base.d

    r349 r359  
    236236class AxiomaticException : Exception { this(char[] msg) { super("!!!!Basic axiom violated: "~msg); } } 
    237237 
    238 void removeFrom(T)(ref T[] array, size_t index) { 
     238void removeFrom(T, U)(ref T[] array, U index) { 
    239239  if (index!<array.length) throw new Exception("remove: Index "~toString(index)~" not in [0.."~toString(array.length)~"] !"); 
    240240  // this also ensures array length is >0 
  • trunk/tools/tools/mersenne.d

    r287 r359  
    6060} 
    6161 
    62 private Mersenne deflt; 
    63 void seed(uint s) { deflt = new Mersenne; deflt.seed(s); } 
    64 uint rand() { if (!deflt) seed(23); return deflt(); } 
     62import tools.threads; 
     63private TLS!(Mersenne) deflt; 
     64uint glob_seed = 23;  
     65void seed(uint s) { auto res = new Mersenne; res.seed(s); deflt.set(res); seed = s; } 
    6566 
    66 float randf() { if (!deflt) seed(23); return 1f * deflt() / (1f * typeof(deflt()).max); } 
     67int i; 
     68static 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 
     73uint rand() { return deflt()(); } 
     74 
     75float randf() { return rand() / (1f * typeof(rand()).max); } 
  • trunk/tools/tools/threadpool.d

    r319 r359  
    44import std.thread, tools.threads; 
    55import tools.log, tools.functional; 
     6 
     7Lock lock; // perpetually blocking 
     8static this() { New(lock); lock.lock; } 
     9void halt() { lock.lock; } 
    610 
    711struct task { 
     
    1822  bool finish=false; 
    1923  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  } 
    2136  class Future(T) { 
    2237    bool done; MessageChannel!(bool) channel; 
     
    5166  } 
    5267  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  } 
    5475  Thread mainthr=null; 
    5576  bool idle() { 
    5677    if (tasks.active) { 
    57       auto task = tasks.get(); 
     78      auto task = getTask(); 
    5879      try task(); 
    5980      catch (TaskUnfinished tu) addTask(task.name, task.dg); 
     
    6586    New(tasks); 
    6687    mainthr=Thread.getThis; 
    67     while (count--) { auto thr=new poolthread(tasks); threadnames[thr] = "Freshling PoolThread"; thr.start; threads~=thr; } 
     88    while (count--) newThread; 
    6889  } 
    6990} 
     
    7394  Thread mainthr=null; 
    7495  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; } 
    7698  ~this() { fail("POOLTHREAD DELETED. BAD."); } 
    7799  int run() { 
    78100    while (true) { 
    79       auto t=tasksource.get(); 
     101      auto t=getTask(); 
    80102      threadnames[this] = t.name; 
    81103      try (t()); 
  • trunk/tools/tools/threads.d

    r306 r359  
    8181        if (key == TLS_OUT_OF_INDEXES) throw new Exception("Out of indexes!"); 
    8282        maker = m; 
     83      } 
     84      void set(Ref r) { 
     85        check("set:TlsSetValue", TlsSetValue(key, cast(void*) r)); 
    8386      } 
    8487      Ref ptr() { 
     
    196199        maker = m; 
    197200      } 
     201      void set(Ref r) { 
     202        check("set:pthread_setspecific", pthread_setspecific(key, cast(void*) r)); 
     203      } 
    198204      Ref ptr() { 
    199205        auto res = cast(Ref) cast(void*) pthread_getspecific(key); 
  • trunk/tools/tools/time.d

    r336 r359  
    2424    pragma(msg, "Is this correct? I'm not sure. Somebody on win32 verify or fix."); 
    2525  } 
     26  double sec() { return clock()*1.0/clocks_per_sec; } 
    2627} else { 
    2728  extern(C) { 
     
    3738      return tv.tv_sec*1_000_000 + tv.tv_usec; 
    3839    } 
     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    } 
    3946  } 
    4047} 
    41  
    42 float sec() { return µsec() / 1_000_000f; } 
    4348 
    4449/*struct SchedTask {