amiga-tz/library/lib_user.c

230 lines
8.0 KiB
C

/*
* Copyright (c) 2015 Carsten Larsen
* Copyright (c) 2002-2006 by Olaf Barthel <olsen (at) sourcery.han.de>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of Olaf Barthel nor the names of contributors
* may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/
#include "time_header.h"
#include "lib.h"
struct ExecBase* SysBase;
struct Library* DOSBase;
struct Library* UtilityBase;
struct Library* MathIeeeDoubBasBase;
struct Library* LocaleBase;
/* The following function is called as part of the library initialization,
right after the library is loaded. This function should perform only
initialization operations which can be accomplished quickly and without
delay because only one library initialization function at a time can
be run by the operating system. This means that until this function has
has returned, no other program or operating system component will be able
to open a library.
By the time this function is invoked the library base has already been
initialized. It has to return TRUE for success and FALSE otherwise. */
bool UserLibInit(
struct Library* sysbase,
struct TimezoneBase* sb,
struct UserData* ud)
{
bool result = true;
SysBase = (struct ExecBase*)sysbase;
/* We reset the use count of the user data to 0, so that we can
keep track of how many users are actually using it. Note that
the contents of the UserData structure are completely random
when this function is called. */
ud->ud_UseCount = 0;
/* Also remember the SysBase pointer. */
ud->ud_SysBase = sysbase;
#if defined(__amigaos4__)
{
ud->ud_IExec = (struct ExecIFace *)((struct ExecBase *)sysbase)->MainInterface;
}
#endif /* __amigaos4__ */
/* The library base pointer may come in handy if library
functions are to call other library functions. These
calls should go through the normal library calling
mechanism rather than invoke these functions directly. */
ud->ud_ZoneinfoBase = sb;
lclptr = NULL;
gmtptr = NULL;
DOSBase = NULL;
MathIeeeDoubBasBase = NULL;
UtilityBase = NULL;
LocaleBase = NULL;
TimeSemaphore = NULL;
DOSBase = OpenLibrary(DOSLIB_NAME, LIB_VERSION);
if (!DOSBase) {
result = false;
}
MathIeeeDoubBasBase = OpenLibrary (MATHLIB_NAME, LIB_VERSION);
if (!MathIeeeDoubBasBase) {
result = false;
}
UtilityBase = OpenLibrary(UTILLIB_NAME, LIB_VERSION);
if (!UtilityBase) {
result = false;
}
LocaleBase = OpenLibrary(LOCALELIB_NAME, LIB_VERSION);
if (!LocaleBase) {
result = false;
}
TimeSemaphore = (struct SignalSemaphore*)AllocMem(
sizeof(struct SignalSemaphore),
MEMF_ANY|MEMF_PUBLIC|MEMF_CLEAR);
InitSemaphore(TimeSemaphore);
lcl_is_set = 0;
timezone = 0;
altzone = 0;
daylight = 0;
zonechg = 0;
errno = 0;
return result;
}
/* The following function is called whenever the library is opened. Since
by the time is called the library may have had another opener, it cannot
rely upon the sb->sb_Library.lib_OpenCnt value to be current, it must
find other means to keep track of whether it was invoked for the first
time or not. In this function initializations such as resource allocations
are performed. Unlike the UserLibInit() function, these initializations
can take their time to complete since these will execute on the
schedule of the caller. Take care: some callers may be plain Tasks which
cannot call all dos.library functions! This function has to return TRUE for
success and FALSE otherwise. */
bool UserLibOpen(struct UserData * ud)
{
/* For the thread safe build, invoke the clib2 shared library
initialization code. Note that this is not strictly
necessary. In fact, you should not need this functionality
if you stick to use Amiga operating system routines only
and stay away from using 'C' runtime library functions
that are not reentrant, such as malloc() or fprintf().
Use this feature only if you are porting code to the Amiga
which cannot be easily converted to follow the AmigaOS
API definitions only. */
#if defined(__THREAD_SAFE)
{
/* Note that the clib2 library initialization is
called exactly once, when the first client
opens this library. */
if(ud->ud_UseCount == 0 && !__lib_init(ud->ud_SysBase))
goto out;
}
#endif /* __THREAD_SAFE */
/* Remember that one more customer is using this data structure. */
ud->ud_UseCount++;
tzset();
return true;
}
/* The following function is called whenever the library is closed. Its
purpose is to release any resources allocated by the UserLibOpen()
function, such as when the last caller has closed the library. */
void UserLibClose(struct UserData * ud)
{
/* Remember that one less customer is using this data structure. */
ud->ud_UseCount--;
/* For the thread safe build, invoke the clib2 shared library
cleanup code. Note that this is not strictly
necessary. In fact, you should not need this functionality
if you stick to use Amiga operating system routines only
and stay away from using 'C' runtime library functions
that are not reentrant, such as malloc() or fprintf().
Use this feature only if you are porting code to the Amiga
which cannot be easily converted to follow the AmigaOS
API definitions only. */
#if defined(__THREAD_SAFE)
{
/* Note that the clib2 library cleanup code is
called exactly once, when the last client
closes this library. */
if(ud->ud_UseCount == 0)
__lib_exit();
}
#endif /* __THREAD_SAFE */
}
/* The following function is called shortly before the library is to be
unloaded from memory. Its purpose is to release any resources
allocated by the UserLibInit() function. Note that this function must
not break the Forbid() condition, i.e. it must not call Wait() directly
or indirectly. */
void UserLibExpunge(struct UserData * ud)
{
if(lclptr) {
FreeMem(lclptr, sizeof(struct state));
}
if(gmtptr) {
FreeMem(gmtptr, sizeof(struct state));
}
if(TimeSemaphore) {
FreeMem(TimeSemaphore, sizeof(struct SignalSemaphore));
}
if (LocaleBase) {
CloseLibrary(LocaleBase);
}
if (UtilityBase) {
CloseLibrary(UtilityBase);
}
if (MathIeeeDoubBasBase) {
CloseLibrary(MathIeeeDoubBasBase);
}
if (DOSBase) {
CloseLibrary(DOSBase);
}
}