Motr  M0
mutex.c
Go to the documentation of this file.
1 /* -*- C -*- */
2 /*
3  * Copyright (c) 2015-2020 Seagate Technology LLC and/or its Affiliates
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * For any questions about this software or licensing,
18  * please email opensource@seagate.com or cortx-questions@seagate.com.
19  *
20  */
21 
22 
23 
30 #define M0_TRACE_SUBSYSTEM M0_TRACE_SUBSYS_LIB
31 #include "lib/mutex.h"
32 #include "lib/thread.h" /* m0_thread_self */
33 #include "lib/misc.h" /* M0_IS0 */
34 
35 M0_INTERNAL void m0_mutex_init(struct m0_mutex *mutex)
36 {
37  M0_SET0(mutex);
39 }
40 M0_EXPORTED(m0_mutex_init);
41 
42 M0_INTERNAL void m0_mutex_fini(struct m0_mutex *mutex)
43 {
44  M0_PRE(mutex->m_owner == NULL);
46 }
47 M0_EXPORTED(m0_mutex_fini);
48 
49 M0_INTERNAL void m0_mutex_lock(struct m0_mutex *mutex)
50 {
51  struct m0_mutex_addb2 *ma = mutex->m_addb2;
52 
54  if (ma == NULL)
56  else {
57  M0_ADDB2_HIST(ma->ma_id, &ma->ma_wait, m0_ptr_wrap(mutex),
59  ma->ma_taken = m0_time_now();
60  }
63 }
64 M0_EXPORTED(m0_mutex_lock);
65 
66 M0_INTERNAL void m0_mutex_unlock(struct m0_mutex *mutex)
67 {
68  struct m0_mutex_addb2 *ma = mutex->m_addb2;
69 
71  mutex->m_owner = NULL;
72  if (ma != NULL) {
73  m0_time_t hold = m0_time_now() - ma->ma_taken;
74  uint64_t datum = m0_ptr_wrap(mutex);
75 
76  m0_addb2_hist_mod_with(&ma->ma_hold, hold, datum);
77  if (ma->ma_id != 0)
78  M0_ADDB2_ADD(ma->ma_id + 1, hold, datum);
79  }
81 }
82 M0_EXPORTED(m0_mutex_unlock);
83 
84 M0_INTERNAL int m0_mutex_trylock(struct m0_mutex *mutex)
85 {
86  int try = m0_arch_mutex_trylock(&mutex->m_arch);
87  if (try == 0) {
90  }
91  return try;
92 }
93 M0_EXPORTED(m0_mutex_trylock);
94 
95 M0_INTERNAL bool m0_mutex_is_locked(const struct m0_mutex *mutex)
96 {
97  return mutex->m_owner == m0_thread_self();
98 }
99 M0_EXPORTED(m0_mutex_is_locked);
100 
101 M0_INTERNAL bool m0_mutex_is_not_locked(const struct m0_mutex *mutex)
102 {
103  return mutex->m_owner != m0_thread_self();
104 }
105 M0_EXPORTED(m0_mutex_is_not_locked);
106 
107 #undef M0_TRACE_SUBSYSTEM
108 
111 /*
112  * Local variables:
113  * c-indentation-style: "K&R"
114  * c-basic-offset: 8
115  * tab-width: 8
116  * fill-column: 80
117  * scroll-step: 1
118  * End:
119  */
120 /*
121  * vim: tabstop=8 shiftwidth=8 noexpandtab textwidth=80 nowrap
122  */
M0_INTERNAL int m0_mutex_trylock(struct m0_mutex *mutex)
Definition: mutex.c:84
#define M0_PRE(cond)
M0_INTERNAL void m0_mutex_unlock(struct m0_mutex *mutex)
Definition: mutex.c:66
#define NULL
Definition: misc.h:38
uint64_t m0_time_t
Definition: time.h:37
M0_INTERNAL bool m0_mutex_is_not_locked(const struct m0_mutex *mutex)
Definition: mutex.c:101
Definition: sock.c:772
#define M0_SET0(obj)
Definition: misc.h:64
M0_INTERNAL void m0_mutex_lock(struct m0_mutex *mutex)
Definition: mutex.c:49
M0_ADDB2_ADD(M0_AVI_FS_CREATE, new_fid.f_container, new_fid.f_key, mode, rc)
M0_INTERNAL void m0_arch_mutex_init(struct m0_arch_mutex *mutex)
Definition: mutex.c:38
#define M0_ASSERT(cond)
M0_INTERNAL bool m0_mutex_is_locked(const struct m0_mutex *mutex)
Definition: mutex.c:95
M0_INTERNAL void m0_arch_mutex_lock(struct m0_arch_mutex *mutex)
Definition: mutex.c:50
m0_time_t m0_time_now(void)
Definition: time.c:134
M0_INTERNAL void m0_mutex_init(struct m0_mutex *mutex)
Definition: mutex.c:35
struct m0_mutex_addb2 * m_addb2
Definition: mutex.h:50
void m0_addb2_hist_mod_with(struct m0_addb2_hist *hist, int64_t val, uint64_t datum)
Definition: histogram.c:73
struct m0_arch_mutex m_arch
Definition: mutex.h:48
M0_INTERNAL struct m0_thread * m0_thread_self(void)
Definition: thread.c:122
struct m0_mutex mutex
Definition: format.h:170
M0_INTERNAL void m0_mutex_fini(struct m0_mutex *mutex)
Definition: mutex.c:42
M0_INTERNAL void m0_arch_mutex_unlock(struct m0_arch_mutex *mutex)
Definition: mutex.c:61
M0_INTERNAL uint64_t m0_ptr_wrap(const void *p)
Definition: misc.c:277
#define M0_ADDB2_HIST(id, hist, datum,...)
Definition: histogram.h:147
Definition: mutex.h:47
M0_INTERNAL int m0_arch_mutex_trylock(struct m0_arch_mutex *mutex)
Definition: mutex.c:56
struct m0_thread * m_owner
Definition: mutex.h:49
M0_INTERNAL void m0_arch_mutex_fini(struct m0_arch_mutex *mutex)
Definition: mutex.c:44