Path: utzoo!utgpu!water!watmath!clyde!att!osu-cis!tut.cis.ohio-state.edu!rutgers!apple!well!aleks
From: aleks@well.UUCP (Brian J. Witt)
Newsgroups: comp.sys.amiga.tech
Subject: Re: Negative Open Counts (was Re: IEEE libraries)
Summary: Try re-enterant code practices
Keywords: open counts, re-enterency
Message-ID: <7193@well.UUCP>
Date: 24 Sep 88 19:20:29 GMT
References: <1356@percival.UUCP>  <1596@sbcs.sunysb.edu> <2643@sugar.uu.net> <1370@percival.UUCP> 
Reply-To: aleks@well.UUCP (Brian J. Witt)
Distribution: na
Organization: Spam Control Central
Lines: 56

[The line eater is dead-- the line eater is dead!!!]

It seems that if you're going to open the math library and your runtime
_may_ also open the math library, that you should keep track of your open
seperate from the runtime library.  Carloyn Schneppner (I hope I spelled
that correctly) has outlined how to do this.  Its like this, for sure!
This uses the re-enterency technique.  You may have many openers in
your process all sharing one global variable.  The routine that first
opened the library should be the routine that is responsible for closing
the library.  And once closed, it should modify that single global var
sothat should any one else need it later, they can deal with that 
situation.

     main ()
     {
           struct Library  *myMathBase = NULL;

           if (runtimeMathBase == NULL) {
                 if ((myMathBase=OpenLibrary(..)) == NULL)
                     error ("!");
                 runtimeMathBase = myMathBase;
           }

           /*  Insert useful code ..  */

           /* Cleanup and go home: */
           if (myMathBase != NULL)
           {
                 CloseLibrary(runtimeMathBase);
                 runtimeMathBase = NULL;    /* WE closed it */
           }
       } /* main */

This should work out well, no matter who needs the library.

1)  The startupcode has already opened the library.  Great, you don't
    close it.  The runtime rundown code will close its own library.

2)  The startup has not opened the library.  Ok, you do it for the
    system and assign it to the system library base.  Since you
    did the open, at cleanup time you close the library and set the
    varaible to NULL.  The runtime rundown code sees the var NULL and
    should ignore it.

I needed this solution when using IconBase under Manx.  It seems its
already declared in wb_startup().  In my program it's delcared 'extern'
and since I didn't really trust anyone to tell me if it was opened or 
not, I did it this way.

Hope it helps!
brian

---------------------------------------------------------------------
    "Some days, doctor, I wake up and I don't even know who I am.."
     -- confessions of an abstraact data type
---------------------------------------------------------------------