Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

Mutex.java 2.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /* -*- Mode: Java; -*-
  2. Copyright (c) Xerox Corporation 1998-2002. All rights reserved.
  3. Use and copying of this software and preparation of derivative works based
  4. upon this software are permitted. Any distribution of this software or
  5. derivative works must comply with all applicable United States export control
  6. laws.
  7. This software is made available AS IS, and Xerox Corporation makes no warranty
  8. about the software, its performance or its conformity to any specification.
  9. |<--- this code is formatted to fit into 80 columns --->|
  10. |<--- this code is formatted to fit into 80 columns --->|
  11. |<--- this code is formatted to fit into 80 columns --->|
  12. */
  13. package coordination;
  14. import java.lang.String;
  15. class Mutex implements Exclusion {
  16. String[] methodNames;
  17. MethodState[] methodStates;
  18. String prettyName;
  19. Mutex (String[] _methodNames) {
  20. methodNames = _methodNames;
  21. methodStates = new MethodState[methodNames.length];
  22. for (int i = 0; i < methodNames.length; i++) {
  23. methodStates[i] = new MethodState();
  24. }
  25. }
  26. private boolean isMethodIn (String _methodName) {
  27. for (int i = 0; i < methodNames.length; i++) {
  28. if (_methodName.equals(methodNames[i]))
  29. return(true);
  30. }
  31. return(false);
  32. }
  33. private MethodState getMethodState (String _methodName) {
  34. for (int i = 0; i < methodNames.length; i++) {
  35. if (_methodName.equals(methodNames[i]))
  36. return(methodStates[i]);
  37. }
  38. return(null);
  39. }
  40. public boolean testExclusion (String _methodName) {
  41. Thread ct = Thread.currentThread();
  42. //
  43. // Loop through each of the other methods in this exclusion set, to be sure
  44. // that no other thread is running them. Note that we have to be careful
  45. // about selfex.
  46. //
  47. for (int i = 0; i < methodNames.length; i++) {
  48. if (!_methodName.equals(methodNames[i])) {
  49. if (methodStates[i].hasOtherThreadThan(ct))
  50. return(false);
  51. }
  52. }
  53. return (true);
  54. }
  55. public void enterExclusion (String _methodName) {
  56. MethodState methodState = getMethodState(_methodName);
  57. methodState.enterInThread(Thread.currentThread());
  58. }
  59. public void exitExclusion (String _methodName) {
  60. MethodState methodState = getMethodState(_methodName);
  61. methodState.exitInThread(Thread.currentThread());
  62. }
  63. public void printNames() {
  64. System.out.print("Mutex names: ");
  65. for (int i = 0; i < methodNames.length; i++)
  66. System.out.print(methodNames[i] + " ");
  67. System.out.println();
  68. }
  69. }