--
--	occampi.occ -- occam-pi related support routines (partially KRoC specific)
--	Copyright (C) 2004-2006 Fred Barnes <frmb@kent.ac.uk>
--
--	This program is free software; you can redistribute it and/or modify
--	it under the terms of the GNU General Public License as published by
--	the Free Software Foundation; either version 2 of the License, or
--	(at your option) any later version.
--
--	This program is distributed in the hope that it will be useful,
--	but WITHOUT ANY WARRANTY; without even the implied warranty of
--	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
--	GNU General Public License for more details.
--
--	You should have received a copy of the GNU General Public License
--	along with this program; if not, write to the Free Software
--	Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
--

--** @module forall

--{{{  options and comments
#OPTION "E V"
#COMMENT "occam-pi support routines, Copyright (C) 2004-2005 Fred Barnes"
--}}}

#IF DEFINED(OCCBUILD.KROC)

#PRAGMA TRANSLATE MPBARSYNC "MPBARSYNC%O"
#PRAGMA TRANSLATE MPBARRESIGN "MPBARRESIGN%O"

-- these are entry-points in the run-time system (ccsp/common/mobproc.c)
#PRAGMA EXTERNAL "PROC C.do.mpp.serialise (MOBILE.PROC p, RESULT MOBILE []BYTE r) = 0"
#PRAGMA EXTERNAL "PROC C.do.mpp.deserialise (MOBILE []BYTE r, RESULT MOBILE.PROC p) = 0"
#PRAGMA EXTERNAL "PROC C.do.mpp.checkroutine (VAL []BYTE n, RESULT BOOL a) = 0"
#PRAGMA EXTERNAL "PROC C.do.mpp.loadlibrary (VAL []BYTE l, RESULT BOOL r) = 0"
#PRAGMA EXTERNAL "PROC C.do.mpp.unloadlibrary (VAL []BYTE l, RESULT BOOL r) = 0"

--{{{  MPBARSYNC ()
--
--  used to synchronise on the MOBILE process barrier
--
PROC MPBARSYNC () SUSPEND
  INT saved:
  ASM
    -- save return-address
    LDL 0
    ST saved

    RESERVELOWWS 4          -- damaged ..?

    LDL .MPPTR
    CJ :doerr
    J :skip
    :doerr
    SETERR
    :skip

    -- decrement BCNT
    LDL .MPPTR
    LDNL ##MPP.BARRIER
    WSMAP -1, 15        -- code pointer
    WSMAP -2, 5         -- general pointer
    CODEMAP
    MT.SYNC

    -- are we the last process ?
    LDL 0
    CJ :out
    THROWAWAY

    -- yes; switch back to activator
    :wakeup
    LDL .MPPTR
    DUP
    DUP
    LDNL ##MPP.WPTR
    GAJW
    REV
    STNL ##MPP.WPTR
    STL ##MPA.TEMP.VS
    LDLABELP :cont
    LDL ##MPA.TEMP.VS
    STNL ##MPP.IPTR
    LDL ##MPA.TEMP.VS
    LDNL ##MPP.AIPTR
    GCALL
    THROWAWAY

    -- re-activated, reset barrier and reschedule processes
    CODEMAP
    :cont
    LDL 0
    LDL .MPPTR
    STNL ##MPP.AIPTR

    LDL .MPPTR
    LDNL ##MPP.BARRIER
    MT.SYNC

    -- restore and return
    :out
    LD saved
    STL 0
:
--}}}
--{{{  PROC MPBARRESIGN () SUSPEND
--
--  used to resign a parallel process from a MOBILE process barrier
--
PROC MPBARRESIGN () SUSPEND
  INT saved:
  ASM
    -- save return-address
    LDL 0
    ST saved

    RESERVELOWWS 4          -- damaged ...?

    LDL .MPPTR
    DUP
    CJ :skip
    LDNL ##MPP.BARRIER
    LDC 1
    MT.RESIGN

    -- restore and return
    :skip
    LD saved
    STL 0
:
--}}}

--{{{  PROC MPP.SERIALISE (MOBILE.PROC p, RESULT MOBILE []BYTE r)
--
--  used to serialise a mobile process
--
PROC MPP.SERIALISE (MOBILE.PROC p, RESULT MOBILE []BYTE r)
  SEQ
    C.do.mpp.serialise (p, r)
:
--}}}
--{{{  PROC MPP.DESERIALISE (MOBILE []BYTE r, RESULT MOBILE.PROC p)
--
--  used to de-serialise a mobile process
--
PROC MPP.DESERIALISE (MOBILE []BYTE r, RESULT MOBILE.PROC p)
  SEQ
    C.do.mpp.deserialise (r, p)
:
--}}}
--{{{  PROC MPP.CHECKROUTINE (VAL []BYTE n, RESULT BOOL a)
--
--  this is used to find out whether the code for a mobile process is
--  available.
--
PROC MPP.CHECKROUTINE (VAL []BYTE n, RESULT BOOL a)
  SEQ
    C.do.mpp.checkroutine (n, a)
:
--}}}
--{{{  PROC MPP.LOADLIBRARY (VAL []BYTE l, RESULT BOOL r)
--
--  this is used to load code at run-time; KRoC run-time interprets
--  the string to get at routines
--
PROC MPP.LOADLIBRARY (VAL []BYTE l, RESULT BOOL r)
  SEQ
    C.do.mpp.loadlibrary (l, r)
:
--}}}
--{{{  PROC MPP.UNLOADLIBRARY (VAL []BYTE l, RESULT BOOL r)
--
--  this is used to unload code at run-time
--
PROC MPP.UNLOADLIBRARY (VAL []BYTE l, RESULT BOOL r)
  SEQ
    C.do.mpp.unloadlibrary (l, r)
:
--}}}

#ENDIF

