@@ -1,33 +1,37 @@
|
|
1 |
// Copyright 2021 Jeisson Hidalgo-Cespedes <jeisson.hidalgo@ucr.ac.cr> CC-BY-4
|
2 |
// TODO(all): Implement a thread-safe queue
|
3 |
|
4 |
#ifndef QUEUE_H
|
5 |
#define QUEUE_H
|
6 |
|
7 |
#include <pthread.h>
|
8 |
#include <stdbool.h>
|
9 |
|
10 |
typedef struct queue_node {
|
11 |
size_t data;
|
12 |
struct queue_node* next;
|
13 |
} queue_node_t;
|
14 |
|
15 |
typedef struct {
|
|
|
16 |
queue_node_t* head;
|
17 |
queue_node_t* tail;
|
18 |
} queue_t;
|
19 |
|
20 |
/**
|
21 |
* @todo: document all interfaces
|
22 |
-
* @
|
23 |
*/
|
24 |
int queue_init(queue_t* queue);
|
25 |
|
26 |
int queue_destroy(queue_t* queue);
|
27 |
|
|
|
|
|
|
|
28 |
bool queue_is_empty(queue_t* queue);
|
29 |
int queue_enqueue(queue_t* queue, const size_t data);
|
30 |
int queue_dequeue(queue_t* queue, size_t* data);
|
31 |
void queue_clear(queue_t* queue);
|
32 |
|
33 |
#endif // QUEUE_H
|
1 |
// Copyright 2021 Jeisson Hidalgo-Cespedes <jeisson.hidalgo@ucr.ac.cr> CC-BY-4
|
2 |
// TODO(all): Implement a thread-safe queue
|
3 |
|
4 |
#ifndef QUEUE_H
|
5 |
#define QUEUE_H
|
6 |
|
7 |
#include <pthread.h>
|
8 |
#include <stdbool.h>
|
9 |
|
10 |
typedef struct queue_node {
|
11 |
size_t data;
|
12 |
struct queue_node* next;
|
13 |
} queue_node_t;
|
14 |
|
15 |
typedef struct {
|
16 |
+
pthread_mutex_t can_access_queue;
|
17 |
queue_node_t* head;
|
18 |
queue_node_t* tail;
|
19 |
} queue_t;
|
20 |
|
21 |
/**
|
22 |
* @todo: document all interfaces
|
23 |
+
* @remarks This subroutine is NOT thread-safe
|
24 |
*/
|
25 |
int queue_init(queue_t* queue);
|
26 |
|
27 |
int queue_destroy(queue_t* queue);
|
28 |
|
29 |
+
/**
|
30 |
+
* @remarks This subroutine is thread-safe
|
31 |
+
*/
|
32 |
bool queue_is_empty(queue_t* queue);
|
33 |
int queue_enqueue(queue_t* queue, const size_t data);
|
34 |
int queue_dequeue(queue_t* queue, size_t* data);
|
35 |
void queue_clear(queue_t* queue);
|
36 |
|
37 |
#endif // QUEUE_H
|