summaryrefslogtreecommitdiff
path: root/src/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.c')
-rwxr-xr-x[-rw-r--r--]src/main.c211
1 files changed, 130 insertions, 81 deletions
diff --git a/src/main.c b/src/main.c
index d1048f0..e08b964 100644..100755
--- a/src/main.c
+++ b/src/main.c
@@ -1,103 +1,152 @@
1#include "arena.h" 1///usr/bin/true; gcc -std=c2x -Wall -Wextra -Wpedantic -pedantic-errors -fanalyzer -Wanalyzer-too-complex -ggdb -g3 -O0 main.c -o main && ./main; exit $?
2#include "ll.h"
3#include "encryption.h"
4#include "scanner.h"
5#include "shared.h"
6#include "threadpool.h"
7 2
3// #include <errno.h>
4// #include <error.h>
5// #include <stdio.h>
6// #include <threads.h>
7// #include <unistd.h>
8// #include <string.h>
9
10#include <stdlib.h>
8#include <errno.h> 11#include <errno.h>
9#include <error.h> 12#include <sys/types.h>
10#include <threads.h>
11#include <unistd.h>
12#include <string.h> 13#include <string.h>
14#include <stdio.h>
13 15
14typedef int (*thingcb)(void*);
15struct thing {
16 thingcb cb;
17 void *arg;
18};
19 16
20typedef struct queue { 17void * xcalloc(size_t nmemb, size_t size) {
21 mtx_t *cqmutex; 18 void *mem = calloc(nmemb, size);
22 cnd_t *cqcnd; 19 if(!mem)
23 unsigned char cstatus; 20 abort();
24 21
25 int len; 22 return mem;
26 int used; 23}
27
28 struct thing **queue;
29} ccqueue;
30 24
31#define QUEUE_LEN 10
32 25
33int consumer(void *queue) {
34 if(!queue)
35 thrd_exit(-1);
36 26
37 ccqueue *real = (ccqueue *)queue; 27typedef void (*fcallback)(void*);
38 for(struct thing *thing = NULL;;) { 28typedef struct gdata {
39 cnd_wait(real->cqcnd, real->cqmutex); 29 void *data;
40 if(real->cstatus) 30 fcallback freer;
41 thrd_exit(-1); 31} gdata;
32
33gdata * gdata_init(void *data, fcallback fcb) {
34 gdata *gd = xcalloc(1, sizeof(*gd));
35 gd->data = data;
36 gd->freer = fcb;
37
38 return gd;
39}
40
41void gdata_free(gdata *gd) {
42 if(!gd)
43 return;
44
45 if(gd->freer != NULL)
46 gd->freer(gd->data);
47 free(gd);
42 48
43 if(real->used > 0) { 49 return;
44 thing = real->queue[real->used - 1]; 50}
45 real->used--;
46 }
47 51
48 if(thing != NULL) 52void * gdata_getdata(gdata * const gd) {
49 if(thing->arg != NULL) 53 if(!gd) {
50 thing->cb(thing->arg); 54 errno = EINVAL;
55 return NULL;
51 } 56 }
57
58 return gd->data;
59}
52 60
53 thrd_exit(0); 61void gdata_destruct(gdata *gd, void **storage) {
62 if(!gd || !storage)
63 return;
64
65 *storage = gd->data;
66 free(gd);
67 return;
54} 68}
55 69
56int testcb(void *data) {
57 if(!data)
58 return -1;
59 70
60 printf("%s\n", (char*)data); 71
61 return 0; 72typedef struct stack {
73 gdata **arr;
74 int size;
75 int used;
76} stack;
77
78stack * stack_init(int size) {
79 if(size < 1)
80 return NULL;
81
82 stack *st = xcalloc(1, sizeof(*st));
83 st->size = size;
84 st->used = 0;
85 st->arr = xcalloc(st->size, sizeof(gdata*));
86
87 return st;
62} 88}
63 89
64int main() { 90void stack_free(stack *st) {
65 // error(1, ENOTSUP, "No main file lol"); 91 if(!st)
66 92 return;
67 // Manually implement a threadpool and consumer in a "dumb" way to make sure I'm doing the correct thing 93
68 ccqueue *cqueue = xcalloc(1, sizeof(*cqueue)); 94 for(int i = 0; i < st->used; i++)
69 cqueue->cqmutex = xcalloc(1, sizeof(*cqueue->cqmutex)); mtx_init(cqueue->cqmutex, mtx_plain); 95 gdata_free(st->arr[i]);
70 cqueue->cqcnd = xcalloc(1, sizeof(*cqueue->cqcnd)); cnd_init(cqueue->cqcnd); 96 free(st->arr);
71 cqueue->cstatus = 0; 97 free(st);
72 cqueue->len = QUEUE_LEN;
73 cqueue->used = 0;
74 cqueue->queue = xcalloc(cqueue->len, sizeof(struct thing *));
75
76 thrd_t *thread = xcalloc(1, sizeof(*thread));
77 thrd_create(thread, consumer, (void *)cqueue);
78
79 mtx_lock(cqueue->cqmutex);
80 cqueue->queue[cqueue->used] = xcalloc(1, sizeof(struct thing));
81 cqueue->queue[cqueue->used]->cb = testcb;
82 cqueue->queue[cqueue->used]->arg = "This is some data";
83 cqueue->used++;
84 mtx_unlock(cqueue->cqmutex);
85 cnd_signal(cqueue->cqcnd);
86
87 sleep(1);
88 98
89 mtx_lock(cqueue->cqmutex); 99 return;
90 cqueue->cstatus++; 100}
91 mtx_unlock(cqueue->cqmutex);
92 cnd_broadcast(cqueue->cqcnd);
93 101
94 // Destroy and free everything 102int stack_push(stack *st, gdata *data) {
95 mtx_destroy(cqueue->cqmutex); cnd_destroy(cqueue->cqcnd); 103 if(!st || !data)
96 free(cqueue->cqmutex); free(cqueue->cqcnd); 104 return -1;
97 free(cqueue->queue); 105 if(st->used == st->size)
98 free(cqueue); 106 return 0;
99 107
100 return 0; 108 st->arr[st->used++] = data;
109
110 return st->used;
111}
112
113int stack_pushd(stack *st, void *data, fcallback fcb) {
114 return stack_push(st, gdata_init(data, fcb));
115}
116
117gdata * stack_pop(stack *st) {
118 if(!st)
119 return NULL;
120 if(st->used <= 0)
121 return NULL;
122
123 gdata *gd = st->arr[--st->used];
124 return gd;
101} 125}
102 126
103// Ok well now I'm pissed. The canceling works fine but it's not actually consuming the thing \ No newline at end of file 127void * stack_popd(stack *st) {
128 gdata *gd = stack_pop(st);
129 void *data = NULL;
130
131 gdata_destruct(gd, &data);
132 return data;
133}
134
135
136
137int main() {
138 // stack *st = stack_init(10);
139 // stack_pushd(st, (void*)10, NULL);
140 // stack_pushd(st, (void*)11, NULL);
141 // stack_pushd(st, (void*)12, NULL);
142 // stack_pushd(st, strdup("This is some data"), free);
143
144 // char *data = stack_popd(st);
145 // printf("%s\n", (data) ? data : "null");
146 // free(data);
147
148 // stack_free(st);
149
150
151 return 0;
152} \ No newline at end of file