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