Changeset 384

Show
Ignore:
Timestamp:
07/06/08 12:49:00 (2 months ago)
Author:
FeepingCreature
Message:
  • Threadpool fix and parallel foreach
Files:

Legend:

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

    r383 r384  
    10521052 
    10531053struct _BoolSet(string OP, T...) { 
    1054   T values; 
     1054  TupleMap!(Unstatic, T) values; 
    10551055  int opEquals(U)(U other) { 
    10561056    bool res = other == values[0]; 
     
    11151115R delegate(T) toDg(R, T...)(R function(T) fn) { return fn /fix/ stuple(); } 
    11161116 
    1117 string between(string text, string from, string to) { 
    1118   int pos1; 
    1119   if (from.length) pos1 = text.find(from); 
    1120   else pos1 = 0; 
    1121   if (pos1 == -1) return null; 
    1122   text = text[pos1 + from.length .. $]; 
    1123   int pos2; 
    1124   if (to.length) pos2 = text.find(to); 
    1125   else pos2 = text.length; 
    1126   if (pos2 == -1) return null; 
    1127   return text[0 .. pos2]; 
    1128 
    1129  
    1130 string[] betweens(string text, string from, string to) { 
     1117string between(string text, string from, string to, bool adhere_right = false) { 
     1118  int pos1, pos2; 
     1119  if (adhere_right) { 
     1120    if (to.length) pos2 = text.find(to); 
     1121    else pos2 = text.length; 
     1122    if (pos2 == -1) return null; 
     1123     
     1124    if (from.length) pos1 = text[0 .. pos2].rfind(from); 
     1125    else pos1 = 0; 
     1126    if (pos1 == -1) return null; 
     1127     
     1128    return text[pos1 + from.length .. pos2]; 
     1129     
     1130  } else { 
     1131    if (from.length) pos1 = text.find(from); 
     1132    if (pos1 == -1) return null; 
     1133    text = text[pos1 + from.length .. $]; 
     1134     
     1135    if (to.length) pos2 = text.find(to); 
     1136    else pos2 = text.length; 
     1137    if (pos2 == -1) return null; 
     1138     
     1139    return text[0 .. pos2]; 
     1140  } 
     1141
     1142 
     1143const GLOMP_RIGHT = true; 
     1144string[] betweens(string text, string from, string to, bool adhere_right = false) { 
    11311145  string[] res; 
    11321146  while (true) { 
    1133     auto pos1 = text.find(from); if (pos1 == -1) break; 
    1134     text = text[pos1 + from.length .. $]; 
    1135     auto pos2 = text.find(to); if (pos2 == -1) break; 
    1136     res ~= text[0 .. pos2]; 
     1147    int pos2; 
     1148    if (adhere_right) { 
     1149      pos2 = text.find(to); if (pos2 == -1) break; 
     1150      auto pos1 = text[0 .. pos2].rfind(from); if (pos1 != -1) { 
     1151        res ~= text[pos1 + from.length .. pos2]; 
     1152      } 
     1153    } else { 
     1154      auto pos1 = text.find(from); if (pos1 == -1) break; 
     1155      text = text[pos1 + from.length .. $]; 
     1156      pos2 = text.find(to); if (pos2 == -1) break; 
     1157      res ~= text[0 .. pos2]; 
     1158    } 
    11371159    text = text[pos2 + to.length .. $]; 
    11381160  } 
  • trunk/tools/tools/downloader.d

    r373 r384  
    1414  if (tld.find("%")!=-1) return false; 
    1515  return true; 
     16} 
     17 
     18string toSize(int i) { 
     19  // example result: 24.3K 
     20  string sizeformat(string suffix, ulong max, ulong i) { 
     21    auto r = toString(cast(int) ((i * 10f) / max)); 
     22    r = r[0 .. $-1] ~ "."~r[$-1]; // 243 -> 24.3 
     23    return r~suffix; 
     24  } 
     25  ulong[string] map = ["T": 1_000_000_000_000UL, "G": 1_000_000_000UL, "M": 1_000_000UL, "K": 1_000UL]; 
     26  foreach (suffix; ["T", "G", "M", "K"]) { 
     27    if (i > map[suffix])  
     28      return sizeformat(suffix, map[suffix], i); 
     29  } 
     30  return Format(i, "B"); 
     31} 
     32 
     33import tools.tests; 
     34unittest { 
     35  mustEqual("toSizeTest", toSize(123456), "123.4K"); 
    1636} 
    1737 
     
    3959    if (!entry.length) continue; 
    4060    auto sp = entry.split("="); 
    41     logln(" - ", sp); 
    4261    tags[sp[0]] = (sp.length>1)?(sp[1..$].join("=")):""; 
    4362  } 
     
    86105 
    87106class cbs { 
    88   void delegate(float)[][string] entries; 
    89   void doCbs(string url, float v) { synchronized(this) { 
     107  void delegate(int, int)[][string] entries; 
     108  void delegate(int, int)[void delegate(float)] redirect; 
     109  void doCbs(string url, int i, int k) { synchronized(this) { 
    90110    if (url in entries) { 
    91       foreach (dg; entries[url]) dg(v); 
    92     } 
    93   }} 
    94   void remove(string url, void delegate(float) rem) { synchronized(this) { 
     111      foreach (dg; entries[url]) dg(i, k); 
     112    } 
     113  }} 
     114  void remove(string url, void delegate(int, int) rem) { synchronized(this) { 
    95115    if (!(url in entries)) return; 
    96116    if (entries[url].length==1) { entries.remove(url); return; } 
     
    100120    entries[url].length=entries[url].length-1; 
    101121  }} 
     122  void remove(string url, void delegate(float) rem) { synchronized(this) { 
     123    if (!(rem in redirect)) return; 
     124    remove(url, redirect[rem]); 
     125  }} 
    102126  void add(string url, void delegate(float) n) { synchronized(this) { 
     127    entries[url]~= redirect[n] = n /apply/ (typeof(n) n, int at, int max) { n((at * 1f) / max); }; 
     128  }} 
     129  void add(string url, void delegate(int, int) n) { synchronized(this) { 
    103130    entries[url]~=n; 
    104131  }} 
     
    242269        if (!quiet) logln("Following redirect to ", loc); 
    243270        if (redirect) { *redirect=loc; res=null; return true; } 
    244         void delegate(float) dg=(float f) { download_callbacks.doCbs(param, f); }; 
     271        auto dg = (int at, int max) { download_callbacks.doCbs(param, at, max); }; 
    245272        download_callbacks.add(loc, dg); 
    246273        scope(exit) download_callbacks.remove(loc, dg); 
     
    254281    else { 
    255282      // At this point we know the header has already been received 
    256       if (length) download_callbacks.doCbs(param, ((received.length-header.length)*1.0)/length); 
     283      if (length) download_callbacks.doCbs(param, received.length-header.length, length); 
    257284    } 
    258285    return false; 
  • trunk/tools/tools/mersenne.d

    r367 r384  
    7878private TLS!(Mersenne) deflt; 
    7979uint glob_seed = 23;  
    80 void seed(uint s) { auto res = new Mersenne; res.seed(s); deflt.set(res); seed = s; } 
    8180 
    8281int i; 
  • trunk/tools/tools/threadpool.d

    r362 r384  
    4646    bool finished() { if (done) return done; synchronized(this) return done || channel.active; } 
    4747  } 
     48  void mt_foreach(T)(T[] array, void delegate(int, T) dg) { 
     49    int left = array.length; 
     50    auto sync = new Object; 
     51    foreach (i, entry; array) addTask(stuple(i, entry) /apply/ (int i, T foo) { dg(i, foo); synchronized(sync) left--; }); 
     52    while (left) idle(); 
     53  } 
    4854  Future!(T) future(T)(T delegate() dg) { 
    4955    auto res=new Future!(T); 
     
    7682  Thread mainthr=null; 
    7783  bool idle() { 
    78     if (tasks.active) { 
    79       auto task = getTask(); 
     84    typeof(getTask()) task; 
     85    if (!tasks.try_get(task)) return true; 
     86    else { 
    8087      try task(); 
    8188      catch (TaskUnfinished tu) addTask(task.name, task.dg); 
    8289      catch (Exception e) logln("Idle task failed with ", e); 
    8390      return false; 
    84     } else return true; 
     91    } 
    8592  } 
    8693  this(int count) { 
  • trunk/tools/tools/threads.d

    r373 r384  
    309309    sem.release; 
    310310  } 
     311  bool try_get(out T t) { 
     312    static if (SinglePut) put_block.checkDifferent; 
     313    static if (SingleGet) { 
     314      get_block.checkSame; 
     315      if (sem.try_acquire) { 
     316        assert (mesgs.has); 
     317        t = mesgs.pop; 
     318        return true; 
     319      } else return false; 
     320    } else { 
     321      if (sem.try_acquire) { 
     322        assert (mesgs.has); 
     323        synchronized(this) t = mesgs.pop; 
     324        return true; 
     325      } else return false; 
     326    } 
     327  } 
    311328  T get() { 
    312329    static if (SinglePut) put_block.checkDifferent;