atomic_lock.C 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * CDE - Common Desktop Environment
  3. *
  4. * Copyright (c) 1993-2012, The Open Group. All rights reserved.
  5. *
  6. * These libraries and programs are free software; you can
  7. * redistribute them and/or modify them under the terms of the GNU
  8. * Lesser General Public License as published by the Free Software
  9. * Foundation; either version 2 of the License, or (at your option)
  10. * any later version.
  11. *
  12. * These libraries and programs are distributed in the hope that
  13. * they will be useful, but WITHOUT ANY WARRANTY; without even the
  14. * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  15. * PURPOSE. See the GNU Lesser General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with these libraries and programs; if not, write
  20. * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
  21. * Floor, Boston, MA 02110-1301 USA
  22. */
  23. /*
  24. * $XConsortium: atomic_lock.C /main/4 1996/11/01 10:19:09 drk $
  25. *
  26. * Copyright (c) 1993 HAL Computer Systems International, Ltd.
  27. * All rights reserved. Unpublished -- rights reserved under
  28. * the Copyright Laws of the United States. USE OF A COPYRIGHT
  29. * NOTICE IS PRECAUTIONARY ONLY AND DOES NOT IMPLY PUBLICATION
  30. * OR DISCLOSURE.
  31. *
  32. * THIS SOFTWARE CONTAINS CONFIDENTIAL INFORMATION AND TRADE
  33. * SECRETS OF HAL COMPUTER SYSTEMS INTERNATIONAL, LTD. USE,
  34. * DISCLOSURE, OR REPRODUCTION IS PROHIBITED WITHOUT THE
  35. * PRIOR EXPRESS WRITTEN PERMISSION OF HAL COMPUTER SYSTEMS
  36. * INTERNATIONAL, LTD.
  37. *
  38. * RESTRICTED RIGHTS LEGEND
  39. * Use, duplication, or disclosure by the Government is subject
  40. * to the restrictions as set forth in subparagraph (c)(l)(ii)
  41. * of the Rights in Technical Data and Computer Software clause
  42. * at DFARS 252.227-7013.
  43. *
  44. * HAL COMPUTER SYSTEMS INTERNATIONAL, LTD.
  45. * 1315 Dell Avenue
  46. * Campbell, CA 95008
  47. *
  48. */
  49. #include "atomic_lock.h"
  50. #include <X11/Xosdefs.h>
  51. #include <errno.h>
  52. #ifdef X_NOT_STDC_ENV
  53. extern int errno;
  54. #endif
  55. atomic_lock::atomic_lock(const char* p)
  56. {
  57. int len = MIN(strlen(p), PATHSIZ - 1);
  58. *((char *) memcpy(v_path, p, len) + len) = '\0';
  59. }
  60. atomic_lock::~atomic_lock()
  61. {
  62. }
  63. /////////////////////////////////////////
  64. // from Rochkind's Adv. UNIX programming
  65. /////////////////////////////////////////
  66. Boolean atomic_lock::lock()
  67. {
  68. int fd, tries;
  69. tries = 0;
  70. while ( (fd = creat(v_path, 0)) == -1 && errno == EACCES ) {
  71. if ( ++tries >= MAXTRIES )
  72. break;
  73. sleep (NAPTIME);
  74. }
  75. if ( fd == -1 || close(fd) == -1 ) {
  76. perror("lock");
  77. return false;
  78. } else
  79. return true;
  80. }
  81. Boolean atomic_lock::unlock()
  82. {
  83. if ( unlink(v_path) == -1 ) {
  84. MESSAGE(cerr, "unlock(): unlink() failed");
  85. throw(systemException(errno));
  86. }
  87. return true;
  88. }