[62] | 1 | !--------------------------------------------------------------! |
---|
[74] | 2 | ! Copyright (C) 2012 LEGI - UMR 5519 / CNRS |
---|
| 3 | ! http://www.legi.grenoble-inp.fr/ |
---|
| 4 | ! Licence: GNU Lesser General Public License - LGPLv2 or later |
---|
| 5 | ! Author: Gabriel Moreau |
---|
| 6 | ! Forge: |
---|
| 7 | ! http://servforge.legi.grenoble-inp.fr/projects/soft-trokata/ |
---|
[68] | 8 | ! $Id: signal_checkpoint.F90 74 2012-05-29 13:47:41Z g7moreau $ |
---|
[62] | 9 | !--------------------------------------------------------------! |
---|
| 10 | |
---|
| 11 | module Signal_Checkpoint |
---|
| 12 | |
---|
| 13 | #ifdef __INTEL_COMPILER |
---|
| 14 | use IFPORT, only: signal |
---|
| 15 | #endif |
---|
| 16 | |
---|
| 17 | implicit none |
---|
[71] | 18 | save |
---|
[62] | 19 | private |
---|
| 20 | |
---|
[73] | 21 | ! Signal list |
---|
| 22 | integer,parameter :: SIGHUP = 1 ! Signal HUP |
---|
| 23 | integer,parameter :: SIGINT = 2 ! Signal INT |
---|
| 24 | integer,parameter :: SIGQUIT = 3 ! Signal QUIT |
---|
| 25 | integer,parameter :: SIGUSR1 = 10 ! Signal USR1 |
---|
| 26 | integer,parameter :: SIGUSR2 = 12 ! Signal USR2 |
---|
| 27 | integer,parameter :: SIGTERM = 15 ! Signal TERM |
---|
[62] | 28 | |
---|
[72] | 29 | ! Internal counter |
---|
[73] | 30 | integer :: SIGNAL_RECEIVED_COUNT_ = 0 ! Global signal counter |
---|
| 31 | logical :: CODE_ERROR_ON_EXIT_ = .false. ! Global return state |
---|
[62] | 32 | |
---|
[73] | 33 | ! Public interface |
---|
[67] | 34 | public :: SIGHUP |
---|
| 35 | public :: SIGINT |
---|
| 36 | public :: SIGQUIT |
---|
| 37 | public :: SIGUSR1 |
---|
[62] | 38 | public :: SIGUSR2 |
---|
[67] | 39 | public :: SIGTERM |
---|
[62] | 40 | public :: signal_checkpoint_connect |
---|
| 41 | public :: signal_checkpoint_is_received |
---|
| 42 | public :: signal_checkpoint_received_times |
---|
[70] | 43 | public :: signal_checkpoint_ask_for_exit_code |
---|
[62] | 44 | |
---|
| 45 | !--------------------------------------------------------------! |
---|
| 46 | contains |
---|
| 47 | !--------------------------------------------------------------! |
---|
| 48 | |
---|
[70] | 49 | subroutine signal_checkpoint_connect (SIG_NUM, EXIT) |
---|
[73] | 50 | integer,intent(in) :: SIG_NUM |
---|
| 51 | logical,intent(in),optional :: EXIT |
---|
[62] | 52 | |
---|
| 53 | #ifdef __INTEL_COMPILER |
---|
[63] | 54 | integer :: ERR |
---|
[70] | 55 | |
---|
| 56 | if (present(EXIT)) then |
---|
[71] | 57 | ERR = signal(SIG_NUM, trap_callback_intel_exit_, -1) |
---|
| 58 | else |
---|
| 59 | ERR = signal(SIG_NUM, trap_callback_intel_count_, -1) |
---|
[70] | 60 | end if |
---|
[62] | 61 | #endif |
---|
[64] | 62 | |
---|
[62] | 63 | #ifdef __GNUC__ |
---|
[64] | 64 | intrinsic signal |
---|
| 65 | |
---|
[70] | 66 | if (present(EXIT)) then |
---|
[71] | 67 | call signal(SIG_NUM, trap_callback_exit_) |
---|
| 68 | else |
---|
| 69 | call signal(SIG_NUM, trap_callback_count_) |
---|
[70] | 70 | end if |
---|
[62] | 71 | #endif |
---|
| 72 | |
---|
| 73 | end subroutine |
---|
| 74 | |
---|
| 75 | !--------------------------------------------------------------! |
---|
[64] | 76 | |
---|
[62] | 77 | function signal_checkpoint_is_received () result (IS_RECEIVED) |
---|
| 78 | logical :: IS_RECEIVED |
---|
| 79 | |
---|
[73] | 80 | IS_RECEIVED = ( SIGNAL_RECEIVED_COUNT_ > 0 ) |
---|
[62] | 81 | end function |
---|
| 82 | |
---|
| 83 | !--------------------------------------------------------------! |
---|
[64] | 84 | |
---|
[62] | 85 | function signal_checkpoint_received_times () result (RECEIVED_TIMES) |
---|
| 86 | integer :: RECEIVED_TIMES |
---|
| 87 | |
---|
[73] | 88 | RECEIVED_TIMES = SIGNAL_RECEIVED_COUNT_ |
---|
[62] | 89 | end function |
---|
| 90 | |
---|
| 91 | !--------------------------------------------------------------! |
---|
[70] | 92 | |
---|
| 93 | function signal_checkpoint_ask_for_exit_code () result (EXIT) |
---|
| 94 | logical :: EXIT |
---|
| 95 | |
---|
[73] | 96 | EXIT = CODE_ERROR_ON_EXIT_ |
---|
[70] | 97 | end function |
---|
| 98 | |
---|
| 99 | !--------------------------------------------------------------! |
---|
[62] | 100 | !--------------------------------------------------------------! |
---|
| 101 | |
---|
[73] | 102 | subroutine trap_callback_count_ |
---|
[62] | 103 | |
---|
[73] | 104 | SIGNAL_RECEIVED_COUNT_ = SIGNAL_RECEIVED_COUNT_ + 1 |
---|
[71] | 105 | end subroutine |
---|
| 106 | |
---|
[62] | 107 | !--------------------------------------------------------------! |
---|
[71] | 108 | |
---|
[73] | 109 | subroutine trap_callback_exit_ |
---|
[71] | 110 | |
---|
[73] | 111 | CODE_ERROR_ON_EXIT_ = .true. |
---|
[71] | 112 | call trap_callback_count_ |
---|
| 113 | end subroutine |
---|
| 114 | |
---|
[62] | 115 | !--------------------------------------------------------------! |
---|
| 116 | |
---|
[71] | 117 | function trap_callback_intel_exit_ (SIG_NUM) result (ONE) |
---|
[73] | 118 | integer,intent(in) :: SIG_NUM |
---|
| 119 | integer :: ONE |
---|
[62] | 120 | |
---|
[71] | 121 | call trap_callback_exit_ |
---|
| 122 | ONE = 1 |
---|
| 123 | end function |
---|
[62] | 124 | |
---|
[64] | 125 | !--------------------------------------------------------------! |
---|
| 126 | |
---|
[71] | 127 | function trap_callback_intel_count_ (SIG_NUM) result (ONE) |
---|
[73] | 128 | integer,intent(in) :: SIG_NUM |
---|
| 129 | integer :: ONE |
---|
[62] | 130 | |
---|
[71] | 131 | call trap_callback_count_ |
---|
[63] | 132 | ONE = 1 |
---|
[71] | 133 | end function |
---|
[64] | 134 | |
---|
| 135 | !--------------------------------------------------------------! |
---|
[71] | 136 | end module |
---|
[64] | 137 | !--------------------------------------------------------------! |
---|