[Cbe-oss-dev] [PATCH] libspe2: fix range of locks

Kazunori Asayama asayama at sm.sony.co.jp
Wed Dec 6 21:42:40 EST 2006


Attached is a patch to fix the problem in libspe2 2.0.1:

  - The ranges of locking are not enough.

----
Index: libspe2/spebase/create.c
===================================================================
--- libspe2.orig/spebase/create.c
+++ libspe2/spebase/create.c
@@ -79,16 +79,16 @@ void _base_spe_context_unlock(spe_contex
 
 int open_if_closed(struct spe_context *spe, enum fd_name fdesc)
 {
-	if (spe->base_private->spe_fds_array[(int)fdesc] != -1) { // already open
-		spe->base_private->spe_fds_refcount[(int)fdesc]++;
-		return spe->base_private->spe_fds_array[(int)fdesc];
-	}
-	
 	_base_spe_context_lock(spe, fdesc);
 	
-	spe->base_private->spe_fds_array[(int)fdesc] = openat(spe->base_private->fd_spe_dir, spe_fd_name[(int)fdesc], spe_fd_mode[(int)fdesc]);
-	if (spe->base_private->spe_fds_array[(int)fdesc] > 0)
+	if (spe->base_private->spe_fds_array[(int)fdesc] != -1) { // already open
 		spe->base_private->spe_fds_refcount[(int)fdesc]++;
+	}
+	else {
+		spe->base_private->spe_fds_array[(int)fdesc] = openat(spe->base_private->fd_spe_dir, spe_fd_name[(int)fdesc], spe_fd_mode[(int)fdesc]);
+		if (spe->base_private->spe_fds_array[(int)fdesc] > 0)
+			spe->base_private->spe_fds_refcount[(int)fdesc]++;
+	}
 
 	_base_spe_context_unlock(spe, fdesc);
 	
@@ -97,23 +97,20 @@ int open_if_closed(struct spe_context *s
 
 void close_if_open(struct spe_context *spe, enum fd_name fdesc)
 {
+	_base_spe_context_lock(spe, fdesc);
+
 	if (spe->base_private->spe_fds_array[(int)fdesc] != -1 &&
 		spe->base_private->spe_fds_refcount[(int)fdesc] == 1){
-		_base_spe_context_lock(spe, fdesc);
 		
 		spe->base_private->spe_fds_refcount[(int)fdesc]--;
 		close(spe->base_private->spe_fds_array[(int)fdesc]);
 		
 		spe->base_private->spe_fds_array[(int)fdesc] = -1;
-		
-		_base_spe_context_unlock(spe, fdesc);
 	}else if (spe->base_private->spe_fds_refcount[(int)fdesc] > 0) {
-		_base_spe_context_lock(spe, fdesc);
-		
 		spe->base_private->spe_fds_refcount[(int)fdesc]--;
-		
-		_base_spe_context_unlock(spe, fdesc);
 	}
+		
+	_base_spe_context_unlock(spe, fdesc);
 }
 
 spe_context_ptr_t _base_spe_context_create(unsigned int flags, spe_gang_context_ptr_t gctx)



More information about the cbe-oss-dev mailing list