99#include <signal.h>
1010
1111#include "_vmprof.h"
12- #include "vmprof_common.h"
1312
13+ static volatile int is_enabled = 0 ;
1414static destructor Original_code_dealloc = 0 ;
1515static PyObject * (* _default_eval_loop )(PyFrameObject * , int ) = 0 ;
1616
1717#if VMPROF_UNIX
1818#include "trampoline.h"
1919#include "machine.h"
2020#include "symboltable.h"
21- #include "vmprof_unix .h"
21+ #include "vmprof_main .h"
2222#else
23- #include "vmprof_win .h"
23+ #include "vmprof_main_win32 .h"
2424#endif
2525#include "vmp_stack.h"
2626
@@ -156,7 +156,7 @@ void emit_all_code_objects(PyObject * seen_code_ids)
156156
157157static void cpyprof_code_dealloc (PyObject * co )
158158{
159- if (vmprof_is_enabled () ) {
159+ if (is_enabled ) {
160160 emit_code_object ((PyCodeObject * )co );
161161 /* xxx error return values are ignored */
162162 }
@@ -187,7 +187,7 @@ static PyObject *enable_vmprof(PyObject* self, PyObject *args)
187187 return NULL ;
188188 }
189189
190- if (vmprof_is_enabled () ) {
190+ if (is_enabled ) {
191191 PyErr_SetString (PyExc_ValueError , "vmprof is already enabled" );
192192 return NULL ;
193193 }
@@ -217,13 +217,13 @@ static PyObject *enable_vmprof(PyObject* self, PyObject *args)
217217 return NULL ;
218218 }
219219
220- vmprof_set_enabled ( 1 ) ;
220+ is_enabled = 1 ;
221221
222222 Py_RETURN_NONE ;
223223}
224224
225225static PyObject * vmp_is_enabled (PyObject * module , PyObject * noargs ) {
226- if (vmprof_is_enabled () ) {
226+ if (is_enabled ) {
227227 Py_RETURN_TRUE ;
228228 }
229229 Py_RETURN_FALSE ;
@@ -237,7 +237,7 @@ disable_vmprof(PyObject *module, PyObject *noargs)
237237 return NULL ;
238238 }
239239
240- vmprof_set_enabled ( 0 ) ;
240+ is_enabled = 0 ;
241241
242242 if (PyErr_Occurred ())
243243 return NULL ;
@@ -362,7 +362,7 @@ start_sampling(PyObject *module, PyObject *noargs)
362362#ifdef VMPROF_UNIX
363363static PyObject * vmp_get_profile_path (PyObject * module , PyObject * noargs ) {
364364 PyObject * o ;
365- if (vmprof_is_enabled () ) {
365+ if (is_enabled ) {
366366 char buffer [4096 ];
367367 buffer [0 ] = 0 ;
368368 ssize_t buffer_len = vmp_fd_to_path (vmp_profile_fileno (), buffer , 4096 );
@@ -382,19 +382,21 @@ static PyObject *
382382insert_real_time_thread (PyObject * module , PyObject * noargs ) {
383383 ssize_t thread_count ;
384384
385- if (!vmprof_is_enabled () ) {
385+ if (!is_enabled ) {
386386 PyErr_SetString (PyExc_ValueError , "vmprof is not enabled" );
387387 return NULL ;
388388 }
389389
390- if (vmprof_get_signal_type () != SIGALRM ) {
390+ if (signal_type != SIGALRM ) {
391391 PyErr_SetString (PyExc_ValueError , "vmprof is not in real time mode" );
392392 return NULL ;
393393 }
394394
395- vmprof_aquire_lock ();
395+ while (__sync_lock_test_and_set (& spinlock , 1 )) {
396+ }
397+
396398 thread_count = insert_thread (pthread_self (), -1 );
397- vmprof_release_lock ( );
399+ __sync_lock_release ( & spinlock );
398400
399401 return PyLong_FromSsize_t (thread_count );
400402}
@@ -403,19 +405,21 @@ static PyObject *
403405remove_real_time_thread (PyObject * module , PyObject * noargs ) {
404406 ssize_t thread_count ;
405407
406- if (!vmprof_is_enabled () ) {
408+ if (!is_enabled ) {
407409 PyErr_SetString (PyExc_ValueError , "vmprof is not enabled" );
408410 return NULL ;
409411 }
410412
411- if (vmprof_get_signal_type () != SIGALRM ) {
413+ if (signal_type != SIGALRM ) {
412414 PyErr_SetString (PyExc_ValueError , "vmprof is not in real time mode" );
413415 return NULL ;
414416 }
415417
416- vmprof_aquire_lock ();
418+ while (__sync_lock_test_and_set (& spinlock , 1 )) {
419+ }
420+
417421 thread_count = remove_thread (pthread_self (), -1 );
418- vmprof_release_lock ( );
422+ __sync_lock_release ( & spinlock );
419423
420424 return PyLong_FromSsize_t (thread_count );
421425}
0 commit comments