Skip to content

Commit 8fefd59

Browse files
committed
project init
1 parent 107ec54 commit 8fefd59

13 files changed

+682
-0
lines changed

ContextSwitch.S

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
.globl ContextSwitch // make the Symbol ContextSwitch available in link time
2+
ContextSwitch:
3+
4+
// According to x86_64 calling convention
5+
// first argument to ContextSwitch is stored in %rdi
6+
// second argument to ContextSwitch is stored in %rsi
7+
8+
// Therefore %rdi -> addrss of Main Conext struct
9+
// Use Offset addressing to store each registers to Main Context
10+
movq %rsp, 0x00(%rdi)
11+
movq %r15, 0x08(%rdi)
12+
movq %r14, 0x10(%rdi)
13+
movq %r13, 0x18(%rdi)
14+
movq %r12, 0x20(%rdi)
15+
movq %rbp, 0x28(%rdi)
16+
movq %rbx, 0x30(%rdi)
17+
18+
// Therefore %rsi -> addrss of Child Conext struct
19+
// Use Offset addressing to load each registers from Child Context
20+
movq 0x00(%rsi), %rsp
21+
movq 0x08(%rsi), %r15
22+
movq 0x10(%rsi), %r14
23+
movq 0x18(%rsi), %r13
24+
movq 0x20(%rsi), %r12
25+
movq 0x28(%rsi), %rbp
26+
movq 0x30(%rsi), %rbx
27+
28+
ret // rip is loaded with [rsp,rsp + 8]

