Loading

Paste #pivo85vbj

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

Comments