Skip to content

Commit 4c73660

Browse files
xiaoxiang781216Alan C. Assis
authored andcommitted
libc/semaphore: Go the fast path even arch doesn't support atomic
since the simulated atomic operation is still fast than the slow path Signed-off-by: Xiang Xiao <[email protected]>
1 parent fb14b54 commit 4c73660

File tree

3 files changed

+15
-49
lines changed

3 files changed

+15
-49
lines changed

libs/libc/semaphore/sem_post.c

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -119,37 +119,30 @@ int sem_post(FAR sem_t *sem)
119119

120120
int nxsem_post(FAR sem_t *sem)
121121
{
122-
bool mutex;
123122
bool fastpath = true;
123+
bool mutex;
124124

125125
DEBUGASSERT(sem != NULL);
126126

127-
/* We don't do atomic fast path in case of LIBC_ARCH_ATOMIC because that
128-
* uses spinlocks, which can't be called from userspace. Also in the kernel
129-
* taking the slow path directly is faster than locking first in here
130-
*/
131-
132-
#ifndef CONFIG_LIBC_ARCH_ATOMIC
133-
134127
mutex = NXSEM_IS_MUTEX(sem);
135128

136129
/* Disable fast path if priority protection is enabled on the semaphore */
137130

138-
# ifdef CONFIG_PRIORITY_PROTECT
131+
#ifdef CONFIG_PRIORITY_PROTECT
139132
if ((sem->flags & SEM_PRIO_MASK) == SEM_PRIO_PROTECT)
140133
{
141134
fastpath = false;
142135
}
143-
# endif
136+
#endif
144137

145138
/* Disable fast path on a counting semaphore with priority inheritance */
146139

147-
# ifdef CONFIG_PRIORITY_INHERITANCE
140+
#ifdef CONFIG_PRIORITY_INHERITANCE
148141
if (!mutex && (sem->flags & SEM_PRIO_MASK) != SEM_PRIO_NONE)
149142
{
150143
fastpath = false;
151144
}
152-
# endif
145+
#endif
153146

154147
while (fastpath)
155148
{
@@ -181,10 +174,6 @@ int nxsem_post(FAR sem_t *sem)
181174
return OK;
182175
}
183176
}
184-
#else
185-
UNUSED(mutex);
186-
UNUSED(fastpath);
187-
#endif
188177

189178
return nxsem_post_slow(sem);
190179
}

libs/libc/semaphore/sem_trywait.c

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,8 @@ int sem_trywait(FAR sem_t *sem)
107107

108108
int nxsem_trywait(FAR sem_t *sem)
109109
{
110-
bool mutex;
111110
bool fastpath = true;
111+
bool mutex;
112112

113113
DEBUGASSERT(sem != NULL);
114114

@@ -119,32 +119,25 @@ int nxsem_trywait(FAR sem_t *sem)
119119
up_interrupt_context());
120120
#endif
121121

122-
/* We don't do atomic fast path in case of LIBC_ARCH_ATOMIC because that
123-
* uses spinlocks, which can't be called from userspace. Also in the kernel
124-
* taking the slow path directly is faster than locking first in here
125-
*/
126-
127-
#ifndef CONFIG_LIBC_ARCH_ATOMIC
128-
129122
mutex = NXSEM_IS_MUTEX(sem);
130123

131124
/* Disable fast path if priority protection is enabled on the semaphore */
132125

133-
# ifdef CONFIG_PRIORITY_PROTECT
126+
#ifdef CONFIG_PRIORITY_PROTECT
134127
if ((sem->flags & SEM_PRIO_MASK) == SEM_PRIO_PROTECT)
135128
{
136129
fastpath = false;
137130
}
138-
# endif
131+
#endif
139132

140133
/* Disable fast path on a counting semaphore with priority inheritance */
141134

142-
# ifdef CONFIG_PRIORITY_INHERITANCE
135+
#ifdef CONFIG_PRIORITY_INHERITANCE
143136
if (!mutex && (sem->flags & SEM_PRIO_MASK) != SEM_PRIO_NONE)
144137
{
145138
fastpath = false;
146139
}
147-
# endif
140+
#endif
148141

149142
while (fastpath)
150143
{
@@ -177,10 +170,5 @@ int nxsem_trywait(FAR sem_t *sem)
177170
}
178171
}
179172

180-
#else
181-
UNUSED(mutex);
182-
UNUSED(fastpath);
183-
#endif
184-
185173
return nxsem_trywait_slow(sem);
186174
}

libs/libc/semaphore/sem_wait.c

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,8 @@ int sem_wait(FAR sem_t *sem)
135135

136136
int nxsem_wait(FAR sem_t *sem)
137137
{
138-
bool mutex;
139138
bool fastpath = true;
139+
bool mutex;
140140

141141
DEBUGASSERT(sem != NULL);
142142

@@ -147,32 +147,25 @@ int nxsem_wait(FAR sem_t *sem)
147147
up_interrupt_context());
148148
#endif
149149

150-
/* We don't do atomic fast path in case of LIBC_ARCH_ATOMIC because that
151-
* uses spinlocks, which can't be called from userspace. Also in the kernel
152-
* taking the slow path directly is faster than locking first in here
153-
*/
154-
155-
#ifndef CONFIG_LIBC_ARCH_ATOMIC
156-
157150
mutex = NXSEM_IS_MUTEX(sem);
158151

159152
/* Disable fast path if priority protection is enabled on the semaphore */
160153

161-
# ifdef CONFIG_PRIORITY_PROTECT
154+
#ifdef CONFIG_PRIORITY_PROTECT
162155
if ((sem->flags & SEM_PRIO_MASK) == SEM_PRIO_PROTECT)
163156
{
164157
fastpath = false;
165158
}
166-
# endif
159+
#endif
167160

168161
/* Disable fast path on a counting semaphore with priority inheritance */
169162

170-
# ifdef CONFIG_PRIORITY_INHERITANCE
163+
#ifdef CONFIG_PRIORITY_INHERITANCE
171164
if (!mutex && (sem->flags & SEM_PRIO_MASK) != SEM_PRIO_NONE)
172165
{
173166
fastpath = false;
174167
}
175-
# endif
168+
#endif
176169

177170
while (fastpath)
178171
{
@@ -204,10 +197,6 @@ int nxsem_wait(FAR sem_t *sem)
204197
return OK;
205198
}
206199
}
207-
#else
208-
UNUSED(mutex);
209-
UNUSED(fastpath);
210-
#endif
211200

212201
return nxsem_wait_slow(sem);
213202
}

0 commit comments

Comments
 (0)