Skip to content

Commit

Permalink
Use flockfile when available but not on freertos and windows.
Browse files Browse the repository at this point in the history
  • Loading branch information
floitsch committed Oct 25, 2024
1 parent 3e8e412 commit fc779bd
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 14 deletions.
4 changes: 2 additions & 2 deletions lib/core/print.toit
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ If $add-newline is true adds a "\n" to the output.
Does not yield the currently running task.
*/
write-on-stdout_ message/string add-newline/bool -> none:
#primitive.core.write-string-on-stdout
#primitive.core.write-on-stdout

/**
Dumps the string of $object and a newline on stderr and flushes it.
Expand All @@ -78,7 +78,7 @@ If $add-newline is true adds a "\n" to the output.
Does not yield the currently running task.
*/
write-on-stderr_ message/string add-newline/bool -> none:
#primitive.core.write-string-on-stderr
#primitive.core.write-on-stderr

/**
Print service used by $print.
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/propagation/type_primitive_core.cc
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ TYPE_PRIMITIVE(process_stats) {
}

TYPE_PRIMITIVE_ANY(string_write_to_byte_array) // TODO(kasper): Returns an argument.
TYPE_PRIMITIVE_NULL(write_string_on_stdout)
TYPE_PRIMITIVE_NULL(write_string_on_stderr)
TYPE_PRIMITIVE_NULL(write_on_stdout)
TYPE_PRIMITIVE_NULL(write_on_stderr)

TYPE_PRIMITIVE_INT(time)
TYPE_PRIMITIVE_ARRAY(time_info)
Expand Down
4 changes: 2 additions & 2 deletions src/primitive.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ namespace toit {
M(bignum, MODULE_BIGNUM) \

#define MODULE_CORE(PRIMITIVE) \
PRIMITIVE(write_string_on_stdout, 2) \
PRIMITIVE(write_string_on_stderr, 2) \
PRIMITIVE(write_on_stdout, 2) \
PRIMITIVE(write_on_stderr, 2) \
PRIMITIVE(time, 1) \
PRIMITIVE(time_info, 2) \
PRIMITIVE(seconds_since_epoch_local, 7) \
Expand Down
54 changes: 46 additions & 8 deletions src/primitive_core.cc
Original file line number Diff line number Diff line change
Expand Up @@ -68,24 +68,62 @@ namespace toit {

MODULE_IMPLEMENTATION(core, MODULE_CORE)

static void write_to(FILE* stream, const uint8* bytes, word length) {
#if defined(TOIT_WINDOWS)
static Object* write_on_std(const uint8_t* bytes, size_t length, bool is_stdout, bool newline) {
// Get the appropriate console handle.
HANDLE console = GetStdHandle(is_stdout ? STD_OUTPUT_HANDLE : STD_ERROR_HANDLE);
if (console == INVALID_HANDLE_VALUE) {
return Primitive::os_error(GetLastError(), process);
}

DWORD written;
WriteConsoleA(console, bytes, (DWORD)length, &written, NULL);

if (newline) {
WriteConsoleA(console, "\r\n", 2, &written, NULL);
} else {
fflush(stream);
}

return process->null_object();
}
#elif !defined(TOIT_FREERTOS) && (_POSIX_C_SOURCE >= 199309L || _BSD_SOURCE)
static Object* write_on_std(const uint8_t* bytes, size_t length, bool is_stdout, bool newline, Process* process) {
FILE* stream = is_stdout ? stdout : stderr;
flockfile(stream);
for (word i = 0; i < length; i++) {
putc_unlocked(bytes[i], stream);
fwrite_unlocked(bytes, 1, length, stream);
if (newline) {
fputc_unlocked('\n', stream);
} else {
fflush_unlocked(stream);
}
funlockfile(stream);
fflush(stream);
return process->null_object();
}
#else
static Object* write_on_std(const uint8_t* bytes, size_t length, bool is_stdout, bool newline, Process* process) {
FILE* stream = is_stdout ? stdout : stderr;
fwrite(bytes, 1, length, stream);
if (newline) {
fputc('\n', stream);
} else {
fflush(stream);
}
return process->null_object();
}
#endif

PRIMITIVE(write_string_on_stdout) {
PRIMITIVE(write_on_stdout) {
ARGS(Blob, message, bool, add_newline);
write_to(stdout, message.address(), message.length());
bool is_stdout;
write_on_std(message.address(), message.length(), is_stdout=true, add_newline, process);
return process->null_object();
}

PRIMITIVE(write_string_on_stderr) {
PRIMITIVE(write_on_stderr) {
ARGS(Blob, message, bool, add_newline);
write_to(stderr, message.address(), message.length());
bool is_stdout;
write_on_std(message.address(), message.length(), is_stdout=false, add_newline, process);
return process->null_object();
}

Expand Down

0 comments on commit fc779bd

Please sign in to comment.