source: trunk/signal-checkpoint/signal_checkpoint.F90 @ 90

Last change on this file since 90 was 90, checked in by g7moreau, 12 years ago
  • Add support for XLF compiler
  • Property svn:keywords set to Id
File size: 3.5 KB
Line 
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 90 2012-10-10 15:53:41Z g7moreau $
9!--------------------------------------------------------------!
10
11module Signal_Checkpoint
12
13#ifdef __INTEL_COMPILER
14use IFPORT, only: signal
15#endif
16
17implicit none
18save
19private
20
21! Signal list
22integer,parameter :: SIGHUP  =  1  ! Signal HUP
23integer,parameter :: SIGINT  =  2  ! Signal INT
24integer,parameter :: SIGQUIT =  3  ! Signal QUIT
25integer,parameter :: SIGUSR1 = 10  ! Signal USR1
26integer,parameter :: SIGUSR2 = 12  ! Signal USR2
27integer,parameter :: SIGTERM = 15  ! Signal TERM
28
29! Internal counter
30integer :: SIGNAL_RECEIVED_COUNT_ = 0       ! Global signal counter
31logical :: CODE_ERROR_ON_EXIT_    = .false. ! Global return state
32
33! Public interface
34public :: SIGHUP
35public :: SIGINT
36public :: SIGQUIT
37public :: SIGUSR1
38public :: SIGUSR2
39public :: SIGTERM
40public :: signal_checkpoint_connect
41public :: signal_checkpoint_is_received
42public :: signal_checkpoint_received_times
43public :: signal_checkpoint_ask_for_exit_code
44
45!--------------------------------------------------------------!
46contains
47!--------------------------------------------------------------!
48
49subroutine signal_checkpoint_connect (SIG_NUM, EXIT)
50   integer,intent(in)          :: SIG_NUM
51   logical,intent(in),optional :: EXIT
52
53#ifdef __INTEL_COMPILER
54   integer :: ERR
55
56   if (present(EXIT)) then
57      ERR = signal(SIG_NUM, trap_callback_intel_exit_, -1)
58   else
59      ERR = signal(SIG_NUM, trap_callback_intel_count_, -1)
60   end if
61#endif
62
63#if defined (__GNUC__) || defined (XLF)
64   intrinsic signal
65
66   if (present(EXIT)) then
67      call signal(SIG_NUM, trap_callback_exit_)
68   else
69      call signal(SIG_NUM, trap_callback_count_)
70   end if
71#endif
72
73end subroutine
74
75!--------------------------------------------------------------!
76
77function signal_checkpoint_is_received () result (IS_RECEIVED)
78   logical :: IS_RECEIVED
79
80   IS_RECEIVED = ( SIGNAL_RECEIVED_COUNT_ > 0 )
81end function
82
83!--------------------------------------------------------------!
84
85function signal_checkpoint_received_times () result (RECEIVED_TIMES)
86   integer :: RECEIVED_TIMES
87
88   RECEIVED_TIMES = SIGNAL_RECEIVED_COUNT_
89end function
90
91!--------------------------------------------------------------!
92
93function signal_checkpoint_ask_for_exit_code () result (EXIT)
94   logical :: EXIT
95   
96   EXIT = CODE_ERROR_ON_EXIT_
97end function
98
99!--------------------------------------------------------------!
100!--------------------------------------------------------------!
101
102subroutine trap_callback_count_
103
104   SIGNAL_RECEIVED_COUNT_ = SIGNAL_RECEIVED_COUNT_ + 1
105end subroutine
106
107!--------------------------------------------------------------!
108
109subroutine trap_callback_exit_
110
111   CODE_ERROR_ON_EXIT_ = .true.
112   call trap_callback_count_
113end subroutine
114
115!--------------------------------------------------------------!
116
117function trap_callback_intel_exit_ (SIG_NUM) result (ONE)
118   integer,intent(in) :: SIG_NUM
119   integer            :: ONE
120
121   call trap_callback_exit_
122   ONE = 1
123end function
124
125!--------------------------------------------------------------!
126
127function trap_callback_intel_count_ (SIG_NUM) result (ONE)
128   integer,intent(in) :: SIG_NUM
129   integer            :: ONE
130
131   call trap_callback_count_
132   ONE = 1
133end function
134
135!--------------------------------------------------------------!
136end module
137!--------------------------------------------------------------!
Note: See TracBrowser for help on using the repository browser.