Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sched/pthread: There is no need to use sched_[un]lock #14089

Merged
merged 6 commits into from
Oct 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions arch/arm/src/at32/at32_rtc_lowerhalf.c
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,7 @@ static int at32_setrelative(struct rtc_lowerhalf_s *lower,
struct tm time;
time_t seconds;
int ret = -EINVAL;
irqstate_t flags;

DEBUGASSERT(lower != NULL && alarminfo != NULL);
DEBUGASSERT(alarminfo->id == RTC_ALARMA || alarminfo->id == RTC_ALARMB);
Expand All @@ -522,7 +523,7 @@ static int at32_setrelative(struct rtc_lowerhalf_s *lower,
* about being suspended and working on an old time.
*/

sched_lock();
flags = enter_critical_section();

/* Get the current time in broken out format */

Expand Down Expand Up @@ -552,7 +553,7 @@ static int at32_setrelative(struct rtc_lowerhalf_s *lower,
ret = at32_setalarm(lower, &setalarm);
}

sched_unlock();
leave_critical_section(flags);
}

return ret;
Expand All @@ -565,6 +566,7 @@ static int at32_setrelative(struct rtc_lowerhalf_s *lower,
#endif
struct timespec ts;
int ret = -EINVAL;
irqstate_t flags;

DEBUGASSERT(lower != NULL && alarminfo != NULL && alarminfo->id == 0);
priv = (struct at32_lowerhalf_s *)lower;
Expand All @@ -575,7 +577,7 @@ static int at32_setrelative(struct rtc_lowerhalf_s *lower,
* about being suspended and working on an old time.
*/

sched_lock();
flags = enter_critical_section();

/* Get the current time in seconds */

Expand All @@ -585,7 +587,7 @@ static int at32_setrelative(struct rtc_lowerhalf_s *lower,
ret = up_rtc_getdatetime(&time);
if (ret < 0)
{
sched_unlock();
leave_critical_section(flags);
return ret;
}

Expand All @@ -598,7 +600,7 @@ static int at32_setrelative(struct rtc_lowerhalf_s *lower,
ret = up_rtc_gettime(&ts);
if (ret < 0)
{
sched_unlock();
leave_critical_section(flags);
return ret;
}
#else
Expand Down Expand Up @@ -629,7 +631,7 @@ static int at32_setrelative(struct rtc_lowerhalf_s *lower,
cbinfo->priv = NULL;
}

sched_unlock();
leave_critical_section(flags);
}

return ret;
Expand Down Expand Up @@ -734,6 +736,7 @@ static int at32_rdalarm(struct rtc_lowerhalf_s *lower,
{
struct alm_rdalarm_s lowerinfo;
int ret = -EINVAL;
irqstate_t flags;

DEBUGASSERT(lower != NULL && alarminfo != NULL && alarminfo->time != NULL);
DEBUGASSERT(alarminfo->id == RTC_ALARMA || alarminfo->id == RTC_ALARMB);
Expand All @@ -744,14 +747,14 @@ static int at32_rdalarm(struct rtc_lowerhalf_s *lower,
* about being suspended and working on an old time.
*/

sched_lock();
flags = enter_critical_section();

lowerinfo.ar_id = alarminfo->id;
lowerinfo.ar_time = alarminfo->time;

ret = at32_rtc_rdalarm(&lowerinfo);

sched_unlock();
leave_critical_section(flags);
}

return ret;
Expand Down
7 changes: 4 additions & 3 deletions arch/arm/src/cxd56xx/cxd56_rtc_lowerhalf.c
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,7 @@ static int cxd56_setrelative(struct rtc_lowerhalf_s *lower,
struct timespec ts;
time_t seconds;
int ret = -EINVAL;
irqstate_t flags;

DEBUGASSERT(lower != NULL && alarminfo != NULL);
DEBUGASSERT((RTC_ALARM0 <= alarminfo->id) &&
Expand All @@ -372,15 +373,15 @@ static int cxd56_setrelative(struct rtc_lowerhalf_s *lower,
* about being suspended and working on an old time.
*/

sched_lock();
flags = enter_critical_section();

#if defined(CONFIG_RTC_HIRES)
/* Get the higher resolution time */

ret = up_rtc_gettime(&ts);
if (ret < 0)
{
sched_unlock();
leave_critical_section(flags);
return ret;
}
#else
Expand All @@ -406,7 +407,7 @@ static int cxd56_setrelative(struct rtc_lowerhalf_s *lower,

ret = cxd56_setalarm(lower, &setalarm);

sched_unlock();
leave_critical_section(flags);
}

return ret;
Expand Down
14 changes: 8 additions & 6 deletions arch/arm/src/kinetis/kinetis_rtc_lowerhalf.c
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,7 @@ kinetis_setrelative(struct rtc_lowerhalf_s *lower,
time_t seconds;
struct timespec ts;
int ret = -EINVAL;
irqstate_t flags;

DEBUGASSERT(lower != NULL && alarminfo != NULL);
DEBUGASSERT(alarminfo->id == RTC_ALARMA);
Expand All @@ -374,15 +375,15 @@ kinetis_setrelative(struct rtc_lowerhalf_s *lower,
* about being suspended and working on an old time.
*/

sched_lock();
flags = enter_critical_section();

#if defined(CONFIG_RTC_DATETIME)
/* Get the broken out time and convert to seconds */

ret = up_rtc_getdatetime(&time);
if (ret < 0)
{
sched_unlock();
leave_critical_section(flags);
return ret;
}

Expand All @@ -394,7 +395,7 @@ kinetis_setrelative(struct rtc_lowerhalf_s *lower,
ret = up_rtc_gettime(&ts);
if (ret < 0)
{
sched_unlock();
leave_critical_section(flags);
return ret;
}
#endif
Expand All @@ -421,7 +422,7 @@ kinetis_setrelative(struct rtc_lowerhalf_s *lower,

ret = kinetis_setalarm(lower, &setalarm);

sched_unlock();
leave_critical_section(flags);
}

return ret;
Expand Down Expand Up @@ -498,6 +499,7 @@ static int kinetis_rdalarm(struct rtc_lowerhalf_s *lower,
{
struct timespec ts;
int ret = -EINVAL;
irqstate_t flags;

DEBUGASSERT(lower != NULL && alarminfo != NULL && alarminfo->time != NULL);
DEBUGASSERT(alarminfo->id == RTC_ALARMA);
Expand All @@ -508,12 +510,12 @@ static int kinetis_rdalarm(struct rtc_lowerhalf_s *lower,
* about being suspended and working on an old time.
*/

sched_lock();
flags = enter_critical_section();
ret = kinetis_rtc_rdalarm(&ts);

localtime_r((const time_t *)&ts.tv_sec,
(struct tm *)alarminfo->time);
sched_unlock();
leave_critical_section(flags);
}

return ret;
Expand Down
5 changes: 3 additions & 2 deletions arch/arm/src/lpc54xx/lpc54_rtc_lowerhalf.c
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,7 @@ static int lpc54_setrelative(struct rtc_lowerhalf_s *lower,
struct lpc54_cbinfo_s *cbinfo;
struct timespec ts;
int ret = -EINVAL;
irqstate_t flags;

DEBUGASSERT(lower != NULL && alarminfo != NULL && alarminfo->id == 0);
priv = (struct lpc54_lowerhalf_s *)lower;
Expand All @@ -375,7 +376,7 @@ static int lpc54_setrelative(struct rtc_lowerhalf_s *lower,
* about being suspended and working on an old time.
*/

sched_lock();
flags = enter_critical_section();

/* Get the current time in seconds */

Expand Down Expand Up @@ -403,7 +404,7 @@ static int lpc54_setrelative(struct rtc_lowerhalf_s *lower,
cbinfo->priv = NULL;
}

sched_unlock();
leave_critical_section(flags);
}

return ret;
Expand Down
9 changes: 5 additions & 4 deletions arch/arm/src/max326xx/common/max326_rtc_lowerhalf.c
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,7 @@ static int max326_setrelative(struct rtc_lowerhalf_s *lower,
#endif
struct timespec ts;
int ret = -EINVAL;
irqstate_t flags;

DEBUGASSERT(lower != NULL && alarminfo != NULL && alarminfo->id == 0);
priv = (struct max326_lowerhalf_s *)lower;
Expand All @@ -440,7 +441,7 @@ static int max326_setrelative(struct rtc_lowerhalf_s *lower,
* about being suspended and working on an old time.
*/

sched_lock();
flags = enter_critical_section();

/* Get the current time in seconds */

Expand All @@ -450,7 +451,7 @@ static int max326_setrelative(struct rtc_lowerhalf_s *lower,
ret = up_rtc_getdatetime(&time);
if (ret < 0)
{
sched_unlock();
leave_critical_section(flags);
return ret;
}

Expand All @@ -463,7 +464,7 @@ static int max326_setrelative(struct rtc_lowerhalf_s *lower,
ret = up_rtc_gettime(&ts);
if (ret < 0)
{
sched_unlock();
leave_critical_section(flags);
return ret;
}
#else
Expand Down Expand Up @@ -494,7 +495,7 @@ static int max326_setrelative(struct rtc_lowerhalf_s *lower,
cbinfo->priv = NULL;
}

sched_unlock();
leave_critical_section(flags);
}

return ret;
Expand Down
4 changes: 0 additions & 4 deletions arch/arm/src/sama5/sam_mcan.c
Original file line number Diff line number Diff line change
Expand Up @@ -3035,7 +3035,6 @@ static int mcan_send(struct can_dev_s *dev, struct can_msg_s *msg)
* the MCAN device was opened O_NONBLOCK.
*/

sched_lock();
mcan_buffer_reserve(priv);

/* Get exclusive access to the MCAN peripheral */
Expand All @@ -3044,12 +3043,9 @@ static int mcan_send(struct can_dev_s *dev, struct can_msg_s *msg)
if (ret < 0)
{
mcan_buffer_release(priv);
sched_unlock();
return ret;
}

sched_unlock();

/* Get our reserved Tx FIFO/queue put index */

regval = mcan_getreg(priv, SAM_MCAN_TXFQS_OFFSET);
Expand Down
9 changes: 0 additions & 9 deletions arch/arm/src/samv7/sam_mcan.c
Original file line number Diff line number Diff line change
Expand Up @@ -3019,16 +3019,10 @@ static int mcan_send(struct can_dev_s *dev, struct can_msg_s *msg)
* not full and cannot become full at least until we add our packet to
* the FIFO.
*
* We can't get exclusive access to MCAN resources here because that
* lock the MCAN while we wait for a free buffer. Instead, the
* scheduler is locked here momentarily. See discussion in
* mcan_buffer_reserve() for an explanation.
*
* REVISIT: This needs to be extended in order to handler case where
* the MCAN device was opened O_NONBLOCK.
*/

sched_lock();
mcan_buffer_reserve(priv);

/* Get exclusive access to the MCAN peripheral */
Expand All @@ -3037,12 +3031,9 @@ static int mcan_send(struct can_dev_s *dev, struct can_msg_s *msg)
if (ret < 0)
{
mcan_buffer_release(priv);
sched_unlock();
return ret;
}

sched_unlock();

/* Get our reserved Tx FIFO/queue put index */

regval = mcan_getreg(priv, SAM_MCAN_TXFQS_OFFSET);
Expand Down
10 changes: 4 additions & 6 deletions arch/arm/src/stm32/stm32_qencoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

#include <nuttx/arch.h>
#include <nuttx/irq.h>
#include <nuttx/spinlock.h>
#include <nuttx/sensors/qencoder.h>

#include <arch/board/board.h>
Expand Down Expand Up @@ -1184,6 +1185,7 @@ static int stm32_position(struct qe_lowerhalf_s *lower, int32_t *pos)
{
struct stm32_lowerhalf_s *priv = (struct stm32_lowerhalf_s *)lower;
#ifndef CONFIG_STM32_QENCODER_DISABLE_EXTEND16BTIMERS
irqstate_t flags;
int32_t position;
int32_t verify;
uint32_t count;
Expand All @@ -1192,19 +1194,15 @@ static int stm32_position(struct qe_lowerhalf_s *lower, int32_t *pos)

/* Loop until we are certain that no interrupt occurred between samples */

flags = spin_lock_irqsave(NULL);
do
{
/* Don't let another task preempt us until we get the measurement.
* The timer interrupt may still be processed
*/

sched_lock();
position = priv->position;
count = stm32_getreg32(priv, STM32_GTIM_CNT_OFFSET);
verify = priv->position;
sched_unlock();
}
while (position != verify);
spin_unlock_irqrestore(NULL, flags);

/* Return the position measurement */

Expand Down
Loading
Loading