00001 // Dikstra style termination detection -*- C++ -*- 00002 /* 00003 Galois, a framework to exploit amorphous data-parallelism in irregular 00004 programs. 00005 00006 Copyright (C) 2011, The University of Texas at Austin. All rights reserved. 00007 UNIVERSITY EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES CONCERNING THIS SOFTWARE 00008 AND DOCUMENTATION, INCLUDING ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR ANY 00009 PARTICULAR PURPOSE, NON-INFRINGEMENT AND WARRANTIES OF PERFORMANCE, AND ANY 00010 WARRANTY THAT MIGHT OTHERWISE ARISE FROM COURSE OF DEALING OR USAGE OF TRADE. 00011 NO WARRANTY IS EITHER EXPRESS OR IMPLIED WITH RESPECT TO THE USE OF THE 00012 SOFTWARE OR DOCUMENTATION. Under no circumstances shall University be liable 00013 for incidental, special, indirect, direct or consequential damages or loss of 00014 profits, interruption of business, or related expenses which may arise from use 00015 of Software or Documentation, including but not limited to those resulting from 00016 defects in Software and/or Documentation, or loss or inaccuracy of data of any 00017 kind. 00018 */ 00019 00020 namespace GaloisRuntime { 00021 00022 //Dikstra dual-ring termination algorithm 00023 class TerminationDetection { 00024 public: 00025 class tokenHolder { 00026 friend class TerminationDetection; 00027 volatile long tokenIsBlack; 00028 volatile long hasToken; 00029 volatile long processIsBlack; 00030 public: 00031 tokenHolder() :tokenIsBlack(false), hasToken(false), processIsBlack(true) {} 00032 inline void workHappened() { 00033 processIsBlack = true; 00034 } 00035 }; 00036 private: 00037 PerCPU<tokenHolder> data; 00038 volatile bool globalTerm; 00039 bool lastWasWhite; 00040 public: 00041 00042 TerminationDetection(); 00043 00044 inline void workHappened() { 00045 data.get().workHappened(); 00046 } 00047 00048 tokenHolder* getLocalTokenHolder() { 00049 return &data.get(); 00050 } 00051 00052 void localTermination(); 00053 00054 // Returns 00055 bool globalTermination() { 00056 return globalTerm; 00057 } 00058 00059 }; 00060 00061 }