initHookRegister() make idempotent and MustSucceed

This commit is contained in:
Michael Davidsaver
2025-02-05 09:26:46 -08:00
committed by Andrew Johnson
parent ee1a49045a
commit 13d6ca598c
2 changed files with 18 additions and 6 deletions

View File

@ -21,6 +21,7 @@
#include "ellLib.h"
#include "epicsMutex.h"
#include "epicsThread.h"
#include "cantProceed.h"
#include "initHooks.h"
@ -52,19 +53,26 @@ static void initHookInit(void)
int initHookRegister(initHookFunction func)
{
initHookLink *newHook;
ELLNODE *cur;
if (!func) return 0;
initHookInit();
newHook = (initHookLink *)malloc(sizeof(initHookLink));
if (!newHook) {
printf("Cannot malloc a new initHookLink\n");
return -1;
epicsMutexMustLock(listLock);
for(cur = ellFirst(&functionList); cur; cur = ellNext(cur)) {
const initHookLink *fn = CONTAINER(cur, initHookLink, node);
if(fn->func==func) {
/* silently ignore duplicate */
epicsMutexUnlock(listLock);
return 0;
}
}
newHook = (initHookLink *)mallocMustSucceed(sizeof(initHookLink), "initHookRegister");
newHook->func = func;
epicsMutexMustLock(listLock);
ellAdd(&functionList, &newHook->node);
epicsMutexUnlock(listLock);
return 0;

View File

@ -163,7 +163,11 @@ typedef void (*initHookFunction)(initHookState state);
*
* Registers \p func for initHook notifications
* \param func Pointer to application's notification function.
* \return 0 if Ok, -1 on error (memory allocation failure).
* \return Always zero. (before UNRELEASED could return -1 on allocation failure)
*
* \since UNRELEASED initHookRegister is idempotent.
* Previously, repeated registrations would result
* in duplicate calls to the hook function.
*/
LIBCOM_API int initHookRegister(initHookFunction func);