Added signal catching and handling in C and Cython
This commit is contained in:
@ -14,7 +14,8 @@ include_directories(${CMAKE_CURRENT_SOURCE_DIR})
|
||||
|
||||
file(GLOB_RECURSE CSRC "*.c")
|
||||
|
||||
add_library(cobitools3 SHARED obierrno.c
|
||||
add_library(cobitools3 SHARED obisig.c
|
||||
obierrno.c
|
||||
obidms_taxonomy.c
|
||||
obiblob_indexer.c
|
||||
obi_lcs.c
|
||||
|
@ -24,6 +24,7 @@
|
||||
#include "obidms.h"
|
||||
#include "obidebug.h"
|
||||
#include "obierrno.h"
|
||||
#include "obisig.h"
|
||||
#include "obitypes.h"
|
||||
#include "obiview.h"
|
||||
#include "obi_lcs.h"
|
||||
@ -171,6 +172,8 @@ int build_reference_db(const char* dms_name,
|
||||
char threshold_str[5];
|
||||
char* new_comments;
|
||||
|
||||
signal(SIGINT, sig_handler);
|
||||
|
||||
// Discuss keeping the matrix view or not
|
||||
matrix_view_name = calloc((strlen(o_view_name)+strlen("_matrix")+1), sizeof(char));
|
||||
if (matrix_view_name == NULL)
|
||||
@ -321,6 +324,9 @@ int build_reference_db(const char* dms_name,
|
||||
// For each pair
|
||||
for (i=0; i<(matrix_with_lca_view->infos)->line_count; i++)
|
||||
{
|
||||
if (! keep_running)
|
||||
return -1;
|
||||
|
||||
// Read all taxids associated with the first sequence and compute their LCA
|
||||
// Read line index
|
||||
idx1 = obi_get_int_with_elt_idx_and_col_p_in_view(matrix_with_lca_view, matrix_idx1_column, i, 0);
|
||||
@ -440,6 +446,9 @@ int build_reference_db(const char* dms_name,
|
||||
// Going through matrix once, filling refs arrays on the go for efficiency
|
||||
for (i=0; i<(matrix_with_lca_view->infos)->line_count; i++)
|
||||
{
|
||||
if (! keep_running)
|
||||
return -1;
|
||||
|
||||
// Read ref line indexes
|
||||
idx1 = obi_get_int_with_elt_idx_and_col_p_in_view(matrix_with_lca_view, matrix_idx1_column, i, 0);
|
||||
idx2 = obi_get_int_with_elt_idx_and_col_p_in_view(matrix_with_lca_view, matrix_idx2_column, i, 0);
|
||||
|
@ -24,6 +24,7 @@
|
||||
#include "obidms.h"
|
||||
#include "obidebug.h"
|
||||
#include "obierrno.h"
|
||||
#include "obisig.h"
|
||||
#include "obitypes.h"
|
||||
#include "obiview.h"
|
||||
#include "sse_banded_LCS_alignment.h"
|
||||
@ -202,6 +203,8 @@ int obi_clean(const char* dms_name,
|
||||
|
||||
int max_threads = 1;
|
||||
|
||||
signal(SIGINT, sig_handler);
|
||||
|
||||
#ifdef _OPENMP
|
||||
max_threads = omp_get_max_threads();
|
||||
if ((thread_count == -1) || (thread_count > max_threads))
|
||||
@ -408,7 +411,7 @@ int obi_clean(const char* dms_name,
|
||||
|
||||
#pragma omp parallel default(none) \
|
||||
shared(thread_count, seq_count, blob_array, complete_sample_count_array, alignment_result_array, \
|
||||
stop, blob1, i, obi_errno, stderr, max_ratio, iseq_column, i_view, \
|
||||
stop, blob1, i, obi_errno, keep_running, stderr, max_ratio, iseq_column, i_view, \
|
||||
similarity_mode, reference, normalize, threshold, ktable, status_column, o_view, sample_count)
|
||||
{
|
||||
index_t j;
|
||||
@ -430,6 +433,9 @@ int obi_clean(const char* dms_name,
|
||||
#pragma omp for schedule(dynamic, sample_count/thread_count + (sample_count % thread_count != 0)) // Avoid 0 which blocks the program
|
||||
for (sample=0; sample < sample_count; sample++)
|
||||
{
|
||||
if (! keep_running)
|
||||
stop = true;
|
||||
|
||||
sample_count_array = complete_sample_count_array+(sample*seq_count);
|
||||
|
||||
// Get count for this sample
|
||||
|
@ -25,6 +25,7 @@
|
||||
#include "obiview.h"
|
||||
#include "obidebug.h"
|
||||
#include "obierrno.h"
|
||||
#include "obisig.h"
|
||||
#include "obitypes.h"
|
||||
#include "obiview.h"
|
||||
|
||||
@ -718,6 +719,8 @@ int obi_ecopcr(const char* i_dms_name,
|
||||
int32_t erri;
|
||||
int32_t errj;
|
||||
|
||||
signal(SIGINT, sig_handler);
|
||||
|
||||
if (circular)
|
||||
{
|
||||
circular = strlen(primer1);
|
||||
@ -999,6 +1002,9 @@ int obi_ecopcr(const char* i_dms_name,
|
||||
fprintf(stderr,"\rDone : %f %% ", p);
|
||||
}
|
||||
|
||||
if (! keep_running)
|
||||
return -1;
|
||||
|
||||
checkedSequence++;
|
||||
|
||||
// Get the taxid
|
||||
|
@ -25,6 +25,7 @@
|
||||
#include "obidms.h"
|
||||
#include "obidebug.h"
|
||||
#include "obierrno.h"
|
||||
#include "obisig.h"
|
||||
#include "obitypes.h"
|
||||
#include "obiview.h"
|
||||
#include "obidmscolumn.h"
|
||||
@ -273,6 +274,8 @@ int obi_ecotag(const char* dms_name,
|
||||
buffer_size = 1024;
|
||||
best_match_ids_buffer_size = 1024;
|
||||
|
||||
signal(SIGINT, sig_handler);
|
||||
|
||||
// Open main DMS containing the query sequences and where the output will be written
|
||||
dms = obi_open_dms(dms_name, false);
|
||||
if (dms == NULL)
|
||||
@ -471,6 +474,9 @@ int obi_ecotag(const char* dms_name,
|
||||
|
||||
for (j=0; j < ref_count; j++)
|
||||
{
|
||||
if (! keep_running)
|
||||
return -1;
|
||||
|
||||
// Get reference sequence and its index
|
||||
ref_seq_idx = obi_get_index_with_elt_idx_and_col_p_in_view(ref_view, ref_seq_column, j, 0);
|
||||
blob2 = obi_get_blob_with_elt_idx_and_col_p_in_view(ref_view, ref_seq_column, j, 0);
|
||||
|
@ -21,6 +21,7 @@
|
||||
#include "obi_lcs.h"
|
||||
#include "obidebug.h"
|
||||
#include "obierrno.h"
|
||||
#include "obisig.h"
|
||||
#include "obitypes.h"
|
||||
#include "obiview.h"
|
||||
#include "sse_banded_LCS_alignment.h"
|
||||
@ -428,6 +429,8 @@ int obi_lcs_align_one_column(const char* dms_name,
|
||||
OBIDMS_column_p ali_length_column = NULL;
|
||||
OBIDMS_column_p score_column = NULL;
|
||||
|
||||
signal(SIGINT, sig_handler);
|
||||
|
||||
k = 0;
|
||||
|
||||
// Open DMS
|
||||
@ -574,6 +577,9 @@ int obi_lcs_align_one_column(const char* dms_name,
|
||||
if (i%100 == 0)
|
||||
fprintf(stderr,"\rDone : %f %% ", (i / (float) seq_count)*100);
|
||||
|
||||
if (! keep_running)
|
||||
return -1;
|
||||
|
||||
// Get first id idx
|
||||
id1_idx = obi_get_index_with_elt_idx_and_col_p_in_view(seq_view, id_column, i, 0); // TODO Could there be multiple IDs per line?
|
||||
// Get first sequence and its index
|
||||
@ -725,6 +731,8 @@ int obi_lcs_align_two_columns(const char* dms_name,
|
||||
OBIDMS_column_p ali_length_column = NULL;
|
||||
OBIDMS_column_p score_column = NULL;
|
||||
|
||||
signal(SIGINT, sig_handler);
|
||||
|
||||
k = 0;
|
||||
|
||||
// Open DMS
|
||||
@ -979,6 +987,9 @@ int obi_lcs_align_two_columns(const char* dms_name,
|
||||
|
||||
for (j=0; j < seq2_count; j++)
|
||||
{
|
||||
if (! keep_running)
|
||||
return -1;
|
||||
|
||||
// Get second sequence and its index
|
||||
seq2_idx = obi_get_index_with_elt_idx_and_col_p_in_view(seq2_view, i_seq2_column, j, seq2_elt_idx);
|
||||
blob2 = obi_get_blob_with_elt_idx_and_col_p_in_view(seq2_view, i_seq2_column, j, seq2_elt_idx);
|
||||
|
12
src/obidms.c
12
src/obidms.c
@ -32,6 +32,7 @@
|
||||
#include "utils.h"
|
||||
#include "obilittlebigman.h"
|
||||
#include "libjson/json_utils.h"
|
||||
#include "obisig.h"
|
||||
|
||||
|
||||
#define DEBUG_LEVEL 0 // TODO has to be defined somewhere else (cython compil flag?)
|
||||
@ -1627,6 +1628,8 @@ int obi_import_view(const char* dms_path_1, const char* dms_path_2, const char*
|
||||
OBIDMS_column_header_p header = NULL;
|
||||
OBIDMS_column_header_p header_2 = NULL;
|
||||
|
||||
signal(SIGINT, sig_handler);
|
||||
|
||||
dms_1 = obi_open_dms(dms_path_1, false);
|
||||
if (dms_1 == NULL)
|
||||
{
|
||||
@ -1676,6 +1679,9 @@ int obi_import_view(const char* dms_path_1, const char* dms_path_2, const char*
|
||||
// Import each column and update with the new version number
|
||||
for (i=0; i < (view_1->infos->column_count); i++)
|
||||
{
|
||||
if (! keep_running)
|
||||
return -1;
|
||||
|
||||
new_version = obi_import_column(dms_path_1, dms_path_2, ((((view_1->infos)->column_references)[i]).column_refs).column_name, ((((view_1->infos)->column_references)[i]).column_refs).version);
|
||||
if (new_version == -1)
|
||||
{
|
||||
@ -1708,6 +1714,9 @@ int obi_import_view(const char* dms_path_1, const char* dms_path_2, const char*
|
||||
// Go through columns again to update associated columns
|
||||
for (i=0; i < (view_1->infos->column_count); i++)
|
||||
{
|
||||
if (! keep_running)
|
||||
return -1;
|
||||
|
||||
header = obi_column_get_header_from_name(dms_1, ((((view_1->infos)->column_references)[i]).column_refs).column_name, ((((view_1->infos)->column_references)[i]).column_refs).version);
|
||||
if (header == NULL)
|
||||
{
|
||||
@ -1747,6 +1756,9 @@ int obi_import_view(const char* dms_path_1, const char* dms_path_2, const char*
|
||||
}
|
||||
}
|
||||
|
||||
if (! keep_running)
|
||||
return -1;
|
||||
|
||||
// Close the views
|
||||
if (obi_save_and_close_view(view_1) < 0)
|
||||
{
|
||||
|
@ -136,6 +136,9 @@ extern int obi_errno;
|
||||
*/
|
||||
#define OBIDMS_WORKING (38) /** OBIDMS is tagged as being currently used by a process.
|
||||
*/
|
||||
#define OBI_SIGNAL_CAUGHT (39) /** Caught an interrupting signal.
|
||||
*/
|
||||
/**@}*/
|
||||
|
||||
|
||||
#endif /* OBIERRNO_H_ */
|
||||
|
31
src/obisig.c
Executable file
31
src/obisig.c
Executable file
@ -0,0 +1,31 @@
|
||||
/****************************************************************************
|
||||
* Functions for signal catching and handling *
|
||||
****************************************************************************/
|
||||
|
||||
/**
|
||||
* @file obisig.c
|
||||
* @author Celine Mercier (celine.mercier@metabarcoding.org)
|
||||
* @date September 2019
|
||||
* @brief Functions for signal catching and handling.
|
||||
*/
|
||||
|
||||
#include "obidebug.h"
|
||||
#include "obierrno.h"
|
||||
#include "obisig.h"
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <signal.h>
|
||||
|
||||
|
||||
#define DEBUG_LEVEL 0 // TODO has to be defined somewhere else (cython compil flag?)
|
||||
|
||||
|
||||
bool volatile keep_running = true;
|
||||
|
||||
|
||||
void sig_handler(int signum)
|
||||
{
|
||||
obi_set_errno(OBI_SIGNAL_CAUGHT);
|
||||
obidebug(1, "\nCaught signal: %s\n", strsignal(signum));
|
||||
keep_running = false;
|
||||
}
|
32
src/obisig.h
Executable file
32
src/obisig.h
Executable file
@ -0,0 +1,32 @@
|
||||
/****************************************************************************
|
||||
* Header file for obisig *
|
||||
****************************************************************************/
|
||||
|
||||
/**
|
||||
* @file obisig.h
|
||||
* @author Celine Mercier (celine.mercier@metabarcoding.org)
|
||||
* @date September 2019
|
||||
* @brief Header file for signal catching and handling.
|
||||
*/
|
||||
|
||||
|
||||
#ifndef OBISIG_H_
|
||||
#define OBISIG_H_
|
||||
|
||||
#include <stdint.h>
|
||||
#include <signal.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
|
||||
/**
|
||||
* @brief Signal handling.
|
||||
*
|
||||
* @since September 2019
|
||||
* @author Celine Mercier (celine.mercier@metabarcoding.org)
|
||||
*
|
||||
*/
|
||||
bool volatile keep_running;
|
||||
void sig_handler(int signum);
|
||||
|
||||
|
||||
#endif /* OBISIG_H_ */
|
Reference in New Issue
Block a user