<commit_before>import Foundation
import Runes

@objc class ImageFetcher {
    let imageCache: Cache<UIImage>
    let purger: CachePurger
    let operationQueue: AsyncQueue
    let lockQueue = dispatch_queue_create("com.thoughtbot.pop-n-lock", nil)
    var inProgress: [String] = []

    init() {
        imageCache = Cache<UIImage>()
        purger = CachePurger(cache: imageCache)
        operationQueue = AsyncQueue(name: "PoppinsCacheQueue", maxOperations: NSOperationQueueDefaultMaxConcurrentOperationCount)
        NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("didReceiveMemoryWarning"), name: UIApplicationDidReceiveMemoryWarningNotification, object: .None)
    }

    deinit {
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }

    func didReceiveMemoryWarning() {
        inProgress = []
    }

    func fetchImage(size: CGSize, path: String) -> UIImage? {
        if let image = imageCache.itemForKey(path) {
            return image
        } else {
            if size == CGSizeZero { return .None }
            dispatch_async(lockQueue) {
                if contains(self.inProgress, path) { return }
                self.inProgress.append(path)

                let operation = ImageFetcherOperation(path: path, size: size) { image in
                    dispatch_async(self.lockQueue) {
                        curry(self.imageCache.setItem) <^> image <*> path
                        self.inProgress.removeAtIndex <^> find(self.inProgress, path)
                        NSNotificationCenter.defaultCenter().postNotificationName("CacheDidUpdate", object: .None)
                    }
                }
                self.operationQueue.addOperation(operation)
            }
            return .None
        }
    }
}
<commit_msg>Synchronize access to inProgress<commit_after>import Foundation
import Runes

@objc class ImageFetcher {
    let imageCache: Cache<UIImage>
    let purger: CachePurger
    let operationQueue: AsyncQueue
    var inProgress: [String] = []

    init() {
        imageCache = Cache<UIImage>()
        purger = CachePurger(cache: imageCache)
        operationQueue = AsyncQueue(name: "PoppinsCacheQueue", maxOperations: NSOperationQueueDefaultMaxConcurrentOperationCount)
        NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("didReceiveMemoryWarning"), name: UIApplicationDidReceiveMemoryWarningNotification, object: .None)
    }

    deinit {
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }

    func didReceiveMemoryWarning() {
        inProgress = []
    }

    func fetchImage(size: CGSize, path: String) -> UIImage? {
        if let image = imageCache.itemForKey(path) {
            return image
        } else {
            if size == CGSizeZero { return .None }

            dispatch_to_main {
                objc_sync_enter(self.inProgress)
                if contains(self.inProgress, path) { return }
                self.inProgress.append(path)
                objc_sync_exit(self.inProgress)
                
                let operation = ImageFetcherOperation(path: path, size: size) { image in
                    curry(self.imageCache.setItem) <^> image <*> path
                    
                    dispatch_to_main {
                        objc_sync_enter(self.inProgress)
                        self.inProgress.removeAtIndex <^> find(self.inProgress, path)
                        objc_sync_exit(self.inProgress)
                        NSNotificationCenter.defaultCenter().postNotificationName("CacheDidUpdate", object: .None)
                    }
                }
                
                self.operationQueue.addOperation(operation)
            }
            return .None
        }
    }
}
{'fraction_non_alphanumeric': 0.08669141039236479, 'fraction_numerical': 0.0, 'mean_word_length': 2.128524046434494, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4206999', 'n_tokens_mistral': 997, 'n_tokens_neox': 950, 'n_words': 185}