@@ -53,6 +53,7 @@ static struct timespec g_clock_wall_time;
5353static uint64_t g_clock_last_counter ;
5454static uint64_t g_clock_mask ;
5555static long g_clock_adjust ;
56+ static spinlock_t g_clock_lock = SP_UNLOCKED ;
5657
5758/****************************************************************************
5859 * Private Functions
@@ -72,7 +73,7 @@ static int clock_get_current_time(FAR struct timespec *ts,
7273 time_t sec ;
7374 int ret ;
7475
75- flags = enter_critical_section ( );
76+ flags = spin_lock_irqsave ( & g_clock_lock );
7677
7778 ret = up_timer_gettick (& counter );
7879 if (ret < 0 )
@@ -96,7 +97,7 @@ static int clock_get_current_time(FAR struct timespec *ts,
9697 ts -> tv_sec = base -> tv_sec + sec ;
9798
9899errout_in_critical_section :
99- leave_critical_section ( flags );
100+ spin_unlock_irqrestore ( & g_clock_lock , flags );
100101 return ret ;
101102}
102103
@@ -123,7 +124,7 @@ int clock_timekeeping_set_wall_time(FAR const struct timespec *ts)
123124 uint64_t counter ;
124125 int ret ;
125126
126- flags = enter_critical_section ( );
127+ flags = spin_lock_irqsave ( & g_clock_lock );
127128
128129 ret = up_timer_gettick (& counter );
129130 if (ret < 0 )
@@ -137,7 +138,7 @@ int clock_timekeeping_set_wall_time(FAR const struct timespec *ts)
137138 g_clock_last_counter = counter ;
138139
139140errout_in_critical_section :
140- leave_critical_section ( flags );
141+ spin_unlock_irqrestore ( & g_clock_lock , flags );
141142 return ret ;
142143}
143144
@@ -188,7 +189,7 @@ int adjtime(FAR const struct timeval *delta, FAR struct timeval *olddelta)
188189 return -1 ;
189190 }
190191
191- flags = enter_critical_section ( );
192+ flags = spin_lock_irqsave ( & g_clock_lock );
192193
193194 adjust_usec = delta -> tv_sec * USEC_PER_SEC + delta -> tv_usec ;
194195
@@ -199,7 +200,7 @@ int adjtime(FAR const struct timeval *delta, FAR struct timeval *olddelta)
199200
200201 g_clock_adjust = adjust_usec ;
201202
202- leave_critical_section ( flags );
203+ spin_unlock_irqrestore ( & g_clock_lock , flags );
203204
204205 return OK ;
205206}
@@ -217,7 +218,7 @@ void clock_update_wall_time(void)
217218 time_t sec ;
218219 int ret ;
219220
220- flags = enter_critical_section ( );
221+ flags = spin_lock_irqsave ( & g_clock_lock );
221222
222223 ret = up_timer_gettick (& counter );
223224 if (ret < 0 )
@@ -271,7 +272,7 @@ void clock_update_wall_time(void)
271272 g_clock_last_counter = counter ;
272273
273274errout_in_critical_section :
274- leave_critical_section ( flags );
275+ spin_unlock_irqrestore ( & g_clock_lock , flags );
275276}
276277
277278/****************************************************************************
@@ -280,6 +281,9 @@ void clock_update_wall_time(void)
280281
281282void clock_inittimekeeping (FAR const struct timespec * tp )
282283{
284+ irqstate_t flags ;
285+
286+ flags = spin_lock_irqsave (& g_clock_lock );
283287 up_timer_getmask (& g_clock_mask );
284288
285289 if (tp )
@@ -292,6 +296,7 @@ void clock_inittimekeeping(FAR const struct timespec *tp)
292296 }
293297
294298 up_timer_gettick (& g_clock_last_counter );
299+ spin_unlock_irqrestore (& g_clock_lock , flags );
295300}
296301
297302#endif /* CONFIG_CLOCK_TIMEKEEPING */
0 commit comments