commit c4c3b013c016b868cf4ce39621e17359455cec14 Author: Hizenberg Date: Mon Jun 3 12:10:29 2024 +0530 Basic Header file ready diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6d53c27 --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +*.sln +*.vcxproj +*.vcxproj.filters +*.vcxproj.user +x64 +.vs +build +.vscode \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..ed7c1b7 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,37 @@ +# CMakeList.txt : CMake project for Event Loop ( Async Programming ), include source and define +# project specific logic here. +# +cmake_minimum_required (VERSION 3.8) + +project(Event-Loop VERSION 1.0.0 LANGUAGES C CXX) + +set(CMAKE_C_STANDARD 17) +set(CMAKE_C_STANDARD_REQUIRED ON) +set(CMAKE_C_EXTENSIONS OFF) + + +#Output file structure + +#Scheme 1 +set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin/$") +set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib/$") +set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib/$") + +#Scheme 2 +#set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/$/bin") +#set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/$/lib") +#set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/$/lib") + +#Library Name... +set(EVENT_LOOP_LIB evloop) + + +#Header file directory... +set(HEADER_DIR ${CMAKE_SOURCE_DIR}/include) + +#Source file directory... +set(SOURCE_DIR ${CMAKE_SOURCE_DIR}/src) + +#Adding Sub-directory having CMakeLists.txt... +add_subdirectory(src) +#add_subdirectory(test) \ No newline at end of file diff --git a/CMakePresets.json b/CMakePresets.json new file mode 100644 index 0000000..f4bc98b --- /dev/null +++ b/CMakePresets.json @@ -0,0 +1,101 @@ +{ + "version": 3, + "configurePresets": [ + { + "name": "windows-base", + "hidden": true, + "generator": "Ninja", + "binaryDir": "${sourceDir}/out/build/${presetName}", + "installDir": "${sourceDir}/out/install/${presetName}", + "cacheVariables": { + "CMAKE_C_COMPILER": "cl.exe", + "CMAKE_CXX_COMPILER": "cl.exe" + }, + "condition": { + "type": "equals", + "lhs": "${hostSystemName}", + "rhs": "Windows" + } + }, + { + "name": "x64-debug", + "displayName": "x64 Debug", + "inherits": "windows-base", + "architecture": { + "value": "x64", + "strategy": "external" + }, + "cacheVariables": { + "CMAKE_BUILD_TYPE": "Debug" + } + }, + { + "name": "x64-release", + "displayName": "x64 Release", + "inherits": "x64-debug", + "cacheVariables": { + "CMAKE_BUILD_TYPE": "Release" + } + }, + { + "name": "x86-debug", + "displayName": "x86 Debug", + "inherits": "windows-base", + "architecture": { + "value": "x86", + "strategy": "external" + }, + "cacheVariables": { + "CMAKE_BUILD_TYPE": "Debug" + } + }, + { + "name": "x86-release", + "displayName": "x86 Release", + "inherits": "x86-debug", + "cacheVariables": { + "CMAKE_BUILD_TYPE": "Release" + } + }, + { + "name": "linux-debug", + "displayName": "Linux Debug", + "generator": "Ninja", + "binaryDir": "${sourceDir}/out/build/${presetName}", + "installDir": "${sourceDir}/out/install/${presetName}", + "cacheVariables": { + "CMAKE_BUILD_TYPE": "Debug" + }, + "condition": { + "type": "equals", + "lhs": "${hostSystemName}", + "rhs": "Linux" + }, + "vendor": { + "microsoft.com/VisualStudioRemoteSettings/CMake/1.0": { + "sourceDir": "$env{HOME}/.vs/$ms{projectDirName}" + } + } + }, + { + "name": "macos-debug", + "displayName": "macOS Debug", + "generator": "Ninja", + "binaryDir": "${sourceDir}/out/build/${presetName}", + "installDir": "${sourceDir}/out/install/${presetName}", + "cacheVariables": { + "CMAKE_BUILD_TYPE": "Debug" + }, + "condition": { + "type": "equals", + "lhs": "${hostSystemName}", + "rhs": "Darwin" + }, + "vendor": { + "microsoft.com/VisualStudioRemoteSettings/CMake/1.0": { + "sourceDir": "$env{HOME}/.vs/$ms{projectDirName}" + } + } + } + ] +} diff --git a/README.md b/README.md new file mode 100644 index 0000000..cfb26df --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +# Event-Loop-in-C diff --git a/include/event_loop.h b/include/event_loop.h new file mode 100644 index 0000000..b0ef942 --- /dev/null +++ b/include/event_loop.h @@ -0,0 +1,43 @@ +#ifndef __EV_LOOP__ +#define __EV_LOOP__ + + +typedef struct task_ task_t; +typedef struct event_loop_ event_loop_t; + +typedef EL_RES_T(*event_cbk)(void*); + +struct task_ { + + event_cbk cbk; + void* arg; + struct task_* left, * right; +}; + +typedef enum { + + EV_LOOP_IDLE, + EV_LOOP_BUSY +} EV_LOOP_STATE; + +struct event_loop_ { + + /* head to the start of the task array */ + struct task_* task_array_head[TASK_PRIORITY_MAX]; + /* Mutex to enforce Mutual exclusion enqueue/Deque + * Operation in task array. Also used to update event loop + * attributes in mutual exclusive way + */ + pthread_mutex_t ev_loop_mutex; + /* State of event loop */ + EV_LOOP_STATE ev_loop_state; + /* CV to suspened event loop thread */ + pthread_cond_t ev_loop_cv; + /* Event loop thread */ + pthread_t* thread; + /* Current task which event loop thread is executing. + * NULL if event loop is resting in peace*/ + struct task_* current_task; +}; + +#endif \ No newline at end of file diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100644 index 0000000..5de2a47 --- /dev/null +++ b/src/CMakeLists.txt @@ -0,0 +1,7 @@ +set(EVENT_LOOP_SRC "event_loop.c") + +add_library(${EVENT_LOOP_LIB} STATIC + ${EVENT_LOOP_SRC}) + +target_include_directories(${EVENT_LOOP_LIB} PUBLIC + ${HEADER_DIR}) \ No newline at end of file diff --git a/src/event_loop.c b/src/event_loop.c new file mode 100644 index 0000000..e69de29