Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dbg: mutex should be unlocked while calling print handler #1113

Merged
merged 3 commits into from
Apr 30, 2024

Conversation

alfredh
Copy link
Contributor

@alfredh alfredh commented Apr 28, 2024

No description provided.

@cspiel1
Copy link
Collaborator

cspiel1 commented Apr 29, 2024

Wouldn't this make the mutex ineffective?

If dbg output goes to stdout from multiple threads, then the lines could be mixed.

@alfredh
Copy link
Contributor Author

alfredh commented Apr 29, 2024

this is a good question.

stdout is line-buffered and stderr has no buffering.

This patch is about the print handler. It makes sure that the mutex lock is unlocked while calling
the print handler, in case the print handler should call any functions in the re_dbg.h API.

The dbg code does not have any control of what the print_handler is doing.
In general it is good practice to unlock a mutex before calling a callback handler ...

@cspiel1
Copy link
Collaborator

cspiel1 commented Apr 29, 2024

Okay. I see now that dbg_lock protects dbg_vprintf() and dbg_init() against concurrency.

int dbg_level = dbg.level;
dbg_print_h *ph = dbg.ph;
void *arg = dbg.arg;
dbg_unlock();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would does this locking protect?

Copy link
Collaborator

@cspiel1 cspiel1 Apr 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should there be an additional lock in dbg_handler_set()?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The mutex should protect all members of the struct.

@alfredh
Copy link
Contributor Author

alfredh commented Apr 29, 2024

/** Debug configuration */
static struct {
	uint64_t tick;         /**< Init ticks             */
	int level;             /**< Current debug level    */
	enum dbg_flags flags;  /**< Debug flags            */
	dbg_print_h *ph;       /**< Optional print handler */
	void *arg;             /**< Handler argument       */
} dbg = {
	0,
	DBG_INFO,
	DBG_ANSI,
	NULL,
	NULL,
};

@cspiel1
Copy link
Collaborator

cspiel1 commented Apr 29, 2024

Looks good now

@alfredh
Copy link
Contributor Author

alfredh commented Apr 30, 2024

@sreimers please merge to main if you agree ..

@sreimers sreimers merged commit 411208e into main Apr 30, 2024
37 checks passed
@sreimers sreimers deleted the dbg_fmt_vprintf branch April 30, 2024 12:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants