summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichiel Schuurmans <michielschuurmans@gmail.com>2020-12-14 22:45:56 +0100
committerMichiel Schuurmans <michielschuurmans@gmail.com>2020-12-14 22:45:56 +0100
commit53c5d2556b3a6fafc7dd41f56fca3fcb8aae4a14 (patch)
tree88d508c1d063ce8147ad21f2ed3a39f904801ff6
parent6088667f673370d78040fc5c85aa919a8188e540 (diff)
Request: states
Signed-off-by: Michiel Schuurmans <michielschuurmans@gmail.com>
-rw-r--r--main.c14
-rw-r--r--request.h17
-rw-r--r--threads.c17
-rw-r--r--threads.h16
4 files changed, 47 insertions, 17 deletions
diff --git a/main.c b/main.c
index b329b15..f5cdc39 100644
--- a/main.c
+++ b/main.c
@@ -4,6 +4,14 @@
#include <stdbool.h>
#include "threads.h"
+int process(struct REQUEST *request)
+{
+ request->state = REQUEST_PROCESSING;
+
+ request->state = REQUEST_DONE;
+ return 0;
+}
+
int main(int argc, char** argv)
{
log_set_level(LOG_DEBUG);
@@ -15,17 +23,19 @@ int main(int argc, char** argv)
int count = 0;
for (;;) {
struct REQUEST *r = malloc(sizeof(struct REQUEST));
+ r->process = &process;
r->priority = 0;
log_trace("Enqueing request.");
request_enqueue(r);
- sleep(1);
- if (count++ > 20)
+ if (count++ > 165000)
break;
}
+ sleep(20);
+
thread_pool_stop();
return 0;
diff --git a/request.h b/request.h
new file mode 100644
index 0000000..03a0d5d
--- /dev/null
+++ b/request.h
@@ -0,0 +1,17 @@
+#ifndef __REQUEST_H_
+#define __REQUEST_H_
+
+enum REQUEST_STATE {
+ REQUEST_QUEUED = 1,
+ REQUEST_PROCESSING,
+ REQUEST_DONE
+};
+
+struct REQUEST {
+ int id;
+ int priority;
+ enum REQUEST_STATE state;
+ int (*process)(struct REQUEST *request);
+};
+
+#endif
diff --git a/threads.c b/threads.c
index f420db4..85b6369 100644
--- a/threads.c
+++ b/threads.c
@@ -17,12 +17,15 @@ int request_enqueue(struct REQUEST *request)
thread_pool.request_count++;
+ request->state = REQUEST_QUEUED;
+ request->id = thread_pool.request_count;
+
sem_post(&thread_pool.semaphore);
return 1;
}
-static int request_dequeue(struct REQUEST **prerequest)
+int request_dequeue(struct REQUEST **prerequest)
{
int i;
struct REQUEST *request = NULL;
@@ -75,7 +78,10 @@ void* thread_worker(void *arg)
handle->request_count++;
- log_debug("thread %d Handling request", handle->thread_num);
+ log_debug("(%d) thread %d Handling request", handle->request->id, handle->thread_num);
+
+ if (handle->request->process(handle->request))
+ log_error("request finished with errors!");
/* does this work?? */
free(handle->request);
@@ -186,10 +192,9 @@ int thread_pool_init(bool *spawn_flag)
thread_pool.cleanup_delay = 5;
thread_pool.stop_flag = false;
thread_pool.spawn_flag = *spawn_flag;
- thread_pool.max_queue_size = 10;
-
- thread_pool.start_threads = 3;
- thread_pool.max_threads = 5;
+ thread_pool.max_queue_size = 165000;
+ thread_pool.start_threads = 10;
+ thread_pool.max_threads = 50;
if (!*spawn_flag)
return 0;
diff --git a/threads.h b/threads.h
index fc232b6..f71975c 100644
--- a/threads.h
+++ b/threads.h
@@ -7,6 +7,8 @@
#include <stdbool.h>
#include <semaphore.h>
+#include "request.h"
+
#define SEMAPHORE_LOCKED (0)
#define THREAD_RUNNING (1)
@@ -24,10 +26,6 @@
num = load(_x); \
if (cas_decr(_x, num)) break; \
} while (true)
-struct REQUEST {
- int id;
- int priority;
-};
struct THREAD_HANDLE {
struct THREAD_HANDLE *prev;
@@ -57,7 +55,7 @@ struct THREAD_POOL {
uint32_t cleanup_delay;
bool stop_flag;
bool spawn_flag;
- uint32_t max_queue_size;
+ int max_queue_size;
pthread_mutex_t wait_mutex;
/*
@@ -74,9 +72,9 @@ struct THREAD_POOL {
struct atomic_queue_t *queue[NUM_FIFOS];
};
-int thread_pool_init(bool *spawn_flag);
-void thread_pool_stop(void);
+int thread_pool_init(bool *spawn_flag);
+void thread_pool_stop(void);
-int request_enqueue(struct REQUEST *request);
-static int request_dequeue(struct REQUEST **prerequest);
+int request_enqueue(struct REQUEST *request);
+int request_dequeue(struct REQUEST **prerequest);
#endif