fsmutex.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /** BEGIN COPYRIGHT BLOCK
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  35. * Copyright (C) 2005 Red Hat, Inc.
  36. * All rights reserved.
  37. * END COPYRIGHT BLOCK **/
  38. /*
  39. * fsmutex: Mutexes that are filesystem-based so they're available from more
  40. * than one process and address space
  41. *
  42. * Rob McCool
  43. */
  44. #include "base/fsmutex.h"
  45. #ifdef THREAD_ANY
  46. #include "base/crit.h"
  47. #include "base/systhr.h"
  48. #endif
  49. #include "base/util.h"
  50. #ifdef XP_WIN32
  51. typedef HANDLE sys_fsmutex_t;
  52. #endif
  53. #ifdef XP_UNIX
  54. #include "base/file.h"
  55. typedef SYS_FILE sys_fsmutex_t;
  56. #endif
  57. typedef struct {
  58. sys_fsmutex_t mutex;
  59. char *id;
  60. #ifdef THREAD_ANY
  61. CRITICAL crit;
  62. #endif
  63. int flags;
  64. } fsmutex_s;
  65. /* ----------------------------- fsmutex_init ----------------------------- */
  66. #ifdef XP_UNIX
  67. static int
  68. _fsmutex_create(fsmutex_s *fsm, char *name, int number)
  69. {
  70. char tn[256];
  71. SYS_FILE lfd;
  72. int visible = (fsm->flags & FSMUTEX_VISIBLE ? 1 : 0);
  73. util_snprintf(tn, 256, "/tmp/%s.%d", name, number);
  74. if(!visible)
  75. unlink(tn);
  76. if( (lfd = PR_Open(tn, PR_RDWR|PR_CREATE_FILE, 0644)) == NULL)
  77. return -1;
  78. if(!visible)
  79. unlink(tn);
  80. else
  81. fsm->id = PERM_STRDUP(tn);
  82. fsm->mutex = lfd;
  83. return 0;
  84. }
  85. #endif
  86. #ifdef XP_WIN32
  87. static int
  88. _fsmutex_create(fsmutex_s *fsm, char *name, int number)
  89. {
  90. char tn[256];
  91. util_snprintf(tn, sizeof(tn), "%s.%d", name, number);
  92. fsm->id = NULL;
  93. fsm->mutex = CreateMutex(NULL, FALSE,
  94. (fsm->flags & FSMUTEX_VISIBLE ? tn : NULL));
  95. return (fsm->mutex ? 0 : -1);
  96. }
  97. #endif
  98. NSAPI_PUBLIC FSMUTEX
  99. fsmutex_init(char *name, int number, int flags)
  100. {
  101. fsmutex_s *ret = (fsmutex_s *) PERM_MALLOC(sizeof(fsmutex_s));
  102. ret->flags = flags;
  103. if(_fsmutex_create(ret, name, number) == -1) {
  104. PERM_FREE(ret);
  105. return NULL;
  106. }
  107. #ifdef THREAD_ANY
  108. if(flags & FSMUTEX_NEEDCRIT)
  109. ret->crit = crit_init();
  110. #endif
  111. return (FSMUTEX) ret;
  112. }
  113. #ifdef XP_UNIX
  114. NSAPI_PUBLIC void
  115. fsmutex_setowner(FSMUTEX fsm, uid_t uid, gid_t gid)
  116. {
  117. if(!geteuid())
  118. (void) chown( ((fsmutex_s *)fsm)->id, uid, gid);
  119. }
  120. #endif
  121. /* -------------------------- fsmutex_terminate --------------------------- */
  122. #ifdef XP_UNIX
  123. static void
  124. _fsmutex_delete(fsmutex_s *fsm)
  125. {
  126. if(fsm->flags & FSMUTEX_VISIBLE)
  127. unlink(fsm->id);
  128. PERM_FREE(fsm->id);
  129. PR_Close(fsm->mutex);
  130. }
  131. #endif
  132. #ifdef XP_WIN32
  133. static void
  134. _fsmutex_delete(fsmutex_s *fsm)
  135. {
  136. CloseHandle(fsm->mutex);
  137. }
  138. #endif
  139. NSAPI_PUBLIC void
  140. fsmutex_terminate(FSMUTEX id)
  141. {
  142. fsmutex_s *fsm = (fsmutex_s *) id;
  143. _fsmutex_delete(fsm);
  144. #ifdef THREAD_ANY
  145. if(fsm->flags & FSMUTEX_NEEDCRIT)
  146. crit_terminate(fsm->crit);
  147. #endif
  148. PERM_FREE(fsm);
  149. }
  150. /* ----------------------------- fsmutex_lock ----------------------------- */
  151. NSAPI_PUBLIC void
  152. fsmutex_lock(FSMUTEX id)
  153. {
  154. fsmutex_s *fsm = (fsmutex_s *) id;
  155. #ifdef THREAD_ANY
  156. if(fsm->flags & FSMUTEX_NEEDCRIT)
  157. crit_enter(fsm->crit);
  158. #endif
  159. #ifdef XP_UNIX
  160. #ifdef THREAD_NSPR_USER
  161. /* Poll to avoid blocking. XXXrobm If errno is wrong this may go awry. */
  162. while(system_tlock(fsm->mutex) == -1)
  163. systhread_sleep(1000);
  164. #else
  165. system_flock(fsm->mutex );
  166. #endif
  167. #endif
  168. #ifdef XP_WIN32
  169. WaitForSingleObject(fsm->mutex, INFINITE);
  170. #endif
  171. }
  172. /* ---------------------------- fsmutex_unlock ---------------------------- */
  173. NSAPI_PUBLIC void
  174. fsmutex_unlock(FSMUTEX id)
  175. {
  176. fsmutex_s *fsm = (fsmutex_s *) id;
  177. #ifdef XP_UNIX
  178. system_ulock(fsm->mutex);
  179. #endif
  180. #ifdef XP_WIN32
  181. ReleaseMutex(fsm->mutex);
  182. #endif
  183. #ifdef THREAD_ANY
  184. if(fsm->flags & FSMUTEX_NEEDCRIT)
  185. crit_exit(fsm->crit);
  186. #endif
  187. }