Main Page | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals

heartbeat.c File Reference

#include <signal.h>
#include "gwlib/gwlib.h"
#include "msg.h"
#include "heartbeat.h"

Include dependency graph for heartbeat.c:

Include dependency graph

Go to the source code of this file.

Data Structures

struct  hb_info

Functions

int find_hb (void *item, void *pattern)
void heartbeat_thread (void *arg)
long heartbeat_start (hb_send_func_t *send_func, double freq, hb_load_func_t *load_func)
void heartbeat_stop (long hb_thread)

Variables

Listheartbeats = NULL


Function Documentation

int find_hb void *  item,
void *  pattern
[static]
 

Definition at line 85 of file heartbeat.c.

References info(), and hb_info::thread.

Referenced by heartbeat_stop().

00086 {
00087     long *threadnrp;
00088     struct hb_info *info;
00089 
00090     info = item;
00091     threadnrp = pattern;
00092 
00093     return info->thread == *threadnrp;
00094 }

Here is the call graph for this function:

long heartbeat_start hb_send_func_t send_func,
double  freq,
hb_load_func_t load_func
 

Definition at line 126 of file heartbeat.c.

References DEFAULT_HEARTBEAT, hb_info::freq, gwlist_append(), gwlist_create, gwthread_create, heartbeat_thread(), heartbeats, info(), hb_info::load_func, hb_info::running, hb_info::send_func, and hb_info::thread.

Referenced by main().

00128 {
00129     struct hb_info *info;
00130 
00131     /* can't start with send_funct NULL */
00132     if (send_func == NULL)
00133         return -1;
00134 
00135     info = gw_malloc(sizeof(*info));
00136     info->send_func = send_func;
00137     info->freq = (freq <= 0 ? DEFAULT_HEARTBEAT : freq);
00138     info->load_func = load_func;
00139     info->running = 1;
00140     info->thread = gwthread_create(heartbeat_thread, info);
00141     if (info->thread >= 0) {
00142     if (heartbeats == NULL)
00143         heartbeats = gwlist_create();
00144     gwlist_append(heartbeats, info);
00145         return info->thread;
00146     } else {
00147         gw_free(info);
00148         return -1;
00149     }
00150 }

Here is the call graph for this function:

void heartbeat_stop long  hb_thread  ) 
 

Definition at line 160 of file heartbeat.c.

References find_hb(), gw_assert, gwlist_destroy(), gwlist_extract_first(), gwlist_extract_matching(), gwlist_len(), gwthread_join(), gwthread_wakeup(), heartbeats, info(), hb_info::running, hb_info::thread, and warning().

Referenced by main().

00161 {
00162     List *matching_info;
00163     struct hb_info *info;
00164 
00165     /*
00166      * First, check if there are heartbeats to stop.
00167      * If not, do not continue, otherwise this function will crash
00168      */
00169     if (heartbeats == NULL)
00170         return;
00171 
00172     if (hb_thread == ALL_HEARTBEATS) {
00173         while (NULL != (info = gwlist_extract_first(heartbeats))) {
00174             gw_assert(info);
00175             info->running = 0;
00176             gwthread_wakeup(info->thread);
00177             gwthread_join(info->thread);
00178             gw_free(info);
00179         }
00180     } else {
00181         matching_info = gwlist_extract_matching(heartbeats, &hb_thread, find_hb);
00182         if (matching_info == NULL) {
00183             warning(0, "Could not stop heartbeat %ld: not found.", hb_thread);
00184         return;
00185         }
00186         gw_assert(gwlist_len(matching_info) == 1);
00187         info = gwlist_extract_first(matching_info);
00188         gwlist_destroy(matching_info, NULL);
00189      
00190         info->running = 0;
00191         gwthread_wakeup(hb_thread);
00192         gwthread_join(hb_thread);
00193         gw_free(info);
00194     }
00195     if (gwlist_len(heartbeats) == 0) {
00196         gwlist_destroy(heartbeats, NULL);
00197         heartbeats = NULL;
00198     }
00199 }

Here is the call graph for this function:

void heartbeat_thread void *  arg  )  [static]
 

Definition at line 96 of file heartbeat.c.

References hb_info::freq, gwthread_sleep(), heartbeat, info(), hb_info::load_func, msg_create, hb_info::running, and hb_info::send_func.

Referenced by heartbeat_start().

00097 {
00098     struct hb_info *info;
00099     time_t last_hb;
00100 
00101     info = arg;
00102     last_hb = 0;
00103 
00104     while (info->running) {
00105         Msg *msg;
00106 
00107         gwthread_sleep(info->freq);
00108 
00109         /*
00110          * Because the sleep can be interrupted, we might end up sending
00111          * heartbeats faster than the configured heartbeat frequency.
00112          * This is not bad unless we send them way too fast.  Make sure
00113          * our frequency is not more than twice the configured one.
00114          */
00115         if (difftime(time(NULL), last_hb) < info->freq / 2)
00116             continue;
00117 
00118         msg = msg_create(heartbeat);
00119         if (NULL != info->load_func)
00120             msg->heartbeat.load = info->load_func();
00121         info->send_func(msg);
00122         last_hb = time(NULL);
00123     }
00124 }

Here is the call graph for this function:


Variable Documentation

List* heartbeats = NULL [static]
 

Definition at line 80 of file heartbeat.c.

Referenced by heartbeat_start(), and heartbeat_stop().

See file LICENSE for details about the license agreement for using, modifying, copying or deriving work from this software.