config.hpp

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* @file config.hpp
3+
* @author d.deka (https://ddeka0.github.io/)
4+
* @brief
5+
* @version 0.1
6+
* @date 2021-02-14
7+
*
8+
*/
9+
#pragma once
10+
#include "threadCtx.hpp"
11+
12+
#define N 2
13+
/**
14+
* @brief
15+
* Context Switch API
16+
*/
17+
extern "C" void ContextSwitch(RegisterContext* curr,RegisterContext* next);
18+
19+
/**
20+
* @brief
21+
* Each non user space thread has this variable to know
22+
* the current running thread context (user space or non user space)
23+
*/
24+
extern thread_local ThreadCtx* currContext[N];
25+
26+
/**
27+
* @brief
28+
* Limits the size of the user thread stack
29+
*/
30+
extern long long StackSize;

logger.hpp

+121
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/**
2+
* @file logger.hpp
3+
* @author d.deka (https://ddeka0.github.io/)
4+
* @brief
5+
* @version 0.1
6+
* @date 2021-02-14
7+
*
8+
*/
9+
#pragma once
10+
#include <type_traits>
11+
#include <typeinfo>
12+
#ifndef _MSC_VER
13+
# include <cxxabi.h>
14+
#endif
15+
#include <memory>
16+
#include <string>
17+
#include <cstdlib>
18+
#include <iostream>
19+
#include <utility>
20+
21+
#if defined(_MSC_VER)
22+
#define PrintF std::cout << __FUNCSIG__ <<" "<< __LINE__ << std::endl;
23+
#else
24+
#define PrintF std::cout << __PRETTY_FUNCTION__<<" "<< __LINE__ << std::endl;
25+
#endif
26+
27+
28+
// https://stackoverflow.com/questions/29326460/how-to-make-a-variadic-macro-for-stdcout
29+
inline void Log(){
30+
// std::cout is not thread safe
31+
std::cout << std::endl;
32+
}
33+
34+
template<typename First, typename ...Rest>
35+
void Log(First && first, Rest && ...rest) {
36+
// std::cout is not thread safe
37+
std::cout << std::forward<First>(first)<<" ";
38+
Log(std::forward<Rest>(rest)...);
39+
}
40+
41+
42+
43+
// C++ language standard detection
44+
#if (defined(__cplusplus) && __cplusplus >= 202002L) || (defined(_MSVC_LANG) && _MSVC_LANG >= 202002L) \
45+
|| (defined(__cplusplus) && __cplusplus >= 201703L) || (defined(_HAS_CXX17) && _HAS_CXX17 == 1)
46+
47+
#include <string_view>
48+
#include <type_traits>
49+
50+
template <class T>
51+
constexpr std::string_view type_name() {
52+
using namespace std;
53+
#ifdef __clang__
54+
string_view p = __PRETTY_FUNCTION__;
55+
return string_view(p.data() + 34, p.size() - 34 - 1);
56+
#elif defined(__GNUC__)
57+
string_view p = __PRETTY_FUNCTION__;
58+
# if __cplusplus < 201402
59+
return string_view(p.data() + 36, p.size() - 36 - 1);
60+
# else
61+
return string_view(p.data() + 49, p.find(';', 49) - 49);
62+
# endif
63+
#elif defined(_MSC_VER)
64+
string_view p = __FUNCSIG__;
65+
return string_view(p.data() + 84, p.size() - 84 - 7);
66+
#endif
67+
}
68+
69+
template<typename T>
70+
struct refType {
71+
static constexpr std::string_view type = (std::is_lvalue_reference<T>::value) > 0 ? "lvalue":"ravlue";
72+
};
73+
74+
#define getRefType(expr) refType<decltype(expr)>::type
75+
76+
#define GetKeyTypeFromInternalLogic std::string
77+
#define GetValueTypeFromInternalLogic int
78+
79+
#else
80+
81+
82+
template <class T>
83+
std::string
84+
type_name()
85+
{
86+
typedef typename std::remove_reference<T>::type TR;
87+
std::unique_ptr<char, void(*)(void*)> own
88+
(
89+
#ifndef _MSC_VER
90+
abi::__cxa_demangle(typeid(TR).name(), nullptr,
91+
nullptr, nullptr),
92+
#else
93+
nullptr,
94+
#endif
95+
std::free
96+
);
97+
std::string r = own != nullptr ? own.get() : typeid(TR).name();
98+
if (std::is_const<TR>::value)
99+
r += " const";
100+
if (std::is_volatile<TR>::value)
101+
r += " volatile";
102+
if (std::is_lvalue_reference<T>::value)
103+
r += "&";
104+
else if (std::is_rvalue_reference<T>::value)
105+
r += "&&";
106+
return r;
107+
}
108+
109+
template<typename T>
110+
struct refType {
111+
static constexpr const char* type = (std::is_lvalue_reference<T>::value) > 0 ? "lvalue":"ravlue";
112+
};
113+
114+
#define getRefType(expr) refType<decltype(expr)>::type
115+
116+
#define GetKeyTypeFromInternalLogic std::string
117+
#define GetValueTypeFromInternalLogic int
118+
119+
#endif
120+
121+
#define BreakLine std::cout << std::endl << std::endl;

main.cpp

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* @file main.cpp
3+
* @author d.deka (https://ddeka0.github.io/)
4+
* @brief
5+
* @version 0.1
6+
* @date 2021-02-14
7+
*
8+
*/
9+
10+
#include <bits/stdc++.h>
11+
#include "task_system.hpp"
12+
#include "config.hpp"
13+
#include "logger.hpp"
14+
#include <chrono>
15+
16+
thread_local ThreadCtx* currContext[N];
17+
long long StackSize = 0x400000;
18+
19+
void func() {
20+
21+
Log("TID before yield = ",THIS_THREAD_ID);
22+
23+
u_yield();
24+
25+
Log("TID After yield = ",THIS_THREAD_ID);
26+
27+
Log("Hello from func!");
28+
}
29+
30+
int main(int argc,char *argv[]) {
31+
32+
task_system ts;
33+
34+
ts.submit(func);
35+
36+
std::this_thread::sleep_for(std::chrono::milliseconds(3000));
37+
38+
}

notification_queue.cpp

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
* @file notification_queue.cpp
3+
* @author d.deka (https://ddeka0.github.io/)
4+
* @brief
5+
* @version 0.1
6+
* @date 2021-02-14
7+
*
8+
*/
9+
#include "notification_queue.hpp"
10+
#include "logger.hpp"
11+
12+
/** done()
13+
* @brief
14+
* Thread safe queue, where user threads are kept
15+
*/
16+
void notification_queue::done() {
17+
{
18+
lock_t lock{_mutex};
19+
_done = true;
20+
}
21+
_ready.notify_all();
22+
}
23+
24+
/**
25+
* @brief pop(ThreadCtx** x)
26+
*
27+
* @param x
28+
* @return true
29+
* @return false
30+
*/
31+
bool notification_queue::pop(ThreadCtx** x) {
32+
lock_t lock{_mutex};
33+
_ready.wait(lock,[this]() {
34+
Log("Sleeping ....TID = ",THIS_THREAD_ID);
35+
return !_q.empty() || _done;
36+
});
37+
if(_q.empty() || _done) {
38+
return false;
39+
}
40+
*x = std::move(_q.front());
41+
_q.pop_front();
42+
return true;
43+
}
44+
45+
/**
46+
* @brief
47+
*
48+
* @return std::size_t
49+
*/
50+
std::size_t notification_queue::get_size() {
51+
lock_t lock{_mutex};
52+
return _q.size();
53+
}

notification_queue.hpp

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* @file notification_queue.hpp
3+
* @author d.deka (https://ddeka0.github.io/)
4+
* @brief
5+
* @version 0.1
6+
* @date 2021-02-14
7+
*
8+
*/
9+
#pragma once
10+
#include <thread>
11+
#include <concepts>
12+
#include "threadCtx.hpp"
13+
#include <stdio.h>
14+
#include <deque>
15+
#include <condition_variable>
16+
17+
/**
18+
* @brief
19+
* Thread safe queue, where user threads are kept
20+
*/
21+
22+
class notification_queue {
23+
private:
24+
std::deque<ThreadCtx*> _q;
25+
bool _done{false};
26+
std::mutex _mutex;
27+
std::condition_variable _ready;
28+
using lock_t = std::unique_lock<std::mutex>;
29+
30+
public:
31+
void done();
32+
bool pop(ThreadCtx** x);
33+
// Test purpose ...
34+
std::size_t get_size();
35+
36+
template<typename F>
37+
void push(F&& f) {
38+
{
39+
lock_t lock{_mutex};
40+
_q.emplace_back(std::forward<F>(f));
41+
}
42+
_ready.notify_one();
43+
}
44+
};

0 commit comments

Comments
 (0)