Spinning Topp Logo BlackTopp Studios
inc
barrier.cpp
Go to the documentation of this file.
1 // The DAGFrameScheduler is a Multi-Threaded lock free and wait free scheduling library.
2 // © Copyright 2010 - 2016 BlackTopp Studios Inc.
3 /* This file is part of The DAGFrameScheduler.
4 
5  The DAGFrameScheduler is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  The DAGFrameScheduler is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with The DAGFrameScheduler. If not, see <http://www.gnu.org/licenses/>.
17 */
18 /* The original authors have included a copy of the license specified above in the
19  'doc' folder. See 'gpl.txt'
20 */
21 /* We welcome the use of the DAGFrameScheduler to anyone, including companies who wish to
22  Build professional software and charge for their product.
23 
24  However there are some practical restrictions, so if your project involves
25  any of the following you should contact us and we will try to work something
26  out:
27  - DRM or Copy Protection of any kind(except Copyrights)
28  - Software Patents You Do Not Wish to Freely License
29  - Any Kind of Linking to Non-GPL licensed Works
30  - Are Currently In Violation of Another Copyright Holder's GPL License
31  - If You want to change our code and not add a few hundred MB of stuff to
32  your distribution
33 
34  These and other limitations could cause serious legal problems if you ignore
35  them, so it is best to simply contact us or the Free Software Foundation, if
36  you have any questions.
37 
38  Joseph Toppi - toppij@gmail.com
39  John Blackwood - makoenergy02@gmail.com
40 */
41 #ifndef _barrier_cpp
42 #define _barrier_cpp
43 
44 #include "barrier.h"
45 
46 #include <cassert>
47 
48 /// @file
49 /// @brief Contains the implementation for the @ref Mezzanine::Threading::Barrier Barrier synchronization object.
50 
51 namespace Mezzanine
52 {
53  namespace Threading
54  {
55  Barrier::Barrier (const Int32& SynchThreadCount)
56  : ThreadGoal (SynchThreadCount),
57  IsBlocking(1),
58  ThreadCurrent (0)
59  {}
60 
62  {
63  if(0==AtomicAdd(&IsBlocking,0)) // is a barrier breaking right now
64  { return false; }
65 
66  if (AtomicAdd(&ThreadCurrent,1) >= ThreadGoal-1) //
67  {
69  AtomicAdd(&IsBlocking,-1);
70  assert(IsBlocking==0);
71  return true;
72  }
73  else
74  {
75  while(AtomicAdd(&IsBlocking,0)); // intentionally spinning
76 
77  if(1==AtomicAdd(&ThreadCurrent,-1))
78  { AtomicAdd(&IsBlocking,1); }
79 
80  return false;
81  }
82  }
83 
86 
87  } // \Threading namespace
88 } // \Mezzanine namespace
89 #endif
int32_t Int32
An 32-bit integer.
Definition: datatypes.h:124
void SetThreadSyncCount(Int32 NewCount)
Set the Thread count Atomically.
Definition: barrier.cpp:84
bool Wait()
Wait until the specified number of threads reach this point.
Definition: barrier.cpp:61
Int32 IsBlocking
Does calling wait on this barrier block at presemt.
Definition: barrier.h:66
Int32 ThreadCurrent
The number of threads currently waiting.
Definition: barrier.h:69
Int32 AtomicAdd(Int32 *VariableToChange, Int32 Value)
Increments a value in a way guaranteed to not lose any atomic increments.
Int32 AtomicCompareAndSwap32(Int32 *VariableToChange, const Int32 &OldValue, const Int32 &NewValue)
Atomically Compares And Swaps a 32 bit value.
Int32 ThreadGoal
The number of threads to have wait.
Definition: barrier.h:62
Barrier(const Int32 &SynchThreadCount)
Constructor.
Definition: barrier.cpp:55
The bulk of the engine components go in this namspace.
Definition: actor.cpp:56
The declaration of the Mezzanine::Threading::Barrier Barrier synchronization primitive.