2 * Copyright (C) 2004-2007, 2009 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1999-2001 Internet Software Consortium.
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 * PERFORMANCE OF THIS SOFTWARE.
18 /* $Id: app.h,v 1.11 2009/09/02 23:48:03 tbox Exp $ */
28 * \brief ISC Application Support
30 * Dealing with program termination can be difficult, especially in a
31 * multithreaded program. The routines in this module help coordinate
32 * the shutdown process. They are used as follows by the initial (main)
33 * thread of the application:
35 *\li isc_app_start(); Call very early in main(), before
36 * any other threads have been created.
38 *\li isc_app_run(); This will post any on-run events,
39 * and then block until application
40 * shutdown is requested. A shutdown
41 * request is made by calling
42 * isc_app_shutdown(), or by sending
43 * SIGINT or SIGTERM to the process.
44 * After isc_app_run() returns, the
45 * application should shutdown itself.
47 *\li isc_app_finish(); Call very late in main().
49 * Applications that want to use SIGHUP/isc_app_reload() to trigger reloading
50 * should check the result of isc_app_run() and call the reload routine if
51 * the result is ISC_R_RELOAD. They should then call isc_app_run() again
52 * to resume waiting for reload or termination.
54 * Use of this module is not required. In particular, isc_app_start() is
55 * NOT an ISC library initialization routine.
57 * This module also supports per-thread 'application contexts'. With this
58 * mode, a thread-based application will have a separate context, in which
59 * it uses other ISC library services such as tasks or timers. Signals are
60 * not caught in this mode, so that the application can handle the signals
61 * in its preferred way.
64 * Clients must ensure that isc_app_start(), isc_app_run(), and
65 * isc_app_finish() are called at most once. isc_app_shutdown()
66 * is safe to use by any thread (provided isc_app_start() has been
69 * The same note applies to isc_app_ctxXXX() functions, but in this case
70 * it's a per-thread restriction. For example, a thread with an
71 * application context must ensure that isc_app_ctxstart() with the
72 * context is called at most once.
75 * No anticipated impact.
81 * No anticipated impact.
87 #include <isc/eventclass.h>
89 #include <isc/magic.h>
90 #include <isc/result.h>
96 typedef isc_event_t isc_appevent_t;
98 #define ISC_APPEVENT_FIRSTEVENT (ISC_EVENTCLASS_APP + 0)
99 #define ISC_APPEVENT_SHUTDOWN (ISC_EVENTCLASS_APP + 1)
100 #define ISC_APPEVENT_LASTEVENT (ISC_EVENTCLASS_APP + 65535)
103 * app module methods. Only app driver implementations use this structure.
104 * Other clients should use the top-level interfaces (i.e., isc_app_xxx
105 * functions). magic must be ISCAPI_APPMETHODS_MAGIC.
107 typedef struct isc_appmethods {
108 void (*ctxdestroy)(isc_appctx_t **ctxp);
109 isc_result_t (*ctxstart)(isc_appctx_t *ctx);
110 isc_result_t (*ctxrun)(isc_appctx_t *ctx);
111 isc_result_t (*ctxsuspend)(isc_appctx_t *ctx);
112 isc_result_t (*ctxshutdown)(isc_appctx_t *ctx);
113 void (*ctxfinish)(isc_appctx_t *ctx);
114 void (*settaskmgr)(isc_appctx_t *ctx,
115 isc_taskmgr_t *timermgr);
116 void (*setsocketmgr)(isc_appctx_t *ctx,
117 isc_socketmgr_t *timermgr);
118 void (*settimermgr)(isc_appctx_t *ctx,
119 isc_timermgr_t *timermgr);
123 * This structure is actually just the common prefix of an application context
124 * implementation's version of an isc_appctx_t.
126 * Direct use of this structure by clients is forbidden. app implementations
127 * may change the structure. 'magic' must be ISCAPI_APPCTX_MAGIC for any
128 * of the isc_app_ routines to work. app implementations must maintain
129 * all app context invariants.
132 unsigned int impmagic;
134 isc_appmethods_t *methods;
137 #define ISCAPI_APPCTX_MAGIC ISC_MAGIC('A','a','p','c')
138 #define ISCAPI_APPCTX_VALID(c) ((c) != NULL && \
139 (c)->magic == ISCAPI_APPCTX_MAGIC)
144 isc_app_ctxstart(isc_appctx_t *ctx);
149 * \brief Start an ISC library application.
152 * This call should be made before any other ISC library call, and as
153 * close to the beginning of the application as possible.
156 * 'ctx' is a valid application context (for app_ctxstart()).
160 isc_app_onrun(isc_mem_t *mctx, isc_task_t *task, isc_taskaction_t action,
163 * \brief Request delivery of an event when the application is run.
166 *\li isc_app_start() has been called.
174 isc_app_ctxrun(isc_appctx_t *ctx);
179 * \brief Run an ISC library application.
182 *\li The caller (typically the initial thread of an application) will
183 * block until shutdown is requested. When the call returns, the
184 * caller should start shutting down the application.
187 *\li isc_app_[ctx]start() has been called.
190 *\li Any events requested via isc_app_onrun() will have been posted (in
191 * FIFO order) before isc_app_run() blocks.
192 *\li 'ctx' is a valid application context (for app_ctxrun()).
195 *\li ISC_R_SUCCESS Shutdown has been requested.
196 *\li ISC_R_RELOAD Reload has been requested.
200 isc_app_ctxshutdown(isc_appctx_t *ctx);
203 isc_app_shutdown(void);
205 * \brief Request application shutdown.
208 *\li It is safe to call isc_app_shutdown() multiple times. Shutdown will
209 * only be triggered once.
212 *\li isc_app_[ctx]run() has been called.
213 *\li 'ctx' is a valid application context (for app_ctxshutdown()).
217 *\li ISC_R_UNEXPECTED
221 isc_app_ctxsuspend(isc_appctx_t *ctx);
223 * \brief This has the same behavior as isc_app_ctxsuspend().
227 isc_app_reload(void);
229 * \brief Request application reload.
232 *\li isc_app_run() has been called.
236 *\li ISC_R_UNEXPECTED
240 isc_app_ctxfinish(isc_appctx_t *ctx);
243 isc_app_finish(void);
245 * \brief Finish an ISC library application.
248 *\li This call should be made at or near the end of main().
251 *\li isc_app_start() has been called.
252 *\li 'ctx' is a valid application context (for app_ctxfinish()).
255 *\li Any resources allocated by isc_app_start() have been released.
261 * \brief Indicate that a blocking operation will be performed.
264 *\li If a blocking operation is in process, a call to isc_app_shutdown()
265 * or an external signal will abort the program, rather than allowing
266 * clean shutdown. This is primarily useful for reading user input.
269 * \li isc_app_start() has been called.
270 * \li No other blocking operations are in progress.
274 isc_app_unblock(void);
276 * \brief Indicate that a blocking operation is complete.
279 * \li When a blocking operation has completed, return the program to a
280 * state where a call to isc_app_shutdown() or an external signal will
284 * \li isc_app_start() has been called.
285 * \li isc_app_block() has been called by the same thread.
289 isc_appctx_create(isc_mem_t *mctx, isc_appctx_t **ctxp);
291 * \brief Create an application context.
294 *\li 'mctx' is a valid memory context.
295 *\li 'ctxp' != NULL && *ctxp == NULL.
299 isc_appctx_destroy(isc_appctx_t **ctxp);
301 * \brief Destroy an application context.
304 *\li '*ctxp' is a valid application context.
311 isc_appctx_settaskmgr(isc_appctx_t *ctx, isc_taskmgr_t *taskmgr);
313 * \brief Associate a task manager with an application context.
315 * This must be done before running tasks within the application context.
318 *\li 'ctx' is a valid application context.
319 *\li 'taskmgr' is a valid task manager.
323 isc_appctx_setsocketmgr(isc_appctx_t *ctx, isc_socketmgr_t *socketmgr);
325 * \brief Associate a socket manager with an application context.
327 * This must be done before handling socket events within the application
331 *\li 'ctx' is a valid application context.
332 *\li 'socketmgr' is a valid socket manager.
336 isc_appctx_settimermgr(isc_appctx_t *ctx, isc_timermgr_t *timermgr);
338 * \brief Associate a socket timer with an application context.
340 * This must be done before handling timer events within the application
344 *\li 'ctx' is a valid application context.
345 *\li 'timermgr' is a valid timer manager.
348 #ifdef USE_APPIMPREGISTER
350 * See isc_appctx_create() above.
353 (*isc_appctxcreatefunc_t)(isc_mem_t *mctx, isc_appctx_t **ctxp);
356 isc_app_register(isc_appctxcreatefunc_t createfunc);
358 * Register a new application implementation and add it to the list of
359 * supported implementations. This function must be called when a different
360 * event library is used than the one contained in the ISC library.
364 isc__app_register(void);
366 * A short cut function that specifies the application module in the ISC
367 * library for isc_app_register(). An application that uses the ISC library
368 * usually do not have to care about this function: it would call
369 * isc_lib_register(), which internally calls this function.
371 #endif /* USE_APPIMPREGISTER */
375 #endif /* ISC_APP_H */