00001
00002
00003 #include "ace/Mutex.h"
00004
00005 #if !defined (__ACE_INLINE__)
00006 #include "ace/Mutex.inl"
00007 #endif
00008
00009 #include "ace/Log_Msg.h"
00010 #include "ace/OS_NS_string.h"
00011 #include "ace/os_include/sys/os_mman.h"
00012
00013
00014 ACE_RCSID (ace,
00015 Mutex,
00016 "$Id: Mutex.cpp 80826 2008-03-04 14:51:23Z wotte $")
00017
00018 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00019
00020 ACE_ALLOC_HOOK_DEFINE(ACE_Mutex)
00021
00022 void
00023 ACE_Mutex::dump (void) const
00024 {
00025
00026
00027 ACE_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));
00028 #if defined (ACE_HAS_PTHREADS) || defined(ACE_HAS_STHREADS)
00029 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("lockname_ = %s\n"), this->lockname_));
00030 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("process_lock_ = %x\n"), this->process_lock_));
00031 #endif
00032 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\n")));
00033 ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
00034 }
00035
00036 ACE_Mutex::ACE_Mutex (int type, const ACE_TCHAR *name,
00037 ACE_mutexattr_t *arg, mode_t mode)
00038 :
00039 #if defined (ACE_HAS_PTHREADS) || defined(ACE_HAS_STHREADS)
00040 process_lock_ (0),
00041 lockname_ (0),
00042 #endif
00043 removed_ (false)
00044 {
00045
00046
00047
00048 #if defined(ACE_HAS_PTHREADS) || defined (ACE_HAS_STHREADS)
00049 if (type == USYNC_PROCESS)
00050 {
00051
00052 ACE_HANDLE fd = ACE_OS::shm_open (name, O_RDWR | O_CREAT | O_EXCL, mode);
00053 if (fd == ACE_INVALID_HANDLE)
00054 {
00055 if (errno == EEXIST)
00056 fd = ACE_OS::shm_open (name, O_RDWR | O_CREAT, mode);
00057 else
00058 return;
00059 }
00060 else
00061 {
00062
00063 if (ACE_OS::ftruncate (fd,
00064 sizeof (ACE_mutex_t)) == -1)
00065 {
00066 ACE_OS::close (fd);
00067 return;
00068 }
00069 this->lockname_ = ACE_OS::strdup (name);
00070 if (this->lockname_ == 0)
00071 {
00072 ACE_OS::close (fd);
00073 return;
00074 }
00075 }
00076
00077 this->process_lock_ =
00078 (ACE_mutex_t *) ACE_OS::mmap (0,
00079 sizeof (ACE_mutex_t),
00080 PROT_RDWR,
00081 MAP_SHARED,
00082 fd,
00083 0);
00084 ACE_OS::close (fd);
00085 if (this->process_lock_ == MAP_FAILED)
00086 return;
00087
00088 if (this->lockname_
00089 && ACE_OS::mutex_init (this->process_lock_,
00090 type,
00091 name,
00092 arg) != 0)
00093 {
00094 ACE_ERROR ((LM_ERROR,
00095 ACE_TEXT ("%p\n"),
00096 ACE_TEXT ("ACE_Mutex::ACE_Mutex")));
00097 return;
00098 }
00099 }
00100 else
00101 {
00102
00103 #else
00104 ACE_UNUSED_ARG (mode);
00105 #endif
00106
00107 if (ACE_OS::mutex_init (&this->lock_,
00108 type,
00109 name,
00110 arg) != 0)
00111 ACE_ERROR ((LM_ERROR,
00112 ACE_TEXT ("%p\n"),
00113 ACE_TEXT ("ACE_Mutex::ACE_Mutex")));
00114 #if defined(ACE_HAS_PTHREADS) || defined (ACE_HAS_STHREADS)
00115 }
00116 #endif
00117 }
00118
00119 ACE_Mutex::~ACE_Mutex (void)
00120 {
00121
00122 this->remove ();
00123 }
00124
00125 ACE_END_VERSIONED_NAMESPACE_DECL