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