[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 91 2012-10-10 15:55:32Z 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 |
---|
[91] | 28 | integer,parameter :: SIGXCPU = 24 ! Signal XCPU |
---|
[62] | 29 | |
---|
[72] | 30 | ! Internal counter |
---|
[73] | 31 | integer :: SIGNAL_RECEIVED_COUNT_ = 0 ! Global signal counter |
---|
| 32 | logical :: CODE_ERROR_ON_EXIT_ = .false. ! Global return state |
---|
[62] | 33 | |
---|
[73] | 34 | ! Public interface |
---|
[67] | 35 | public :: SIGHUP |
---|
| 36 | public :: SIGINT |
---|
| 37 | public :: SIGQUIT |
---|
| 38 | public :: SIGUSR1 |
---|
[62] | 39 | public :: SIGUSR2 |
---|
[67] | 40 | public :: SIGTERM |
---|
[91] | 41 | public :: SIGXCPU |
---|
[62] | 42 | public :: signal_checkpoint_connect |
---|
| 43 | public :: signal_checkpoint_is_received |
---|
| 44 | public :: signal_checkpoint_received_times |
---|
[70] | 45 | public :: signal_checkpoint_ask_for_exit_code |
---|
[62] | 46 | |
---|
| 47 | !--------------------------------------------------------------! |
---|
| 48 | contains |
---|
| 49 | !--------------------------------------------------------------! |
---|
| 50 | |
---|
[70] | 51 | subroutine signal_checkpoint_connect (SIG_NUM, EXIT) |
---|
[73] | 52 | integer,intent(in) :: SIG_NUM |
---|
| 53 | logical,intent(in),optional :: EXIT |
---|
[62] | 54 | |
---|
| 55 | #ifdef __INTEL_COMPILER |
---|
[63] | 56 | integer :: ERR |
---|
[70] | 57 | |
---|
| 58 | if (present(EXIT)) then |
---|
[71] | 59 | ERR = signal(SIG_NUM, trap_callback_intel_exit_, -1) |
---|
| 60 | else |
---|
| 61 | ERR = signal(SIG_NUM, trap_callback_intel_count_, -1) |
---|
[70] | 62 | end if |
---|
[62] | 63 | #endif |
---|
[64] | 64 | |
---|
[90] | 65 | #if defined (__GNUC__) || defined (XLF) |
---|
[64] | 66 | intrinsic signal |
---|
| 67 | |
---|
[70] | 68 | if (present(EXIT)) then |
---|
[71] | 69 | call signal(SIG_NUM, trap_callback_exit_) |
---|
| 70 | else |
---|
| 71 | call signal(SIG_NUM, trap_callback_count_) |
---|
[70] | 72 | end if |
---|
[62] | 73 | #endif |
---|
| 74 | |
---|
| 75 | end subroutine |
---|
| 76 | |
---|
| 77 | !--------------------------------------------------------------! |
---|
[64] | 78 | |
---|
[62] | 79 | function signal_checkpoint_is_received () result (IS_RECEIVED) |
---|
| 80 | logical :: IS_RECEIVED |
---|
| 81 | |
---|
[73] | 82 | IS_RECEIVED = ( SIGNAL_RECEIVED_COUNT_ > 0 ) |
---|
[62] | 83 | end function |
---|
| 84 | |
---|
| 85 | !--------------------------------------------------------------! |
---|
[64] | 86 | |
---|
[62] | 87 | function signal_checkpoint_received_times () result (RECEIVED_TIMES) |
---|
| 88 | integer :: RECEIVED_TIMES |
---|
| 89 | |
---|
[73] | 90 | RECEIVED_TIMES = SIGNAL_RECEIVED_COUNT_ |
---|
[62] | 91 | end function |
---|
| 92 | |
---|
| 93 | !--------------------------------------------------------------! |
---|
[70] | 94 | |
---|
| 95 | function signal_checkpoint_ask_for_exit_code () result (EXIT) |
---|
| 96 | logical :: EXIT |
---|
| 97 | |
---|
[73] | 98 | EXIT = CODE_ERROR_ON_EXIT_ |
---|
[70] | 99 | end function |
---|
| 100 | |
---|
| 101 | !--------------------------------------------------------------! |
---|
[62] | 102 | !--------------------------------------------------------------! |
---|
| 103 | |
---|
[73] | 104 | subroutine trap_callback_count_ |
---|
[62] | 105 | |
---|
[73] | 106 | SIGNAL_RECEIVED_COUNT_ = SIGNAL_RECEIVED_COUNT_ + 1 |
---|
[71] | 107 | end subroutine |
---|
| 108 | |
---|
[62] | 109 | !--------------------------------------------------------------! |
---|
[71] | 110 | |
---|
[73] | 111 | subroutine trap_callback_exit_ |
---|
[71] | 112 | |
---|
[73] | 113 | CODE_ERROR_ON_EXIT_ = .true. |
---|
[71] | 114 | call trap_callback_count_ |
---|
| 115 | end subroutine |
---|
| 116 | |
---|
[62] | 117 | !--------------------------------------------------------------! |
---|
| 118 | |
---|
[71] | 119 | function trap_callback_intel_exit_ (SIG_NUM) result (ONE) |
---|
[73] | 120 | integer,intent(in) :: SIG_NUM |
---|
| 121 | integer :: ONE |
---|
[62] | 122 | |
---|
[71] | 123 | call trap_callback_exit_ |
---|
| 124 | ONE = 1 |
---|
| 125 | end function |
---|
[62] | 126 | |
---|
[64] | 127 | !--------------------------------------------------------------! |
---|
| 128 | |
---|
[71] | 129 | function trap_callback_intel_count_ (SIG_NUM) result (ONE) |
---|
[73] | 130 | integer,intent(in) :: SIG_NUM |
---|
| 131 | integer :: ONE |
---|
[62] | 132 | |
---|
[71] | 133 | call trap_callback_count_ |
---|
[63] | 134 | ONE = 1 |
---|
[71] | 135 | end function |
---|
[64] | 136 | |
---|
| 137 | !--------------------------------------------------------------! |
---|
[71] | 138 | end module |
---|
[64] | 139 | !--------------------------------------------------------------! |
---|