1 (* Premptive.mod provides the Processes module with a premptive scheduler.
3 Copyright (C) 2020-2024 Free Software Foundation, Inc.
4 Contributed by Gaius Mulley <gaius.mulley@southwales.ac.uk>.
6 This file is part of GNU Modula-2.
8 GNU Modula-2 is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 GNU Modula-2 is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 Under Section 7 of GPL version 3, you are granted additional
19 permissions described in the GCC Runtime Library Exception, version
20 3.1, as published by the Free Software Foundation.
22 You should have received a copy of the GNU General Public License and
23 a copy of the GCC Runtime Library Exception along with this program;
24 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25 <http://www.gnu.org/licenses/>. *)
27 IMPLEMENTATION MODULE Preemptive
;
29 FROM COROUTINES
IMPORT TurnInterrupts
, PROTECTION
;
30 FROM Processes
IMPORT Wait
, Attach
, Detach
, Create
, ProcessId
, Urgency
, Activate
, SuspendMe
;
31 FROM RTint
IMPORT InitTimeVector
, ReArmTimeVector
, IncludeVector
, ExcludeVector
;
32 FROM libc
IMPORT printf
;
36 (* The space we request becomes part of a stack request, which generally
37 has constraints on size and alignment. *)
38 extraWorkSpace
= 10 * 1024 * 1024 ;
41 timer - the timer process which runs at maximum scheduling priority with
42 interrupts off. It sleeps for a time quantum, performs a Wait
43 which will rotate the ready queue and then Waits again.
50 currentSec
: CARDINAL ;
57 old
:= TurnInterrupts (MAX (PROTECTION
)) ;
58 vec
:= InitTimeVector (timeSliceUsec
, timeSliceSec
, MAX (PROTECTION
)) ;
63 Attach (vec
) ; (* attach vector to this process. *)
66 printf ("include vec\n");
70 currentSec
:= timeSliceSec
;
71 currentUsec
:= timeSliceUsec
;
74 printf ("timer process about to Wait\n");
78 printf ("yes 2 seconds elapsed, suspending\n");
83 printf ("timer process wakes up, now calling ReArmTimeVector\n");
85 ReArmTimeVector (vec
, timeSliceUsec
, timeSliceSec
) ;
88 printf ("ReArmTimeVector complete\n");
91 Attach (vec
) ; (* attach vector to this process. *)
94 printf ("finished attach, now include vec\n");
102 initPreemptive - if millisecs > 0 then turn on preemptive scheduling.
103 if millisecs = 0 then preemptive scheduling is turned off.
106 PROCEDURE initPreemptive (seconds
, microsecs
: CARDINAL) ;
108 timeSliceUsec
:= microsecs
;
109 timeSliceSec
:= seconds
;
113 Create (timer
, extraWorkSpace
, MAX (Urgency
), NIL, timerId
) ;
121 timerId
: ProcessId
;
123 timeSliceUsec
: CARDINAL ;