Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature #1

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 37 additions & 4 deletions ITMO_DZ_4/TaskManager.swift
Original file line number Diff line number Diff line change
@@ -1,15 +1,48 @@
import Foundation

protocol Task {
protocol Task: AnyObject {
var priority: Int { get }

var dependencies: [Task] { get }
func execute()
func addDependency(_ task: Task)
}

class TaskManager {
class BasicTask: Task {
var priority: Int
var dependencies: [Task] = []
private let action: () -> Void

func add(_ task: Task) -> Void {
init(priority: Int, action: @escaping () -> Void) {
self.priority = priority
self.action = action
}

func execute() {
action()
}

func addDependency(_ task: Task) {
dependencies.append(task)
}
}

class TaskManager {
private var visited: Set<ObjectIdentifier> = []

func add(_ task: Task) {
execute(task)
}

private func execute(_ task: Task) {
let taskId = ObjectIdentifier(task)
guard !visited.contains(taskId) else { return }
visited.insert(taskId)

let sortedDependencies = task.dependencies.sorted { $0.priority > $1.priority }
for dependency in sortedDependencies {
execute(dependency)
}

task.execute()
}
}
56 changes: 51 additions & 5 deletions ITMO_DZ_4/ThreadSafeArray.swift
Original file line number Diff line number Diff line change
@@ -1,21 +1,67 @@
import Foundation

class ThreadSafeArray<T> {
class ThreadSafeArray<T> {
private var array: [T] = []
private var lock = pthread_rwlock_t()
private var attr = pthread_rwlockattr_t()

init() {
pthread_rwlockattr_init(&attr)
pthread_rwlock_init(&lock, &attr)
}

deinit {
pthread_rwlock_destroy(&lock)
pthread_rwlockattr_destroy(&attr)
}

func append(_ item: T) {
pthread_rwlock_wrlock(&lock)
array.append(item)
pthread_rwlock_unlock(&lock)
}

func remove(at index: Int) {
pthread_rwlock_wrlock(&lock)
array.remove(at: index)
pthread_rwlock_unlock(&lock)
}

}

extension ThreadSafeArray: RandomAccessCollection {
typealias Index = Int
typealias Element = T

var startIndex: Index { return array.startIndex }
var endIndex: Index { return array.endIndex }
var startIndex: Index {
pthread_rwlock_rdlock(&lock)
defer { pthread_rwlock_unlock(&lock) }
return array.startIndex
}

subscript(index: Index) -> Element {
get { return array[index] }
var endIndex: Index {
pthread_rwlock_rdlock(&lock)
defer { pthread_rwlock_unlock(&lock) }
return array.endIndex
}

func index(after i: Index) -> Index {
pthread_rwlock_rdlock(&lock)
defer { pthread_rwlock_unlock(&lock) }
return array.index(after: i)
}

subscript(index: Int) -> T {
get {
pthread_rwlock_rdlock(&lock)
defer { pthread_rwlock_unlock(&lock) }
return array[index]
}
set {
pthread_rwlock_wrlock(&lock)
array[index] = newValue
pthread_rwlock_unlock(&lock)
}
}

}
12 changes: 7 additions & 5 deletions ITMO_DZ_4/main.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import Foundation

let ff = ThreadSafeArray<Int>()
var dd = [1]
dd.append(1)
ff.dropFirst()
let manager = TaskManager()
let taskA = BasicTask(priority: 1) { print("Executing Task A") }
let taskB = BasicTask(priority: 2) { print("Executing Task B") }
let taskC = BasicTask(priority: 3) { print("Executing Task C") }

let sd = ff[0]
taskA.addDependency(taskB)
taskA.addDependency(taskC)

manager.add(taskA)