lock.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. /*
  3. *
  4. * Licensed to the Apache Software Foundation (ASF) under one
  5. * or more contributor license agreements. See the NOTICE file
  6. * distributed with this work for additional information
  7. * regarding copyright ownership. The ASF licenses this file
  8. * to you under the Apache License, Version 2.0 (the
  9. * "License"); you may not use this file except in compliance
  10. * with the License. You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing,
  15. * software distributed under the License is distributed on an
  16. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  17. * KIND, either express or implied. See the License for the
  18. * specific language governing permissions and limitations
  19. * under the License.
  20. *
  21. */
  22. $logger = new HMCLogger("Lock");
  23. $lockFile = $GLOBALS['HMC_CLUSTER_PATH']."/lockfile";
  24. $GLOBALS['fileHdl'] = 0;
  25. function LockAcquire($suffixT = "") {
  26. global $lockFile;
  27. global $logger;
  28. $suffix = strtolower($suffixT);
  29. $suffixLockFile = $lockFile;
  30. if ($suffix != "") {
  31. $suffixLockFile = $lockFile.".".$suffix;
  32. }
  33. if ($suffix != "" && !file_exists($suffixLockFile)) {
  34. $h = fopen($suffixLockFile, "x");
  35. if ($h !== FALSE) {
  36. fclose($h);
  37. }
  38. }
  39. $logger->log_trace("About to acquire lock using $suffixLockFile for pid: " .
  40. json_encode(posix_getpid()));
  41. $fileHdl = fopen($suffixLockFile, "r");
  42. $GLOBALS['fileHdl'.$suffix] = $fileHdl;
  43. $retval = flock($fileHdl, LOCK_EX);
  44. /* Uncomment for debugging purposes
  45. $e = new Exception;
  46. $logger->log_trace("Acquired Lock Status for Process: "
  47. . json_encode(posix_getpid()). " : ". json_encode($retval) . " backtrace: ".
  48. $e->getTraceAsString());
  49. unset($e);
  50. */
  51. return;
  52. }
  53. function LockRelease($suffixT = "") {
  54. global $lockFile;
  55. global $logger;
  56. $suffix = strtolower($suffixT);
  57. $suffixLockFile = $lockFile;
  58. if ($suffix != "") {
  59. $suffixLockFile = $lockFile.".".$suffix;
  60. }
  61. $logger->log_trace("Trying to release lock using $suffixLockFile for process: "
  62. . json_encode(posix_getpid()));
  63. $fileHdl = $GLOBALS['fileHdl'.$suffix];
  64. $retval = flock($fileHdl, LOCK_UN);
  65. /* Uncomment for debugging purposes
  66. $e = new Exception;
  67. $logger->log_trace("Released Lock Status for Process: "
  68. . json_encode(posix_getpid()). " : ". json_encode($retval) . " backtrace: ".
  69. $e->getTraceAsString());
  70. unset($e);
  71. */
  72. }
  73. ?>