Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Nikhil Samith Bysani
committed
Dec 20, 2008
1 parent
0bd9951
commit 22417a3
Showing
6 changed files
with
144 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
#include"bang-signals.h" | ||
#include"bang-types.h" | ||
#include<stdlib.h> | ||
|
||
struct _signal_node { | ||
BANGSignalHandler *handler; | ||
struct _signal_node *next; | ||
}; | ||
|
||
typedef struct _signal_node signal_node; | ||
|
||
/** | ||
* \brief The handlers for each of the signals is kept in a linked list stored | ||
* in an array which is index by the signal's number | ||
*/ | ||
signal_node **signal_handlers; | ||
|
||
void BANG_sig_init() { | ||
int i; | ||
|
||
signal_handlers = (signal_node**) calloc(BANG_NUM_SIGS,sizeof(signal_node*)); | ||
for (i = 0; i < BANG_NUM_SIGS; ++i) { | ||
signal_handlers[i] = NULL; | ||
} | ||
} | ||
|
||
void recursive_sig_free(signal_node *head) { | ||
if (head->next != NULL) { | ||
recursive_sig_free(head->next); | ||
} | ||
free(head); | ||
head = NULL; | ||
} | ||
|
||
void BANG_sig_close() { | ||
int i; | ||
for (i = 0; i < BANG_NUM_SIGS; ++i) { | ||
recursive_sig_free(signal_handlers[i]); | ||
} | ||
free(signal_handlers); | ||
signal_handlers = NULL; | ||
} | ||
|
||
int BANG_install_sighandler(int signal, BANGSignalHandler *handler) { | ||
if (signal_handlers[signal] == NULL) { | ||
signal_handlers[signal] = (signal_node*) malloc(sizeof(signal_node)); | ||
signal_handlers[signal]->handler = handler; | ||
signal_handlers[signal]->next = NULL; | ||
return 0; | ||
} else { | ||
signal_node *cur; | ||
for (cur = signal_handlers[signal]; cur != NULL; cur = cur->next) { | ||
if (cur->next == NULL) { | ||
cur->next =(signal_node*) malloc(sizeof(signal_node)); | ||
cur->next->handler = handler; | ||
cur->next->next = NULL; | ||
return 0; | ||
} | ||
} | ||
} | ||
return -1; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/** | ||
* \file bang-signals.h | ||
* \author Nikhil Bysani | ||
* \date December 20, 2009 | ||
* | ||
* \brief Allows the library to send out signals, and for the applications to | ||
* receive signals. | ||
*/ | ||
#ifndef __BANG_SIGNALS_H | ||
#define __BANG_SIGNALS_H | ||
|
||
#include"bang-types.h" | ||
/** | ||
* \param signal The signal that you want to catch. | ||
* \param handler Function that handles the signal. | ||
* | ||
* \brief Installs a signal such that whenever said signal gets generated | ||
* handler gets called with the appropriate arguments. | ||
*/ | ||
int BANG_install_sighandler(int signal, BANGSignalHandler *handler); | ||
|
||
/** | ||
* \param signal The signal type to acknowledge. | ||
* \param sig_id The id of the signal | ||
* | ||
* \brief Acknowledges that the signal has been received, and that | ||
* the function is ready to take another. | ||
*/ | ||
void BANG_acknowledge_signal(int signal, int sig_id); | ||
|
||
/** | ||
* | ||
* \brief Initializes the signal porition of the library. | ||
*/ | ||
void BANG_sig_init(); | ||
|
||
/** | ||
* | ||
* \brief Frees and stops signals from working. | ||
*/ | ||
void BANG_sig_close(); | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,20 @@ | ||
/** | ||
* \file bang-types.h | ||
* \author Nikhil Bysani | ||
* \date December 20, 2009 | ||
* | ||
* \brief Defines commonly used types in the BANG library. | ||
* | ||
*/ | ||
#ifndef __BANG_TYPES_H | ||
#define __BANG_TYPES_H | ||
|
||
/// A signal handler function, it receives the BANGSIGNUM, and arguments depending on the signal. | ||
typedef void BANGSignalHandler(int,void **args); | ||
typedef void BANGSignalHandler(int,int,void **args); | ||
|
||
#define BANG_NUM_SIGS 1 | ||
enum BANG_signals { | ||
BANG_BIND_SUC = 0 | ||
} BANG_signal; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters