2016-07-10 12:54:39 +00:00
|
|
|
//
|
|
|
|
// Disk.cpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 10/07/2016.
|
|
|
|
// Copyright © 2016 Thomas Harte. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "Disk.hpp"
|
2016-11-26 06:27:06 +00:00
|
|
|
|
|
|
|
using namespace Storage::Disk;
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
int Disk::get_id_for_track_at_position(unsigned int head, unsigned int position) {
|
2016-12-25 03:11:31 +00:00
|
|
|
return (int)(position * get_head_count() + head);
|
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
void Disk::set_track_at_position(unsigned int head, unsigned int position, const std::shared_ptr<Track> &track) {
|
2016-12-26 00:19:22 +00:00
|
|
|
if(get_is_read_only()) return;
|
2016-12-25 03:11:31 +00:00
|
|
|
|
|
|
|
int address = get_id_for_track_at_position(head, position);
|
|
|
|
cached_tracks_[address] = track;
|
2016-12-30 03:15:58 +00:00
|
|
|
|
|
|
|
if(!update_queue_) update_queue_.reset(new Concurrency::AsyncTaskQueue);
|
2016-12-30 22:26:44 +00:00
|
|
|
std::shared_ptr<Track> track_copy(track->clone());
|
|
|
|
update_queue_->enqueue([this, head, position, track_copy] {
|
|
|
|
store_updated_track_at_position(head, position, track_copy, file_access_mutex_);
|
2016-12-30 03:15:58 +00:00
|
|
|
});
|
2016-12-25 03:11:31 +00:00
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
std::shared_ptr<Track> Disk::get_track_at_position(unsigned int head, unsigned int position) {
|
2016-12-25 03:11:31 +00:00
|
|
|
int address = get_id_for_track_at_position(head, position);
|
2016-11-26 06:27:06 +00:00
|
|
|
std::map<int, std::shared_ptr<Track>>::iterator cached_track = cached_tracks_.find(address);
|
|
|
|
if(cached_track != cached_tracks_.end()) return cached_track->second;
|
|
|
|
|
2016-12-30 03:15:58 +00:00
|
|
|
std::lock_guard<std::mutex> lock_guard(file_access_mutex_);
|
2016-11-26 15:35:11 +00:00
|
|
|
std::shared_ptr<Track> track = get_uncached_track_at_position(head, position);
|
2016-11-26 06:27:06 +00:00
|
|
|
cached_tracks_[address] = track;
|
|
|
|
return track;
|
|
|
|
}
|
2016-12-25 03:11:31 +00:00
|
|
|
|
2016-12-30 03:15:58 +00:00
|
|
|
void Disk::store_updated_track_at_position(unsigned int head, unsigned int position, const std::shared_ptr<Track> &track, std::mutex &file_access_mutex) {}
|
2016-12-25 03:11:31 +00:00
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
void Disk::flush_updates() {
|
2016-12-30 03:15:58 +00:00
|
|
|
if(update_queue_) update_queue_->flush();
|
2016-12-25 03:11:31 +00:00
|
|
|
}
|