Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

added pocess manager

  • Loading branch information...
commit deae290d1b854ceb00cf2e5b38964874960ce708 1 parent 33115d0
Mikola Samardak authored
Showing with 98 additions and 0 deletions.
  1. +75 −0 src/engine/procman.c
  2. +23 −0 src/engine/procman.h
75 src/engine/procman.c
View
@@ -0,0 +1,75 @@
+#include <assert.h>
+
+#include "procman.h"
+#include "util.h"
+
+Process* mkproclist(void)
+{
+ Process* proc = malloc(sizeof(Process));
+
+ proc->name = NULL;
+ proc->func = NULL;
+ proc->next = NULL;
+
+ return proc;
+}
+
+Process*
+procpush(const char* name, ProcessFunction func, void* data, Process* list)
+{
+ assert(!nullp(list));
+
+ if(nullp(list->name) && nullp(list->func) && nullp(list->data)) {
+ list->name = name;
+ list->func = func;
+ list->data = data;
+ return list;
+ } else if(nullp(list->next)) {
+ list->next = mkproclist();
+ }
+
+ return procpush(name, func, data, list->next);
+}
+
+bool procpop(const char* name, Process* list)
+{
+ int process_pop(Process* list, Process* prev)
+ {
+ if(!strcmp(list->name, name)) {
+ if(nullp(list->next)) {
+ prev->next = NULL;
+ free(list);
+ } else if(nullp(prev)) {
+ list->name = list->next->name;
+ list->func = list->next->func;
+ list->next = list->next->next;
+ free(list->next);
+ } else {
+ prev->next = list->next;
+ free(list);
+ }
+ return true;
+ } else if(nullp(list->next)) {
+ WARN("Unable to find process");
+ return false;
+ } else
+ return process_pop(list->next, list);
+ }
+
+ return process_pop(list, NULL);
+}
+
+void delproclist(Process* list)
+{
+ if(!nullp(list->next))
+ delproclist(list->next);
+ free(list);
+}
+
+void procdo(Process* list) {
+ assert(!nullp(list));
+
+ for(Process* cons = list; !nullp(cons); list = cons = cons->next)
+ list->func(list->data);
+}
+
23 src/engine/procman.h
View
@@ -0,0 +1,23 @@
+#pragma once
+
+#include <stdbool.h>
+
+typedef void (*ProcessFunction)(void* data);
+
+typedef struct Process {
+ const char* name;
+ ProcessFunction func;
+ void* data;
+ struct Process* next;
+} Process;
+
+extern Process* mkproclist(void);
+
+extern void delproclist(Process* list);
+
+extern Process*
+procpush(const char* name, ProcessFunction func, void* data, Process* list);
+
+extern bool procpop(const char* name, Process* list);
+
+extern void procdo(Process* list);
Please sign in to comment.
Something went wrong with that request. Please try again.