1 /*----------------------------------------------------------------------------
 2  Name:      xmlBlaster/src/c/util/Timeout.h
 3  Project:   xmlBlaster.org
 4  Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
 5  Comment:   Generic Timeout code with POSIX threads
 6  Author:    "Marcel Ruff" <xmlBlaster@marcelruff.info>
 7  -----------------------------------------------------------------------------*/
 8 #ifndef _XMLBLASTER_TIMEOUT_H
 9 #define _XMLBLASTER_TIMEOUT_H
10 
11 #include <util/basicDefs.h> /* for int64_t (C99), Dll_Export, inline, bool etc. */
12 
13 #if defined(_WINDOWS)
14 #  if defined(XB_USE_PTHREADS)
15 #     include <pthreads/pthread.h> /* Our pthreads.h: For timespec, for logging output of thread ID, for Windows and WinCE downloaded from http://sources.redhat.com/pthreads-win32 */
16 #  else
17 #  endif
18 #else
19 # include <pthread.h>
20 # define XB_USE_PTHREADS 1
21 #endif
22 
23 #ifdef __cplusplus
24 #ifndef XMLBLASTER_C_COMPILE_AS_CPP /* 'g++ -DXMLBLASTER_C_COMPILE_AS_CPP ...' allows to compile the lib as C++ code */
25 extern "C" {
26 #endif
27 #endif
28 #include <stdio.h>
29 #include <stdlib.h>
30 
31 
32 struct TimeoutStruct;
33 typedef Dll_Export struct TimeoutStruct Timeout;
34 
35 /* Declare function pointers to use in struct to simulate object oriented access */
36 typedef void (*TimeoutCbFp)(Timeout *timeout, void *userData, void *userData2);
37 
38 typedef int (* XmlBlasterTimeoutSetTimeoutListener)(Timeout *xb,
39       TimeoutCbFp timeoutCbFp, const long int delay, void *userData, void *userData2);
40 
41 /**
42  * Callback specific data is hold here.
43  * So we can in future support many listeners (having a map of TimeoutContainers)
44  */
45 typedef struct Dll_Export TimeoutContainerStruct {
46    long int delay;
47    void *userData;
48    void *userData2;
49    TimeoutCbFp timeoutCbFp;
50 } TimeoutContainer;
51 
52 /**
53  * All client access to Timeout goes over this struct and its function pointers.
54  */
55 struct Dll_Export TimeoutStruct {
56    bool verbose;
57    const char *name; /**< The timer/thread name */
58    pthread_t threadId;
59    bool running;
60    bool ready; /**< On creation wait until thread started */
61    bool selfCleanup;
62    TimeoutContainer timeoutContainer;
63    /**
64     * Add listener and span timer.
65     * @param timeout The this pointer
66     * @param timeoutCbFp The function pointer to call back on timeout
67     * @param delay Repeated call of timeoutCbFp of given delay in millisconds
68     *        If < 1 the timer is reset (set inactive), this can be called
69     *        from a separate thread of from the callback itself
70     * @param userData is passed back to your timeoutCbFp
71     * @param userData2 is passed back to your timeoutCbFp
72     */
73    XmlBlasterTimeoutSetTimeoutListener setTimeoutListener;
74    pthread_mutex_t condition_mutex; /*= PTHREAD_MUTEX_INITIALIZER; */
75    pthread_cond_t  condition_cond; /*  = PTHREAD_COND_INITIALIZER;*/
76 };
77 
78 /**
79  * Get an instance of this Timeout struct.
80  * NOTE: Every call creates a new and independent instance
81  * @param name The name of the thread
82  * @return NULL if bootstrapping failed. If not NULL you need to free it when you are done
83  * usually by calling freeTimeout().
84  */
85 Dll_Export extern Timeout *createTimeout(const char* const name);
86 
87 /**
88  * Free your instance after accessing xmlBlaster.
89  */
90 Dll_Export extern void freeTimeout(Timeout *timeout);
91 
92 #ifdef __cplusplus
93 #ifndef XMLBLASTER_C_COMPILE_AS_CPP
94 }
95 #endif
96 #endif
97 
98 #endif /* _XMLBLASTER_TIMEOUT_H */


syntax highlighted by Code2HTML, v. 0.9.1