xref: /unit/src/nxt_lib.c (revision 0:a63ceefd6ab0)
1 
2 /*
3  * Copyright (C) Igor Sysoev
4  * Copyright (C) NGINX, Inc.
5  */
6 
7 #include <nxt_main.h>
8 
9 
10 nxt_uint_t    nxt_ncpu = 1;
11 nxt_uint_t    nxt_pagesize;
12 nxt_random_t  nxt_random_data;
13 
14 nxt_thread_declare_data(nxt_thread_t, nxt_thread_context);
15 
16 
17 #if (NXT_DEBUG && NXT_FREEBSD)
18 /*
19  * Fill memory with 0xA5 after malloc() and with 0x5A before free().
20  * malloc() options variable has to override the libc symbol, otherwise
21  * it has no effect.
22  */
23 #if __FreeBSD_version < 1000011
24 const char *_malloc_options = "J";
25 #else
26 const char *malloc_conf = "junk:true";
27 #endif
28 #endif
29 
30 
31 nxt_int_t
32 nxt_lib_start(const char *app, char **argv, char ***envp)
33 {
34     int           n;
35     nxt_int_t     flags;
36     nxt_bool_t    update;
37     nxt_thread_t  *thr;
38 
39     flags = nxt_stderr_start();
40 
41     nxt_log_start(app);
42 
43     nxt_pid = getpid();
44     nxt_ppid = getppid();
45 
46 #if (NXT_DEBUG)
47 
48     nxt_main_log.level = NXT_LOG_DEBUG;
49 
50 #if (NXT_LINUX)
51     /* Fill memory with 0xAA after malloc() and with 0x55 before free(). */
52     mallopt(M_PERTURB, 0x55);
53 #endif
54 
55 #if (NXT_MACOSX)
56     /* Fill memory with 0xAA after malloc() and with 0x55 before free(). */
57     setenv("MallocScribble", "1", 0);
58 #endif
59 
60 #endif /* NXT_DEBUG */
61 
62     /* Thread log is required for nxt_malloc() in nxt_strerror_start(). */
63 
64     nxt_thread_init_data(nxt_thread_context);
65     thr = nxt_thread();
66     thr->log = &nxt_main_log;
67 
68 #if (NXT_THREADS)
69     thr->handle = nxt_thread_handle();
70     thr->time.signal = -1;
71 #endif
72 
73     if (nxt_strerror_start() != NXT_OK) {
74         return NXT_ERROR;
75     }
76 
77     if (flags != -1) {
78         nxt_log_debug(thr->log, "stderr flags: 0x%04Xd", flags);
79     }
80 
81 #ifdef _SC_NPROCESSORS_ONLN
82     /* Linux, FreeBSD, Solaris, MacOSX. */
83     n = sysconf(_SC_NPROCESSORS_ONLN);
84 
85 #elif (NXT_HPUX)
86     n = mpctl(MPC_GETNUMSPUS, NULL, NULL);
87 
88 #endif
89 
90     nxt_log_debug(thr->log, "ncpu: %ui", n);
91 
92     if (n > 1) {
93         nxt_ncpu = n;
94     }
95 
96     nxt_thread_spin_init(nxt_ncpu, 0);
97 
98     nxt_random_init(&nxt_random_data);
99 
100     nxt_pagesize = getpagesize();
101 
102     nxt_log_debug(thr->log, "pagesize: %ui", nxt_pagesize);
103 
104     if (argv != NULL) {
105         update = (argv[0] == app);
106 
107         nxt_process_arguments(argv, envp);
108 
109         if (update) {
110             nxt_log_start(nxt_process_argv[0]);
111         }
112     }
113 
114     return NXT_OK;
115 }
116 
117 
118 void
119 nxt_lib_stop(void)
120 {
121     /* TODO: stop engines */
122 
123 #if (NXT_THREADS0)
124 
125     for ( ;; ) {
126         nxt_thread_pool_t  *tp;
127 
128         nxt_thread_spin_lock(&cycle->lock);
129 
130         tp = cycle->thread_pools;
131         cycle->thread_pools = (tp != NULL) ? tp->next : NULL;
132 
133         nxt_thread_spin_unlock(&cycle->lock);
134 
135         if (tp == NULL) {
136             break;
137         }
138 
139         nxt_thread_pool_destroy(tp);
140     }
141 
142 #else
143 
144     exit(0);
145     nxt_unreachable();
146 
147 #endif
148 }
149