Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / native_client_sdk / src / examples / demo / nacl_io_demo / queue.c
blobab0b0ba0075f5365dd8025d91ca8c095a9ebc7b5
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. */
5 #include "queue.h"
7 #include <pthread.h>
8 #include <stdlib.h>
10 #include "ppapi/c/pp_var.h"
12 #define MAX_QUEUE_SIZE 16
14 /** A mutex that guards |g_queue|. */
15 static pthread_mutex_t g_queue_mutex;
17 /** A condition variable that is signalled when |g_queue| is not empty. */
18 static pthread_cond_t g_queue_not_empty_cond;
20 /** A circular queue of messages from JavaScript to be handled.
22 * If g_queue_start < g_queue_end:
23 * all elements in the range [g_queue_start, g_queue_end) are valid.
24 * If g_queue_start > g_queue_end:
25 * all elements in the ranges [0, g_queue_end) and
26 * [g_queue_start, MAX_QUEUE_SIZE) are valid.
27 * If g_queue_start == g_queue_end, and g_queue_size > 0:
28 * all elements in the g_queue are valid.
29 * If g_queue_start == g_queue_end, and g_queue_size == 0:
30 * No elements are valid. */
31 static struct PP_Var g_queue[MAX_QUEUE_SIZE];
33 /** The index of the head of the queue. */
34 static int g_queue_start = 0;
36 /** The index of the tail of the queue, non-inclusive. */
37 static int g_queue_end = 0;
39 /** The size of the queue. */
40 static int g_queue_size = 0;
42 /** Return whether the queue is empty.
44 * NOTE: this function assumes g_queue_mutex lock is held.
45 * @return non-zero if the queue is empty. */
46 static int IsQueueEmpty() { return g_queue_size == 0; }
48 /** Return whether the queue is full.
50 * NOTE: this function assumes g_queue_mutex lock is held.
51 * @return non-zero if the queue is full. */
52 static int IsQueueFull() { return g_queue_size == MAX_QUEUE_SIZE; }
54 /** Initialize the message queue. */
55 void InitializeMessageQueue() {
56 pthread_mutex_init(&g_queue_mutex, NULL);
57 pthread_cond_init(&g_queue_not_empty_cond, NULL);
60 /** Enqueue a message (i.e. add to the end)
62 * If the queue is full, the message will be dropped.
64 * NOTE: this function assumes g_queue_mutex is _NOT_ held.
65 * @param[in] message The message to enqueue.
66 * @return non-zero if the message was added to the queue. */
67 int EnqueueMessage(struct PP_Var message) {
68 pthread_mutex_lock(&g_queue_mutex);
70 /* We shouldn't block the main thread waiting for the queue to not be full,
71 * so just drop the message. */
72 if (IsQueueFull()) {
73 pthread_mutex_unlock(&g_queue_mutex);
74 return 0;
77 g_queue[g_queue_end] = message;
78 g_queue_end = (g_queue_end + 1) % MAX_QUEUE_SIZE;
79 g_queue_size++;
81 pthread_cond_signal(&g_queue_not_empty_cond);
83 pthread_mutex_unlock(&g_queue_mutex);
85 return 1;
88 /** Dequeue a message and return it.
90 * This function blocks until a message is available. It should not be called
91 * on the main thread.
93 * NOTE: this function assumes g_queue_mutex is _NOT_ held.
94 * @return The message at the head of the queue. */
95 struct PP_Var DequeueMessage() {
96 struct PP_Var message;
98 pthread_mutex_lock(&g_queue_mutex);
100 while (IsQueueEmpty()) {
101 pthread_cond_wait(&g_queue_not_empty_cond, &g_queue_mutex);
104 message = g_queue[g_queue_start];
105 g_queue_start = (g_queue_start + 1) % MAX_QUEUE_SIZE;
106 g_queue_size--;
108 pthread_mutex_unlock(&g_queue_mutex);
110 return message;