Skip to content

Commit

Permalink
sched: remove all spin_lock_irqsave(NULL)
Browse files Browse the repository at this point in the history
Signed-off-by: hujun5 <[email protected]>
  • Loading branch information
hujun260 committed Jan 6, 2025
1 parent 3e66498 commit a85840a
Show file tree
Hide file tree
Showing 24 changed files with 97 additions and 60 deletions.
11 changes: 11 additions & 0 deletions drivers/timers/rpmsg_rtc.c
Original file line number Diff line number Diff line change
Expand Up @@ -308,8 +308,12 @@ static int rpmsg_rtc_ept_cb(FAR struct rpmsg_endpoint *ept, FAR void *data,
struct rpmsg_rtc_set_s *msg = data;

#ifdef CONFIG_RTC_RPMSG_SYNC_BASETIME
irqstate_t flags;

flags = spin_lock_irqsave(&g_basetime_lock);
g_basetime.tv_sec = msg->base_sec;
g_basetime.tv_nsec = msg->base_nsec;
spin_unlock_irqrestore(&g_basetime_lock, flags);
#else
struct timespec tp;

Expand Down Expand Up @@ -480,6 +484,7 @@ static int rpmsg_rtc_server_settime(FAR struct rtc_lowerhalf_s *lower,
FAR struct rpmsg_rtc_client_s *client;
FAR struct list_node *node;
struct rpmsg_rtc_set_s msg;
irqstate_t flags;
int ret;

ret = server->lower->ops->settime(server->lower, rtctime);
Expand All @@ -500,8 +505,10 @@ static int rpmsg_rtc_server_settime(FAR struct rtc_lowerhalf_s *lower,
ret = 1; /* Request the upper half skip clock synchronize */
}

flags = spin_lock_irqsave(&g_basetime_lock);
msg.base_sec = g_basetime.tv_sec;
msg.base_nsec = g_basetime.tv_nsec;
spin_unlock_irqrestore(&g_basetime_lock, flags);

nxmutex_lock(&server->lock);

Expand Down Expand Up @@ -730,6 +737,7 @@ static void rpmsg_rtc_server_ns_bind(FAR struct rpmsg_device *rdev,
FAR struct rpmsg_rtc_client_s *client;
struct rpmsg_rtc_set_s msg;
struct rtc_time rtctime;
irqstate_t flags;

client = kmm_zalloc(sizeof(*client));
if (client == NULL)
Expand All @@ -753,8 +761,11 @@ static void rpmsg_rtc_server_ns_bind(FAR struct rpmsg_device *rdev,
{
msg.sec = timegm((FAR struct tm *)&rtctime);
msg.nsec = rtctime.tm_nsec;

flags = spin_lock_irqsave(&g_basetime_lock);
msg.base_sec = g_basetime.tv_sec;
msg.base_nsec = g_basetime.tv_nsec;
spin_unlock_irqrestore(&g_basetime_lock, flags);

msg.header.command = RPMSG_RTC_SYNC;
rpmsg_send(&client->ept, &msg, sizeof(msg));
Expand Down
2 changes: 2 additions & 0 deletions include/nuttx/sched.h
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,8 @@ struct task_group_s
/* Virtual memory mapping info ********************************************/

struct mm_map_s tg_mm_map; /* Task group virtual memory mappings */

spinlock_t tg_lock; /* lock */
};

/* struct tcb_s *************************************************************/
Expand Down
2 changes: 2 additions & 0 deletions sched/clock/clock.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

#include <nuttx/clock.h>
#include <nuttx/compiler.h>
#include <nuttx/spinlock_type.h>

/****************************************************************************
* Pre-processor Definitions
Expand Down Expand Up @@ -66,6 +67,7 @@ extern volatile clock_t g_system_ticks;

#ifndef CONFIG_CLOCK_TIMEKEEPING
extern struct timespec g_basetime;
extern spinlock_t g_basetime_lock;
#endif

/****************************************************************************
Expand Down
8 changes: 4 additions & 4 deletions sched/clock/clock_gettime.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,15 @@ static clock_t clock_process_runtime(FAR struct tcb_s *tcb)

group = tcb->group;

flags = spin_lock_irqsave(NULL);
flags = spin_lock_irqsave(&group->tg_lock);
sq_for_every(&group->tg_members, curr)
{
tcb = container_of(curr, struct tcb_s, member);

runtime += tcb->run_time;
}

spin_unlock_irqrestore(NULL, flags);
spin_unlock_irqrestore(&group->tg_lock, flags);
return runtime;
# else /* HAVE_GROUP_MEMBERS */
return tcb->run_time;
Expand Down Expand Up @@ -109,9 +109,9 @@ void nxclock_gettime(clockid_t clock_id, FAR struct timespec *tp)
* was last set, this gives us the current time.
*/

flags = spin_lock_irqsave(NULL);
flags = spin_lock_irqsave(&g_basetime_lock);
clock_timespec_add(&g_basetime, &ts, tp);
spin_unlock_irqrestore(NULL, flags);
spin_unlock_irqrestore(&g_basetime_lock, flags);
#else
clock_timekeeping_get_wall_time(tp);
#endif
Expand Down
26 changes: 14 additions & 12 deletions sched/clock/clock_initialize.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ volatile clock_t g_system_ticks = INITIAL_SYSTEM_TIMER_TICKS;

#ifndef CONFIG_CLOCK_TIMEKEEPING
struct timespec g_basetime;
spinlock_t g_basetime_lock = SP_UNLOCKED;
#endif

/****************************************************************************
Expand Down Expand Up @@ -160,7 +161,9 @@ static void clock_inittime(FAR const struct timespec *tp)

#ifndef CONFIG_CLOCK_TIMEKEEPING
struct timespec ts;
irqstate_t flags;

flags = spin_lock_irqsave(&g_basetime_lock);
if (tp)
{
memcpy(&g_basetime, tp, sizeof(struct timespec));
Expand All @@ -170,8 +173,12 @@ static void clock_inittime(FAR const struct timespec *tp)
clock_basetime(&g_basetime);
}

spin_unlock_irqrestore(&g_basetime_lock, flags);

clock_systime_timespec(&ts);

flags = spin_lock_irqsave(&g_basetime_lock);

/* Adjust base time to hide initial timer ticks. */

g_basetime.tv_sec -= ts.tv_sec;
Expand All @@ -181,6 +188,8 @@ static void clock_inittime(FAR const struct timespec *tp)
g_basetime.tv_nsec += NSEC_PER_SEC;
g_basetime.tv_sec--;
}

spin_unlock_irqrestore(&g_basetime_lock, flags);
#else
clock_inittimekeeping(tp);
#endif
Expand Down Expand Up @@ -266,13 +275,9 @@ void clock_initialize(void)
#ifdef CONFIG_RTC
void clock_synchronize(FAR const struct timespec *tp)
{
irqstate_t flags;

/* Re-initialize the time value to match the RTC */

flags = enter_critical_section();
clock_inittime(tp);
leave_critical_section(flags);
}
#endif

Expand Down Expand Up @@ -319,10 +324,6 @@ void clock_resynchronize(FAR struct timespec *rtc_diff)
rtc_diff = &rtc_diff_tmp;
}

/* Set the time value to match the RTC */

flags = enter_critical_section();

/* Get RTC time */

ret = clock_basetime(&rtc_time);
Expand All @@ -334,9 +335,11 @@ void clock_resynchronize(FAR struct timespec *rtc_diff)

rtc_diff->tv_sec = 0;
rtc_diff->tv_nsec = 0;
goto skip;
return;
}

/* Set the time value to match the RTC */

/* Get the elapsed time since power up (in milliseconds). This is a
* bias value that we need to use to correct the base time.
*/
Expand All @@ -348,7 +351,9 @@ void clock_resynchronize(FAR struct timespec *rtc_diff)
* was last set, this gives us the current time.
*/

flags = spin_lock_irqsave(&g_basetime_lock);
clock_timespec_add(&bias, &g_basetime, &curr_ts);
spin_unlock_irqrestore(&g_basetime_lock, flags);

/* Check if RTC has advanced past system time. */

Expand Down Expand Up @@ -382,9 +387,6 @@ void clock_resynchronize(FAR struct timespec *rtc_diff)
atomic_fetch_add((FAR atomic_t *)&g_system_ticks, diff_ticks);
#endif
}

