-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add operation_kill_switch and ptp_report_error example
- Loading branch information
Showing
7 changed files
with
67 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Example implementation of function to safely kill the connection in a multithreaded application. | ||
// Copyright 2024 by Daniel C (https://github.com/petabyt/camlib) | ||
|
||
#include <camlib.h> | ||
|
||
void ptp_report_error(struct PtpRuntime *r, char *reason, int code) { | ||
if (r->io_kill_switch) return; | ||
// Prevent any further operations if this is intentional | ||
if (code != 0) { | ||
r->operation_kill_switch = 1; | ||
} | ||
// Wait until we have a lock | ||
ptp_mutex_lock(r); | ||
// If io_kill_switch was set while waiting for a lock | ||
if (r->io_kill_switch) { | ||
ptp_mutex_unlock(r); | ||
return; | ||
} | ||
|
||
// Safely disconnect if intentional | ||
if (code == 0) { | ||
ptp_verbose_log("Closing session\n"); | ||
ptp_close_session(r); | ||
} | ||
|
||
r->operation_kill_switch = 1; | ||
r->io_kill_switch = 1; | ||
|
||
ptp_mutex_unlock(r); | ||
|
||
if (reason == NULL) { | ||
if (code == PTP_IO_ERR) { | ||
ptp_verbose_log("Disconnected: IO Error\n"); | ||
} else { | ||
ptp_verbose_log("Disconnected: Runtime error\n"); | ||
} | ||
} else { | ||
ptp_verbose_log("Disconnected: %s\n", reason); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters