!***********************************************************************
! This file is part of OpenMolcas.                                     *
!                                                                      *
! OpenMolcas is free software; you can redistribute it and/or modify   *
! it under the terms of the GNU Lesser General Public License, v. 2.1. *
! OpenMolcas is distributed in the hope that it will be useful, but it *
! is provided "as is" and without any express or implied warranties.   *
! For more details see the full text of the license in the file        *
! LICENSE or in <http://www.gnu.org/licenses/>.                        *
!***********************************************************************
!***********************************************************************
! Integer Function RsvTsk(igaTsk,iTskLs,nTsk,iStart)                   *
!  -> reserve a task for my node and mark it on the global task list   *
!     as reserved.                                                     *
!     igaTsk:   global array handle to global task list                *
!     iTskLs:   my private task list (my favourite sequence of tasks)  *
!     nTsk:     # of tasks                                             *
!     iStart:   starting value of index of private task list           *
!***********************************************************************

function RsvTsk(igaTsk,iTskLs,nTsk,mTsk,iStart,iS,iE)

#ifdef _MOLCAS_MPP_
#ifdef _GA_
use GA_Wrapper, only: GA_Read_Inc
#else
use stdalloc, only: mma_allocate, mma_deallocate
#endif
#endif
use Definitions, only: iwp

implicit none
integer(kind=iwp) :: RsvTsk
integer(kind=iwp), intent(in) :: igaTsk, nTsk, mTsk
integer(kind=iwp), intent(inout) :: iTskLs(nTsk,2), iStart, iS, iE
#ifdef _MOLCAS_MPP_
integer(kind=iwp) :: iCnt, iTsk
logical(kind=iwp) :: Reserved
#ifndef _GA_
integer(kind=iwp), allocatable :: TSKR(:)
#endif

if (iStart > mTsk) then
  iTsk = 0
  iCnt = nTsk
else
# ifndef _GA_
  call mma_allocate(TSKR,mTsk,Label='TSKR')
  call ga_readb_inc(igaTsk,mTsk,iTskLs(iStart,1),TSKR)
# endif
  do iCnt=iStart,mTsk
    iTsk = iTskLs(iCnt,1)
    ! try to reserve iTsk on global task list...
#   ifdef _GA_
    Reserved = ga_read_inc(igaTsk,iTsk,1,1) /= 0
#   else
    Reserved = TSKR(1+iCnt-iStart) /= 0
#   endif
    if (Reserved) then
      iE = iE-1
      iTskLs(iE,2) = iTsk
    else
      iS = iS+1
      iTskLs(iS,2) = iTsk
      exit
    end if
  end do
  if (iCnt > mTsk) iTsk = 0
# ifndef _GA_
  call mma_deallocate(TSKR)
# endif
end if
RsvTsk = iTsk
iStart = iCnt

#else

#include "macros.fh"
unused_var(igaTsk)
unused_var(iTskLs)
unused_var(mTsk)
unused_var(iS)
unused_var(iE)

RsvTsk = 0
iStart = nTsk

#endif

end function RsvTsk
