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