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
)
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 */
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
));
40 ACE_UNUSED_ARG (name
);
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
51 #endif /* ACE_MUTEX_USE_PROCESS_LOCK */
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
)
65 fd
= ACE_OS::shm_open (name
, O_RDWR
| O_CREAT
, mode
);
71 // We own this shared memory object! Let's set its size.
72 if (ACE_OS::ftruncate (fd
,
73 sizeof (ACE_mutex_t
)) == -1)
78 this->lockname_
= ACE_OS::strdup (name
);
79 if (this->lockname_
== 0)
87 (ACE_mutex_t
*) ACE_OS::mmap (0,
94 if (this->process_lock_
== MAP_FAILED
)
98 && ACE_OS::mutex_init (this->process_lock_
,
103 ACELIB_ERROR ((LM_ERROR
, ACE_TEXT ("%p\n"),
104 ACE_TEXT ("ACE_Mutex::ACE_Mutex")));
109 #elif defined ACE_MUTEX_PROCESS_LOCK_IS_SEMA
110 ACE_UNUSED_ARG (mode
);
111 if (type
== USYNC_PROCESS
)
114 this->lockname_
= ACE_OS::strdup (name
);
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
);
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);
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");
152 ACE_END_VERSIONED_NAMESPACE_DECL