tasks.h (2600B)
1 /*
2 * Copyright (c) 2014 Joris Vink <joris@coders.se>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
17 #ifndef __H_KORE_TASKS
18 #define __H_KORE_TASKS
19
20 #include <pthread.h>
21
22 #define KORE_TASK_STATE_CREATED 1
23 #define KORE_TASK_STATE_RUNNING 2
24 #define KORE_TASK_STATE_FINISHED 3
25 #define KORE_TASK_STATE_ABORT 4
26
27 #define KORE_TASK_THREADS 2
28
29 #if defined(__cplusplus)
30 extern "C" {
31 #endif
32
33 #if !defined(KORE_NO_HTTP)
34 struct http_request;
35 #endif
36
37 struct kore_task {
38 struct kore_event evt;
39 int state;
40 int result;
41 pthread_rwlock_t lock;
42
43 #if !defined(KORE_NO_HTTP)
44 struct http_request *req;
45 #endif
46
47 int fds[2];
48 int (*entry)(struct kore_task *);
49 void (*cb)(struct kore_task *);
50
51 struct kore_task_thread *thread;
52
53 TAILQ_ENTRY(kore_task) list;
54 LIST_ENTRY(kore_task) rlist;
55 };
56
57 struct kore_task_thread {
58 u_int8_t idx;
59 pthread_t tid;
60 pthread_mutex_t lock;
61 pthread_cond_t cond;
62 TAILQ_HEAD(, kore_task) tasks;
63
64 TAILQ_ENTRY(kore_task_thread) list;
65 };
66
67 void kore_task_init(void);
68 void kore_task_handle(void *, int);
69 void kore_task_run(struct kore_task *);
70 void kore_task_finish(struct kore_task *);
71 void kore_task_destroy(struct kore_task *);
72 int kore_task_finished(struct kore_task *);
73
74 #if !defined(KORE_NO_HTTP)
75 void kore_task_bind_request(struct kore_task *,
76 struct http_request *);
77 #endif
78 void kore_task_bind_callback(struct kore_task *,
79 void (*cb)(struct kore_task *));
80 void kore_task_create(struct kore_task *,
81 int (*entry)(struct kore_task *));
82
83 u_int32_t kore_task_channel_read(struct kore_task *, void *, u_int32_t);
84 void kore_task_channel_write(struct kore_task *, void *, u_int32_t);
85
86 void kore_task_set_state(struct kore_task *, int);
87 void kore_task_set_result(struct kore_task *, int);
88
89 int kore_task_state(struct kore_task *);
90 int kore_task_result(struct kore_task *);
91
92 extern u_int16_t kore_task_threads;
93
94 #if defined(__cplusplus)
95 }
96 #endif
97
98 #endif