Merge pull request #2309 from mitza-oci/warnings
[ACE_TAO.git] / ACE / ace / Mutex.cpp
blob9a41c715ddbe55c76846846504d353a2b9beb96f
1 #include "ace/Mutex.h"
3 #if !defined (__ACE_INLINE__)
4 #include "ace/Mutex.inl"
5 #endif /* __ACE_INLINE__ */
7 #include "ace/Log_Category.h"
8 #include "ace/OS_NS_string.h"
9 #include "ace/os_include/sys/os_mman.h"
10 #if defined (ACE_HAS_ALLOC_HOOKS)
11 # include "ace/Malloc_Base.h"
12 #endif /* ACE_HAS_ALLOC_HOOKS */
14 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
16 ACE_ALLOC_HOOK_DEFINE (ACE_Mutex)
18 void
19 ACE_Mutex::dump () const
21 #if defined (ACE_HAS_DUMP)
22 // ACE_TRACE ("ACE_Mutex::dump");
24 ACELIB_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));
25 # ifdef ACE_MUTEX_USE_PROCESS_LOCK
26 ACELIB_DEBUG ((LM_DEBUG, ACE_TEXT ("lockname_ = %s\n"), this->lockname_));
27 ACELIB_DEBUG ((LM_DEBUG, ACE_TEXT ("process_lock_ = %x\n"), this->process_lock_));
28 # endif /* ACE_MUTEX_USE_PROCESS_LOCK */
29 ACELIB_DEBUG ((LM_DEBUG, ACE_TEXT ("\n")));
30 ACELIB_DEBUG ((LM_DEBUG, ACE_END_DUMP));
31 #endif /* ACE_HAS_DUMP */
34 int
35 ACE_Mutex::unlink (const ACE_TCHAR *name)
37 #ifdef ACE_MUTEX_PROCESS_LOCK_IS_SEMA
38 return ACE_OS::sema_unlink (ACE_TEXT_ALWAYS_CHAR (name));
39 #else
40 ACE_UNUSED_ARG (name);
41 return 0;
42 #endif
45 ACE_Mutex::ACE_Mutex (int type, const ACE_TCHAR *name,
46 ACE_mutexattr_t *arg, mode_t mode)
48 #ifdef ACE_MUTEX_USE_PROCESS_LOCK
49 process_lock_ (0),
50 lockname_ (0),
51 #endif /* ACE_MUTEX_USE_PROCESS_LOCK */
52 removed_ (false)
54 // ACE_TRACE ("ACE_Mutex::ACE_Mutex");
56 // These platforms need process-wide mutex to be in shared memory.
57 #ifdef ACE_MUTEX_PROCESS_LOCK_IS_MUTEX
58 if (type == USYNC_PROCESS)
60 // Let's see if the shared memory entity already exists.
61 ACE_HANDLE fd = ACE_OS::shm_open (name, O_RDWR | O_CREAT | O_EXCL, mode);
62 if (fd == ACE_INVALID_HANDLE)
64 if (errno == EEXIST)
65 fd = ACE_OS::shm_open (name, O_RDWR | O_CREAT, mode);
66 else
67 return;
69 else
71 // We own this shared memory object! Let's set its size.
72 if (ACE_OS::ftruncate (fd,
73 sizeof (ACE_mutex_t)) == -1)
75 ACE_OS::close (fd);
76 return;
78 this->lockname_ = ACE_OS::strdup (name);
79 if (this->lockname_ == 0)
81 ACE_OS::close (fd);
82 return;
86 this->process_lock_ =
87 (ACE_mutex_t *) ACE_OS::mmap (0,
88 sizeof (ACE_mutex_t),
89 PROT_RDWR,
90 MAP_SHARED,
91 fd,
92 0);
93 ACE_OS::close (fd);
94 if (this->process_lock_ == MAP_FAILED)
95 return;
97 if (this->lockname_
98 && ACE_OS::mutex_init (this->process_lock_,
99 type,
100 name,
101 arg) != 0)
103 ACELIB_ERROR ((LM_ERROR, ACE_TEXT ("%p\n"),
104 ACE_TEXT ("ACE_Mutex::ACE_Mutex")));
106 return;
109 #elif defined ACE_MUTEX_PROCESS_LOCK_IS_SEMA
110 ACE_UNUSED_ARG (mode);
111 if (type == USYNC_PROCESS)
113 if (name)
114 this->lockname_ = ACE_OS::strdup (name);
115 else
117 const size_t un_len = (ACE_UNIQUE_NAME_LEN + 1) * sizeof (ACE_TCHAR);
118 ACE_TCHAR *const un =
119 # ifdef ACE_HAS_ALLOC_HOOKS
120 (ACE_TCHAR *) ACE_Allocator::instance ()->malloc (un_len);
121 # else
122 (ACE_TCHAR *) ACE_OS::malloc (un_len);
123 # endif /* ACE_HAS_ALLOC_HOOKS */
124 un[0] = ACE_TEXT ('/');
125 ACE_OS::unique_name (this, un + 1, ACE_UNIQUE_NAME_LEN);
126 this->lockname_ = un;
129 this->process_lock_ = &this->process_sema_;
130 if (ACE_OS::sema_init (&this->process_sema_, 1 /*mutex unlocked*/,
131 USYNC_PROCESS, this->lockname_) != 0)
132 ACELIB_ERROR ((LM_ERROR, ACE_TEXT ("%p\n"),
133 ACE_TEXT ("ACE_Mutex::ACE_Mutex")));
134 ACE_OS::sema_avoid_unlink (&this->process_sema_, true);
135 return;
137 #else
138 ACE_UNUSED_ARG (mode);
139 #endif /* ACE_MUTEX_PROCESS_LOCK_IS_MUTEX */
141 if (ACE_OS::mutex_init (&this->lock_, type, name, arg) != 0)
142 ACELIB_ERROR ((LM_ERROR, ACE_TEXT ("%p\n"),
143 ACE_TEXT ("ACE_Mutex::ACE_Mutex")));
146 ACE_Mutex::~ACE_Mutex ()
148 // ACE_TRACE ("ACE_Mutex::~ACE_Mutex");
149 this->remove ();
152 ACE_END_VERSIONED_NAMESPACE_DECL