Skip to content

Commit

Permalink
Merge pull request #410 from vibe-d/log_yield_lock_improvement
Browse files Browse the repository at this point in the history
Fix issues with the yield locking in the logging code
  • Loading branch information
s-ludwig authored Aug 6, 2024
2 parents 61b2c77 + 0c98823 commit f26f33b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
9 changes: 9 additions & 0 deletions source/vibe/core/core.d
Original file line number Diff line number Diff line change
Expand Up @@ -1660,6 +1660,15 @@ unittest {
assert(!tf.isInYieldLock());
}


/** Less strict version of `yieldLock` that only locks if called within a task.
*/
auto taskYieldLock(string file = __FILE__, int line = __LINE__)
@safe nothrow {
if (!Fiber.getThis()) return typeof(yieldLock()).init;
return yieldLock(file, line);
}

debug (VibeRunningTasks) {
/** Dumps a list of all active tasks of the calling thread.
*/
Expand Down
8 changes: 4 additions & 4 deletions source/vibe/core/log.d
Original file line number Diff line number Diff line change
Expand Up @@ -1075,7 +1075,7 @@ unittest { // ensure arguments are evaluated lazily
}

private struct LogOutputRange {
import vibe.core.core : yieldLock;
import vibe.core.core : taskYieldLock;

LogLine info;
ScopedLock!Logger* logger;
Expand All @@ -1084,7 +1084,7 @@ private struct LogOutputRange {

this(ref ScopedLock!Logger logger, string file, int line, LogLevel level)
{
auto l = yieldLock();
auto l = taskYieldLock();

() @trusted { this.logger = &logger; } ();
try {
Expand All @@ -1111,7 +1111,7 @@ private struct LogOutputRange {

void finalize()
{
auto l = yieldLock();
auto l = taskYieldLock();

logger.endLine();
}
Expand All @@ -1121,7 +1121,7 @@ private struct LogOutputRange {
if (text.empty)
return;

auto l = yieldLock();
auto l = taskYieldLock();

if (logger.multilineLogger) {
logger.put(text);
Expand Down
6 changes: 4 additions & 2 deletions source/vibe/core/task.d
Original file line number Diff line number Diff line change
Expand Up @@ -707,18 +707,20 @@ final package class TaskFiber : Fiber {
@safe nothrow @nogc {
// only record the outermost lock
if (m_yieldLockCount > 0) return;
assert (m_yieldLockCount == 0, "Yield lock counter negative!?");
m_yieldLockFile = file;
m_yieldLockLine = line;
}

package void acquireYieldLock()
@safe nothrow @nogc {
assert (m_yieldLockCount >= 0, "Acquiring yield lock with negative yield lock counter!?");
m_yieldLockCount++;
}

package void releaseYieldLock()
@safe nothrow @nogc {
assert(m_yieldLockCount > 0);
assert (m_yieldLockCount > 0, "Releasing yield lock with non-positive yield lock counter!?");
if (!--m_yieldLockCount) {
m_yieldLockFile = null;
m_yieldLockLine = -1;
Expand All @@ -729,7 +731,7 @@ final package class TaskFiber : Fiber {
@safe nothrow {
if (m_yieldLockCount > 0 && m_yieldLockFile.length)
logError("Yield lock violation for lock at %s:%s", m_yieldLockFile, m_yieldLockLine);
assert(m_yieldLockCount == 0, "May not yield while in an active yieldLock()!");
assert (m_yieldLockCount == 0, "May not yield while in an active yieldLock()!");
}

package bool isInYieldLock()
Expand Down

0 comments on commit f26f33b

Please sign in to comment.