-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththread-safe-job-stack.h
More file actions
47 lines (39 loc) · 1.64 KB
/
thread-safe-job-stack.h
File metadata and controls
47 lines (39 loc) · 1.64 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
//
// Created by dran on 3/5/22.
//
#ifndef NS_PA_2_THREAD_SAFE_JOB_STACK_H
#define NS_PA_2_THREAD_SAFE_JOB_STACK_H
#include <pthread.h>
#include "job.h"
#include "constants.h"
struct job_stack {
job_t** content_base;
int top;
int max_job_count;
int reserve_job_count;
pthread_mutex_t mutex;
pthread_cond_t not_empty;
pthread_cond_t not_full;
int finished;
};
typedef struct job_stack job_stack_t;
// the # of reserve_slots must be greater than # of consumers that also want to produce sometimes
job_stack_t* job_stack_construct(int max_jobs, int reserve_slots);
// job stack MUST BE empty before calling destruct
void job_stack_destruct(job_stack_t* to_free);
// this function is used for push new jobs onto stack, should be used by job producers
// will block until stack has less than `max_job_count` items
int job_stack_push(job_stack_t* stack, job_t* ptr_in);
// TODO : upgrade this object to a priority queue based on job->expiration_time
// this function should be used by job consumers that sometimes have to produce too
// there is no wait for push, there will always be space in the reserve_slots
// this function is for pushing existing jobs back onto stack
// pushes to the bottom of stack
int job_stack_push_back(job_stack_t* stack, job_t* ptr_in);
// this function returns FINISHED `job_stack_signal_finish` has been called
int job_stack_pop(job_stack_t* stack, job_t** ptr_out);
int job_stack_signal_finish(job_stack_t* stack);
// private functions below
job_t* job_stack_get_item(job_stack_t* stack, int index);
void job_stack_set_item(job_stack_t* stack, int index, job_t* ptr_in);
#endif //NS_PA_2_THREAD_SAFE_JOB_STACK_H