Motr  M0
semaphore.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 <linux/jiffies.h> /* timespec_to_jiffies */
24 #include "lib/semaphore.h"
25 #include "lib/assert.h"
26 
27 #define M0_TRACE_SUBSYSTEM M0_TRACE_SUBSYS_M0T1FS
28 #include "lib/trace.h"
29 
38 M0_INTERNAL int m0_semaphore_init(struct m0_semaphore *semaphore,
39  unsigned value)
40 {
41  sema_init(&semaphore->s_sem, value);
42  return 0;
43 }
44 
45 M0_INTERNAL void m0_semaphore_fini(struct m0_semaphore *semaphore)
46 {
47 }
48 
49 M0_INTERNAL void m0_semaphore_down(struct m0_semaphore *semaphore)
50 {
51  int flag_was_set = 0;
52 
53  while (down_interruptible(&semaphore->s_sem) != 0)
54  flag_was_set |= test_and_clear_thread_flag(TIF_SIGPENDING);
55 
56  if (flag_was_set)
57  set_thread_flag(TIF_SIGPENDING);
58 }
59 
60 M0_INTERNAL bool m0_semaphore_trydown(struct m0_semaphore *semaphore)
61 {
62  return !down_trylock(&semaphore->s_sem);
63 }
64 
65 M0_INTERNAL void m0_semaphore_up(struct m0_semaphore *semaphore)
66 {
67  up(&semaphore->s_sem);
68 }
69 
70 M0_INTERNAL unsigned m0_semaphore_value(struct m0_semaphore *semaphore)
71 {
72  return semaphore->s_sem.count;
73 }
74 
75 M0_INTERNAL bool m0_semaphore_timeddown(struct m0_semaphore *semaphore,
76  const m0_time_t abs_timeout)
77 {
78  m0_time_t nowtime = m0_time_now();
79  m0_time_t abs_timeout_realtime = m0_time_to_realtime(abs_timeout);
80  m0_time_t reltime;
81  unsigned long reljiffies;
82  struct timespec ts;
83 
84  /* same semantics as user_space semaphore: allow abs_time < now */
85  if (abs_timeout_realtime > nowtime)
86  reltime = m0_time_sub(abs_timeout_realtime, nowtime);
87  else
88  reltime = 0;
89  ts.tv_sec = m0_time_seconds(reltime);
90  ts.tv_nsec = m0_time_nanoseconds(reltime);
91  reljiffies = timespec_to_jiffies(&ts);
92  return down_timeout(&semaphore->s_sem, reljiffies) == 0;
93 }
94 
97 #undef M0_TRACE_SUBSYSTEM
98 
99 /*
100  * Local variables:
101  * c-indentation-style: "K&R"
102  * c-basic-offset: 8
103  * tab-width: 8
104  * fill-column: 80
105  * scroll-step: 1
106  * End:
107  */
M0_INTERNAL bool m0_semaphore_trydown(struct m0_semaphore *semaphore)
Definition: semaphore.c:60
uint64_t m0_time_t
Definition: time.h:37
M0_INTERNAL bool m0_semaphore_timeddown(struct m0_semaphore *semaphore, const m0_time_t abs_timeout)
Definition: semaphore.c:75
uint64_t m0_time_nanoseconds(const m0_time_t time)
Definition: time.c:89
int const char const void * value
Definition: dir.c:325
struct semaphore s_sem
Definition: semaphore.h:36
M0_INTERNAL unsigned m0_semaphore_value(struct m0_semaphore *semaphore)
Definition: semaphore.c:70
m0_time_t m0_time_now(void)
Definition: time.c:134
M0_INTERNAL int m0_semaphore_init(struct m0_semaphore *semaphore, unsigned value)
Definition: semaphore.c:38
uint64_t m0_time_seconds(const m0_time_t time)
Definition: time.c:83
M0_INTERNAL void m0_semaphore_fini(struct m0_semaphore *semaphore)
Definition: semaphore.c:45
m0_time_t m0_time_sub(const m0_time_t t1, const m0_time_t t2)
Definition: time.c:65
M0_INTERNAL void m0_semaphore_down(struct m0_semaphore *semaphore)
Definition: semaphore.c:49
M0_INTERNAL void m0_semaphore_up(struct m0_semaphore *semaphore)
Definition: semaphore.c:65
M0_INTERNAL m0_time_t m0_time_to_realtime(m0_time_t abs_time)
Definition: time.c:160