Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(262)

Side by Side Diff: native_client_sdk/src/libraries/ppapi_main/ppapi_queue.cc

Issue 15011003: ppapi_simple (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Copyright Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2013 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 #include <assert.h>
6 #include <stdlib.h>
7 #include <string.h>
8
9 #include "ppapi_main/ppapi_queue.h"
10
11 PPAPIQueue::PPAPIQueue()
12 : read_(0),
13 write_(0),
14 freed_(0),
15 size_(0),
16 array_(NULL) { }
17
18 PPAPIQueue::~PPAPIQueue() {
19 // Messages may be leaked if the queue is not empty.
20 assert(read_ == write_);
21
22 delete[] array_;
23 }
24
25 bool PPAPIQueue::SetSize(uint32_t queue_size) {
26 assert(queue_size > 0);
27
28 if (array_) return false;
29
30 array_ = new void*[queue_size];
31 size_ = queue_size;
32
33 memset(array_, 0, sizeof(void*) * queue_size);
34 return true;
35 }
36
37 bool PPAPIQueue::AddNewMessage(void* msg) {
38 // Writing a NULL message is illegal
39 assert(array_ != NULL);
40 assert(msg != NULL);
41
42 // If the slot not empty, the queue must be full. Calling RemoveStaleMessage
43 // may create space by freeing messages that have already been read.
44 if (array_[write_] != NULL) return false;
45
46 // Write to the spot
47 array_[write_] = msg;
48
49 // Fence to make sure the payload and payload pointer are visible.
50 // Since Win32 is x86 which provides ordered writes, we don't need to
51 // synchronize in that case.
52 #ifndef WIN32
53 __sync_synchronize();
54 #endif
55
56 // Increment the write pointer, to signal it's readable.
57 write_ = (write_ + 1) % size_;
58 return true;
59 }
60
61 void* PPAPIQueue::RemoveStaleMessage() {
62 assert(array_ != NULL);
63
64 // If freed and read pointer are equal, this hasn't been read yet
65 if (freed_ == read_) return NULL;
66
67 assert(array_[freed_] != NULL);
68
69 void* ret = array_[freed_];
70 array_[freed_] = NULL;
71
72 freed_ = (freed_ + 1) % size_;
73 return ret;
74 }
75
76 void* PPAPIQueue::AcquireTopMessage() {
77 // Assert that we aren't already reading a message.
78 assert(last_msg_ == NULL);
79
80 // If read and write pointers are equal, the queue is empty.
81 if (read_ == write_) return NULL;
82
83 // Track the last message to look for illegal API use.
84 last_msg_ = array_[read_];
85 return last_msg_;
86 }
87
88 void PPAPIQueue::ReleaseTopMessage(void* msg) {
89 // Verify we currently acquire message.
90 assert(msg != NULL);
91 assert(msg == last_msg_);
92
93 last_msg_ = NULL;
94
95 // Signal that the message can be freed.
96 read_ = (read_ + 1) % size_;
97 }
OLDNEW
« no previous file with comments | « native_client_sdk/src/libraries/ppapi_main/ppapi_queue.h ('k') | native_client_sdk/src/libraries/ppapi_simple/library.dsc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698