<commit_before>#ifndef _PASSENGER_APPLICATION_POOL_H_
#define _PASSENGER_APPLICATION_POOL_H_

#include <boost/shared_ptr.hpp>
#include <string>
#include <map>

#include "SpawnManager.h"

namespace Passenger {

using namespace std;
using namespace boost;

class ApplicationPool {
public:
	virtual ~ApplicationPool() {};
	
	virtual ApplicationPtr get(const string &appRoot, const string &user = "", const string &group = "") = 0;
};

// TODO: document this
class StandardApplicationPool: public ApplicationPool {
private:
	typedef map<string, ApplicationPtr> ApplicationMap;

	SpawnManager spawnManager;
	ApplicationMap apps;
	
	string normalizePath(const string &path) {
		// TODO
		return path;
	}
	
public:
	StandardApplicationPool(const string &spawnManagerCommand,
	             const string &logFile = "",
	             const string &environment = "production",
	             const string &rubyCommand = "ruby")
	: spawnManager(spawnManagerCommand, logFile, environment, rubyCommand) {}
	
	// TODO: improve algorithm
	// TODO: make thread-safe
	// TODO: make it possible to share an ApplicationPool between processes
	virtual ApplicationPtr get(const string &appRoot, const string &user = "", const string &group = "") {
		string normalizedAppRoot(normalizePath(appRoot));
		//scoped_lock l(lock);
		
		ApplicationPtr app;
		//ApplicationMap::iterator it(apps.find(appRoot));
		//if (it == apps.end()) {
			app = spawnManager.spawn(appRoot, user, group);
		//	apps[appRoot] = app;
		//} else {
		//	app = it->second;
		//}
		return app;
	}
};

typedef shared_ptr<ApplicationPool> ApplicationPoolPtr;

}; // namespace Passenger

#endif /* _PASSENGER_APPLICATION_POOL_H_ */<commit_msg>Make StandardApplicationPool thread-safe, and make it cache spawned applications with a naive algorithm.<commit_after>#ifndef _PASSENGER_APPLICATION_POOL_H_
#define _PASSENGER_APPLICATION_POOL_H_

#include <boost/shared_ptr.hpp>
#include <boost/thread/mutex.hpp>

#include <string>
#include <map>

#include "SpawnManager.h"

namespace Passenger {

using namespace std;
using namespace boost;

class ApplicationPool {
public:
	virtual ~ApplicationPool() {};
	
	virtual ApplicationPtr get(const string &appRoot, const string &user = "", const string &group = "") = 0;
};

// TODO: document this
class StandardApplicationPool: public ApplicationPool {
private:
	typedef map<string, ApplicationPtr> ApplicationMap;

	SpawnManager spawnManager;
	ApplicationMap apps;
	mutex lock;
	bool threadSafe;
	
	string normalizePath(const string &path) {
		// TODO
		return path;
	}
	
public:
	StandardApplicationPool(const string &spawnManagerCommand,
	             const string &logFile = "",
	             const string &environment = "production",
	             const string &rubyCommand = "ruby")
	: spawnManager(spawnManagerCommand, logFile, environment, rubyCommand) {
		threadSafe = false;
	}
	
	void setThreadSafe() {
		threadSafe = true;
	}
	
	// TODO: improve algorithm
	virtual ApplicationPtr get(const string &appRoot, const string &user = "", const string &group = "") {
		string normalizedAppRoot(normalizePath(appRoot));
		ApplicationPtr app;
		mutex::scoped_lock l(lock, threadSafe);
		
		ApplicationMap::iterator it(apps.find(appRoot));
		if (it == apps.end()) {
			app = spawnManager.spawn(appRoot, user, group);
			apps[appRoot] = app;
		} else {
			app = it->second;
		}
		return app;
	}
};

typedef shared_ptr<ApplicationPool> ApplicationPoolPtr;

}; // namespace Passenger

#endif /* _PASSENGER_APPLICATION_POOL_H_ */
{'fraction_non_alphanumeric': 0.10968660968660969, 'fraction_numerical': 0.0005698005698005698, 'mean_word_length': 4.41820987654321, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 16, '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': '5369352', 'n_tokens_mistral': 1116, 'n_tokens_neox': 1032, 'n_words': 282}