Motr  M0
semaphore.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/semaphore.h"
24 #include "lib/assert.h"
25 #include "lib/errno.h"
26 #include "lib/types.h" /* INT_MAX */
27 #include "lib/arith.h" /* min_check */
28 
37 M0_INTERNAL int m0_semaphore_init(struct m0_semaphore *semaphore,
38  unsigned value)
39 {
40  int rc;
41  int err;
42 
43  rc = sem_init(&semaphore->s_sem, 0, value);
44  err = rc == 0 ? 0 : errno;
45  M0_ASSERT_INFO(rc == 0, "rc=%d errno=%d", rc, err);
46  return -err;
47 }
48 
49 M0_INTERNAL void m0_semaphore_fini(struct m0_semaphore *semaphore)
50 {
51  int rc;
52 
53  rc = sem_destroy(&semaphore->s_sem);
54  M0_ASSERT_INFO(rc == 0, "rc=%d errno=%d", rc, errno);
55 }
56 
57 M0_INTERNAL void m0_semaphore_down(struct m0_semaphore *semaphore)
58 {
59  int rc;
60 
61  do
62  rc = sem_wait(&semaphore->s_sem);
63  while (rc == -1 && errno == EINTR);
64  M0_ASSERT_INFO(rc == 0, "rc=%d errno=%d", rc, errno);
65 }
66 
67 M0_INTERNAL void m0_semaphore_up(struct m0_semaphore *semaphore)
68 {
69  int rc;
70 
71  rc = sem_post(&semaphore->s_sem);
72  M0_ASSERT_INFO(rc == 0, "rc=%d errno=%d", rc, errno);
73 }
74 
75 M0_INTERNAL bool m0_semaphore_trydown(struct m0_semaphore *semaphore)
76 {
77  int rc;
78 
79  do
80  rc = sem_trywait(&semaphore->s_sem);
81  while (rc == -1 && errno == EINTR);
82  M0_ASSERT_INFO(rc == 0 || (rc == -1 && errno == EAGAIN),
83  "rc=%d errno=%d", rc, errno);
84  errno = 0;
85  return rc == 0;
86 }
87 
88 M0_INTERNAL unsigned m0_semaphore_value(struct m0_semaphore *semaphore)
89 {
90  int rc;
91  int result;
92 
93  rc = sem_getvalue(&semaphore->s_sem, &result);
94  M0_ASSERT_INFO(rc == 0, "rc=%d errno=%d", rc, errno);
95  M0_POST(result >= 0);
96  return result;
97 }
98 
99 M0_INTERNAL bool m0_semaphore_timeddown(struct m0_semaphore *semaphore,
100  const m0_time_t abs_timeout)
101 {
102  m0_time_t abs_timeout_realtime = m0_time_to_realtime(abs_timeout);
103  struct timespec ts = {
104  .tv_sec = m0_time_seconds(abs_timeout_realtime),
105  .tv_nsec = m0_time_nanoseconds(abs_timeout_realtime)
106  };
107  int rc;
108 
109  /*
110  * Workaround for sem_timedwait(3) on Centos >= 7.2, which returns
111  * -ETIMEDOUT immediately if tv_sec is greater than
112  * gettimeofday(2) + INT_MAX.
113  *
114  * For more information refer to:
115  * https://bugzilla.redhat.com/show_bug.cgi?id=1412082
116  * https://jts.seagate.com/browse/CASTOR-1990
117  * `git blame` these lines and read commit message
118  * doc/workarounds.md
119  *
120  * It should be reverted when glibc is fixed in future RedHat releases.
121  */
122  ts.tv_sec = min_check(ts.tv_sec, (time_t)(INT_MAX - 1));
123  /* ----- end of workaround ----- */
124 
125  do
126  rc = sem_timedwait(&semaphore->s_sem, &ts);
127  while (rc == -1 && errno == EINTR);
128  M0_ASSERT_INFO(rc == 0 || (rc == -1 && errno == ETIMEDOUT),
129  "rc=%d errno=%d", rc, errno);
130  if (rc == -1 && errno == ETIMEDOUT)
131  errno = 0;
132  return rc == 0;
133 }
134 
137 /*
138  * Local variables:
139  * c-indentation-style: "K&R"
140  * c-basic-offset: 8
141  * tab-width: 8
142  * fill-column: 80
143  * scroll-step: 1
144  * End:
145  */
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
#define min_check(a, b)
Definition: arith.h:88
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_INTERNAL int m0_semaphore_init(struct m0_semaphore *semaphore, unsigned value)
Definition: semaphore.c:38
#define M0_POST(cond)
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
#define M0_ASSERT_INFO(cond, fmt,...)
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
int32_t rc
Definition: trigger_fop.h:47
M0_INTERNAL m0_time_t m0_time_to_realtime(m0_time_t abs_time)
Definition: time.c:160