|
| 1 | +#ifndef Py_FIRMAMENT2_H |
| 2 | +#define Py_FIRMAMENT2_H |
| 3 | +#ifdef __cplusplus |
| 4 | +extern "C" { |
| 5 | +#endif |
| 6 | + |
| 7 | +#include "Python.h" |
| 8 | +#include <pythread.h> |
| 9 | +#include <stdint.h> |
| 10 | +#include <stdlib.h> /* getenv */ |
| 11 | +#include <stdio.h> /* printf, fflush */ |
| 12 | +#include <string.h> /* strlen, memcpy */ |
| 13 | +#include <stdatomic.h> |
| 14 | +#if defined(_WIN32) |
| 15 | + #include <windows.h> |
| 16 | +#else |
| 17 | + #include <unistd.h> /* getpid */ |
| 18 | + #include <time.h> /* clock_gettime */ |
| 19 | +#endif |
| 20 | + |
| 21 | +/* ---------------- Global gate ---------------- */ |
| 22 | +static inline int _firm2_enabled(void) { |
| 23 | + static int cached = -1; |
| 24 | + if (cached == -1) { |
| 25 | + const char *env = getenv("FIRMAMENT2_ENABLE"); |
| 26 | + cached = (env && env[0] != '\0') ? 1 : 0; |
| 27 | + } |
| 28 | + return cached; |
| 29 | +} |
| 30 | + |
| 31 | +/* ---------------- Envelope helpers ---------------- */ |
| 32 | +static inline unsigned long _firm2_pid(void) { |
| 33 | +#if defined(_WIN32) |
| 34 | + return (unsigned long)GetCurrentProcessId(); |
| 35 | +#else |
| 36 | + return (unsigned long)getpid(); |
| 37 | +#endif |
| 38 | +} |
| 39 | + |
| 40 | +static inline unsigned long long _firm2_tid(void) { |
| 41 | + /* PyThread_get_thread_ident() returns a long. Cast to 64-bit. */ |
| 42 | + return (unsigned long long)PyThread_get_thread_ident(); |
| 43 | +} |
| 44 | + |
| 45 | +static inline long long _firm2_now_ns(void) { |
| 46 | +#if !defined(_WIN32) |
| 47 | + struct timespec ts; |
| 48 | + if (clock_gettime(CLOCK_REALTIME, &ts) == 0) { |
| 49 | + return ((long long)ts.tv_sec * 1000000000LL) + (long long)ts.tv_nsec; |
| 50 | + } |
| 51 | +#endif |
| 52 | + return 0; |
| 53 | +} |
| 54 | + |
| 55 | +static inline unsigned long long _firm2_next_eid(void) { |
| 56 | + static _Atomic unsigned long long ctr = 0; |
| 57 | + return ++ctr; /* monotonic per-process event id */ |
| 58 | +} |
| 59 | + |
| 60 | +/* ---------------- Per-source (compilation unit) TLS ---------------- |
| 61 | + * Implemented in ceval.c so all core files can link against them. |
| 62 | + */ |
| 63 | +void _firm2_source_begin(const char *filename_utf8); |
| 64 | +void _firm2_source_end(void); |
| 65 | +const char *_firm2_current_filename(void); |
| 66 | +const char *_firm2_current_source_id_hex(void); |
| 67 | + |
| 68 | +#ifdef __cplusplus |
| 69 | +} |
| 70 | +#endif |
| 71 | +#endif /* Py_FIRMAMENT2_H */ |
0 commit comments