mirror of
https://github.com/upa/mscp.git
synced 2026-02-27 18:54:42 +08:00
use pthread_cleanup to acquire and release lock
In chunk_prepare(), if multiple threads wait for acquiring f->lock, and then pthread_cancel() is called, the waiting threads are never canceled because pthread_mutex_lock() is not a cancellation point. So, use pthread_cleanup_push/pop to release the lock.
This commit is contained in:
13
src/atomic.h
13
src/atomic.h
@@ -55,4 +55,17 @@ static inline void lock_release(lock *l)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static inline void lock_release_via_cleanup(void *l)
|
||||||
|
{
|
||||||
|
lock_release(l);
|
||||||
|
}
|
||||||
|
|
||||||
|
#define LOCK_ACQUIRE_THREAD(l) \
|
||||||
|
lock_acquire(l); \
|
||||||
|
pthread_cleanup_push(lock_release_via_cleanup, l)
|
||||||
|
|
||||||
|
|
||||||
|
#define LOCK_RELEASE_THREAD(l) \
|
||||||
|
pthread_cleanup_pop(1)
|
||||||
|
|
||||||
#endif /* _ATOMIC_H_ */
|
#endif /* _ATOMIC_H_ */
|
||||||
|
|||||||
@@ -554,7 +554,7 @@ int chunk_prepare(struct chunk *c, sftp_session sftp)
|
|||||||
struct file *f = c->f;
|
struct file *f = c->f;
|
||||||
int ret = 0;
|
int ret = 0;
|
||||||
|
|
||||||
lock_acquire(&f->lock); /* XXX: is always acquiring lock per-chunk heavy? */
|
LOCK_ACQUIRE_THREAD(&f->lock);
|
||||||
if (f->state == FILE_STATE_INIT) {
|
if (f->state == FILE_STATE_INIT) {
|
||||||
if (file_dst_prepare(f, f->dst_is_remote ? sftp : NULL) < 0) {
|
if (file_dst_prepare(f, f->dst_is_remote ? sftp : NULL) < 0) {
|
||||||
ret = -1;
|
ret = -1;
|
||||||
@@ -565,7 +565,7 @@ int chunk_prepare(struct chunk *c, sftp_session sftp)
|
|||||||
}
|
}
|
||||||
|
|
||||||
out:
|
out:
|
||||||
lock_release(&f->lock);
|
LOCK_RELEASE_THREAD();
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -507,9 +507,9 @@ void *mscp_copy_thread(void *arg)
|
|||||||
pthread_cleanup_push(mscp_copy_thread_cleanup, t);
|
pthread_cleanup_push(mscp_copy_thread_cleanup, t);
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
lock_acquire(&m.chunk_lock);
|
LOCK_ACQUIRE_THREAD(&m.chunk_lock);
|
||||||
c = chunk_acquire(&m.chunk_list);
|
c = chunk_acquire(&m.chunk_list);
|
||||||
lock_release(&m.chunk_lock);
|
LOCK_RELEASE_THREAD();
|
||||||
|
|
||||||
if (!c)
|
if (!c)
|
||||||
break; /* no more chunks */
|
break; /* no more chunks */
|
||||||
|
|||||||
Reference in New Issue
Block a user