Skip to content

Commit

Permalink
Minor tweaks, fix CI multithreading code
Browse files Browse the repository at this point in the history
  • Loading branch information
petabyt committed Sep 15, 2024
1 parent 5a9637b commit 7c18175
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 15 deletions.
10 changes: 2 additions & 8 deletions src/camlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -253,17 +253,11 @@ PUB int ptp_get_event(struct PtpRuntime *r, struct PtpEventContainer *ec);
/// @memberof PtpRuntime
PUB void ptp_mutex_unlock(struct PtpRuntime *r);

/// @brief Completely unlock the mutex for the current thread, to ensure there isn't a deadlock
/// @brief Completely unlock the mutex for the current thread, to ensure there isn't a deadlock.
/// This is normally used on handling errors, and when exiting a thread.
/// @memberof PtpRuntime
PUB void ptp_mutex_unlock_thread(struct PtpRuntime *r);

/// @brief Keep the mutex locked one more time for the current thread
/// @note When calling a thread-safe function, this will garuntee the mutex locked, in the
/// case that you want to continue using the buffer. Must be unlocked or will cause deadlock.
/// @note camlib uses a recursive mutex.
/// @memberof PtpRuntime
PUB void ptp_mutex_keep_locked(struct PtpRuntime *r);

/// @brief Lock the IO mutex - only should be used by backend
/// @memberof PtpRuntime
PUB void ptp_mutex_lock(struct PtpRuntime *r);
Expand Down
47 changes: 46 additions & 1 deletion src/cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,61 @@ static int parse_run(struct Options *o, int argc, char **argv, int i) {
return rc;
}

static int test(void) {
struct PtpRuntime *r = ptp_new(PTP_USB);

if (ptp_device_init(r)) {
printf("Device connection error\n");
return 1;
}

int rc = ptp_open_session(r);
if (rc) return rc;

struct PtpDeviceInfo di;

char buffer[2048];
rc = ptp_get_device_info(r, &di);
if (rc) return rc;
ptp_device_info_json(&di, buffer, sizeof(buffer));
printf("%s\n", buffer);

rc = ptp_eos_set_remote_mode(r, 1);
if (rc) return rc;
rc = ptp_eos_set_event_mode(r, 1);
if (rc) return rc;

for (int i = 0; i < 10; i++) {
rc = ptp_eos_get_event(r);
if (rc) return rc;
usleep(10000);
}

rc = ptp_close_session(r);
if (rc) return rc;

ptp_device_close(r);
ptp_close(r);
return 0;
}

int main(int argc, char **argv) {
extern int camlib_verbose;
camlib_verbose = 0;
camlib_verbose = 1;
struct Options o;
for (int i = 1; i < argc; i++) {
if (!strcmp(argv[i], "--help")) {
return usage();
} else if (!strcmp(argv[i], "--run")) {
return parse_run(&o, argc, argv, i + 1);
} else if (!strcmp(argv[i], "--test")) {
int rc = test();
printf("Return code: %d\n", rc);
return rc;
}
}

return test();

return usage();
}
2 changes: 1 addition & 1 deletion src/operations.c
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ int ptp_get_storage_ids(struct PtpRuntime *r, struct PtpArray **a) {
cmd.code = PTP_OC_GetStorageIDs;
cmd.param_length = 0;

ptp_mutex_keep_locked(r);
ptp_mutex_lock(r);

int rc = ptp_send(r, &cmd);

Expand Down
7 changes: 2 additions & 5 deletions src/transport.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,8 @@ int ptp_send_packet(struct PtpRuntime *r, int length) {
return ptpusb_send_bulk_packets(r, length);
}
int sent = 0;
int x;
while (1) {
if (r->connection_type == PTP_IP || r->connection_type == PTP_IP_USB) {
x = ptpip_cmd_write(r, r->data + sent, length);
}
int x = ptpip_cmd_write(r, r->data + sent, length);

if (x < 0) {
ptp_verbose_log("send_bulk_packet: %d\n", __func__, x);
Expand Down Expand Up @@ -204,7 +201,7 @@ int ptpusb_read_all_packets(struct PtpRuntime *r) {
read += rc;

if (read < 12) {
ptp_verbose_log("Couldn't get basic packet: %d\n", rc);
ptp_verbose_log("Couldn't get min packet size: %d\n", rc);
return PTP_IO_ERR;
}

Expand Down
2 changes: 2 additions & 0 deletions test/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -213,11 +213,13 @@ static void *thread(void *arg) {
struct PtpRuntime *r = (struct PtpRuntime *)arg;

if ((rand() & 1) == 0) {
ptp_mutex_lock(r);
struct PtpDeviceInfo di;
int rc = ptp_get_device_info(r, &di);
if (rc) goto err;
char buffer[4096];
ptp_device_info_json(&di, buffer, sizeof(buffer));
ptp_mutex_unlock(r);
printf("%s\n", buffer);
} else {
struct PtpArray *arr;
Expand Down

0 comments on commit 7c18175

Please sign in to comment.