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