root/juggler/tags/1.0.7/Sync/vjBarrier.h
| Revision 8789, 6.0 kB (checked in by patrickh, 7 years ago) | |
|---|---|
| |
| Line | |
|---|---|
| 1 | /*************** <auto-copyright.pl BEGIN do not edit this line> ************** |
| 2 | * |
| 3 | * VR Juggler is (C) Copyright 1998, 1999, 2000, 2001, 2002 |
| 4 | * by Iowa State University |
| 5 | * |
| 6 | * Original Authors: |
| 7 | * Allen Bierbaum, Christopher Just, |
| 8 | * Patrick Hartling, Kevin Meinert, |
| 9 | * Carolina Cruz-Neira, Albert Baker |
| 10 | * |
| 11 | * This library is free software; you can redistribute it and/or |
| 12 | * modify it under the terms of the GNU Library General Public |
| 13 | * License as published by the Free Software Foundation; either |
| 14 | * version 2 of the License, or (at your option) any later version. |
| 15 | * |
| 16 | * This library is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 19 | * Library General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU Library General Public |
| 22 | * License along with this library; if not, write to the |
| 23 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 24 | * Boston, MA 02111-1307, USA. |
| 25 | * |
| 26 | * ----------------------------------------------------------------- |
| 27 | * File: $RCSfile$ |
| 28 | * Date modified: $Date$ |
| 29 | * Version: $Revision$ |
| 30 | * ----------------------------------------------------------------- |
| 31 | * |
| 32 | *************** <auto-copyright.pl END do not edit this line> ***************/ |
| 33 | |
| 34 | |
| 35 | #ifndef _VJBarrier_h_ |
| 36 | #define _VJBarrier_h_ |
| 37 | |
| 38 | #include <vjConfig.h> |
| 39 | |
| 40 | #ifdef VJ_IRIX_SPROC // ---- SGI IPC Barrier ------ // |
| 41 | # include <ulocks.h> |
| 42 | # include <Sync/vjBarrierSGI.h> |
| 43 | typedef vjBarrierSGI vjBarrier; |
| 44 | #else |
| 45 | |
| 46 | #include <Sync/vjCond.h> |
| 47 | #include <Sync/vjMutex.h> |
| 48 | #include <Sync/vjGuard.h> |
| 49 | |
| 50 | |
| 51 | //--------------------------------------------------------------------- |
| 52 | //: Helper class for vjBarrier |
| 53 | //--------------------------------------------------------------------- |
| 54 | class vjSubBarrier |
| 55 | { |
| 56 | public: |
| 57 | // ------------------------------------------------------------------------ |
| 58 | //: Initialization. |
| 59 | // ------------------------------------------------------------------------ |
| 60 | vjSubBarrier (int count, vjMutex* lock) : barrierFinished(lock), |
| 61 | runningThreads(count) |
| 62 | {} |
| 63 | |
| 64 | vjCond barrierFinished; // True if this generation of the barrier is done. |
| 65 | |
| 66 | int runningThreads; //: Number of threads that are still running. |
| 67 | |
| 68 | // ------------------------------------------------------------------------ |
| 69 | //: Dump the state of an object. |
| 70 | // ------------------------------------------------------------------------ |
| 71 | void dump (void) |
| 72 | { |
| 73 | std::cerr << "vjSubBarrier::dump" << std::endl; |
| 74 | this->barrierFinished.dump(); |
| 75 | } |
| 76 | }; |
| 77 | |
| 78 | //--------------------------------------------------------------------- |
| 79 | //: Implements "barrier synchronization". |
| 80 | // |
| 81 | // This class allows <count> number of threads to synchronize |
| 82 | // their completion (so-called "barrier synchronization"). The |
| 83 | // implementation uses a "sub-barrier generation numbering" |
| 84 | // scheme to avoid overhead and to ensure that all threads exit |
| 85 | // the barrier correct. This code is based on an article from |
| 86 | // SunOpsis Vol. 4, No. 1 by Richard Marejka |
| 87 | // (Richard.Marejka@canada.sun.com). |
| 88 | //!PUBLIC_API: |
| 89 | //--------------------------------------------------------------------- |
| 90 | class vjBarrier |
| 91 | { |
| 92 | public: |
| 93 | // ------------------------------------------------------------------------ |
| 94 | //: Initialize the barrier to synchronize <count> threads. |
| 95 | // ------------------------------------------------------------------------ |
| 96 | vjBarrier (int count) : currentGeneration(0), |
| 97 | count(count), |
| 98 | subBarrier1(count, &mutex), |
| 99 | subBarrier2(count, &mutex) |
| 100 | { |
| 101 | //std::cerr << "vjBarrier::vjBarrier: Entering." << std::endl; |
| 102 | subBarrier[0] = &subBarrier1; |
| 103 | subBarrier[1] = &subBarrier2; |
| 104 | } |
| 105 | |
| 106 | // ----------------------------------------------------------------------- |
| 107 | //: Block the caller until all <count> threads have called <wait> and |
| 108 | //+ then allow all the caller threads to continue in parallel. |
| 109 | // ----------------------------------------------------------------------- |
| 110 | int wait(void); |
| 111 | |
| 112 | // ----------------------------------------------------------------------- |
| 113 | //: Tell the barrier to increase the count of the number of threads to |
| 114 | //+ syncronize. |
| 115 | // ----------------------------------------------------------------------- |
| 116 | void addProcess() |
| 117 | { |
| 118 | std::cerr << "vjBarrier::addProcess: Not implemented yet." |
| 119 | << std::endl; |
| 120 | } |
| 121 | |
| 122 | // ----------------------------------------------------------------------- |
| 123 | //: Tell the barrier to decrease the count of the number of threads to |
| 124 | //+ syncronize. |
| 125 | // ----------------------------------------------------------------------- |
| 126 | void removeProcess() |
| 127 | { |
| 128 | std::cerr << "vjBarrier::removeProcess: Not implemented yet." |
| 129 | << std::endl; |
| 130 | } |
| 131 | |
| 132 | // ----------------------------------------------------------------------- |
| 133 | //: Dump the state of an object. |
| 134 | // ----------------------------------------------------------------------- |
| 135 | void dump (void) const {} |
| 136 | |
| 137 | private: |
| 138 | vjMutex mutex; // Serialize access to the barrier state. |
| 139 | |
| 140 | // Either 0 or 1, depending on whether we are the first generation |
| 141 | // of waiters or the next generation of waiters. |
| 142 | int currentGeneration; |
| 143 | |
| 144 | |
| 145 | int count; // Total number of threads that can be waiting at any one time. |
| 146 | |
| 147 | vjSubBarrier subBarrier1; |
| 148 | vjSubBarrier subBarrier2; |
| 149 | vjSubBarrier* subBarrier[2]; |
| 150 | // We keep two <sub_barriers>, one for the first "generation" of |
| 151 | // waiters, and one for the next "generation" of waiters. This |
| 152 | // efficiently solves the problem of what to do if all the first |
| 153 | // generation waiters don't leave the barrier before one of the |
| 154 | // threads calls wait() again (i.e., starts up the next generation |
| 155 | // barrier). |
| 156 | |
| 157 | // = Prevent assignment and initialization. |
| 158 | void operator= (const vjBarrier &) {} |
| 159 | vjBarrier (const vjBarrier &) : subBarrier1(0, &mutex), subBarrier2(0, &mutex) {} |
| 160 | }; |
| 161 | |
| 162 | |
| 163 | #endif /* VJ_IRIX_SPROC */ |
| 164 | #endif /* _VJBarrier_h_ */ |
Note: See TracBrowser for help on using the browser.
