1184610Salfred/* Licensed to the Apache Software Foundation (ASF) under one or more
2184610Salfred * contributor license agreements.  See the NOTICE file distributed with
3184610Salfred * this work for additional information regarding copyright ownership.
4184610Salfred * The ASF licenses this file to You under the Apache License, Version 2.0
5184610Salfred * (the "License"); you may not use this file except in compliance with
6184610Salfred * the License.  You may obtain a copy of the License at
7184610Salfred *
8184610Salfred *     http://www.apache.org/licenses/LICENSE-2.0
9184610Salfred *
10184610Salfred * Unless required by applicable law or agreed to in writing, software
11184610Salfred * distributed under the License is distributed on an "AS IS" BASIS,
12184610Salfred * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13184610Salfred * See the License for the specific language governing permissions and
14184610Salfred * limitations under the License.
15184610Salfred */
16184610Salfred
17184610Salfred#include "apr.h"
18184610Salfred#include "apr_general.h"
19184610Salfred#include "apr_pools.h"
20184610Salfred#include "apr_signal.h"
21184610Salfred#include "apr_atomic.h"
22184610Salfred
23184610Salfred#include "apr_arch_proc_mutex.h" /* for apr_proc_mutex_unix_setup_lock() */
24184610Salfred#include "apr_arch_internal_time.h"
25184610Salfred
26184610Salfred
27184610SalfredAPR_DECLARE(apr_status_t) apr_app_initialize(int *argc,
28184610Salfred                                             const char * const * *argv,
29184610Salfred                                             const char * const * *env)
30228484Shselasky{
31228484Shselasky    /* An absolute noop.  At present, only Win32 requires this stub, but it's
32228484Shselasky     * required in order to move command arguments passed through the service
33184610Salfred     * control manager into the process, and it's required to fix the char*
34184610Salfred     * data passed in from win32 unicode into utf-8, win32's apr internal fmt.
35184610Salfred     */
36184610Salfred    return apr_initialize();
37184610Salfred}
38184610Salfred
39185948Sthompsastatic int initialized = 0;
40185948Sthompsa
41185948SthompsaAPR_DECLARE(apr_status_t) apr_initialize(void)
42185948Sthompsa{
43185948Sthompsa    apr_pool_t *pool;
44185948Sthompsa    apr_status_t status;
45185948Sthompsa
46185948Sthompsa    if (initialized++) {
47185948Sthompsa        return APR_SUCCESS;
48185948Sthompsa    }
49185948Sthompsa
50185948Sthompsa#if !defined(BEOS) && !defined(OS2)
51185948Sthompsa    apr_proc_mutex_unix_setup_lock();
52185948Sthompsa    apr_unix_setup_time();
53193640Sariff#endif
54193640Sariff
55185948Sthompsa    if ((status = apr_pool_initialize()) != APR_SUCCESS)
56185948Sthompsa        return status;
57280322Shselasky
58280322Shselasky    if (apr_pool_create(&pool, NULL) != APR_SUCCESS) {
59185948Sthompsa        return APR_ENOPOOL;
60185948Sthompsa    }
61185948Sthompsa
62185948Sthompsa    apr_pool_tag(pool, "apr_initialize");
63185948Sthompsa
64185948Sthompsa    /* apr_atomic_init() used to be called from here aswell.
65184610Salfred     * Pools rely on mutexes though, which can be backed by
66184610Salfred     * atomics.  Due to this circular dependency
67184610Salfred     * apr_pool_initialize() is taking care of calling
68184610Salfred     * apr_atomic_init() at the correct time.
69228484Shselasky     */
70228484Shselasky
71    apr_signal_init(pool);
72
73    return APR_SUCCESS;
74}
75
76APR_DECLARE_NONSTD(void) apr_terminate(void)
77{
78    initialized--;
79    if (initialized) {
80        return;
81    }
82    apr_pool_terminate();
83
84}
85
86APR_DECLARE(void) apr_terminate2(void)
87{
88    apr_terminate();
89}
90