/* * Copyright (c) 2005-2006 Thomas Weise * * E-Mail : weise@vs.uni-kassel.de * Creation Date : 2006-10-16 * Creator : Thomas Weise * Original Filename: Bakery.java * Version : 1.0.0 * Last modification: 2007-10-16 * by: Thomas Weise * * License : GNU LESSER GENERAL PUBLIC LICENSE * Version 2.1, February 1999 * You should have received a copy of this license along * with this library; if not, write to theFree Software * Foundation, Inc. 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301, USA or download the license * under http://www.gnu.org/licenses/lgpl.html or * http://www.gnu.org/copyleft/lesser.html. * * Warranty : This software is provided "as is" without any * warranty; without even the implied warranty of * merchantability or fitness for a particular purpose. * See the Gnu Lesser General Public License for more * details. *//** * Diese Klasse testet die Implementierung von Lamport's Bakery * Algorithmus. * * @author Thomas Weise */public class Bakery { int N, waehlt[], nummer[]; /** * Erzeuge eine neue Instanz des Bakery-Algorithmus * * @param processCount * die (maximale) Anzahl der Prozesse */ public Bakery(final int processCount) { super(); // waehlt und nummer werden implizit mit 0 initialisiert N = processCount; waehlt = new int[N]; nummer = new int[N]; for (int i = 0; i < N; i++) { waehlt[i] = 0; nummer[i] = 0; } } /** * Betrete die B�ckerei. * * @param pid * Der id-Wert des Prozesses, der rein will. */ public final void enterBakery(final int pid) { waehlt[pid] = 1; int max = 0; for (int i = 0; i < N; i++) { if (nummer[i] > max) { max = nummer[i]; } } nummer[pid] = max + 1; waehlt[pid] = 0; for (int k = 0; k < N; k++) { while (waehlt[k] != 0) { //Thread.yield(); } while ( (nummer[k] != 0) && ( (nummer[k] < nummer[pid]) || ( (nummer[k] == nummer[pid]) && (k < pid) ) )) { //Thread.yield(); } } } /** * Verlasse die B�ckerei. * * @param pid * Der id-Wert des Prozesses, der raus will. */ public void leaveBakery(final int pid) { nummer[pid] = 0; }}