77#include < hx/Thread.h>
88#include < hx/Telemetry.h>
99#include < hx/OS.h>
10+ #include < mutex>
1011
1112
1213namespace hx
@@ -62,23 +63,19 @@ class Telemetry
6263 Stash ();
6364
6465 // When a profiler exists, the profiler thread needs to exist
65- gThreadMutex . Lock ( );
66+ std::lock_guard<std::mutex> lock ( gThreadMutex );
6667
6768 gThreadRefCount += 1 ;
6869 if (gThreadRefCount == 1 ) {
6970 HxCreateDetachedThread (ProfileMainLoop, 0 );
7071 }
71-
72- gThreadMutex .Unlock ();
7372 }
7473
7574 ~Telemetry ()
7675 {
77- gThreadMutex . Lock ( );
76+ std::lock_guard<std::mutex> lock ( gThreadMutex );
7877
7978 gThreadRefCount -= 1 ;
80-
81- gThreadMutex .Unlock ();
8279 }
8380
8481 // todo
@@ -110,7 +107,7 @@ class Telemetry
110107 stash->gcoverhead = gcOverhead*1000000 ; // usec
111108 gcOverhead = 0 ;
112109
113- alloc_mutex.Lock ();
110+ alloc_mutex.lock ();
114111
115112 if (_last_obj!=0 ) lookup_last_object_type ();
116113
@@ -122,7 +119,7 @@ class Telemetry
122119 allocation_data = new std::vector<int >();
123120 }
124121
125- alloc_mutex.Unlock ();
122+ alloc_mutex.unlock ();
126123
127124 int i,size;
128125 stash->names = 0 ;
@@ -146,18 +143,19 @@ class Telemetry
146143 allocStacksStashed = allocStacks.size ();
147144 }
148145
149- gStashMutex .Lock ();
150- stashed.push_back (*stash);
151- gStashMutex .Unlock ();
146+ {
147+ std::lock_guard<std::mutex> lock (gStashMutex );
148+
149+ stashed.push_back (*stash);
150+ }
152151
153152 IgnoreAllocs (-1 );
154153 }
155154
156155 TelemetryFrame* Dump ()
157156 {
158- gStashMutex . Lock ( );
157+ std::lock_guard<std::mutex> lock ( gStashMutex );
159158 if (stashed.size ()<1 ) {
160- gStashMutex .Unlock ();
161159 return 0 ;
162160 }
163161
@@ -171,7 +169,6 @@ class Telemetry
171169 stashed.pop_front (); // Destroy item that was Dumped last call
172170
173171 front = &stashed.front ();
174- gStashMutex .Unlock ();
175172
176173 // printf(" -- dumped stash, allocs=%d, alloc[max]=%d\n", front->allocations->size(), front->allocations->size()>0 ? front->allocations->at(front->allocations->size()-1) : 0);
177174
@@ -200,7 +197,7 @@ class Telemetry
200197 const char * type = " _uninitialized" ;
201198
202199 int obj_id = __hxt_ptr_id (_last_obj);
203- alloc_mutex.Lock ();
200+ alloc_mutex.lock ();
204201 std::map<void *, hx::Telemetry*>::iterator exist = alloc_map.find (_last_obj);
205202 if (exist != alloc_map.end () && _last_obj!=(NULL )) {
206203 type = " _unknown" ;
@@ -218,7 +215,7 @@ class Telemetry
218215 // printf("Updating last allocation %016lx type to %s\n", _last_obj, type);
219216 }
220217 }
221- alloc_mutex.Unlock ();
218+ alloc_mutex.unlock ();
222219 allocation_data->at (_last_loc+2 ) = GetNameIdx (type);
223220 _last_obj = 0 ;
224221 }
@@ -255,7 +252,7 @@ class Telemetry
255252 double t0 = __hxcpp_time_stamp ();
256253
257254 Telemetry* telemetry = 0 ;
258- alloc_mutex.Lock ();
255+ alloc_mutex.lock ();
259256 std::map<void *, hx::Telemetry*>::iterator iter = alloc_map.begin ();
260257 while (iter != alloc_map.end ()) {
261258 void * obj = iter->first ;
@@ -272,7 +269,7 @@ class Telemetry
272269 iter++;
273270 }
274271 }
275- alloc_mutex.Unlock ();
272+ alloc_mutex.unlock ();
276273
277274 // Report overhead on one of the telemetry instances
278275 // TODO: something better?
@@ -338,19 +335,19 @@ class Telemetry
338335
339336 std::vector<int > *allocation_data;
340337
341- static HxMutex gStashMutex ;
342- static HxMutex gThreadMutex ;
338+ static std::mutex gStashMutex ;
339+ static std::mutex gThreadMutex ;
343340 static int gThreadRefCount ;
344341 static int gProfileClock ;
345342
346- static HxMutex alloc_mutex;
343+ static std::mutex alloc_mutex;
347344 static std::map<void *, Telemetry*> alloc_map;
348345};
349- /* static */ HxMutex Telemetry::gStashMutex ;
350- /* static */ HxMutex Telemetry::gThreadMutex ;
346+ /* static */ std::mutex Telemetry::gStashMutex ;
347+ /* static */ std::mutex Telemetry::gThreadMutex ;
351348/* static */ int Telemetry::gThreadRefCount ;
352349/* static */ int Telemetry::gProfileClock ;
353- /* static */ HxMutex Telemetry::alloc_mutex;
350+ /* static */ std::mutex Telemetry::alloc_mutex;
354351/* static */ std::map<void *, Telemetry*> Telemetry::alloc_map;
355352
356353
@@ -483,14 +480,14 @@ void hx::Telemetry::HXTAllocation(void* obj, size_t inSize, const char* type)
483480 // ExternalInterface.external_handler()), etc
484481#ifndef HXCPP_PROFILE_EXTERNS
485482 if (stack->getCurrentStackFrame ()->position ->className ==hx::EXTERN_CLASS_NAME ) {
486- alloc_mutex.Unlock ();
483+ alloc_mutex.unlock ();
487484 return ;
488485 }
489486#endif
490487
491488 int obj_id = __hxt_ptr_id (obj);
492489
493- alloc_mutex.Lock ();
490+ alloc_mutex.lock ();
494491
495492 // HXT debug: Check for id collision
496493#ifdef HXCPP_TELEMETRY_DEBUG
@@ -524,7 +521,7 @@ void hx::Telemetry::HXTAllocation(void* obj, size_t inSize, const char* type)
524521
525522 // printf("Tracking alloc %s at %016lx, id=%016lx, s=%d for telemetry %016lx, ts=%f\n", type, obj, obj_id, inSize, this, __hxcpp_time_stamp());
526523
527- alloc_mutex.Unlock ();
524+ alloc_mutex.unlock ();
528525}
529526
530527void hx::Telemetry::HXTRealloc (void * old_obj, void * new_obj, int new_size)
@@ -533,7 +530,7 @@ void hx::Telemetry::HXTRealloc(void* old_obj, void* new_obj, int new_size)
533530 int old_obj_id = __hxt_ptr_id (old_obj);
534531 int new_obj_id = __hxt_ptr_id (new_obj);
535532
536- alloc_mutex.Lock ();
533+ alloc_mutex.lock ();
537534
538535 // Only track reallocations of objects currently known to be allocated
539536 std::map<void *, hx::Telemetry*>::iterator exist = alloc_map.find (old_obj);
@@ -559,7 +556,7 @@ void hx::Telemetry::HXTRealloc(void* old_obj, void* new_obj, int new_size)
559556 HXTReclaimInternal (old_obj); // count old as reclaimed
560557 } else {
561558 // printf("Not tracking re-alloc of untracked %016lx, id=%016lx\n", old_obj, old_obj_id);
562- alloc_mutex.Unlock ();
559+ alloc_mutex.unlock ();
563560 return ;
564561 }
565562
@@ -568,7 +565,7 @@ void hx::Telemetry::HXTRealloc(void* old_obj, void* new_obj, int new_size)
568565
569566 // printf("Tracking re-alloc from %016lx, id=%016lx to %016lx, id=%016lx at %f\n", old_obj, old_obj_id, new_obj, new_obj_id, __hxcpp_time_stamp());
570567
571- alloc_mutex.Unlock ();
568+ alloc_mutex.unlock ();
572569}
573570
574571} // end namespace hx
0 commit comments