This is Unofficial EPICS BASE Doxygen Site
initHooks.c
Go to the documentation of this file.
1 /*************************************************************************\
2 * Copyright (c) 2009 UChicago Argonne LLC, as Operator of Argonne
3 * National Laboratory.
4 * Copyright (c) 2002 The Regents of the University of California, as
5 * Operator of Los Alamos National Laboratory.
6 * EPICS BASE is distributed subject to a Software License Agreement found
7 * in file LICENSE that is included with this distribution.
8 \*************************************************************************/
9 /*
10  * Authors: Benjamin Franksen (BESY) and Marty Kraimer
11  * Date: 06-01-91
12  * major Revision: 07JuL97
13  */
14 
15 #include <stdlib.h>
16 #include <stddef.h>
17 #include <stdio.h>
18 
19 #include "dbDefs.h"
20 #include "ellLib.h"
21 #include "epicsMutex.h"
22 #include "epicsThread.h"
23 
24 #include "initHooks.h"
25 
26 typedef struct initHookLink {
29 } initHookLink;
30 
31 static ELLLIST functionList = ELLLIST_INIT;
32 static epicsMutexId listLock;
33 
34 /*
35  * Lazy initialization functions
36  */
37 static void initHookOnce(void *arg)
38 {
39  listLock = epicsMutexMustCreate();
40 }
41 
42 static void initHookInit(void)
43 {
44  static epicsThreadOnceId onceFlag = EPICS_THREAD_ONCE_INIT;
45  epicsThreadOnce(&onceFlag, initHookOnce, NULL);
46 }
47 
48 /*
49  * To be called before iocInit reaches state desired.
50  */
52 {
53  initHookLink *newHook;
54 
55  if (!func) return 0;
56 
57  initHookInit();
58 
59  newHook = (initHookLink *)malloc(sizeof(initHookLink));
60  if (!newHook) {
61  printf("Cannot malloc a new initHookLink\n");
62  return -1;
63  }
64  newHook->func = func;
65 
66  epicsMutexMustLock(listLock);
67  ellAdd(&functionList, &newHook->node);
68  epicsMutexUnlock(listLock);
69  return 0;
70 }
71 
72 /*
73  * Called by iocInit at various points during initialization.
74  * This function must only be called by iocInit and relatives.
75  */
77 {
78  initHookLink *hook;
79 
80  initHookInit();
81 
82  epicsMutexMustLock(listLock);
83  hook = (initHookLink *)ellFirst(&functionList);
84  epicsMutexUnlock(listLock);
85 
86  while (hook != NULL) {
87  hook->func(state);
88 
89  epicsMutexMustLock(listLock);
90  hook = (initHookLink *)ellNext(&hook->node);
91  epicsMutexUnlock(listLock);
92  }
93 }
94 
95 void initHookFree(void)
96 {
97  initHookInit();
98  epicsMutexMustLock(listLock);
99  ellFree(&functionList);
100  epicsMutexUnlock(listLock);
101 }
102 
103 /*
104  * Call any time you want to print out a state name.
105  */
106 const char *initHookName(int state)
107 {
108  const char *stateName[] = {
109  "initHookAtIocBuild",
110  "initHookAtBeginning",
111  "initHookAfterCallbackInit",
112  "initHookAfterCaLinkInit",
113  "initHookAfterInitDrvSup",
114  "initHookAfterInitRecSup",
115  "initHookAfterInitDevSup",
116  "initHookAfterInitDatabase",
117  "initHookAfterFinishDevSup",
118  "initHookAfterScanInit",
119  "initHookAfterInitialProcess",
120  "initHookAfterCaServerInit",
121  "initHookAfterIocBuilt",
122  "initHookAtIocRun",
123  "initHookAfterDatabaseRunning",
124  "initHookAfterCaServerRunning",
125  "initHookAfterIocRunning",
126  "initHookAtIocPause",
127  "initHookAfterCaServerPaused",
128  "initHookAfterDatabasePaused",
129  "initHookAfterIocPaused",
130  "initHookAfterInterruptAccept",
131  "initHookAtEnd"
132  };
133  if (state < 0 || state >= NELEMENTS(stateName)) {
134  return "Not an initHookState";
135  }
136  return stateName[state];
137 }
void initHookAnnounce(initHookState state)
Definition: initHooks.c:76
#define printf
Definition: epicsStdio.h:41
#define epicsMutexMustCreate()
Create an epicsMutex semaphore for use from C code.
Definition: epicsMutex.h:179
#define NULL
Definition: catime.c:38
#define ELLLIST_INIT
Value of an empty list.
Definition: ellLib.h:63
void epicsStdCall epicsMutexUnlock(epicsMutexId pmutexNode)
Release the semaphore.
Definition: epicsMutex.cpp:140
Miscellaneous macro definitions.
void initHookFree(void)
Definition: initHooks.c:95
A doubly-linked list library.
void ellAdd(ELLLIST *pList, ELLNODE *pNode)
Adds a node to the end of a list.
Definition: ellLib.c:24
#define ellNext(PNODE)
Find the next node in list.
Definition: ellLib.h:99
#define ellFree(PLIST)
Free up the list.
Definition: ellLib.h:108
#define EPICS_THREAD_ONCE_INIT
Definition: epicsThread.h:109
APIs for the epicsMutex mutual exclusion semaphore.
LIBCOM_API void epicsStdCall epicsThreadOnce(epicsThreadOnceId *id, EPICSTHREADFUNC, void *arg)
void(* initHookFunction)(initHookState state)
Definition: initHooks.h:66
#define NELEMENTS(A)
Definition: aToIPAddr.c:21
List node type.
Definition: ellLib.h:45
const char * initHookName(int state)
Definition: initHooks.c:106
int initHookRegister(initHookFunction func)
Definition: initHooks.c:51
initHookState
Definition: initHooks.h:25
List header type.
Definition: ellLib.h:56
C++ and C descriptions for a thread.
#define epicsMutexMustLock(ID)
Claim a semaphore (see epicsMutexLock()).
Definition: epicsMutex.h:214
struct initHookLink initHookLink
#define ellFirst(PLIST)
Find the first node in list.
Definition: ellLib.h:89