skip:
leave_critical_section(flags);
}
#endif

Expand Down
8 changes: 5 additions & 3 deletions sched/clock/clock_settime.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

#include <nuttx/arch.h>
#include <nuttx/irq.h>
#include <nuttx/spinlock.h>
#include <sys/time.h>

#include "clock/clock.h"
Expand Down Expand Up @@ -71,16 +72,17 @@ void nxclock_settime(clockid_t clock_id, FAR const struct timespec *tp)
* possible.
*/

flags = enter_critical_section();

/* Get the elapsed time since power up (in milliseconds). This is a
* bias value that we need to use to correct the base time.
*/

clock_systime_timespec(&bias);

flags = spin_lock_irqsave(&g_basetime_lock);

clock_timespec_subtract(tp, &bias, &g_basetime);

leave_critical_section(flags);
spin_unlock_irqrestore(&g_basetime_lock, flags);

/* Setup the RTC (lo- or high-res) */

Expand Down
4 changes: 2 additions & 2 deletions sched/clock/clock_systime_timespec.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ int clock_systime_timespec(FAR struct timespec *ts)

up_rtc_gettime(ts);

flags = spin_lock_irqsave(NULL);
flags = spin_lock_irqsave(&g_basetime_lock);
clock_timespec_subtract(ts, &g_basetime, ts);
spin_unlock_irqrestore(NULL, flags);
spin_unlock_irqrestore(&g_basetime_lock, flags);
}
else
{
Expand Down
1 change: 1 addition & 0 deletions sched/group/group_create.c
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ void group_postinitialize(FAR struct task_tcb_s *tcb)

DEBUGASSERT(tcb && tcb->cmn.group);
group = tcb->cmn.group;
spin_lock_init(&group->tg_lock);

/* Allocate mm_map list if required */

Expand Down
6 changes: 3 additions & 3 deletions sched/group/group_exitinfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,14 @@ int group_exitinfo(pid_t pid, FAR struct binary_s *bininfo)
irqstate_t flags;

DEBUGASSERT(bininfo != NULL);
flags = spin_lock_irqsave(NULL);
flags = enter_critical_section();

/* Get the TCB associated with the PID */

tcb = nxsched_get_tcb(pid);
if (tcb == NULL)
{
spin_unlock_irqrestore(NULL, flags);
leave_critical_section(flags);
return -ESRCH;
}

Expand All @@ -90,7 +90,7 @@ int group_exitinfo(pid_t pid, FAR struct binary_s *bininfo)

group->tg_bininfo = bininfo;

spin_unlock_irqrestore(NULL, flags);
leave_critical_section(flags);
return OK;
}

