Changeset 2784 for branches


Ignore:
Timestamp:
Aug 27, 2006, 4:07:16 PM (19 years ago)
Author:
bird
Message:

Added usage counting to _CRT_init and _CRT_term. Fixing #114.

Location:
branches/libc-0.6/src/emx/src/lib/startup
Files:
1 deleted
1 edited

Legend:

Unmodified
Added
Removed
  • branches/libc-0.6/src/emx/src/lib/startup/startup.c

    r2021 r2784  
    1 /* startup.c (emx+gcc) -- Copyright (c) 1990-1998 by Eberhard Mattes */
     1/** $Id: $ */
     2/** @file
     3 *
     4 * kLIBC - CRT init and termination code.
     5 *
     6 * Copyright (c) 1990-1998 by Eberhard Mattes
     7 * Copyright (c) 2004-2006 knut st. osmundsen <bird@innotek.de>
     8 *
     9 *
     10 * This file is part of kLIBC.
     11 *
     12 * kLIBC is free software; you can redistribute it and/or modify
     13 * it under the terms of the GNU Lesser General Public License as published
     14 * by the Free Software Foundation; either version 2 of the License, or
     15 * (at your option) any later version.
     16 *
     17 * kLIBC is distributed in the hope that it will be useful,
     18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
     19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     20 * GNU Lesser General Public License for more details.
     21 *
     22 * You should have received a copy of the GNU Lesser General Public License
     23 * along with kLIBC; if not, write to the Free Software
     24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
     25 *
     26 */
    227
    3 #include <stdio.h>
    4 #include <stdlib.h>
    5 #include <fcntl.h>
     28
     29
     30/*******************************************************************************
     31*   Header Files                                                               *
     32*******************************************************************************/
    633#include <errno.h>
    7 #include <sys/ioctl.h>
    8 #include <emx/io.h>
    9 #include <emx/syscalls.h>
     34#include <sys/builtin.h>
    1035#include <emx/startup.h>
    1136#define __LIBC_LOG_GROUP    __LIBC_LOG_GRP_INITTERM
     
    1338
    1439
    15 /* Initialize the C run-time library.  This function is called from
    16    crt0.s. */
    17 int _CRT_init (void)
     40/*******************************************************************************
     41*   Global Variables                                                           *
     42*******************************************************************************/
     43/** The balance between _CRT_init and _CRT_term calls. */
     44static volatile int32_t gcCRTReferences = 0;
     45
     46
     47/**
     48 * Initializes the C runtime library.
     49 *
     50 * A _CRT_init() call should be matched by a _CRT_term() call as we are
     51 * keeping a count of the number of C runtime users.
     52 *
     53 * This function is normally called from _DLL_InitTerm and crt0.s.
     54 *
     55 * @returns 0 on success. -1 on failure.
     56 */
     57int _CRT_init(void)
    1858{
    19   LIBCLOG_ENTER("\n");
    20   static int startup_flag;
     59    LIBCLOG_ENTER("\n");
    2160
    22   /* Protect against multiple calls (for instance, by _CRT_init() in a
    23      _DLL_InitTerm() and by the application program's startup code). */
    24   if (startup_flag)
     61    /*
     62     * On initialize once.
     63     */
     64    int32_t cRefs = __atomic_increment_s32(&gcCRTReferences);
     65    if (cRefs != 1)
     66        LIBCLOG_RETURN_MSG(0, "ret 0 (cRefs=%d)\n", cRefs);
     67
     68    /*
     69     * Call the weak initializers.
     70     */
     71    __ctordtorInit1(&__crtinit1__);
     72
     73    /*
     74     * ANSI X3.159-1989, 4.1.3: "The value of errno is zero at program startup..."
     75     *
     76     * The above code usually sets errno to EBADF, therefore we reset errno
     77     * to zero before calling main().
     78     */
     79    errno = 0;
    2580    LIBCLOG_RETURN_INT(0);
    26   startup_flag = 1;
    27 
    28   /* Initialize streams etc. if required. */
    29   __ctordtorInit1 (&__crtinit1__);
    30 
    31   /* ANSI X3.159-1989, 4.1.3: "The value of errno is zero at program
    32      startup..."
    33 
    34      The above code usually sets errno to EBADF, therefore we reset
    35      errno to zero before calling main(). */
    36   errno = 0;
    37 
    38   LIBCLOG_RETURN_INT(0);
    3981}
    4082
     83
     84/**
     85 * Terminates the C runtime library.
     86 *
     87 * A _CRT_term() call must be paired with a _CRT_init() call as we're
     88 * counting the number of C runtime users.
     89 *
     90 * This function is normally called from _DLL_InitTerm and crt0.s.
     91 */
     92void _CRT_term(void)
     93{
     94    LIBCLOG_ENTER("\n");
     95    int32_t cRefs = __atomic_decrement_s32(&gcCRTReferences);
     96    if (cRefs == 0)
     97    {
     98        /*
     99         * Call the weak terminators.
     100         */
     101        __ctordtorTerm1(&__crtexit1__);
     102    }
     103    else if (cRefs < 0)
     104        LIBCLOG_ERROR_RETURN_MSG_VOID("cRefs=%d\n", cRefs);
     105    LIBCLOG_RETURN_VOID();
     106}
    41107
    42108
     
    118184 * @todo update this with fork and performance changes!
    119185 */
     186
Note: See TracChangeset for help on using the changeset viewer.