Skip to content

Commit

Permalink
[Queue] Implement SPMC Queue
Browse files Browse the repository at this point in the history
  • Loading branch information
unkn0wn107 committed Jul 23, 2024
1 parent 6810629 commit f6e4a7a
Show file tree
Hide file tree
Showing 16 changed files with 127 additions and 181 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# By: agaley <[email protected]> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2023/12/15 15:51:13 by agaley #+# #+# #
# Updated: 2024/07/22 18:45:13 by agaley ### ########lyon.fr #
# Updated: 2024/07/22 23:16:54 by agaley ### ########lyon.fr #
# #
# **************************************************************************** #

Expand Down Expand Up @@ -35,7 +35,7 @@ SRC = $(SRC_DIR)/Server.cpp \
$(SRC_DIR)/Config.cpp $(SRC_DIR)/ConfigManager.cpp $(SRC_DIR)/ConfigParser.cpp \
$(SRC_DIR)/FileManager.cpp \
$(SRC_DIR)/ConnectionHandler.cpp $(SRC_DIR)/CacheHandler.cpp \
$(SRC_DIR)/Worker.cpp $(SRC_DIR)/EventQueue.cpp $(SRC_DIR)/EventData.cpp \
$(SRC_DIR)/Worker.cpp $(SRC_DIR)/EventData.cpp \
$(SRC_DIR)/HTTPRequest.cpp $(SRC_DIR)/HTTPResponse.cpp $(SRC_DIR)/URI.cpp \
$(SRC_DIR)/CGIHandler.cpp $(SRC_DIR)/VirtualServer.cpp \
$(SRC_DIR)/Exception.cpp $(SRC_DIR)/HTTPMethods.cpp \
Expand Down
9 changes: 4 additions & 5 deletions src/CGIHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* CGIHandler.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mchenava < mchenava@student.42lyon.fr> +#+ +:+ +#+ */
/* By: agaley <agaley@student.42lyon.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/30 16:11:05 by agaley #+# #+# */
/* Updated: 2024/07/23 01:08:08 by mchenava ### ########.fr */
/* Updated: 2024/07/23 02:12:47 by agaley ### ########lyon.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -44,10 +44,9 @@ CGIHandler::CGIHandler(HTTPRequest& request,
CGIHandler::~CGIHandler() {
kill(_pid, SIGKILL);
waitpid(_pid, NULL, 0);
if (_inpipefd[0] != -1)
{
if (_outpipefd[0] != -1) {
delEvent();
close(_inpipefd[0]);
close(_outpipefd[0]);
}
if (_inpipefd[1] != -1)
close(_inpipefd[1]);
Expand Down
8 changes: 4 additions & 4 deletions src/CacheHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: agaley <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/24 23:54:38 by agaley #+# #+# */
/* Updated: 2024/07/18 13:46:07 by agaley ### ########lyon.fr */
/* Updated: 2024/07/22 23:21:10 by agaley ### ########lyon.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -39,7 +39,7 @@ CacheHandler::CacheEntry::~CacheEntry() {
delete response;
}

CacheHandler& CacheHandler::init(EventQueue& eventQueue) {
CacheHandler& CacheHandler::init(SPMCQueue<struct epoll_event>& eventQueue) {
_instance = new CacheHandler(eventQueue);
return *_instance;
}
Expand All @@ -53,7 +53,7 @@ void CacheHandler::deleteInstance() {
_instance = NULL;
}

CacheHandler::CacheHandler(EventQueue& eventQueue)
CacheHandler::CacheHandler(SPMCQueue<struct epoll_event>& eventQueue)
: _log(Logger::getInstance()), _eventQueue(eventQueue), _cache(), _maxAge(MAX_AGE) {
pthread_mutex_init(&_mutex, NULL);
}
Expand Down Expand Up @@ -113,7 +113,7 @@ void CacheHandler::storeResponse(const std::string& key, const HTTPResponse& res
struct epoll_event event;
event.data.ptr = *it;
event.events = EPOLLIN | EPOLLET | EPOLLONESHOT;
_eventQueue.push(event);
_eventQueue.enqueue(event);
}
entry.waitingEventsData.clear();
pthread_mutex_unlock(&_mutex);
Expand Down
20 changes: 10 additions & 10 deletions src/CacheHandler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: agaley <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/24 23:54:58 by agaley #+# #+# */
/* Updated: 2024/07/18 13:46:13 by agaley ### ########lyon.fr */
/* Updated: 2024/07/22 23:20:59 by agaley ### ########lyon.fr */
/* */
/* ************************************************************************** */

Expand All @@ -21,17 +21,17 @@

#include "Common.hpp"
#include "EventData.hpp"
#include "EventQueue.hpp"
#include "HTTPRequest.hpp"
#include "HTTPResponse.hpp"
#include "SPMCQueue.hpp"

struct EventData;

