xref: /unit/src/nxt_thread_time.h (revision 0:a63ceefd6ab0)
1 
2 /*
3  * Copyright (C) Igor Sysoev
4  * Copyright (C) NGINX, Inc.
5  */
6 
7 #ifndef _NXT_THREAD_TIME_H_INCLUDED_
8 #define _NXT_THREAD_TIME_H_INCLUDED_
9 
10 
11 #define NXT_THREAD_TIME_LOCAL  0
12 #define NXT_THREAD_TIME_GMT    1
13 
14 #define NXT_THREAD_TIME_SEC    0
15 #define NXT_THREAD_TIME_MSEC   1
16 
17 
18 typedef struct {
19     nxt_atomic_t               slot;
20     u_char                     *(*handler)(u_char *buf, nxt_realtime_t *now,
21                                    struct tm *tm, size_t size,
22                                    const char *format);
23     const char                 *format;
24     size_t                     size;
25 
26     uint8_t                    timezone;  /* 1 bit */
27     uint8_t                    msec;      /* 1 bit */
28 } nxt_time_string_t;
29 
30 
31 typedef struct {
32     nxt_time_t                 last;
33 #if (NXT_DEBUG)
34     nxt_msec_t                 last_msec;
35 #endif
36     nxt_str_t                  string;
37 } nxt_time_string_cache_t;
38 
39 
40 typedef struct {
41     nxt_monotonic_time_t       now;
42 
43     nxt_time_t                 last_gmtime;
44     nxt_time_t                 last_localtime;
45     struct tm                  gmtime;
46     struct tm                  localtime;
47 
48     uint32_t                   no_cache;  /* 1 bit */
49 
50     /*
51      * The flag indicating a signal state of a thread.
52      * It is used to handle local time of the thread:
53      *   -1 means that the thread never runs in a signal context;
54      *    0 means that the thread may run in a signal context but not now;
55      *   >0 means that the thread runs in a signal context right now.
56      */
57     nxt_atomic_int_t           signal;
58 
59     nxt_atomic_uint_t          nstrings;
60     nxt_time_string_cache_t    *strings;
61 } nxt_thread_time_t;
62 
63 
64 #if (NXT_THREADS)
65 void nxt_time_thread_start(nxt_msec_t interval);
66 #endif
67 
68 
69 NXT_EXPORT void nxt_thread_time_update(nxt_thread_t *thr);
70 void nxt_thread_time_free(nxt_thread_t *thr);
71 NXT_EXPORT nxt_time_t nxt_thread_time(nxt_thread_t *thr);
72 NXT_EXPORT nxt_realtime_t *nxt_thread_realtime(nxt_thread_t *thr);
73 NXT_EXPORT u_char *nxt_thread_time_string(nxt_thread_t *thr,
74     nxt_time_string_t *ts, u_char *buf);
75 
76 
77 #define                                                                       \
78 nxt_thread_monotonic_time(thr)                                                \
79     (thr)->time.now.monotonic
80 
81 
82 #if (NXT_DEBUG)
83 
84 #define                                                                       \
85 nxt_thread_time_debug_update(thr)                                             \
86     nxt_thread_time_update(thr)
87 
88 #else
89 
90 #define                                                                       \
91 nxt_thread_time_debug_update(thr)
92 
93 #endif
94 
95 
96 NXT_EXPORT void nxt_gmtime(nxt_time_t s, struct tm *tm);
97 
98 
99 #endif /* _NXT_THREAD_TIME_H_INCLUDED_ */
100