Motr  M0
instance.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014-2020 Seagate Technology LLC and/or its Affiliates
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * For any questions about this software or licensing,
17  * please email opensource@seagate.com or cortx-questions@seagate.com.
18  *
19  */
20 
21 
22 #include "module/instance.h"
23 #include "module/param.h" /* m0_param_sources_tlist_init */
24 #include "lib/thread.h" /* m0_thread_tls */
25 
32 M0_INTERNAL int m0_init_once(struct m0 *instance);
33 M0_INTERNAL void m0_fini_once(void);
34 M0_INTERNAL int m0_subsystems_init(void);
35 M0_INTERNAL void m0_subsystems_fini(void);
36 M0_INTERNAL int m0_quiesce_init(void);
37 M0_INTERNAL void m0_quiesce_fini(void);
38 
39 M0_LOCKERS__DEFINE(M0_INTERNAL, m0_inst, m0, i_lockers);
40 
41 M0_INTERNAL struct m0 *m0_get(void)
42 {
43  struct m0 *result = m0_thread_tls()->tls_m0_instance;
44  M0_POST(result != NULL);
45  return result;
46 }
47 
48 M0_INTERNAL void m0_set(struct m0 *instance)
49 {
50  M0_PRE(instance != NULL);
52 }
53 
54 static int level_inst_enter(struct m0_module *module)
55 {
56  switch (module->m_cur + 1) {
57  case M0_LEVEL_INST_PREPARE: {
58  struct m0 *inst = M0_AMB(inst, module, i_self);
59 
60  m0_param_sources_tlist_init(&inst->i_param_sources);
61  m0_inst_lockers_init(inst);
62  return 0;
63  }
65  return m0_quiesce_init();
66  case M0_LEVEL_INST_ONCE:
67  return m0_init_once(container_of(module, struct m0, i_self));
69  return m0_subsystems_init();
70  }
71  M0_IMPOSSIBLE("Unexpected level: %d", module->m_cur + 1);
72 }
73 
74 static void level_inst_leave(struct m0_module *module)
75 {
76  struct m0 *inst = M0_AMB(inst, module, i_self);
77 
79 
80  m0_inst_lockers_fini(inst);
81  m0_param_sources_tlist_fini(&inst->i_param_sources);
82 }
83 
84 static const struct m0_modlev levels_inst[] = {
86  .ml_name = "M0_LEVEL_INST_PREPARE",
87  .ml_enter = level_inst_enter,
88  .ml_leave = level_inst_leave
89  },
91  .ml_name = "M0_LEVEL_INST_QUIESCE_SYSTEM",
92  .ml_enter = level_inst_enter,
93  .ml_leave = (void *)m0_quiesce_fini
94  },
95  [M0_LEVEL_INST_ONCE] = {
96  .ml_name = "M0_LEVEL_INST_ONCE",
97  .ml_enter = level_inst_enter,
98  .ml_leave = (void *)m0_fini_once
99  },
101  .ml_name = "M0_LEVEL_INST_SUBSYSTEMS",
102  .ml_enter = level_inst_enter,
103  .ml_leave = (void *)m0_subsystems_fini
104  },
105  [M0_LEVEL_INST_READY] = {
106  .ml_name = "M0_LEVEL_INST_READY"
107  }
108 };
109 
110 M0_INTERNAL void m0_instance_setup(struct m0 *instance)
111 {
112  m0_module_setup(&instance->i_self, "m0 instance",
114 }
115 
#define M0_PRE(cond)
#define NULL
Definition: misc.h:38
static void level_inst_leave(struct m0_module *module)
Definition: instance.c:74
M0_INTERNAL void m0_set(struct m0 *instance)
Definition: instance.c:48
static const struct m0_modlev levels_inst[]
Definition: instance.c:84
M0_INTERNAL void m0_fini_once(void)
Definition: init.c:302
struct m0 * tls_m0_instance
Definition: thread.h:65
const char * ml_name
Definition: module.h:114
M0_INTERNAL void m0_instance_setup(struct m0 *instance)
Definition: instance.c:110
M0_INTERNAL struct m0 * m0_get(void)
Definition: instance.c:41
#define container_of(ptr, type, member)
Definition: misc.h:33
M0_INTERNAL void m0_subsystems_fini(void)
Definition: init.c:338
M0_INTERNAL struct m0_thread_tls * m0_thread_tls(void)
Definition: kthread.c:67
M0_INTERNAL void m0_quiesce_fini(void)
Definition: init.c:348
#define M0_AMB(obj, ptr, field)
Definition: misc.h:320
M0_INTERNAL void m0_module_setup(struct m0_module *module, const char *name, const struct m0_modlev *level, int level_nr, struct m0 *instance)
Definition: module.c:193
Definition: instance.h:80
M0_INTERNAL int m0_quiesce_init(void)
Definition: init.c:343
static int level_inst_enter(struct m0_module *module)
Definition: instance.c:54
#define M0_POST(cond)
int m_cur
Definition: module.h:160
M0_INTERNAL int m0_subsystems_init(void)
Definition: init.c:333
static struct m0 instance
Definition: main.c:78
M0_LOCKERS__DEFINE(M0_INTERNAL, m0_inst, m0, i_lockers)
M0_INTERNAL int m0_init_once(struct m0 *instance)
Definition: init.c:279
struct m0_module i_self
Definition: instance.h:88
struct m0_tl i_param_sources
Definition: instance.h:105
#define ARRAY_SIZE(a)
Definition: misc.h:45
#define M0_IMPOSSIBLE(fmt,...)