diff options
author | Junyan He <junyan.he@linux.intel.com> | 2014-06-18 14:42:15 +0800 |
---|---|---|
committer | Zhigang Gong <zhigang.gong@intel.com> | 2014-06-19 17:31:56 +0800 |
commit | 0c98415a79d4129629bb383a1b8658bcfa641598 (patch) | |
tree | a391aaeac3e48e79ecddfa5c22294f420e92398b | |
parent | 3b48353885fa19efd04980a0ed8623f103ad7fae (diff) |
Add a lock in the place of printf output
If multi-thread run the kernel simultaneously, the output
may interlace with each other. Add a lock to avoid this.
Signed-off-by: Junyan He <junyan.he@linux.intel.com>
Reviewed-by: Zhigang Gong <zhigang.gong@linux.intel.com>
-rw-r--r-- | backend/src/ir/printf.cpp | 3 | ||||
-rw-r--r-- | backend/src/ir/printf.hpp | 44 |
2 files changed, 37 insertions, 10 deletions
diff --git a/backend/src/ir/printf.cpp b/backend/src/ir/printf.cpp index 3729155c..0a943ac1 100644 --- a/backend/src/ir/printf.cpp +++ b/backend/src/ir/printf.cpp @@ -29,6 +29,8 @@ namespace gbe { namespace ir { + pthread_mutex_t PrintfSet::lock = PTHREAD_MUTEX_INITIALIZER; + uint32_t PrintfSet::append(PrintfFmt* fmt, Unit& unit) { fmts.push_back(*fmt); @@ -74,6 +76,7 @@ namespace gbe void PrintfSet::outputPrintf(void* index_addr, void* buf_addr, size_t global_wk_sz0, size_t global_wk_sz1, size_t global_wk_sz2) { + LockOutput lock; size_t i, j, k; std::string pf_str; vector<int>* contents = NULL; diff --git a/backend/src/ir/printf.hpp b/backend/src/ir/printf.hpp index 5763a651..b49ad0da 100644 --- a/backend/src/ir/printf.hpp +++ b/backend/src/ir/printf.hpp @@ -89,12 +89,14 @@ namespace gbe void *ptr; }; - PrintfSlot(void) { + PrintfSlot(void) + { type = PRINTF_SLOT_TYPE_NONE; ptr = NULL; } - PrintfSlot(const char * s) { + PrintfSlot(const char * s) + { type = PRINTF_SLOT_TYPE_STRING; int len = strlen(s); str = (char*)malloc((len + 1) * sizeof(char)); @@ -102,13 +104,15 @@ namespace gbe str[len] = 0; } - PrintfSlot(PrintfState * st) { + PrintfSlot(PrintfState * st) + { type = PRINTF_SLOT_TYPE_STATE; state = (PrintfState *)malloc(sizeof(PrintfState)); memcpy(state, st, sizeof(PrintfState)); } - PrintfSlot(const PrintfSlot & other) { + PrintfSlot(const PrintfSlot & other) + { if (other.type == PRINTF_SLOT_TYPE_STRING) { int len = strlen(other.str); str = (char*)malloc((len + 1) * sizeof(char)); @@ -125,7 +129,8 @@ namespace gbe } } - PrintfSlot(PrintfSlot && other) { + PrintfSlot(PrintfSlot && other) + { void *p = other.ptr; type = other.type; other.ptr = ptr; @@ -133,7 +138,8 @@ namespace gbe } - ~PrintfSlot(void) { + ~PrintfSlot(void) + { if (ptr) free(ptr); } @@ -144,7 +150,8 @@ namespace gbe class PrintfSet //: public Serializable { public: - PrintfSet(const PrintfSet& other) { + PrintfSet(const PrintfSet& other) + { for (auto &f : other.fmts) { fmts.push_back(f); } @@ -158,18 +165,33 @@ namespace gbe PrintfSet(void) = default; + struct LockOutput { + LockOutput(void) + { + pthread_mutex_lock(&lock); + } + + ~LockOutput(void) + { + pthread_mutex_unlock(&lock); + } + }; + typedef vector<PrintfSlot> PrintfFmt; uint32_t append(PrintfFmt* fmt, Unit &unit); - uint32_t getPrintfNum(void) const { + uint32_t getPrintfNum(void) const + { return fmts.size(); } - uint32_t getPrintfSizeOfSize(void) const { + uint32_t getPrintfSizeOfSize(void) const + { return sizeOfSize; } - uint32_t getPrintfBufferElementSize(uint32_t i) { + uint32_t getPrintfBufferElementSize(uint32_t i) + { PrintfSlot* slot = slots[i]; switch (slot->state->conversion_specifier) { case PRINTF_CONVERSION_I: @@ -189,6 +211,8 @@ namespace gbe vector<PrintfFmt> fmts; vector<PrintfSlot*> slots; uint32_t sizeOfSize; // Total sizeof size. + friend struct LockOutput; + static pthread_mutex_t lock; GBE_CLASS(PrintfSet); }; } /* namespace ir */ |