xref: /unit/src/nxt_thread_time.h (revision 2084:7d479274f334)
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 NXT_EXPORT void nxt_thread_time_update(nxt_thread_t *thr);
65 void nxt_thread_time_free(nxt_thread_t *thr);
66 NXT_EXPORT nxt_time_t nxt_thread_time(nxt_thread_t *thr);
67 NXT_EXPORT nxt_realtime_t *nxt_thread_realtime(nxt_thread_t *thr);
68 NXT_EXPORT u_char *nxt_thread_time_string(nxt_thread_t *thr,
69     nxt_time_string_t *ts, u_char *buf);
70 void nxt_time_thread_start(nxt_msec_t interval);
71 
72 
73 #define nxt_thread_monotonic_time(thr)                                        \
74     (thr)->time.now.monotonic
75 
76 
77 #if (NXT_DEBUG)
78 
79 #define nxt_thread_time_debug_update(thr)                                     \
80     nxt_thread_time_update(thr)
81 
82 #else
83 
84 #define nxt_thread_time_debug_update(thr)
85 
86 #endif
87 
88 
89 NXT_EXPORT void nxt_gmtime(nxt_time_t s, struct tm *tm);
90 
91 
92 #endif /* _NXT_THREAD_TIME_H_INCLUDED_ */
93