fsmutex.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. #ifdef HAVE_CONFIG_H
  39. # include <config.h>
  40. #endif
  41. /*
  42. * fsmutex: Mutexes that are filesystem-based so they're available from more
  43. * than one process and address space
  44. *
  45. * Rob McCool
  46. */
  47. #include "base/fsmutex.h"
  48. #ifdef THREAD_ANY
  49. #include "base/crit.h"
  50. #include "base/systhr.h"
  51. #endif
  52. #include "base/util.h"
  53. #ifdef XP_WIN32
  54. typedef HANDLE sys_fsmutex_t;
  55. #endif
  56. #ifdef XP_UNIX
  57. #include "base/file.h"
  58. typedef SYS_FILE sys_fsmutex_t;
  59. #endif
  60. typedef struct {
  61. sys_fsmutex_t mutex;
  62. char *id;
  63. #ifdef THREAD_ANY
  64. CRITICAL crit;
  65. #endif
  66. int flags;
  67. } fsmutex_s;
  68. /* ----------------------------- fsmutex_init ----------------------------- */
  69. #ifdef XP_UNIX
  70. static int
  71. _fsmutex_create(fsmutex_s *fsm, char *name, int number)
  72. {
  73. char tn[256];
  74. SYS_FILE lfd;
  75. int visible = (fsm->flags & FSMUTEX_VISIBLE ? 1 : 0);
  76. util_snprintf(tn, 256, "/tmp/%s.%d", name, number);
  77. if(!visible)
  78. unlink(tn);
  79. if( (lfd = PR_Open(tn, PR_RDWR|PR_CREATE_FILE, 0644)) == NULL)
  80. return -1;
  81. if(!visible)
  82. unlink(tn);
  83. else
  84. fsm->id = PERM_STRDUP(tn);
  85. fsm->mutex = lfd;
  86. return 0;
  87. }
  88. #endif
  89. #ifdef XP_WIN32
  90. static int
  91. _fsmutex_create(fsmutex_s *fsm, char *name, int number)
  92. {
  93. char tn[256];
  94. util_snprintf(tn, sizeof(tn), "%s.%d", name, number);
  95. fsm->id = NULL;
  96. fsm->mutex = CreateMutex(NULL, FALSE,
  97. (fsm->flags & FSMUTEX_VISIBLE ? tn : NULL));
  98. return (fsm->mutex ? 0 : -1);
  99. }
  100. #endif
  101. NSAPI_PUBLIC FSMUTEX
  102. fsmutex_init(char *name, int number, int flags)
  103. {
  104. fsmutex_s *ret = (fsmutex_s *) PERM_MALLOC(sizeof(fsmutex_s));
  105. ret->flags = flags;
  106. if(_fsmutex_create(ret, name, number) == -1) {
  107. PERM_FREE(ret);
  108. return NULL;
  109. }
  110. #ifdef THREAD_ANY
  111. if(flags & FSMUTEX_NEEDCRIT)
  112. ret->crit = crit_init();
  113. #endif
  114. return (FSMUTEX) ret;
  115. }
  116. #ifdef XP_UNIX
  117. NSAPI_PUBLIC void
  118. fsmutex_setowner(FSMUTEX fsm, uid_t uid, gid_t gid)
  119. {
  120. if(!geteuid())
  121. (void) chown( ((fsmutex_s *)fsm)->id, uid, gid);
  122. }
  123. #endif
  124. /* -------------------------- fsmutex_terminate --------------------------- */
  125. #ifdef XP_UNIX
  126. static void
  127. _fsmutex_delete(fsmutex_s *fsm)
  128. {
  129. if(fsm->flags & FSMUTEX_VISIBLE)
  130. unlink(fsm->id);
  131. PERM_FREE(fsm->id);
  132. PR_Close(fsm->mutex);
  133. }
  134. #endif
  135. #ifdef XP_WIN32
  136. static void
  137. _fsmutex_delete(fsmutex_s *fsm)
  138. {
  139. CloseHandle(fsm->mutex);
  140. }
  141. #endif
  142. NSAPI_PUBLIC void
  143. fsmutex_terminate(FSMUTEX id)
  144. {
  145. fsmutex_s *fsm = (fsmutex_s *) id;
  146. _fsmutex_delete(fsm);
  147. #ifdef THREAD_ANY
  148. if(fsm->flags & FSMUTEX_NEEDCRIT)
  149. crit_terminate(fsm->crit);
  150. #endif
  151. PERM_FREE(fsm);
  152. }
  153. /* ----------------------------- fsmutex_lock ----------------------------- */
  154. NSAPI_PUBLIC void
  155. fsmutex_lock(FSMUTEX id)
  156. {
  157. fsmutex_s *fsm = (fsmutex_s *) id;
  158. #ifdef THREAD_ANY
  159. if(fsm->flags & FSMUTEX_NEEDCRIT)
  160. crit_enter(fsm->crit);
  161. #endif
  162. #ifdef XP_UNIX
  163. #ifdef THREAD_NSPR_USER
  164. /* Poll to avoid blocking. XXXrobm If errno is wrong this may go awry. */
  165. while(system_tlock(fsm->mutex) == -1)
  166. systhread_sleep(1000);
  167. #else
  168. system_flock(fsm->mutex );
  169. #endif
  170. #endif
  171. #ifdef XP_WIN32
  172. WaitForSingleObject(fsm->mutex, INFINITE);
  173. #endif
  174. }
  175. /* ---------------------------- fsmutex_unlock ---------------------------- */
  176. NSAPI_PUBLIC void
  177. fsmutex_unlock(FSMUTEX id)
  178. {
  179. fsmutex_s *fsm = (fsmutex_s *) id;
  180. #ifdef XP_UNIX
  181. system_ulock(fsm->mutex);
  182. #endif
  183. #ifdef XP_WIN32
  184. ReleaseMutex(fsm->mutex);
  185. #endif
  186. #ifdef THREAD_ANY
  187. if(fsm->flags & FSMUTEX_NEEDCRIT)
  188. crit_exit(fsm->crit);
  189. #endif
  190. }