singrdk/base/Applications/Benchmarks/SpecWeb99/webfiles/urlcache.cs

99 lines
3.4 KiB
C#
Raw Permalink Normal View History

2008-11-17 18:29:00 -05:00
// ----------------------------------------------------------------------------
2008-03-05 09:52:00 -05:00
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
2008-11-17 18:29:00 -05:00
// ----------------------------------------------------------------------------
2008-03-05 09:52:00 -05:00
using System;
using System.Collections;
using System.Text;
namespace Singularity.Application.SPECWeb99.WebFiles
{
2008-11-17 18:29:00 -05:00
class Cache
{
2008-03-05 09:52:00 -05:00
private const bool verbose = false;
private UrlSet cache;
private UrlSet free;
private int size;
public Cache(int cacheSize, int deckSize)
{
cache = new UrlSet(cacheSize);
free = new UrlSet(deckSize);
this.size = cacheSize;
}
public FileSpec GetUrl(Client/*!*/ client, int id) {
// first check the local cache
// then see if anyone else is generating the global
if (cache.pos > 0) {
return cache.urls[--cache.pos];
}
// return local free to global free pool
client.globalLock.WaitOne();
while (free.pos > 0) {
if (client.freeDeck.pos >= client.freeDeck.urls.Length) {
//Console.WriteLine(" too many free slots!");
free.urls[--free.pos] = null;
//DebugStub.Break();
free.pos--;
}
else {
client.freeDeck.urls[client.freeDeck.pos++] =
free.urls[--free.pos];
}
}
// now see if there are any urls available
// if not either wait on or generate the global deck
2008-11-17 18:29:00 -05:00
while (client.urlDeck.pos == 0) {
2008-03-05 09:52:00 -05:00
if (client.generatingUrls == false) {
//Console.WriteLine("Generating cache");
UrlSet temp = client.urlDeck;
client.urlDeck = client.spareDeck;
client.spareDeck = temp;
client.generatingUrls = true;
client.syncLock.WaitOne();
client.globalLock.ReleaseMutex();
client.GenerateFileSpecs();
client.globalLock.WaitOne();
client.generatingUrls = false;
client.syncLock.ReleaseMutex();
}
else {
//Console.WriteLine("Waiting for cache");
client.globalLock.ReleaseMutex();
client.syncLock.WaitOne();
client.syncLock.ReleaseMutex();
client.globalLock.WaitOne();
}
}
// the global deck is ok -- prime our cache
2008-11-17 18:29:00 -05:00
if (client.urlDeck.pos > 0) {
for (int i = 0; i < cache.urls.Length && (client.urlDeck.pos > 0); i++) {
2008-03-05 09:52:00 -05:00
cache.urls[cache.pos++] =
client.urlDeck.urls[--client.urlDeck.pos];
}
}
client.globalLock.ReleaseMutex();
return cache.urls[--cache.pos];
}
public void FreeUrl(FileSpec url) {
2008-11-17 18:29:00 -05:00
if (free.pos + 1 >= free.urls.Length) {
2008-03-05 09:52:00 -05:00
//Console.WriteLine("free cache full! de-allocating");
url = null;
return;
}
free.urls[free.pos++] = url;
}
}
}