Go to the documentation of this file.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 84282 2009-01-30 15:04:29Z msmit $")
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 #if defined (ACE_HAS_DUMP)
00026
00027
00028 ACE_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));
00029 #if defined (ACE_HAS_PTHREADS) || defined(ACE_HAS_STHREADS)
00030 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("lockname_ = %s\n"), this->lockname_));
00031 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("process_lock_ = %x\n"), this->process_lock_));
00032 #endif
00033 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\n")));
00034 ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
00035 #endif
00036 }
00037
00038 ACE_Mutex::ACE_Mutex (int type, const ACE_TCHAR *name,
00039 ACE_mutexattr_t *arg, mode_t mode)
00040 :
00041 #if defined (ACE_HAS_PTHREADS) || defined(ACE_HAS_STHREADS)
00042 process_lock_ (0),
00043 lockname_ (0),
00044 #endif
00045 removed_ (false)
00046 {
00047
00048
00049
00050 #if defined(ACE_HAS_PTHREADS) || defined (ACE_HAS_STHREADS)
00051 if (type == USYNC_PROCESS)
00052 {
00053
00054 ACE_HANDLE fd = ACE_OS::shm_open (name, O_RDWR | O_CREAT | O_EXCL, mode);
00055 if (fd == ACE_INVALID_HANDLE)
00056 {
00057 if (errno == EEXIST)
00058 fd = ACE_OS::shm_open (name, O_RDWR | O_CREAT, mode);
00059 else
00060 return;
00061 }
00062 else
00063 {
00064
00065 if (ACE_OS::ftruncate (fd,
00066 sizeof (ACE_mutex_t)) == -1)
00067 {
00068 ACE_OS::close (fd);
00069 return;
00070 }
00071 this->lockname_ = ACE_OS::strdup (name);
00072 if (this->lockname_ == 0)
00073 {
00074 ACE_OS::close (fd);
00075 return;
00076 }
00077 }
00078
00079 this->process_lock_ =
00080 (ACE_mutex_t *) ACE_OS::mmap (0,
00081 sizeof (ACE_mutex_t),
00082 PROT_RDWR,
00083 MAP_SHARED,
00084 fd,
00085 0);
00086 ACE_OS::close (fd);
00087 if (this->process_lock_ == MAP_FAILED)
00088 return;
00089
00090 if (this->lockname_
00091 && ACE_OS::mutex_init (this->process_lock_,
00092 type,
00093 name,
00094 arg) != 0)
00095 {
00096 ACE_ERROR ((LM_ERROR,
00097 ACE_TEXT ("%p\n"),
00098 ACE_TEXT ("ACE_Mutex::ACE_Mutex")));
00099 return;
00100 }
00101 }
00102 else
00103 {
00104
00105 #else
00106 ACE_UNUSED_ARG (mode);
00107 #endif
00108
00109 if (ACE_OS::mutex_init (&this->lock_,
00110 type,
00111 name,
00112 arg) != 0)
00113 ACE_ERROR ((LM_ERROR,
00114 ACE_TEXT ("%p\n"),
00115 ACE_TEXT ("ACE_Mutex::ACE_Mutex")));
00116 #if defined(ACE_HAS_PTHREADS) || defined (ACE_HAS_STHREADS)
00117 }
00118 #endif
00119 }
00120
00121 ACE_Mutex::~ACE_Mutex (void)
00122 {
00123
00124 this->remove ();
00125 }
00126
00127 ACE_END_VERSIONED_NAMESPACE_DECL