Disabled external gits
This commit is contained in:
48
cs453-ca/CS453-2021-project/playground/Makefile
Normal file
48
cs453-ca/CS453-2021-project/playground/Makefile
Normal file
@@ -0,0 +1,48 @@
|
||||
BIN := $(notdir $(lastword $(abspath .)))
|
||||
|
||||
EXT_H := h
|
||||
EXT_HPP := h hh hpp hxx h++
|
||||
EXT_C := c
|
||||
EXT_CXX := C cc cpp cxx c++
|
||||
|
||||
INCLUDE_DIR := .
|
||||
SOURCE_DIR := .
|
||||
|
||||
WILD_EXT = $(strip $(foreach EXT,$($(1)),$(wildcard $(2)/*.$(EXT))))
|
||||
|
||||
HDRS_C := $(call WILD_EXT,EXT_H,$(INCLUDE_DIR))
|
||||
HDRS_CXX := $(call WILD_EXT,EXT_HPP,$(INCLUDE_DIR))
|
||||
SRCS_C := $(call WILD_EXT,EXT_C,$(SOURCE_DIR))
|
||||
SRCS_CXX := $(call WILD_EXT,EXT_CXX,$(SOURCE_DIR))
|
||||
OBJS := $(SRCS_C:%=%.o) $(SRCS_CXX:%=%.o)
|
||||
|
||||
CC := $(CC)
|
||||
CCFLAGS := -Wall -Wextra -Wfatal-errors -O2 -std=c11 -fPIC -I$(INCLUDE_DIR)
|
||||
CXX := $(CXX)
|
||||
CXXFLAGS := -Wall -Wextra -Wfatal-errors -O2 -std=c++17 -fPIC -I$(INCLUDE_DIR)
|
||||
LD := $(if $(SRCS_CXX),$(CXX),$(CC))
|
||||
LDFLAGS :=
|
||||
LDLIBS := -lpthread
|
||||
|
||||
.PHONY: build run clean
|
||||
|
||||
build: $(BIN)
|
||||
run: $(BIN)
|
||||
./$(BIN)
|
||||
clean:
|
||||
$(RM) $(OBJS) $(BIN)
|
||||
|
||||
define BUILD_C
|
||||
%.$(1).o: %.$(1) $$(HDRS_C) Makefile
|
||||
$$(CC) $$(CCFLAGS) -c -o $$@ $$<
|
||||
endef
|
||||
$(foreach EXT,$(EXT_C),$(eval $(call BUILD_C,$(EXT))))
|
||||
|
||||
define BUILD_CXX
|
||||
%.$(1).o: %.$(1) $$(HDRS_CXX) Makefile
|
||||
$$(CXX) $$(CXXFLAGS) -c -o $$@ $$<
|
||||
endef
|
||||
$(foreach EXT,$(EXT_CXX),$(eval $(call BUILD_CXX,$(EXT))))
|
||||
|
||||
$(BIN): $(OBJS) Makefile
|
||||
$(LD) $(LDFLAGS) -o $@ $(OBJS) $(LDLIBS)
|
77
cs453-ca/CS453-2021-project/playground/entrypoint.cpp
Normal file
77
cs453-ca/CS453-2021-project/playground/entrypoint.cpp
Normal file
@@ -0,0 +1,77 @@
|
||||
/**
|
||||
* @file entrypoint.cpp
|
||||
* @author Sébastien Rouault <sebastien.rouault@epfl.ch>
|
||||
*
|
||||
* @section LICENSE
|
||||
*
|
||||
* Copyright © 2018-2019 Sébastien Rouault.
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 3 of the License, or
|
||||
* any later version. Please see https://gnu.org/licenses/gpl.html
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* @section DESCRIPTION
|
||||
*
|
||||
* "Entry point" source file, implementing the playground function 'entry_point' and the lock.
|
||||
**/
|
||||
|
||||
// External headers
|
||||
#include <atomic>
|
||||
#include <iostream>
|
||||
#include <mutex>
|
||||
|
||||
// Internal headers
|
||||
#include "entrypoint.hpp"
|
||||
#include "runner.hpp"
|
||||
|
||||
// -------------------------------------------------------------------------- //
|
||||
// Lock implementation, that you have to complete
|
||||
|
||||
// NOTE: You may want to add data member(s) in 'class Lock' at entrypoint.hpp:30
|
||||
|
||||
/** Lock default constructor.
|
||||
**/
|
||||
Lock::Lock() {
|
||||
// ...
|
||||
}
|
||||
|
||||
/** Lock destructor.
|
||||
**/
|
||||
Lock::~Lock() {
|
||||
// ...
|
||||
}
|
||||
|
||||
/** [thread-safe] Acquire the lock, block if it is already acquired.
|
||||
**/
|
||||
void Lock::lock() {
|
||||
// ...
|
||||
}
|
||||
|
||||
/** [thread-safe] Release the lock, assuming it is indeed held by the caller.
|
||||
**/
|
||||
void Lock::unlock() {
|
||||
// ...
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------- //
|
||||
// Thread accessing the shared memory (a mere shared counter in this program)
|
||||
|
||||
/** Thread entry point.
|
||||
* @param nb Total number of threads
|
||||
* @param id This thread ID (from 0 to nb-1 included)
|
||||
* @param lock Lock to use to protect the shared memory (read & written by 'shared_access')
|
||||
**/
|
||||
void entry_point(size_t nb, size_t id, Lock& lock) {
|
||||
::printf("Hello from thread %lu/%lu\n", id, nb);
|
||||
for (int i = 0; i < 10000; ++i) {
|
||||
::std::lock_guard<Lock> guard{lock}; // Lock is acquired here
|
||||
::shared_access();
|
||||
// Lock is automatically released here (thanks to 'lock_guard', upon leaving the scope)
|
||||
}
|
||||
}
|
48
cs453-ca/CS453-2021-project/playground/entrypoint.hpp
Normal file
48
cs453-ca/CS453-2021-project/playground/entrypoint.hpp
Normal file
@@ -0,0 +1,48 @@
|
||||
/**
|
||||
* @file entrypoint.hpp
|
||||
* @author Sébastien Rouault <sebastien.rouault@epfl.ch>
|
||||
*
|
||||
* @section LICENSE
|
||||
*
|
||||
* Copyright © 2018-2019 Sébastien Rouault.
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 3 of the License, or
|
||||
* any later version. Please see https://gnu.org/licenses/gpl.html
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* @section DESCRIPTION
|
||||
*
|
||||
* Interface for the "entry point" source file.
|
||||
**/
|
||||
|
||||
#pragma once
|
||||
|
||||
// -------------------------------------------------------------------------- //
|
||||
|
||||
/** Your lock class.
|
||||
**/
|
||||
class Lock final {
|
||||
public:
|
||||
/** Deleted copy/move constructor/assignment.
|
||||
**/
|
||||
Lock(Lock const&) = delete;
|
||||
Lock& operator=(Lock const&) = delete;
|
||||
// NOTE: Actually, one could argue it makes sense to implement move,
|
||||
// but we don't care about this feature in our simple playground
|
||||
public:
|
||||
Lock();
|
||||
~Lock();
|
||||
public:
|
||||
void lock();
|
||||
void unlock();
|
||||
};
|
||||
|
||||
// -------------------------------------------------------------------------- //
|
||||
|
||||
void entry_point(size_t, size_t, Lock&);
|
85
cs453-ca/CS453-2021-project/playground/runner.cpp
Normal file
85
cs453-ca/CS453-2021-project/playground/runner.cpp
Normal file
@@ -0,0 +1,85 @@
|
||||
/**
|
||||
* @file runner.cpp
|
||||
* @author Sébastien Rouault <sebastien.rouault@epfl.ch>
|
||||
*
|
||||
* @section LICENSE
|
||||
*
|
||||
* Copyright © 2018-2019 Sébastien Rouault.
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 3 of the License, or
|
||||
* any later version. Please see https://gnu.org/licenses/gpl.html
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* @section DESCRIPTION
|
||||
*
|
||||
* Trivial program that call a function in several threads.
|
||||
**/
|
||||
|
||||
// External headers
|
||||
#include <atomic>
|
||||
#include <iostream>
|
||||
#include <thread>
|
||||
|
||||
// Internal headers
|
||||
#include "entrypoint.hpp"
|
||||
#include "runner.hpp"
|
||||
|
||||
// -------------------------------------------------------------------------- //
|
||||
// Shared memory, access function and consistency check
|
||||
|
||||
static int counter = 0;
|
||||
static ::std::atomic<int> check_counter{0};
|
||||
|
||||
/** Performs some operations on some shared memory.
|
||||
**/
|
||||
void shared_access() {
|
||||
++counter;
|
||||
check_counter.fetch_add(1, ::std::memory_order_relaxed);
|
||||
}
|
||||
|
||||
/** (Empirically) checks that concurrent operations did not break consistency, warn accordingly.
|
||||
**/
|
||||
static void shared_check() {
|
||||
auto calls = check_counter.load(::std::memory_order_relaxed);
|
||||
if (counter == calls) {
|
||||
::std::cout << "** No inconsistency detected (" << counter << " == " << calls << ") **" << ::std::endl;
|
||||
} else {
|
||||
::std::cout << "** Inconsistency detected (" << counter << " != " << calls << ") **" << ::std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------- //
|
||||
// Lock + thread launches and management
|
||||
|
||||
/** Program entry point.
|
||||
* @param argc Arguments count
|
||||
* @param argv Arguments values
|
||||
* @return Program return code
|
||||
**/
|
||||
int main(int, char**) {
|
||||
auto const nbworkers = []() {
|
||||
auto res = ::std::thread::hardware_concurrency();
|
||||
if (res == 0) {
|
||||
::std::cout << "WARNING: unable to query '::std::thread::hardware_concurrency()', falling back to 4 threads" << ::std::endl;
|
||||
res = 4;
|
||||
}
|
||||
return static_cast<size_t>(res);
|
||||
}();
|
||||
Lock lock;
|
||||
::std::thread threads[nbworkers];
|
||||
for (size_t i = 0; i < nbworkers; ++i) {
|
||||
threads[i] = ::std::thread{[&](size_t i) {
|
||||
entry_point(nbworkers, i, lock);
|
||||
}, i};
|
||||
}
|
||||
for (auto&& thread: threads)
|
||||
thread.join();
|
||||
shared_check();
|
||||
return 0;
|
||||
}
|
28
cs453-ca/CS453-2021-project/playground/runner.hpp
Normal file
28
cs453-ca/CS453-2021-project/playground/runner.hpp
Normal file
@@ -0,0 +1,28 @@
|
||||
/**
|
||||
* @file runner.hpp
|
||||
* @author Sébastien Rouault <sebastien.rouault@epfl.ch>
|
||||
*
|
||||
* @section LICENSE
|
||||
*
|
||||
* Copyright © 2018-2019 Sébastien Rouault.
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 3 of the License, or
|
||||
* any later version. Please see https://gnu.org/licenses/gpl.html
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* @section DESCRIPTION
|
||||
*
|
||||
* Trivial program that call a function in several threads.
|
||||
**/
|
||||
|
||||
#pragma once
|
||||
|
||||
// -------------------------------------------------------------------------- //
|
||||
|
||||
void shared_access();
|
Reference in New Issue
Block a user