Repository navigation
Expand file tree
/
Copy pathdispatchQueue.h
More file actions
59 lines (42 loc) · 2.01 KB
/
Copy pathdispatchQueue.h
File metadata and controls
59 lines (42 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
* File: dispatchQueue.h
* Author: robert
*
* Modified by: mbea966
*/
#ifndef DISPATCHQUEUE_H
#define DISPATCHQUEUE_H
#include <pthread.h>
#include <semaphore.h>
#define error_exit(MESSAGE) perror(MESSAGE), exit(EXIT_FAILURE)
typedef enum { // whether dispatching a task synchronously or asynchronously
ASYNC, SYNC
} task_dispatch_type_t;
typedef enum { // The type of dispatch queue.
CONCURRENT, SERIAL
} queue_type_t;
typedef struct task {
char name[64]; // to identify it when debugging
void (*work)(void *); // the function to perform
void *params; // parameters to pass to the function
struct task *next_task; // stores the task to be accessed next
sem_t *task_semaphore; // tracks whether a task has been completed
} task_t;
typedef struct dispatch_queue_t dispatch_queue_t; // the dispatch queue type
struct dispatch_queue_t {
queue_type_t queue_type; // the type of queue - serial or concurrent
task_t *head; // pointer to the first task to be executed
pthread_t *threads; // pointer to list of threads for queue to use
sem_t *queue_semaphore; // tracks how many tasks are ready to execute
pthread_mutex_t *lock; // used to a lock a queue to protect from concurrent access
volatile int threads_executing; // stores the number of threads currently executing tasks
};
task_t *task_create(void (*)(void *), void *, char*);
void task_destroy(task_t *);
dispatch_queue_t *dispatch_queue_create(queue_type_t);
void dispatch_queue_destroy(dispatch_queue_t *);
int dispatch_async(dispatch_queue_t *, task_t *);
int dispatch_sync(dispatch_queue_t *, task_t *);
void dispatch_for(dispatch_queue_t *, long, void (*)(long));
int dispatch_queue_wait(dispatch_queue_t *);
#endif /* DISPATCHQUEUE_H */