Loading

Paste #prtrb9a0r

  1. Index: src/station_cmd.cpp
  2. ===================================================================
  3. --- src/station_cmd.cpp (revision 27177)
  4. +++ src/station_cmd.cpp (working copy)
  5. @@ -3515,7 +3515,7 @@
  6.         if (ge2.link_graph == INVALID_LINK_GRAPH) {
  7.             if (LinkGraph::CanAllocateItem()) {
  8.                 lg = new LinkGraph(cargo);
  9. -               LinkGraphSchedule::Instance()->Queue(lg);
  10. +               LinkGraphSchedule::instance.Queue(lg);
  11.                 ge2.link_graph = lg->index;
  12.                 ge2.node = lg->AddNode(st2);
  13.             } else {
  14. @@ -3537,11 +3537,11 @@
  15.         if (ge1.link_graph != ge2.link_graph) {
  16.             LinkGraph *lg2 = LinkGraph::Get(ge2.link_graph);
  17.             if (lg->Size() < lg2->Size()) {
  18. -               LinkGraphSchedule::Instance()->Unqueue(lg);
  19. +               LinkGraphSchedule::instance.Unqueue(lg);
  20.                 lg2->Merge(lg); // Updates GoodsEntries of lg
  21.                 lg = lg2;
  22.             } else {
  23. -               LinkGraphSchedule::Instance()->Unqueue(lg2);
  24. +               LinkGraphSchedule::instance.Unqueue(lg2);
  25.                 lg->Merge(lg2); // Updates GoodsEntries of lg2
  26.             }
  27.         }
  28. @@ -3663,7 +3663,7 @@
  29.     if (ge.link_graph == INVALID_LINK_GRAPH) {
  30.         if (LinkGraph::CanAllocateItem()) {
  31.             lg = new LinkGraph(type);
  32. -           LinkGraphSchedule::Instance()->Queue(lg);
  33. +           LinkGraphSchedule::instance.Queue(lg);
  34.             ge.link_graph = lg->index;
  35.             ge.node = lg->AddNode(st);
  36.         } else {
  37. Index: src/saveload/linkgraph_sl.cpp
  38. ===================================================================
  39. --- src/saveload/linkgraph_sl.cpp   (revision 27177)
  40. +++ src/saveload/linkgraph_sl.cpp   (working copy)
  41. @@ -209,7 +209,7 @@
  42.   */
  43.  static void Load_LGRS()
  44.  {
  45. -   SlObject(LinkGraphSchedule::Instance(), GetLinkGraphScheduleDesc());
  46. +   SlObject(&LinkGraphSchedule::instance, GetLinkGraphScheduleDesc());
  47.  }
  48.  
  49.  /**
  50. @@ -218,7 +218,7 @@
  51.   */
  52.  void AfterLoadLinkGraphs()
  53.  {
  54. -   LinkGraphSchedule::Instance()->SpawnAll();
  55. +   LinkGraphSchedule::instance.SpawnAll();
  56.  }
  57.  
  58.  /**
  59. @@ -250,7 +250,7 @@
  60.   */
  61.  static void Save_LGRS()
  62.  {
  63. -   SlObject(LinkGraphSchedule::Instance(), GetLinkGraphScheduleDesc());
  64. +   SlObject(&LinkGraphSchedule::instance, GetLinkGraphScheduleDesc());
  65.  }
  66.  
  67.  /**
  68. @@ -258,7 +258,7 @@
  69.   */
  70.  static void Ptrs_LGRS()
  71.  {
  72. -   SlObject(LinkGraphSchedule::Instance(), GetLinkGraphScheduleDesc());
  73. +   SlObject(&LinkGraphSchedule::instance, GetLinkGraphScheduleDesc());
  74.  }
  75.  
  76.  extern const ChunkHandler _linkgraph_chunk_handlers[] = {
  77. Index: src/station.cpp
  78. ===================================================================
  79. --- src/station.cpp (revision 27177)
  80. +++ src/station.cpp (working copy)
  81. @@ -107,7 +107,7 @@
  82.         }
  83.         lg->RemoveNode(this->goods[c].node);
  84.         if (lg->Size() == 0) {
  85. -           LinkGraphSchedule::Instance()->Unqueue(lg);
  86. +           LinkGraphSchedule::instance.Unqueue(lg);
  87.             delete lg;
  88.         }
  89.     }
  90. Index: src/cheat_gui.cpp
  91. ===================================================================
  92. --- src/cheat_gui.cpp   (revision 27177)
  93. +++ src/cheat_gui.cpp   (working copy)
  94. @@ -102,7 +102,7 @@
  95.     if (p1 == _cur_year) return _cur_year;
  96.  
  97.     Date new_date = ConvertYMDToDate(p1, ymd.month, ymd.day);
  98. -   LinkGraphSchedule::Instance()->ShiftDates(new_date - _date);
  99. +   LinkGraphSchedule::instance.ShiftDates(new_date - _date);
  100.     SetDate(new_date, _date_fract);
  101.     EnginesMonthlyLoop();
  102.     SetWindowDirty(WC_STATUS_BAR, 0);
  103. Index: src/linkgraph/linkgraphschedule.cpp
  104. ===================================================================
  105. --- src/linkgraph/linkgraphschedule.cpp (revision 27177)
  106. +++ src/linkgraph/linkgraphschedule.cpp (working copy)
  107. @@ -17,6 +17,13 @@
  108.  #include "flowmapper.h"
  109.  
  110.  /**
  111. + * Static instance of LinkGraphSchedule.
  112. + * Note: This instance is created on task start.
  113. + *       Lazy creation on first usage results in a data race between the threads.
  114. + */
  115. +/* static */ LinkGraphSchedule LinkGraphSchedule::instance;
  116. +
  117. +/**
  118.   * Start the next job in the schedule.
  119.   */
  120.  void LinkGraphSchedule::SpawnNext()
  121. @@ -66,9 +73,8 @@
  122.  /* static */ void LinkGraphSchedule::Run(void *j)
  123.  {
  124.     LinkGraphJob *job = (LinkGraphJob *)j;
  125. -   LinkGraphSchedule *schedule = LinkGraphSchedule::Instance();
  126. -   for (uint i = 0; i < lengthof(schedule->handlers); ++i) {
  127. -       schedule->handlers[i]->Run(*job);
  128. +   for (uint i = 0; i < lengthof(instance.handlers); ++i) {
  129. +       instance.handlers[i]->Run(*job);
  130.     }
  131.  }
  132.  
  133. @@ -88,12 +94,11 @@
  134.   */
  135.  /* static */ void LinkGraphSchedule::Clear()
  136.  {
  137. -   LinkGraphSchedule *inst = LinkGraphSchedule::Instance();
  138. -   for (JobList::iterator i(inst->running.begin()); i != inst->running.end(); ++i) {
  139. +   for (JobList::iterator i(instance.running.begin()); i != instance.running.end(); ++i) {
  140.         (*i)->JoinThread();
  141.     }
  142. -   inst->running.clear();
  143. -   inst->schedule.clear();
  144. +   instance.running.clear();
  145. +   instance.schedule.clear();
  146.  }
  147.  
  148.  /**
  149. @@ -134,15 +139,6 @@
  150.  }
  151.  
  152.  /**
  153. - * Retrieve the link graph schedule or create it if necessary.
  154. - */
  155. -/* static */ LinkGraphSchedule *LinkGraphSchedule::Instance()
  156. -{
  157. -   static LinkGraphSchedule inst;
  158. -   return &inst;
  159. -}
  160. -
  161. -/**
  162.   * Spawn or join a link graph job or compress a link graph if any link graph is
  163.   * due to do so.
  164.   */
  165. @@ -151,9 +147,9 @@
  166.     if (_date_fract != LinkGraphSchedule::SPAWN_JOIN_TICK) return;
  167.     Date offset = _date % _settings_game.linkgraph.recalc_interval;
  168.     if (offset == 0) {
  169. -       LinkGraphSchedule::Instance()->SpawnNext();
  170. +       LinkGraphSchedule::instance.SpawnNext();
  171.     } else if (offset == _settings_game.linkgraph.recalc_interval / 2) {
  172. -       LinkGraphSchedule::Instance()->JoinNext();
  173. +       LinkGraphSchedule::instance.JoinNext();
  174.     }
  175.  }
  176.  
  177. Index: src/linkgraph/linkgraphschedule.h
  178. ===================================================================
  179. --- src/linkgraph/linkgraphschedule.h   (revision 27177)
  180. +++ src/linkgraph/linkgraphschedule.h   (working copy)
  181. @@ -51,8 +51,8 @@
  182.  public:
  183.     /* This is a tick where not much else is happening, so a small lag might go unnoticed. */
  184.     static const uint SPAWN_JOIN_TICK = 21; ///< Tick when jobs are spawned or joined every day.
  185. +   static LinkGraphSchedule instance;
  186.  
  187. -   static LinkGraphSchedule *Instance();
  188.     static void Run(void *j);
  189.     static void Clear();
  190.  
  191. Index: src/linkgraph/mcf.cpp
  192. ===================================================================
  193. --- src/linkgraph/mcf.cpp   (revision 27177)
  194. +++ src/linkgraph/mcf.cpp   (working copy)
  195. @@ -378,11 +378,10 @@
  196.   */
  197.  bool MCF1stPass::EliminateCycles(PathVector &path, NodeID origin_id, NodeID next_id)
  198.  {
  199. -   static Path *invalid_path = new Path(INVALID_NODE, true);
  200.     Path *at_next_pos = path[next_id];
  201.  
  202.     /* this node has already been searched */
  203. -   if (at_next_pos == invalid_path) return false;
  204. +   if (at_next_pos == Path::invalid_path) return false;
  205.  
  206.     if (at_next_pos == NULL) {
  207.         /* Summarize paths; add up the paths with the same source and next hop
  208. @@ -430,7 +429,7 @@
  209.          * could be found in this branch, thus it has to be searched again next
  210.          * time we spot it.
  211.          */
  212. -       path[next_id] = found ? NULL : invalid_path;
  213. +       path[next_id] = found ? NULL : Path::invalid_path;
  214.         return found;
  215.     }
  216.  
  217. Index: src/linkgraph/linkgraphjob.cpp
  218. ===================================================================
  219. --- src/linkgraph/linkgraphjob.cpp  (revision 27177)
  220. +++ src/linkgraph/linkgraphjob.cpp  (working copy)
  221. @@ -20,6 +20,13 @@
  222.  INSTANTIATE_POOL_METHODS(LinkGraphJob)
  223.  
  224.  /**
  225. + * Static instance of an invalid path.
  226. + * Note: This instance is created on task start.
  227. + *       Lazy creation on first usage results in a data race between the threads.
  228. + */
  229. +/* static */ Path *Path::invalid_path = new Path(INVALID_NODE, true);
  230. +
  231. +/**
  232.   * Create a link graph job from a link graph. The link graph will be copied so
  233.   * that the calculations don't interfer with the normal operations on the
  234.   * original. The job is immediately started.
  235. Index: src/linkgraph/linkgraphjob.h
  236. ===================================================================
  237. --- src/linkgraph/linkgraphjob.h    (revision 27177)
  238. +++ src/linkgraph/linkgraphjob.h    (working copy)
  239. @@ -343,6 +343,8 @@
  240.   */
  241.  class Path {
  242.  public:
  243. +   static Path *invalid_path;
  244. +
  245.     Path(NodeID n, bool source = false);
  246.  
  247.     /** Get the node this leg passes. */
  248. Index: src/toolbar_gui.cpp
  249. ===================================================================
  250. --- src/toolbar_gui.cpp (revision 27177)
  251. +++ src/toolbar_gui.cpp (working copy)
  252. @@ -1119,7 +1119,7 @@
  253.     _settings_game.game_creation.starting_year = Clamp(year, MIN_YEAR, MAX_YEAR);
  254.     Date new_date = ConvertYMDToDate(_settings_game.game_creation.starting_year, 0, 1);
  255.     /* If you open a savegame as scenario there may already be link graphs.*/
  256. -   LinkGraphSchedule::Instance()->ShiftDates(new_date - _date);
  257. +   LinkGraphSchedule::instance.ShiftDates(new_date - _date);
  258.     SetDate(new_date, 0);
  259.  }
  260.  
  261.  

Comments