Skip to content

Commit

Permalink
Swap to use C++11 thread ids. uv_thread did not work ...
Browse files Browse the repository at this point in the history
  • Loading branch information
btsimonh committed Mar 20, 2018
1 parent 22e520e commit 016099c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
29 changes: 19 additions & 10 deletions cc/core/CustomAllocator.cc
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
#include "CustomAllocator.h"

//#include <iostream>


cv::UMatData* CustomMatAllocator::allocate(int dims, const int* sizes, int type,
void* data0, size_t* step, int flags, cv::UMatUsageFlags usageFlags) const
{
cv::UMatData* u = stdAllocator->allocate(dims, sizes, type, data0, step, flags, usageFlags);

if (NULL != u){
u->prevAllocator = u->currAllocator = this;
if( !(u->flags & cv::UMatData::USER_ALLOCATED) ){
// make mem change atomic
variables->MemTotalChangeMutex.lock();
variables->TotalMem += u->size;
variables->CountMemAllocs ++;
variables->MemTotalChangeMutex.unlock();
this->FixupJSMem();
try {
variables->MemTotalChangeMutex.lock();
variables->TotalMem += u->size;
variables->CountMemAllocs ++;
variables->MemTotalChangeMutex.unlock();
this->FixupJSMem();
} catch (...){
printf("exception adjusting memory\n");
}
}
}
return u;
Expand Down Expand Up @@ -80,17 +84,22 @@ __int64 CustomMatAllocator::readnumdeallocated(){

void CustomMatAllocator::FixupJSMem() const {
// we can only do this IF we are on the main thread.
uv_thread_t me = uv_thread_self();

if (uv_thread_equal(&(variables->main_thread), &me)){
std::thread::id this_id = std::this_thread::get_id();

if (variables->main_thread_id == this_id){
//std::cout << "thead is main " << this_id << "\n";
variables->MemTotalChangeMutex.lock();
__int64 adjust = variables->TotalMem - variables->TotalJSMem;
variables->TotalJSMem += adjust;
variables->MemTotalChangeMutex.unlock();

if (adjust){
//printf("will call Nan ajust by %d\n", (int)adjust);
Nan::AdjustExternalMemory(adjust);
//printf("done ajust by %d\n", (int)adjust);
}
} else {
//std::cout << "thead not main " << this_id << "\n";
}
}

6 changes: 4 additions & 2 deletions cc/core/CustomAllocator.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef __FF_CUSTOMALLOCATOR_H__
#define __FF_CUSTOMALLOCATOR_H__

#include <thread>

#include "Converters.h"
#include "Size.h"
#include "coreUtils.h"
Expand Down Expand Up @@ -28,7 +30,7 @@ class CustomMatAllocator : public cv::MatAllocator
__int64 TotalJSMem; // total mem told to JS so far

// the main JS thread
uv_thread_t main_thread;
std::thread::id main_thread_id;
} VARIABLES;

CustomMatAllocator( ) {
Expand All @@ -39,7 +41,7 @@ class CustomMatAllocator : public cv::MatAllocator
variables->CountMemDeAllocs = 0;
variables->TotalJSMem = 0; // total mem told to JS so far

variables->main_thread = uv_thread_self(); // get the main thread at create
variables->main_thread_id = std::this_thread::get_id();
}
~CustomMatAllocator( ) {
delete variables;
Expand Down

0 comments on commit 016099c

Please sign in to comment.