https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101824

            Bug ID: 101824
           Summary: VOLATILE not honored
           Product: gcc
           Version: 11.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: fortran
          Assignee: unassigned at gcc dot gnu.org
          Reporter: martin.diehl at kuleuven dot be
  Target Milestone: ---

I have a wrapper to C to control a Fortran program with IPC signals:


/* c_interface.c */
#include <signal.h>

void signalterm_c(void (*handler)(int)){
  signal(SIGTERM, handler);
}

void signalusr1_c(void (*handler)(int)){
  signal(SIGUSR1, handler);
}



! c_interface.f90
module c_interface

  implicit none
  interface

  subroutine signalterm_C(handler) bind(C)
    use, intrinsic :: ISO_C_Binding, only: C_FUNPTR

    type(C_FUNPTR), intent(in), value :: handler
  end subroutine signalterm_C

  subroutine signalusr1_C(handler) bind(C)
    use, intrinsic :: ISO_C_Binding, only: C_FUNPTR

    type(C_FUNPTR), intent(in), value :: handler
  end subroutine signalusr1_C

  end interface

end module c_interface


! wait_for_SIGTERM.f90
program wait_for_SIGTERM
  use, intrinsic :: ISO_C_binding

  use c_interface

  implicit none
  logical, volatile :: &
    interface_SIGTERM, &  ! termination signal
    interface_SIGUSR1     ! 1. user-defined signal

  call init

  do while( .not. interface_SIGTERM)


  enddo

contains

subroutine init()

  call signalterm_c(c_funloc(catchSIGTERM))
  call signalusr1_c(c_funloc(catchSIGUSR1))
  call interface_setSIGTERM(.false.)
  call interface_setSIGUSR1(.false.)

end subroutine init


subroutine catchSIGTERM(signal) bind(C)

  integer(C_INT), value :: signal


  print'(a,i0)', ' received signal ',signal
  call interface_setSIGTERM(.not. interface_SIGTERM)

end subroutine catchSIGTERM


subroutine catchSIGUSR1(signal) bind(C)

  integer(C_INT), value :: signal


  print'(a,i0)', ' received signal ',signal
  call interface_setSIGUSR1(.not. interface_SIGUSR1)

end subroutine catchSIGUSR1


subroutine interface_setSIGTERM(state)

  logical, intent(in) :: state


  interface_SIGTERM = state
  print*, 'set SIGTERM to',state

end subroutine interface_setSIGTERM


subroutine interface_setSIGUSR1(state)

  logical, intent(in) :: state


  interface_SIGUSR1 = state
  print*, 'set SIGUSR1 to',state

end subroutine interface_setSIGUSR1

end program



Compile and run (tested on Linux):

gfortran c_interface.c c_interface.f90 wait_for_SIGTERM.f90 -o wait
./wait&
kill -SIGUSR1 XXXX
kill -SIGUSR1 XXXX
kill -SIGTERM XXXX

If compiled without optimization flag, the loop exits on SIGTERM (indented
behavior). If compiled with `-O3`, it runs forever. This behavior is
independent of the VOLATILE attribute.

There is a little discussion on
https://fortran-lang.discourse.group/t/volatile-needed/1648

Reply via email to