[En-Nut-Discussion] Thread-safe version of asctime

Ole Reinhardt ole.reinhardt at embedded-it.de
Wed May 7 15:46:06 CEST 2014


Hi Harald,

Am 07.05.2014 12:50, schrieb Harald Kipp:
> the recently added asctime.c contains a few comments
> 
> /*
>   Note: This function is *not* thread safe, because it uses a static
> variable
>   to store the calculated values. To be safe, you must surround the call
> to asctime
>   _and_ the usage of the returned pointer with NutEnterCritical() and
> NutExitCritical()!
>   Provided for compatibility to std c lib.
> */
> 
> which I think are misleading. I cannot see, at which place we have to
> expect a context switch.

The problem is not asctime() itself, but the fact that it returns a
static pointer to the converted time value. So it could change
unexpected if there is a thread switch after valling asctime() and a
second thread is calling asctime as well.

> The problem with static buffers is still there, e.g.
> 
>   printf("%s %s", asctime(t1), asctime(t2));
> 
> will print out t2 twice and discard t1. IMHO, NutEnterCritical()
> wouldn't solve this.

Exactly.

So, yes, perhaps the hint is missleading. It was taken over from the
asctime man-page and does not reflect the cooperative thread switching
of Nut/OS correctly.

Shall I change the comment?

> Furthermore I'm not sure about
> 
> static const char wday_name[][3] = {
>     "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
> };
> 
> AFAIK, sizeof("Sun") is 4, not 3.

Totally true! Thanks! Shall I change it? Or have you just checked in a fix?

Btw: Have you seen my question about the code in nut/mod?

Best regards,

Ole

-- 
kernel concepts GmbH            Tel: +49-271-771091-14
Sieghuetter Hauptweg 48         Mob: +49-177-7420433
D-57072 Siegen
http://www.embedded-it.de
http://www.kernelconcepts.de


More information about the En-Nut-Discussion mailing list