39 lines
911 B
C
39 lines
911 B
C
#include <mprintf.h>
|
|
#include <printf.h>
|
|
#include <process_self.h>
|
|
#include <stdarg.h>
|
|
#include <stdatomic.h>
|
|
#include <streams.h>
|
|
#include <string.h>
|
|
#include <system.h>
|
|
|
|
static char mprintf_buffer[MPRINTF_BUF_MAX];
|
|
static atomic_flag mprintf_buffer_lock1 = ATOMIC_FLAG_INIT;
|
|
|
|
static void mprintf_buffer_lock (void) {
|
|
while (atomic_flag_test_and_set_explicit (&mprintf_buffer_lock1, memory_order_acquire)) {
|
|
#if defined(__x86_64__)
|
|
__asm__ volatile ("pause" ::: "memory");
|
|
#endif
|
|
}
|
|
}
|
|
|
|
static void mprintf_buffer_unlock (void) {
|
|
atomic_flag_clear_explicit (&mprintf_buffer_lock1, memory_order_release);
|
|
}
|
|
|
|
void mprintf (const char* fmt, ...) {
|
|
va_list args;
|
|
va_start (args, fmt);
|
|
|
|
mprintf_buffer_lock ();
|
|
|
|
int len = vsnprintf (mprintf_buffer, MPRINTF_BUF_MAX, fmt, args);
|
|
|
|
va_end (args);
|
|
|
|
stream_write (process_get_pgid (), STREAM_OUT, mprintf_buffer, len);
|
|
|
|
mprintf_buffer_unlock ();
|
|
}
|