diff mbox

[2/8] V4L: File handles: Add refcount to v4l2_fh

Message ID 1265479331-20595-2-git-send-email-sakari.ailus@maxwell.research.nokia.com (mailing list archive)
State RFC
Headers show

Commit Message

Sakari Ailus Feb. 6, 2010, 6:02 p.m. UTC
None
diff mbox

Patch

diff --git a/drivers/media/video/v4l2-fh.c b/drivers/media/video/v4l2-fh.c
index 48bd32f..1728e1c 100644
--- a/drivers/media/video/v4l2-fh.c
+++ b/drivers/media/video/v4l2-fh.c
@@ -25,22 +25,32 @@ 
 #include <media/v4l2-dev.h>
 #include <media/v4l2-fh.h>
 
-int v4l2_fh_new(struct video_device *vdev, struct v4l2_fh *fh)
+void v4l2_fh_new(struct video_device *vdev, struct v4l2_fh *fh)
 {
 	unsigned long flags;
 
+	spin_lock_init(&fh->lock);
+	atomic_set(&fh->refcount, 1);
+
 	spin_lock_irqsave(&vdev->fhs.lock, flags);
 	list_add(&fh->list, &vdev->fhs.list);
 	spin_unlock_irqrestore(&vdev->fhs.lock, flags);
-
-	return 0;
 }
 EXPORT_SYMBOL_GPL(v4l2_fh_new);
 
+int v4l2_fh_get(struct video_device *vdev, struct v4l2_fh *fh)
+{
+	return !atomic_add_unless(&fh->refcount, 1, 0);
+}
+EXPORT_SYMBOL_GPL(v4l2_fh_get);
+
 void v4l2_fh_put(struct video_device *vdev, struct v4l2_fh *fh)
 {
 	unsigned long flags;
 
+	if (atomic_dec_return(&fh->refcount))
+		return;
+
 	spin_lock_irqsave(&vdev->fhs.lock, flags);
 	list_del(&fh->list);
 	spin_unlock_irqrestore(&vdev->fhs.lock, flags);
diff --git a/include/media/v4l2-fh.h b/include/media/v4l2-fh.h
index b9e277c..51d6508 100644
--- a/include/media/v4l2-fh.h
+++ b/include/media/v4l2-fh.h
@@ -28,8 +28,12 @@ 
 #include <linux/types.h>
 #include <linux/list.h>
 
+#include <asm/atomic.h>
+
 struct v4l2_fh {
 	struct list_head	list;
+	atomic_t		refcount;
+	spinlock_t		lock;
 };
 
 /* File handle related data for video_device. */
@@ -42,7 +46,8 @@  struct v4l2_fhs {
 
 struct video_device;
 
-int v4l2_fh_new(struct video_device *vdev, struct v4l2_fh *fh);
+void v4l2_fh_new(struct video_device *vdev, struct v4l2_fh *fh);
+int v4l2_fh_get(struct video_device *vdev, struct v4l2_fh *fh);
 void v4l2_fh_put(struct video_device *vdev, struct v4l2_fh *fh);
 void v4l2_fh_init(struct video_device *vdev);