Expand Down
4 changes: 2 additions & 2 deletions sched/group/group_join.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,9 @@ void group_join(FAR struct pthread_tcb_s *tcb)

/* Add the member to the group */

flags = spin_lock_irqsave(NULL);
flags = spin_lock_irqsave(&group->tg_lock);
sq_addfirst(&tcb->cmn.member, &group->tg_members);
spin_unlock_irqrestore(NULL, flags);
spin_unlock_irqrestore(&group->tg_lock, flags);
}

#endif /* !CONFIG_DISABLE_PTHREAD */
4 changes: 2 additions & 2 deletions sched/group/group_leave.c
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,9 @@ void group_leave(FAR struct tcb_s *tcb)
/* Remove the member from group. */

#ifdef HAVE_GROUP_MEMBERS
flags = spin_lock_irqsave(NULL);
flags = spin_lock_irqsave(&group->tg_lock);
sq_rem(&tcb->member, &group->tg_members);
spin_unlock_irqrestore(NULL, flags);
spin_unlock_irqrestore(&group->tg_lock, flags);

/* Have all of the members left the group? */

Expand Down
11 changes: 6 additions & 5 deletions sched/irq/irq_attach.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
* Private Data
****************************************************************************/

static spinlock_t g_irqlock = SP_UNLOCKED;
#ifdef CONFIG_ARCH_MINIMAL_VECTORTABLE_DYNAMIC
static int g_irqmap_count = 1;
#endif
Expand Down Expand Up @@ -67,13 +68,13 @@ int irq_to_ndx(int irq)
{
DEBUGASSERT(g_irqmap_count < CONFIG_ARCH_NUSER_INTERRUPTS);

irqstate_t flags = spin_lock_irqsave(NULL);
irqstate_t flags = spin_lock_irqsave(&g_irqlock);
if (g_irqmap[irq] == 0)
{
g_irqmap[irq] = g_irqmap_count++;
}

spin_unlock_irqrestore(NULL, flags);
spin_unlock_irqrestore(&g_irqlock, flags);
return g_irqmap[irq];
}
#endif
Expand Down Expand Up @@ -107,7 +108,7 @@ int irq_attach(int irq, xcpt_t isr, FAR void *arg)
* to the unexpected interrupt handler.
*/

flags = spin_lock_irqsave(NULL);
flags = spin_lock_irqsave(&g_irqlock);
if (isr == NULL)
{
/* Disable the interrupt if we can before detaching it. We might
Expand Down Expand Up @@ -141,7 +142,7 @@ int irq_attach(int irq, xcpt_t isr, FAR void *arg)
if (is_irqchain(ndx, isr))
{
ret = irqchain_attach(ndx, isr, arg);
spin_unlock_irqrestore(NULL, flags);
spin_unlock_irqrestore(&g_irqlock, flags);
return ret;
}
#endif
Expand All @@ -156,7 +157,7 @@ int irq_attach(int irq, xcpt_t isr, FAR void *arg)
g_irqvector[ndx].count = 0;
#endif

spin_unlock_irqrestore(NULL, flags);
spin_unlock_irqrestore(&g_irqlock, flags);
ret = OK;
}

Expand Down
Loading

0 comments on commit a85840a

Please sign in to comment.