OLD | NEW |
| (Empty) |
1 /* Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
2 * Use of this source code is governed by a BSD-style license that can be | |
3 * found in the LICENSE file. | |
4 */ | |
5 | |
6 #include "queue.h" | |
7 | |
8 #include <pthread.h> | |
9 #include <stdlib.h> | |
10 | |
11 #define MAX_QUEUE_SIZE 16 | |
12 | |
13 /** A mutex that guards |g_queue|. */ | |
14 static pthread_mutex_t g_queue_mutex; | |
15 | |
16 /** A condition variable that is signalled when |g_queue| is not empty. */ | |
17 static pthread_cond_t g_queue_not_empty_cond; | |
18 | |
19 /** A circular queue of messages from JavaScript to be handled. | |
20 * | |
21 * If g_queue_start < g_queue_end: | |
22 * all elements in the range [g_queue_start, g_queue_end) are valid. | |
23 * If g_queue_start > g_queue_end: | |
24 * all elements in the ranges [0, g_queue_end) and | |
25 * [g_queue_start, MAX_QUEUE_SIZE) are valid. | |
26 * If g_queue_start == g_queue_end, and g_queue_size > 0: | |
27 * all elements in the g_queue are valid. | |
28 * If g_queue_start == g_queue_end, and g_queue_size == 0: | |
29 * No elements are valid. */ | |
30 static char* g_queue[MAX_QUEUE_SIZE]; | |
31 | |
32 /** The index of the head of the queue. */ | |
33 static int g_queue_start = 0; | |
34 | |
35 /** The index of the tail of the queue, non-inclusive. */ | |
36 static int g_queue_end = 0; | |
37 | |
38 /** The size of the queue. */ | |
39 static int g_queue_size = 0; | |
40 | |
41 /** Return whether the queue is empty. | |
42 * | |
43 * NOTE: this function assumes g_queue_mutex lock is held. | |
44 * @return non-zero if the queue is empty. */ | |
45 static int IsQueueEmpty() { | |
46 return g_queue_size == 0; | |
47 } | |
48 | |
49 /** Return whether the queue is full. | |
50 * | |
51 * NOTE: this function assumes g_queue_mutex lock is held. | |
52 * @return non-zero if the queue is full. */ | |
53 static int IsQueueFull() { | |
54 return g_queue_size == MAX_QUEUE_SIZE; | |
55 } | |
56 | |
57 /** Initialize the message queue. */ | |
58 void InitializeMessageQueue() { | |
59 pthread_mutex_init(&g_queue_mutex, NULL); | |
60 pthread_cond_init(&g_queue_not_empty_cond, NULL); | |
61 } | |
62 | |
63 /** Enqueue a message (i.e. add to the end) | |
64 * | |
65 * If the queue is full, the message will be dropped. | |
66 * | |
67 * NOTE: this function assumes g_queue_mutex is _NOT_ held. | |
68 * @param[in] message The message to enqueue. | |
69 * @return non-zero if the message was added to the queue. */ | |
70 int EnqueueMessage(char* message) { | |
71 pthread_mutex_lock(&g_queue_mutex); | |
72 | |
73 /* We shouldn't block the main thread waiting for the queue to not be full, | |
74 * so just drop the message. */ | |
75 if (IsQueueFull()) { | |
76 pthread_mutex_unlock(&g_queue_mutex); | |
77 free(message); | |
78 return 0; | |
79 } | |
80 | |
81 g_queue[g_queue_end] = message; | |
82 g_queue_end = (g_queue_end + 1) % MAX_QUEUE_SIZE; | |
83 g_queue_size++; | |
84 | |
85 pthread_cond_signal(&g_queue_not_empty_cond); | |
86 | |
87 pthread_mutex_unlock(&g_queue_mutex); | |
88 | |
89 return 1; | |
90 } | |
91 | |
92 /** Dequeue a message and return it. | |
93 * | |
94 * This function blocks until a message is available. It should not be called | |
95 * on the main thread. | |
96 * | |
97 * NOTE: this function assumes g_queue_mutex is _NOT_ held. | |
98 * @return The message at the head of the queue. */ | |
99 char* DequeueMessage() { | |
100 char* message = NULL; | |
101 | |
102 pthread_mutex_lock(&g_queue_mutex); | |
103 | |
104 while (IsQueueEmpty()) { | |
105 pthread_cond_wait(&g_queue_not_empty_cond, &g_queue_mutex); | |
106 } | |
107 | |
108 message = g_queue[g_queue_start]; | |
109 g_queue_start = (g_queue_start + 1) % MAX_QUEUE_SIZE; | |
110 g_queue_size--; | |
111 | |
112 pthread_mutex_unlock(&g_queue_mutex); | |
113 | |
114 return message; | |
115 } | |
OLD | NEW |