Skip to content

Instantly share code, notes, and snippets.

@kkirsanov
Last active October 19, 2018 11:41
Show Gist options
  • Save kkirsanov/b4f7001ef2041eebdca38e05893ab07c to your computer and use it in GitHub Desktop.
Save kkirsanov/b4f7001ef2041eebdca38e05893ab07c to your computer and use it in GitHub Desktop.
--{-# OPTIONS_GHC -Wall -Werror #-}
module Scheduler
( someFunc,
) where
import Data.Maybe (isJust, isNothing)
import qualified Data.Map as M
data SchedulerState = SchedulerState
{ workers :: Workers
, tasks :: Tasks
, timer :: Double
, tidCounter :: TID
} deriving (Show)
data Task = Task
{ taskClass :: TaskClass
, tid :: TID
, taskState :: TaskState
, taskStart :: Double
, taskEnd :: Maybe Double
} deriving (Show)
type TID = Integer
instance Eq Task where
a == b = tid a == tid b
data TaskState
= TaskReady
| TaskWorking
| TaskEnd
| TaskFail {failCounter::Int}
deriving (Show, Eq)
newtype TaskClass = TaskClass String
deriving (Show, Eq, Ord)
newtype Worker = Worker
{ workerTask :: Maybe Task
} deriving (Show)
type PID = Integer
type TimeStep = Double
data Workers = Workers
{ readyWorkers :: M.Map PID Worker
, busyWorkers :: M.Map PID Worker
}
deriving (Show)
data Tasks = Tasks
{ readyTasks :: M.Map TID Task
, workingTasks :: M.Map TID Task
, endTasks :: M.Map TID Task
, failTasks :: M.Map TID Task
}
deriving (Show)
emptyTasks::Tasks
emptyTasks = Tasks e e e e
where
e = M.empty
emptyWorkers::Workers
emptyWorkers = Workers e e
where
e = M.empty
addTask::TaskClass->SchedulerState->SchedulerState
addTask cl ss = SchedulerState (workers ss) new_tasks (timer ss) new_tid
where
new_tid = 1 + tidCounter ss
ts = tasks ss
mkTask::TaskClass->Task
mkTask cl = Task cl new_tid TaskReady (timer ss) Nothing
new_tasks = Tasks
{ readyTasks = M.insert new_tid (mkTask cl) $ readyTasks ts
, workingTasks = workingTasks ts
, endTasks = endTasks ts
, failTasks = failTasks ts
}
initScheduler::Integer->SchedulerState
initScheduler worker_count = SchedulerState workers emptyTasks 0 0
where
workers = Workers
{ readyWorkers = M.fromList $ zip [0..] $ repeat $ Worker Nothing
, busyWorkers = M.empty
}
{-
scheduleFCFS::SchedulerState->SchedulerState
scheduleFCFS s = SchedulerState new_workers new_tasks new_timer new_counter
where
new_timer = 0.01 + timer s
new_counter = tidCounter s
--}
blankScheduler = initScheduler 10
scheduler = addTask (TaskClass "test1") blankScheduler
someFunc :: IO ()
someFunc = print scheduler
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment