Debian Codegraph @

pyinstrument/low_level/pyi_timing_thread.c

raw symbols SCIP
1 #include "pyi_timing_thread.h"
2
3 #include <Python.h>
4 #include <time.h>
5 #include <float.h>
6
7 #include "pyi_floatclock.h"
8
9 static volatile double current_time = 0.0;
10
11 static PyThread_type_lock subscriber_lock = NULL;
12 static PyThread_type_lock update_lock = NULL;
13 static int thread_should_exit = 0;
14 static int thread_alive = 0;
15
16 // Structure to hold subscriptions
17 typedef struct Subscription {
18 double interval;
19 int id;
20 } Subscription;
21
22 #define MAX_SUBSCRIBERS 1000
23 static Subscription subscribers[MAX_SUBSCRIBERS];
24 static int subscriber_count = 0;
25
26 static double get_interval(double max_interval) {
27 double min_interval = max_interval;
28
29 for (int i = 0; i < subscriber_count; i++) {
30 if (subscribers[i].interval < min_interval) {
31 min_interval = subscribers[i].interval;
32 }
33 }
34
35 return min_interval;
36 }
37
38 static void timing_thread(void* args) {
39 while (!thread_should_exit) {
40 double interval = get_interval(1.0);
41 // sleep for the interval, or until we're woken up by a change
42 PyLockStatus status = PyThread_acquire_lock_timed(
43 update_lock,
44 (PY_TIMEOUT_T)(interval * 1e6),
45 0
46 );
47 if (status == PY_LOCK_ACQUIRED) {
48 // rather than finishing the wait, another thread signaled a
49 // change by releasing the lock. The lock was just for the sake of
50 // the wakeup, so let's release it again.
51 PyThread_release_lock(update_lock);
52 }
53 current_time = pyi_floatclock(PYI_FLOATCLOCK_DEFAULT);
54 }
55 }
56
57 int pyi_timing_thread_subscribe(double desiredInterval) {
58 if (subscriber_lock == NULL) {
59 subscriber_lock = PyThread_allocate_lock();
60 }
61 if (update_lock == NULL) {
62 update_lock = PyThread_allocate_lock();
63 }
64
65 PyThread_acquire_lock(subscriber_lock, WAIT_LOCK);
66
67 if (!thread_alive) {
68 PyThread_acquire_lock(update_lock, WAIT_LOCK); // Initially hold the lock
69 thread_should_exit = 0;
70 PyThread_start_new_thread(timing_thread, NULL);
71 thread_alive = 1;
72
73 // initialise the current_time in case it's read immediately
74 current_time = pyi_floatclock(PYI_FLOATCLOCK_DEFAULT);
75 }
76
77 int new_id = 0;
78
79 // find an unused ID
80 for (; new_id < MAX_SUBSCRIBERS; new_id++) {
81 int already_exists = 0;
82 for (int i = 0; i < subscriber_count; i++) {
83 if (subscribers[i].id == new_id) {
84 already_exists = 1;
85 break;
86 }
87 }
88 if (!already_exists) {
89 break;
90 }
91 }
92 if (new_id == MAX_SUBSCRIBERS) {
93 // Too many subscribers
94 PyThread_release_lock(subscriber_lock);
95 return PYI_TIMING_THREAD_TOO_MANY_SUBSCRIBERS;
96 }
97
98 int index = subscriber_count;
99 subscribers[index].id = new_id;
100 subscribers[index].interval = desiredInterval;
101 subscriber_count++;
102
103 // signal a possible change in the interval
104 PyThread_release_lock(update_lock);
105 PyThread_acquire_lock(update_lock, WAIT_LOCK);
106
107 PyThread_release_lock(subscriber_lock);
108 return new_id;
109 }
110
111 int pyi_timing_thread_unsubscribe(int id) {
112 PyThread_acquire_lock(subscriber_lock, WAIT_LOCK);
113
114 int removals = 0;
115
116 for (int i = 0; i < subscriber_count; i++) {
117 if (subscribers[i].id == id) {
118 // Removal: overwrite this one with with the last element and decrement count.
119 subscribers[i] = subscribers[subscriber_count-1];
120 subscriber_count--;
121 removals++;
122 break;
123 }
124 }
125
126 // if the last subscriber was removed, stop the thread
127 if (subscriber_count == 0) {
128 thread_should_exit = 1;
129 PyThread_release_lock(update_lock);
130 thread_alive = 0;
131 }
132
133 PyThread_release_lock(subscriber_lock);
134
135 if (removals == 0) {
136 return PYI_TIMING_THREAD_NOT_SUBSCRIBED;
137 } else {
138 return 0;
139 }
140 }
141
142 double pyi_timing_thread_get_time(void) {
143 return current_time;
144 }
145
146 double pyi_timing_thread_get_interval(void) {
147 if (thread_alive) {
148 return get_interval(DBL_MAX);
149 } else {
150 return -1.0;
151 }
152 }