Skip to content

Commit

Permalink
Add macro option to not use mutex
Browse files Browse the repository at this point in the history
  • Loading branch information
petabyt committed Aug 10, 2023
1 parent 5a05ce3 commit e4941eb
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/bind.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ int bind_status(struct BindReq *bind, struct PtpRuntime *r) {

int bind_init(struct BindReq *bind, struct PtpRuntime *r) {
if (bind_initialized) {
ptp_generic_close();
ptp_generic_close(r);
if (r->di != NULL) free(r->di);
}

Expand Down
6 changes: 6 additions & 0 deletions src/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,26 @@ void ptp_generic_reset(struct PtpRuntime *r) {
r->di = NULL;
r->connection_type = PTP_USB;
r->caller_unlocks_mutex = 0;
r->mutex = NULL;
}

void ptp_generic_init(struct PtpRuntime *r) {
ptp_generic_reset(r);
r->data = malloc(CAMLIB_DEFAULT_SIZE);
r->data_length = CAMLIB_DEFAULT_SIZE;

#ifndef CAMLIB_DONT_USE_MUTEX
r->mutex = malloc(sizeof(pthread_mutex_t));
if (pthread_mutex_init(r->mutex, NULL)) {
ptp_verbose_log("Failed to init mutex\n");
free(r->mutex);
r->mutex = NULL;
}
#endif
}

void ptp_mutex_lock(struct PtpRuntime *r) {
if (r->mutex == NULL) return;
pthread_mutex_lock(r->mutex);
}

Expand All @@ -40,6 +45,7 @@ void ptp_mutex_keep_locked(struct PtpRuntime *r) {
}

void ptp_mutex_unlock(struct PtpRuntime *r) {
if (r->mutex == NULL) return;
pthread_mutex_unlock(r->mutex);
r->caller_unlocks_mutex = 0;
}
Expand Down

0 comments on commit e4941eb

Please sign in to comment.