Motr  M0
timer.c
Go to the documentation of this file.
1 /* -*- C -*- */
2 /*
3  * Copyright (c) 2013-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 #include "lib/timer.h"
24 
25 #include "lib/misc.h" /* M0_SET0 */
26 #include "lib/assert.h" /* M0_PRE */
27 #include "lib/thread.h" /* m0_enter_awkward */
28 
29 #define M0_TRACE_SUBSYSTEM M0_TRACE_SUBSYS_LIB
30 #include "lib/trace.h"
31 
39 M0_INTERNAL int m0_timer_init(struct m0_timer *timer,
40  enum m0_timer_type type,
41  struct m0_timer_locality *loc,
42  m0_timer_callback_t callback,
43  unsigned long data)
44 {
45  int rc;
46 
47  M0_PRE(callback != NULL);
49 
50  M0_SET0(timer);
51  timer->t_type = type;
52  timer->t_expire = 0;
53  timer->t_callback = callback;
54  timer->t_data = data;
55 
56  rc = m0_timer_ops[timer->t_type].tmr_init(timer, loc);
57 
58  if (rc == 0)
59  timer->t_state = M0_TIMER_INITED;
60 
61  M0_LEAVE("%p, rc=%d", timer, rc);
62  return rc;
63 }
64 
65 M0_INTERNAL void m0_timer_fini(struct m0_timer *timer)
66 {
67  M0_ENTRY("%p", timer);
69 
70  m0_timer_ops[timer->t_type].tmr_fini(timer);
71  timer->t_state = M0_TIMER_UNINIT;
72  M0_LEAVE("%p", timer);
73 }
74 
75 M0_INTERNAL void m0_timer_start(struct m0_timer *timer,
76  m0_time_t expire)
77 {
79 
80  timer->t_expire = expire;
81 
82  timer->t_state = M0_TIMER_RUNNING;
83  m0_timer_ops[timer->t_type].tmr_start(timer);
84 }
85 
86 M0_INTERNAL void m0_timer_stop(struct m0_timer *timer)
87 {
88  M0_PRE(timer->t_state == M0_TIMER_RUNNING);
89 
90  m0_timer_ops[timer->t_type].tmr_stop(timer);
91  timer->t_state = M0_TIMER_STOPPED;
92 }
93 
94 M0_INTERNAL bool m0_timer_is_started(const struct m0_timer *timer)
95 {
96  return timer->t_state == M0_TIMER_RUNNING;
97 }
98 
99 M0_INTERNAL void m0_timer_callback_execute(struct m0_timer *timer)
100 {
102  timer->t_callback(timer->t_data);
103  m0_exit_awkward();
104 }
105 
106 #undef M0_TRACE_SUBSYSTEM
107 
110 /*
111  * Local variables:
112  * c-indentation-style: "K&R"
113  * c-basic-offset: 8
114  * tab-width: 8
115  * fill-column: 80
116  * scroll-step: 1
117  * End:
118  */
M0_INTERNAL void m0_enter_awkward(void)
Definition: kthread.c:249
void(* tmr_start)(struct m0_timer *timer)
Definition: timer.h:129
int(* tmr_init)(struct m0_timer *timer, struct m0_timer_locality *loc)
Definition: timer.h:127
enum m0_timer_state t_state
Definition: timer.h:49
#define M0_PRE(cond)
m0_time_t t_expire
Definition: timer.h:47
#define NULL
Definition: misc.h:38
uint64_t m0_time_t
Definition: time.h:37
M0_LEAVE()
struct m0_bufvec data
Definition: di.c:40
M0_INTERNAL int m0_timer_init(struct m0_timer *timer, enum m0_timer_type type, struct m0_timer_locality *loc, m0_timer_callback_t callback, unsigned long data)
Definition: timer.c:39
#define M0_SET0(obj)
Definition: misc.h:64
Definition: timer.h:39
M0_INTERNAL void m0_timer_fini(struct m0_timer *timer)
Definition: timer.c:65
#define M0_ENTRY(...)
Definition: trace.h:170
m0_timer_callback_t t_callback
Definition: timer.h:43
M0_INTERNAL void m0_timer_stop(struct m0_timer *timer)
Definition: timer.c:86
M0_INTERNAL void m0_timer_start(struct m0_timer *timer, m0_time_t expire)
Definition: timer.c:75
m0_timer_type
Definition: timer.h:103
unsigned long t_data
Definition: timer.h:45
M0_INTERNAL bool m0_timer_is_started(const struct m0_timer *timer)
Definition: timer.c:94
unsigned long(* m0_timer_callback_t)(unsigned long data)
Definition: timer.h:96
M0_INTERNAL void m0_exit_awkward(void)
Definition: kthread.c:262
enum m0_timer_type t_type
Definition: timer.h:41
void(* tmr_fini)(struct m0_timer *timer)
Definition: timer.h:128
M0_INTERNAL const struct m0_timer_operations m0_timer_ops[]
Definition: timer.c:93
void(* tmr_stop)(struct m0_timer *timer)
Definition: timer.h:130
int type
Definition: dir.c:1031
int32_t rc
Definition: trigger_fop.h:47
M0_INTERNAL void m0_timer_callback_execute(struct m0_timer *timer)
Definition: timer.c:99