class CacheHandler {
public:
static const time_t MAX_AGE;

static CacheHandler& init(EventQueue& eventQueue);
static CacheHandler& init(SPMCQueue<struct epoll_event>& eventQueue);
static CacheHandler& getInstance();
static void deleteInstance();

Expand All @@ -54,19 +54,19 @@ class CacheHandler {
void deleteCache(const std::string& key);

private:
CacheHandler(EventQueue& eventQueue);
CacheHandler(SPMCQueue<struct epoll_event>& eventQueue);
~CacheHandler();
CacheHandler(const CacheHandler&);
CacheHandler& operator=(const CacheHandler&);

typedef std::map<std::string, CacheEntry> CacheMap;

static CacheHandler* _instance;
Logger& _log;
EventQueue& _eventQueue;
CacheMap _cache;
time_t _maxAge;
pthread_mutex_t _mutex;
static CacheHandler* _instance;
Logger& _log;
SPMCQueue<struct epoll_event>& _eventQueue;
CacheMap _cache;
time_t _maxAge;
pthread_mutex_t _mutex;

unsigned long _hash(const std::string& str) const;
};
Expand Down
4 changes: 2 additions & 2 deletions src/ConfigManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ class ConfigManager {
* Retrieve all config.
* @return The configuration object.
*/
Config& getConfig();
static void printConfig();
Config& getConfig();
static void printConfig();

static void loadConfig(const std::string& filepath);

Expand Down
15 changes: 7 additions & 8 deletions src/ConnectionHandler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* ConnectionHandler.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mchenava < mchenava@student.42lyon.fr> +#+ +:+ +#+ */
/* By: agaley <agaley@student.42lyon.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/30 16:11:25 by agaley #+# #+# */
/* Updated: 2024/07/23 02:07:00 by mchenava ### ########.fr */
/* Updated: 2024/07/23 02:24:08 by agaley ### ########lyon.fr */
/* */
/* ************************************************************************** */

Expand All @@ -23,21 +23,21 @@
#include "CacheHandler.hpp"
#include "Common.hpp"
#include "Config.hpp"
#include "EventQueue.hpp"
#include "HTTPRequest.hpp"
#include "HTTPResponse.hpp"
#include "Logger.hpp"
#include "SPMCQueue.hpp"
#include "VirtualServer.hpp"

class VirtualServer;
class CGIHandler;

class ConnectionHandler {
public:
ConnectionStatus getConnectionStatus() const;
std::string getStatusString() const;
int processConnection(struct epoll_event& event);
void setInternalServerError();
ConnectionStatus getConnectionStatus() const;
std::string getStatusString() const;
int processConnection(struct epoll_event& event);
void setInternalServerError();

ConnectionHandler(int clientSocket,
int epollSocket,
Expand Down Expand Up @@ -88,7 +88,6 @@ class ConnectionHandler {
CGIHandler* _cgiHandler;
CGIState _cgiState;
int _step;
pthread_mutex_t _requestTimesMutex;

void _receiveRequest(struct epoll_event& event);
void _processRequest(struct epoll_event& event);
Expand Down
64 changes: 0 additions & 64 deletions src/EventQueue.cpp

This file was deleted.

35 changes: 0 additions & 35 deletions src/EventQueue.hpp

This file was deleted.

75 changes: 75 additions & 0 deletions src/SPMCQueue.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* SPMCQueue.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: agaley <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/18 02:48:04 by agaley #+# #+# */
/* Updated: 2024/07/22 23:16:34 by agaley ### ########lyon.fr */
/* */
/* ************************************************************************** */

#ifndef SPSCQUEUE_H
#define SPSCQUEUE_H

template <typename T>
class SPMCQueue {
private:
struct Node {
Node* next;
T data;
Node() : next(NULL) {}
};

Node* _head;
Node* _tail;
pthread_mutex_t _head_mutex;
pthread_mutex_t _tail_mutex;

public:
SPMCQueue() : _head(new Node()), _tail(_head) {
pthread_mutex_init(&_head_mutex, NULL);
pthread_mutex_init(&_tail_mutex, NULL);
}

~SPMCQueue() {
while (Node* old_head = _head) {
_head = _head->next;
delete old_head;
}
pthread_mutex_destroy(&_head_mutex);
pthread_mutex_destroy(&_tail_mutex);
}

void enqueue(const T& data) {
Node* n = new Node();
n->data = data;
n->next = NULL;

pthread_mutex_lock(&_head_mutex);
_head->next = n;
_head = n;
pthread_mutex_unlock(&_head_mutex);
}

bool dequeue(T& result) {
pthread_mutex_lock(&_tail_mutex);
Node* old_tail = _tail;
Node* new_tail = old_tail->next;

if (new_tail == NULL) {
pthread_mutex_unlock(&_tail_mutex);
return false;
}

result = new_tail->data;
_tail = new_tail;
pthread_mutex_unlock(&_tail_mutex);

delete old_tail;
return true;
}
};

#endif
Loading

0 comments on commit f6e4a7a

Please sign in to comment.