-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PR-URL: #147 Reviewed-BY: hyj1991 <[email protected]>
- Loading branch information
1 parent
88fc7ed
commit c114f45
Showing
10 changed files
with
200 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#include "environment_registry.h" | ||
|
||
namespace xprofiler { | ||
|
||
EnvironmentRegistry::NoExitScope::NoExitScope(EnvironmentRegistry* registry) | ||
: registry_(registry), lock_(registry->mutex_) { | ||
registry_->disallow_exit_ = true; | ||
} | ||
|
||
EnvironmentRegistry::NoExitScope::~NoExitScope() { | ||
registry_->disallow_exit_ = false; | ||
} | ||
|
||
void EnvironmentRegistry::Register(v8::Isolate* isolate, | ||
std::unique_ptr<EnvironmentData> env) { | ||
CHECK(disallow_exit_); | ||
map_.emplace(isolate, std::move(env)); | ||
} | ||
|
||
void EnvironmentRegistry::Unregister(v8::Isolate* isolate) { | ||
CHECK(disallow_exit_); | ||
CHECK_NE(map_.find(isolate), map_.end()); | ||
map_.erase(isolate); | ||
} | ||
|
||
EnvironmentData* EnvironmentRegistry::Get(v8::Isolate* isolate) { | ||
CHECK(disallow_exit_); | ||
auto it = map_.find(isolate); | ||
CHECK_NE(it, map_.end()); | ||
return it->second.get(); | ||
} | ||
|
||
EnvironmentRegistry::Iterator EnvironmentRegistry::begin() { | ||
CHECK(disallow_exit_); | ||
return Iterator(map_.begin()); | ||
} | ||
|
||
EnvironmentRegistry::Iterator EnvironmentRegistry::end() { | ||
CHECK(disallow_exit_); | ||
return Iterator(map_.end()); | ||
} | ||
|
||
} // namespace xprofiler |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
#ifndef XPROFILER_SRC_ENVIRONMENT_REGISTRY_H | ||
#define XPROFILER_SRC_ENVIRONMENT_REGISTRY_H | ||
|
||
#include <memory> | ||
#include <unordered_map> | ||
|
||
#include "environment_data.h" | ||
#include "nan.h" | ||
#include "xpf_mutex-inl.h" | ||
|
||
namespace xprofiler { | ||
|
||
class EnvironmentRegistry { | ||
using Map = | ||
std::unordered_map<v8::Isolate*, std::unique_ptr<EnvironmentData>>; | ||
|
||
public: | ||
class NoExitScope { | ||
public: | ||
explicit NoExitScope(EnvironmentRegistry* registry); | ||
NoExitScope(const NoExitScope& other) = delete; | ||
~NoExitScope(); | ||
|
||
private: | ||
EnvironmentRegistry* registry_; | ||
Mutex::ScopedLock lock_; | ||
}; | ||
|
||
class Iterator { | ||
public: | ||
EnvironmentData* operator*() { return it_->second.get(); }; | ||
bool operator==(const Iterator& other) { return it_ == other.it_; }; | ||
bool operator==(Iterator& other) { return it_ == other.it_; }; | ||
bool operator!=(const Iterator& other) { return it_ != other.it_; }; | ||
bool operator!=(Iterator& other) { return it_ != other.it_; }; | ||
|
||
Iterator operator++() { return Iterator(it_++); } | ||
|
||
private: | ||
friend EnvironmentRegistry; | ||
explicit Iterator(Map::iterator it) : it_(it){}; | ||
Map::iterator it_; | ||
}; | ||
|
||
EnvironmentRegistry(){}; | ||
// Disallow copy | ||
EnvironmentRegistry(const EnvironmentRegistry& other) = delete; | ||
|
||
void Register(v8::Isolate* isolate, std::unique_ptr<EnvironmentData> env); | ||
void Unregister(v8::Isolate* isolate); | ||
EnvironmentData* Get(v8::Isolate* isolate); | ||
|
||
Iterator begin(); | ||
Iterator end(); | ||
|
||
private: | ||
friend NoExitScope; | ||
|
||
bool disallow_exit_ = false; | ||
Mutex mutex_; | ||
Map map_; | ||
}; | ||
|
||
} // namespace xprofiler | ||
|
||
#endif /* XPROFILER_SRC_ENVIRONMENT_REGISTRY_H */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#include "process_data.h" | ||
|
||
namespace xprofiler { | ||
|
||
namespace per_process { | ||
ProcessData process_data; | ||
} | ||
|
||
ProcessData* ProcessData::Get() { return &per_process::process_data; } | ||
|
||
} // namespace xprofiler |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters