kvm tools: Use mutex_lock() and mutex_unlock() wrappers

This patch implements less hostile mutex_lock() and mutex_lock() wrappers on
top of the pthread API equivalents as suggested by Ingo Molnar:

  glibc/pthreads mutex API semantics are pretty silly IMO.

  I *think* it would be better to try to match the kernel API here, and provide
  trivial wrappers around mutex_lock()/mutex_unlock(). We wont ever bring down
  threads in a hostile way, so we wont actually need the error returns. CPU
  threads should probably only exit once the kvm process exits, after all
  cleanup has been done.

  That way usage would be more obvious and more familar to kernel developers :-)

  [ It would also open up the possibility, in the far future, to bring lockdep to
    user-space ;-) ]

Cc: Asias He <asias.hejun@gmail.com>
Cc: Cyrill Gorcunov <gorcunov@gmail.com>
Cc: Ingo Molnar <mingo@elte.hu>
Signed-off-by: Pekka Enberg <penberg@kernel.org>
This commit is contained in:
Pekka Enberg
2015-06-01 16:39:42 +01:00
committed by Will Deacon
parent 5f457e0bcb
commit 4ef0f4d620
3 changed files with 32 additions and 20 deletions
+7 -12
View File
@@ -2,6 +2,7 @@
#include "kvm/read-write.h"
#include "kvm/ioport.h"
#include "kvm/mutex.h"
#include "kvm/util.h"
#include "kvm/term.h"
#include "kvm/kvm.h"
@@ -116,8 +117,7 @@ void serial8250__inject_interrupt(struct kvm *self)
{
struct serial8250_device *dev = &devices[0];
if (pthread_mutex_lock(&dev->mutex) != 0)
die("pthread_mutex_lock");
mutex_lock(&dev->mutex);
serial8250__receive(self, dev);
@@ -133,8 +133,7 @@ void serial8250__inject_interrupt(struct kvm *self)
kvm__irq_line(self, dev->irq, 1);
}
if (pthread_mutex_unlock(&dev->mutex) != 0)
die("pthread_mutex_unlock");
mutex_unlock(&dev->mutex);
}
void serial8250__inject_sysrq(struct kvm *self)
@@ -165,8 +164,7 @@ static bool serial8250_out(struct kvm *self, uint16_t port, void *data, int size
if (!dev)
return false;
if (pthread_mutex_lock(&dev->mutex) != 0)
die("pthread_mutex_lock");
mutex_lock(&dev->mutex);
offset = port - dev->iobase;
@@ -239,8 +237,7 @@ static bool serial8250_out(struct kvm *self, uint16_t port, void *data, int size
}
out_unlock:
if (pthread_mutex_unlock(&dev->mutex) != 0)
die("pthread_mutex_unlock");
mutex_unlock(&dev->mutex);
return ret;
}
@@ -255,8 +252,7 @@ static bool serial8250_in(struct kvm *self, uint16_t port, void *data, int size,
if (!dev)
return false;
if (pthread_mutex_lock(&dev->mutex) != 0)
die("pthread_mutex_lock");
mutex_lock(&dev->mutex);
offset = port - dev->iobase;
@@ -321,8 +317,7 @@ static bool serial8250_in(struct kvm *self, uint16_t port, void *data, int size,
goto out_unlock;
}
out_unlock:
if (pthread_mutex_unlock(&dev->mutex) != 0)
die("pthread_mutex_unlock");
mutex_unlock(&dev->mutex);
return ret;
}
+20
View File
@@ -0,0 +1,20 @@
#ifndef KVM__MUTEX_H
#define KVM__MUTEX_H
#include <pthread.h>
#include "kvm/util.h"
static inline void mutex_lock(pthread_mutex_t *mutex)
{
if (pthread_mutex_lock(mutex) != 0)
die("unexpected pthread_mutex_lock() failure!");
}
static inline void mutex_unlock(pthread_mutex_t *mutex)
{
if (pthread_mutex_unlock(mutex) != 0)
die("unexpected pthread_mutex_unlock() failure!");
}
#endif /* KVM__MUTEX_H */
+5 -8
View File
@@ -5,6 +5,7 @@
#include "kvm/disk-image.h"
#include "kvm/virtio.h"
#include "kvm/ioport.h"
#include "kvm/mutex.h"
#include "kvm/util.h"
#include "kvm/kvm.h"
#include "kvm/pci.h"
@@ -70,8 +71,7 @@ static bool virtio_blk_pci_io_in(struct kvm *self, uint16_t port, void *data, in
unsigned long offset;
bool ret = true;
if (pthread_mutex_lock(&blk_device.mutex) != 0)
die("pthread_mutex_lock");
mutex_lock(&blk_device.mutex);
offset = port - IOPORT_VIRTIO_BLK;
@@ -108,8 +108,7 @@ static bool virtio_blk_pci_io_in(struct kvm *self, uint16_t port, void *data, in
};
out_unlock:
if (pthread_mutex_unlock(&blk_device.mutex) != 0)
die("pthread_mutex_unlock");
mutex_unlock(&blk_device.mutex);
return ret;
}
@@ -180,8 +179,7 @@ static bool virtio_blk_pci_io_out(struct kvm *self, uint16_t port, void *data, i
unsigned long offset;
bool ret = true;
if (pthread_mutex_lock(&blk_device.mutex) != 0)
die("pthread_mutex_lock");
mutex_lock(&blk_device.mutex);
offset = port - IOPORT_VIRTIO_BLK;
@@ -226,8 +224,7 @@ static bool virtio_blk_pci_io_out(struct kvm *self, uint16_t port, void *data, i
};
out_unlock:
if (pthread_mutex_unlock(&blk_device.mutex) != 0)
die("pthread_mutex_unlock");
mutex_unlock(&blk_device.mutex);
return ret;
}