[SyncFS] Build indexes from FileTracker entries on disk.
[chromium-blink-merge.git] / native_client_sdk / src / examples / demo / nacl_io_demo / queue.c
blob043ee555c4829414d6da49415614d3e73a508159
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 */
6 #include "queue.h"
8 #include <pthread.h>
9 #include <stdlib.h>
11 #include "ppapi/c/pp_var.h"
13 #define MAX_QUEUE_SIZE 16
15 /** A mutex that guards |g_queue|. */
16 static pthread_mutex_t g_queue_mutex;
18 /** A condition variable that is signalled when |g_queue| is not empty. */
19 static pthread_cond_t g_queue_not_empty_cond;
21 /** A circular queue of messages from JavaScript to be handled.
23 * If g_queue_start < g_queue_end:
24 * all elements in the range [g_queue_start, g_queue_end) are valid.
25 * If g_queue_start > g_queue_end:
26 * all elements in the ranges [0, g_queue_end) and
27 * [g_queue_start, MAX_QUEUE_SIZE) are valid.
28 * If g_queue_start == g_queue_end, and g_queue_size > 0:
29 * all elements in the g_queue are valid.
30 * If g_queue_start == g_queue_end, and g_queue_size == 0:
31 * No elements are valid. */
32 static struct PP_Var g_queue[MAX_QUEUE_SIZE];
34 /** The index of the head of the queue. */
35 static int g_queue_start = 0;
37 /** The index of the tail of the queue, non-inclusive. */
38 static int g_queue_end = 0;
40 /** The size of the queue. */
41 static int g_queue_size = 0;
43 /** Return whether the queue is empty.
45 * NOTE: this function assumes g_queue_mutex lock is held.
46 * @return non-zero if the queue is empty. */
47 static int IsQueueEmpty() { return g_queue_size == 0; }
49 /** Return whether the queue is full.
51 * NOTE: this function assumes g_queue_mutex lock is held.
52 * @return non-zero if the queue is full. */
53 static int IsQueueFull() { return g_queue_size == MAX_QUEUE_SIZE; }
55 /** Initialize the message queue. */
56 void InitializeMessageQueue() {
57 pthread_mutex_init(&g_queue_mutex, NULL);
58 pthread_cond_init(&g_queue_not_empty_cond, NULL);
61 /** Enqueue a message (i.e. add to the end)
63 * If the queue is full, the message will be dropped.
65 * NOTE: this function assumes g_queue_mutex is _NOT_ held.
66 * @param[in] message The message to enqueue.
67 * @return non-zero if the message was added to the queue. */
68 int EnqueueMessage(struct PP_Var message) {
69 pthread_mutex_lock(&g_queue_mutex);
71 /* We shouldn't block the main thread waiting for the queue to not be full,
72 * so just drop the message. */
73 if (IsQueueFull()) {
74 pthread_mutex_unlock(&g_queue_mutex);
75 return 0;
78 g_queue[g_queue_end] = message;
79 g_queue_end = (g_queue_end + 1) % MAX_QUEUE_SIZE;
80 g_queue_size++;
82 pthread_cond_signal(&g_queue_not_empty_cond);
84 pthread_mutex_unlock(&g_queue_mutex);
86 return 1;
89 /** Dequeue a message and return it.
91 * This function blocks until a message is available. It should not be called
92 * on the main thread.
94 * NOTE: this function assumes g_queue_mutex is _NOT_ held.
95 * @return The message at the head of the queue. */
96 struct PP_Var DequeueMessage() {
97 struct PP_Var message;
99 pthread_mutex_lock(&g_queue_mutex);
101 while (IsQueueEmpty()) {
102 pthread_cond_wait(&g_queue_not_empty_cond, &g_queue_mutex);
105 message = g_queue[g_queue_start];
106 g_queue_start = (g_queue_start + 1) % MAX_QUEUE_SIZE;
107 g_queue_size--;
109 pthread_mutex_unlock(&g_queue_mutex);
111 return message;