forked from haiwan/sledge
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
103 lines
2.3 KiB
103 lines
2.3 KiB
5 years ago
|
#ifndef SFRT_SOFTWARE_INTERRUPT_H
|
||
|
#define SFRT_SOFTWARE_INTERRUPT_H
|
||
5 years ago
|
|
||
|
#include <stdbool.h>
|
||
|
#include <assert.h>
|
||
|
#include <signal.h>
|
||
|
|
||
5 years ago
|
/***************************************
|
||
|
* Externs
|
||
|
***************************************/
|
||
|
|
||
5 years ago
|
extern __thread volatile sig_atomic_t software_interrupt__is_disabled;
|
||
5 years ago
|
|
||
|
/***************************************
|
||
|
* Public Static Inlines
|
||
|
***************************************/
|
||
|
|
||
|
static inline void
|
||
5 years ago
|
software_interrupt__disable(void)
|
||
5 years ago
|
{
|
||
5 years ago
|
while (__sync_bool_compare_and_swap(&software_interrupt__is_disabled, 0, 1) == false)
|
||
5 years ago
|
;
|
||
|
}
|
||
|
|
||
5 years ago
|
|
||
|
/**
|
||
|
* Enables signals
|
||
|
*/
|
||
5 years ago
|
static inline void
|
||
5 years ago
|
software_interrupt__enable(void)
|
||
5 years ago
|
{
|
||
5 years ago
|
if (__sync_bool_compare_and_swap(&software_interrupt__is_disabled, 1, 0) == false) assert(0);
|
||
5 years ago
|
}
|
||
|
|
||
5 years ago
|
/**
|
||
|
* @returns boolean if signals are enabled
|
||
|
*/
|
||
5 years ago
|
static inline int
|
||
5 years ago
|
software_interrupt__is_enabled(void)
|
||
5 years ago
|
{
|
||
5 years ago
|
return (software_interrupt__is_disabled == 0);
|
||
5 years ago
|
}
|
||
|
|
||
5 years ago
|
/**
|
||
5 years ago
|
* Masks a signal on the current thread
|
||
|
* @param signal - the signal you want to mask
|
||
5 years ago
|
* @return 0 on success. Exits program otherwise
|
||
|
**/
|
||
5 years ago
|
static inline int
|
||
5 years ago
|
software_interrupt__mask_signal(int signal)
|
||
5 years ago
|
{
|
||
|
sigset_t set;
|
||
5 years ago
|
int return_code;
|
||
5 years ago
|
|
||
5 years ago
|
assert(signal == SIGALRM || signal == SIGUSR1);
|
||
|
/* all threads created by the calling thread will have signal blocked */
|
||
5 years ago
|
sigemptyset(&set);
|
||
5 years ago
|
sigaddset(&set, signal);
|
||
|
return_code = pthread_sigmask(SIG_BLOCK, &set, NULL);
|
||
|
if (return_code != 0) {
|
||
|
errno = return_code;
|
||
5 years ago
|
perror("pthread_sigmask");
|
||
|
exit(-1);
|
||
|
}
|
||
|
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
5 years ago
|
/**
|
||
5 years ago
|
* Unmasks a signal on the current thread
|
||
|
* @param signal - the signal you want to unmask
|
||
5 years ago
|
* @return 0 on success. Exits program otherwise
|
||
|
**/
|
||
5 years ago
|
static inline int
|
||
5 years ago
|
software_interrupt__unmask_signal(int signal)
|
||
5 years ago
|
{
|
||
|
sigset_t set;
|
||
5 years ago
|
int return_code;
|
||
5 years ago
|
|
||
5 years ago
|
assert(signal == SIGALRM || signal == SIGUSR1);
|
||
|
/* all threads created by the calling thread will have signal unblocked */
|
||
5 years ago
|
sigemptyset(&set);
|
||
5 years ago
|
sigaddset(&set, signal);
|
||
|
return_code = pthread_sigmask(SIG_UNBLOCK, &set, NULL);
|
||
|
if (return_code != 0) {
|
||
|
errno = return_code;
|
||
5 years ago
|
perror("pthread_sigmask");
|
||
|
exit(-1);
|
||
|
}
|
||
|
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
5 years ago
|
/***************************************
|
||
|
* Exports from module.c
|
||
|
***************************************/
|
||
5 years ago
|
|
||
5 years ago
|
void software_interrupt__initialize(void);
|
||
|
void software_interrupt__arm_timer(void);
|
||
|
void software_interrupt__disarm_timer(void);
|
||
5 years ago
|
|
||
5 years ago
|
#endif /* SFRT_SOFTWARE_INTERRUPT_H */
|