OLD | NEW |
(Empty) | |
| 1 //===-- SRPCStreamer.cpp - Stream bitcode over SRPC ----------------------===// |
| 2 // |
| 3 // The LLVM Compiler Infrastructure |
| 4 // |
| 5 // This file is distributed under the University of Illinois Open Source |
| 6 // License. See LICENSE.TXT for details. |
| 7 // |
| 8 //===----------------------------------------------------------------------===// |
| 9 #if defined(PNACL_BROWSER_TRANSLATOR) |
| 10 #define DEBUG_TYPE "bitcode-stream" |
| 11 #include "SRPCStreamer.h" |
| 12 #include "llvm/Support/Debug.h" |
| 13 #include "llvm/Support/raw_ostream.h" |
| 14 #include <errno.h> |
| 15 |
| 16 using llvm::dbgs; |
| 17 |
| 18 const size_t QueueStreamer::queuesize_limit_; |
| 19 |
| 20 size_t QueueStreamer::GetBytes(unsigned char *buf, size_t len) { |
| 21 size_t total_copied = 0; |
| 22 pthread_mutex_lock(&Mutex); |
| 23 while (!Done && queueSize() < len - total_copied) { |
| 24 size_t size = queueSize(); |
| 25 DEBUG(dbgs() << "QueueStreamer::GetBytes len " << len << " size " << |
| 26 size << " << waiting\n"); |
| 27 queueGet(buf + total_copied, size); |
| 28 total_copied += size; |
| 29 pthread_cond_signal(&Cond); |
| 30 pthread_cond_wait(&Cond, &Mutex); |
| 31 } |
| 32 // If this is the last partial chunk, adjust len such that the amount we |
| 33 // fetch will be just the remaining bytes. |
| 34 if (Done && queueSize() < len - total_copied) { |
| 35 len = queueSize() + total_copied; |
| 36 } |
| 37 queueGet(buf + total_copied, len - total_copied); |
| 38 pthread_cond_signal(&Cond); |
| 39 pthread_mutex_unlock(&Mutex); |
| 40 return len; |
| 41 } |
| 42 |
| 43 size_t QueueStreamer::PutBytes(unsigned char *buf, size_t len) { |
| 44 size_t total_copied = 0; |
| 45 pthread_mutex_lock(&Mutex); |
| 46 while (capacityRemaining() < len - total_copied) { |
| 47 if (Bytes.size() * 2 > queuesize_limit_) { |
| 48 size_t space = capacityRemaining(); |
| 49 queuePut(buf + total_copied, space); |
| 50 total_copied += space; |
| 51 pthread_cond_signal(&Cond); |
| 52 pthread_cond_wait(&Cond, &Mutex); |
| 53 } else { |
| 54 queueResize(); |
| 55 } |
| 56 } |
| 57 queuePut(buf + total_copied, len - total_copied); |
| 58 pthread_cond_signal(&Cond); |
| 59 pthread_mutex_unlock(&Mutex); |
| 60 return len; |
| 61 } |
| 62 |
| 63 void QueueStreamer::SetDone() { |
| 64 // Still need the lock to avoid signaling between the check and |
| 65 // the wait in GetBytes. |
| 66 pthread_mutex_lock(&Mutex); |
| 67 Done = true; |
| 68 pthread_cond_signal(&Cond); |
| 69 pthread_mutex_unlock(&Mutex); |
| 70 } |
| 71 |
| 72 // Double the size of the queue. Called with Mutex to protect Cons/Prod/Bytes. |
| 73 void QueueStreamer::queueResize() { |
| 74 int leftover = Bytes.size() - Cons; |
| 75 DEBUG(dbgs() << "resizing to " << Bytes.size() * 2 << " " << leftover << " " |
| 76 << Prod << " " << Cons << "\n"); |
| 77 Bytes.resize(Bytes.size() * 2); |
| 78 if (Cons > Prod) { |
| 79 // There are unread bytes left between Cons and the previous end of the |
| 80 // buffer. Move them to the new end of the buffer. |
| 81 memmove(&Bytes[Bytes.size() - leftover], &Bytes[Cons], leftover); |
| 82 Cons = Bytes.size() - leftover; |
| 83 } |
| 84 } |
| 85 |
| 86 // Called with Mutex held to protect Cons, Prod, and Bytes |
| 87 void QueueStreamer::queuePut(unsigned char *buf, size_t len) { |
| 88 size_t EndSpace = std::min(len, Bytes.size() - Prod); |
| 89 DEBUG(dbgs() << "put, len " << len << " Endspace " << EndSpace << " p " << |
| 90 Prod << " c " << Cons << "\n"); |
| 91 // Copy up to the end of the buffer |
| 92 memcpy(&Bytes[Prod], buf, EndSpace); |
| 93 // Wrap around if necessary |
| 94 memcpy(&Bytes[0], buf + EndSpace, len - EndSpace); |
| 95 Prod = (Prod + len) % Bytes.size(); |
| 96 } |
| 97 |
| 98 // Called with Mutex held to protect Cons, Prod, and Bytes |
| 99 void QueueStreamer::queueGet(unsigned char *buf, size_t len) { |
| 100 assert(len <= queueSize()); |
| 101 size_t EndSpace = std::min(len, Bytes.size() - Cons); |
| 102 DEBUG(dbgs() << "get, len " << len << " Endspace " << EndSpace << " p " << |
| 103 Prod << " c " << Cons << "\n"); |
| 104 // Copy up to the end of the buffer |
| 105 memcpy(buf, &Bytes[Cons], EndSpace); |
| 106 // Wrap around if necessary |
| 107 memcpy(buf + EndSpace, &Bytes[0], len - EndSpace); |
| 108 Cons = (Cons + len) % Bytes.size(); |
| 109 } |
| 110 |
| 111 llvm::DataStreamer *SRPCStreamer::init(void *(*Callback)(void *), void *arg, |
| 112 std::string *ErrMsg) { |
| 113 int err = pthread_create(&CompileThread, NULL, Callback, arg); |
| 114 if (err) { |
| 115 if (ErrMsg) *ErrMsg = std::string(strerror(errno)); |
| 116 return NULL; |
| 117 } |
| 118 return &Q; |
| 119 } |
| 120 |
| 121 size_t SRPCStreamer::gotChunk(unsigned char *bytes, size_t len) { |
| 122 if (__sync_fetch_and_add(&Error, 0)) return 0; // Atomic read. |
| 123 return Q.PutBytes(bytes, len); |
| 124 } |
| 125 |
| 126 int SRPCStreamer::streamEnd(std::string *ErrMsg) { |
| 127 Q.SetDone(); |
| 128 int err = pthread_join(CompileThread, NULL); |
| 129 __sync_synchronize(); |
| 130 if (Error) { |
| 131 if (ErrMsg) |
| 132 *ErrMsg = std::string("PNaCl Translator Error: " + ErrorMessage); |
| 133 return 1; |
| 134 } else if (err) { |
| 135 if (ErrMsg) *ErrMsg = std::string(strerror(errno)); |
| 136 return err; |
| 137 } |
| 138 return 0; |
| 139 } |
| 140 |
| 141 void SRPCStreamer::setFatalError(const std::string& message) { |
| 142 __sync_fetch_and_add(&Error, 1); |
| 143 ErrorMessage = message; |
| 144 __sync_synchronize(); |
| 145 pthread_exit(NULL); |
| 146 } |
| 147 |
| 148 #endif // __native_client__ |
OLD | NEW |