Merge remote-tracking branch 'remotes/origin/cairocanvas' into windows
[ardour.git] / libs / pbd / localtime_r.cc
1 #ifdef WAF_BUILD
2 #include "libpbd-config.h"
3 #endif
4
5 #ifndef HAVE_LOCALTIME_R
6 #include <time.h>
7 #include <string.h>
8
9 #include "pbd/pthread_utils.h"
10 #include "pbd/localtime_r.h"
11
12 #ifdef localtime_r
13 #undef localtime_r
14 #endif
15
16 struct tm *
17 localtime_r(const time_t *const timep, struct tm *p_tm)
18 {
19         static pthread_mutex_t time_mutex;
20         static int time_mutex_inited = 0;
21         struct tm *tmp;
22
23         if (!time_mutex_inited)
24         {
25                 time_mutex_inited = 1;
26                 pthread_mutex_init(&time_mutex, NULL);
27         }
28
29         pthread_mutex_lock(&time_mutex);
30         tmp = localtime(timep);
31         if (tmp)
32         {
33                 memcpy(p_tm, tmp, sizeof(struct tm));
34                 tmp = p_tm;
35         }
36         pthread_mutex_unlock(&time_mutex);
37
38         return tmp;
39 }
40
41 #endif