Motr  M0
timer.c
Go to the documentation of this file.
1 /* -*- C -*- */
2 /*
3  * Copyright (c) 2012-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 #include "lib/thread.h" /* M0_THREAD_ENTER */
25 
26 #include <linux/jiffies.h> /* timespec_to_jiffies */
27 #include <linux/version.h>
36 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4,14,0)
37 static void timer_kernel_trampoline_callback(struct timer_list *tl)
38 #else
39 static void timer_kernel_trampoline_callback(unsigned long tl)
40 #endif
41 {
42 
43  struct m0_timer *timer = container_of((struct timer_list *)tl,
44  struct m0_timer, t_timer);
45  struct m0_thread th = { 0, };
46  m0_thread_enter(&th, false);
49 }
50 
51 static int timer_kernel_init(struct m0_timer *timer,
52  struct m0_timer_locality *loc)
53 {
54  struct timer_list *tl = &timer->t_timer;
55 
56 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4,14,0)
57  timer_setup(tl, timer_kernel_trampoline_callback, tl->flags);
58 #else
59  init_timer(tl);
60  tl->data = (unsigned long)tl;
61  tl->function = timer_kernel_trampoline_callback;
62 #endif
63  return 0;
64 }
65 
66 static void timer_kernel_fini(struct m0_timer *timer)
67 {
68 }
69 
70 static void timer_kernel_start(struct m0_timer *timer)
71 {
72  struct timespec ts;
73  m0_time_t expire = timer->t_expire;
74  m0_time_t now = m0_time_now();
75 
76  expire = expire > now ? m0_time_sub(expire, now) : 0;
77  ts.tv_sec = m0_time_seconds(expire);
78  ts.tv_nsec = m0_time_nanoseconds(expire);
79  timer->t_timer.expires = jiffies + timespec_to_jiffies(&ts);
80 
81  add_timer(&timer->t_timer);
82 }
83 
84 static void timer_kernel_stop(struct m0_timer *timer)
85 {
86  /*
87  * This function returns whether it has deactivated
88  * a pending timer or not. It always successful.
89  */
90  del_timer_sync(&timer->t_timer);
91 }
92 
93 M0_INTERNAL const struct m0_timer_operations m0_timer_ops[] = {
94  [M0_TIMER_SOFT] = {
96  .tmr_fini = timer_kernel_fini,
97  .tmr_start = timer_kernel_start,
98  .tmr_stop = timer_kernel_stop,
99  },
100  [M0_TIMER_HARD] = {
101  .tmr_init = timer_kernel_init,
102  .tmr_fini = timer_kernel_fini,
103  .tmr_start = timer_kernel_start,
104  .tmr_stop = timer_kernel_stop,
105  },
106 };
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  */
int(* tmr_init)(struct m0_timer *timer, struct m0_timer_locality *loc)
Definition: timer.h:127
m0_time_t t_expire
Definition: timer.h:47
uint64_t m0_time_t
Definition: time.h:37
uint64_t m0_time_nanoseconds(const m0_time_t time)
Definition: time.c:89
struct timer_list t_timer
Definition: timer.h:52
static void timer_kernel_fini(struct m0_timer *timer)
Definition: timer.c:66
#define container_of(ptr, type, member)
Definition: misc.h:33
Definition: timer.h:39
M0_INTERNAL void m0_thread_leave(void)
Definition: kthread.c:108
static void timer_kernel_start(struct m0_timer *timer)
Definition: timer.c:70
m0_time_t m0_time_now(void)
Definition: time.c:134
M0_INTERNAL void m0_thread_enter(struct m0_thread *thread, bool full)
Definition: kthread.c:98
uint64_t m0_time_seconds(const m0_time_t time)
Definition: time.c:83
static void timer_kernel_stop(struct m0_timer *timer)
Definition: timer.c:84
m0_time_t m0_time_sub(const m0_time_t t1, const m0_time_t t2)
Definition: time.c:65
M0_INTERNAL const struct m0_timer_operations m0_timer_ops[]
Definition: timer.c:93
static void timer_kernel_trampoline_callback(struct timer_list *tl)
Definition: timer.c:37
static int timer_kernel_init(struct m0_timer *timer, struct m0_timer_locality *loc)
Definition: timer.c:51
M0_INTERNAL void m0_timer_callback_execute(struct m0_timer *timer)
Definition: timer.c:99