diff --git a/src/db/initHooks.c b/src/db/initHooks.c index 0168ae2c0..9af727d65 100644 --- a/src/db/initHooks.c +++ b/src/db/initHooks.c @@ -1,19 +1,17 @@ /*************************************************************************\ -* Copyright (c) 2002 The University of Chicago, as Operator of Argonne +* Copyright (c) 2008 UChicago Argonne LLC, as Operator of Argonne * National Laboratory. * Copyright (c) 2002 The Regents of the University of California, as * Operator of Los Alamos National Laboratory. -* EPICS BASE Versions 3.13.7 -* and higher are distributed subject to a Software License Agreement found +* EPICS BASE is distributed subject to a Software License Agreement found * in file LICENSE that is included with this distribution. \*************************************************************************/ -/* share/src/db/initHooks.c*/ +/* src/db/initHooks.c */ /* * Authors: Benjamin Franksen (BESY) and Marty Kraimer - * Date: 06-01-91 + * Date: 06-01-91 * major Revision: 07JuL97 */ - #include #include @@ -21,55 +19,86 @@ #include "dbDefs.h" #include "ellLib.h" +#include "epicsThread.h" #define epicsExportSharedSymbols #include "initHooks.h" typedef struct initHookLink { - ELLNODE node; - initHookFunction func; + ELLNODE node; + initHookFunction func; } initHookLink; -static int functionListInited = FALSE; static ELLLIST functionList; -static void initFunctionList(void) +/* + * Lazy initialization functions + */ +static void initHookOnce(void *arg) { ellInit(&functionList); - functionListInited = TRUE; +} + +static void initHookInit(void) +{ + static epicsThreadOnceId onceFlag = EPICS_THREAD_ONCE_INIT; + epicsThreadOnce(&onceFlag, initHookOnce, NULL); } /* * To be called before iocInit reaches state desired. */ -int epicsShareAPI initHookRegister(initHookFunction func) +int initHookRegister(initHookFunction func) { - initHookLink *newHook; + initHookLink *newHook; - if(!functionListInited) initFunctionList(); - newHook = (initHookLink *)malloc(sizeof(initHookLink)); - if (newHook == NULL) - { - printf("Cannot malloc a new initHookLink\n"); - return -1; - } - newHook->func = func; - ellAdd(&functionList,&newHook->node); - return 0; + initHookInit(); + newHook = (initHookLink *)malloc(sizeof(initHookLink)); + if (newHook == NULL) { + printf("Cannot malloc a new initHookLink\n"); + return -1; + } + newHook->func = func; + ellAdd(&functionList, &newHook->node); + return 0; } /* * Called by iocInit at various points during initialization. * Do not call this function from any other function than iocInit. */ -void epicsShareAPI initHooks(initHookState state) +void initHooks(initHookState state) { - initHookLink *hook; + initHookLink *hook; - if(!functionListInited) initFunctionList(); - hook = (initHookLink *)ellFirst(&functionList); - while(hook != NULL) - { - hook->func(state); - hook = (initHookLink *)ellNext(&hook->node); - } + initHookInit(); + hook = (initHookLink *)ellFirst(&functionList); + while (hook != NULL) { + hook->func(state); + hook = (initHookLink *)ellNext(&hook->node); + } +} + +/* + * Call any time you want to print out a state name. + */ +const char *initHookName(initHookState state) +{ + const char *stateName[] = { + "initHookAtBeginning", + "initHookAfterCallbackInit", + "initHookAfterCaLinkInit", + "initHookAfterInitDrvSup", + "initHookAfterInitRecSup", + "initHookAfterInitDevSup", + "initHookAfterInitDatabase", + "initHookAfterFinishDevSup", + "initHookAfterScanInit", + "initHookAfterInitialProcess", + "initHookAfterInterruptAccept", + "initHookAtEnd" + }; + if (state < initHookAtBeginning || state > initHookAtEnd) { + return "Not an initHookState"; + } + return stateName[state]; } diff --git a/src/db/initHooks.h b/src/db/initHooks.h index 46e3224f7..a6325fa27 100644 --- a/src/db/initHooks.h +++ b/src/db/initHooks.h @@ -1,25 +1,24 @@ /*************************************************************************\ -* Copyright (c) 2002 The University of Chicago, as Operator of Argonne +* Copyright (c) 2008 UChicago Argonne LLC, as Operator of Argonne * National Laboratory. * Copyright (c) 2002 The Regents of the University of California, as * Operator of Los Alamos National Laboratory. -* EPICS BASE Versions 3.13.7 -* and higher are distributed subject to a Software License Agreement found +* EPICS BASE is distributed subject to a Software License Agreement found * in file LICENSE that is included with this distribution. \*************************************************************************/ -/* share/src/db/initHooks.h*/ +/* src/db/initHooks.h */ /* - * Authors: Benjamin Franksen (BESY) and Marty Kraimer - * Date: 06-01-91 + * Authors: Benjamin Franksen (BESY) and Marty Kraimer + * Date: 06-01-91 * major Revision: 07JuL97 */ - -#ifndef INCinitHooksh -#define INCinitHooksh 1 +#ifndef INC_initHooks_H +#define INC_initHooks_H #include "shareLib.h" +/* This enum must agree with the array of names defined in initHookName() */ typedef enum { initHookAtBeginning, initHookAfterCallbackInit, @@ -40,11 +39,11 @@ extern "C" { #endif typedef void (*initHookFunction)(initHookState state); -epicsShareFunc int epicsShareAPI initHookRegister(initHookFunction func); -epicsShareFunc void epicsShareAPI initHooks(initHookState state); - +epicsShareFunc int initHookRegister(initHookFunction func); +epicsShareFunc void initHooks(initHookState state); +epicsShareFunc const char *initHookName(initHookState state); #ifdef __cplusplus } #endif -#endif /*INCinitHooksh*/ +#endif /* INC_initHooks_H */