[Cbe-oss-dev] [PATCH 1/2] libspe2: Make spebase thread-safe

Kazunori Asayama asayama at sm.sony.co.jp
Fri Nov 17 16:51:00 EST 2006


Attached is a patch to make the 'spebase' part of libspe2 thread-safe.

--
(ASAYAMA Kazunori
  (asayama at sm.sony.co.jp))
t
-------------- next part --------------
Index: libspe2/spebase/spebase.h
===================================================================
--- libspe2.orig/spebase/spebase.h
+++ libspe2/spebase/spebase.h
@@ -29,6 +29,8 @@ extern "C"
 
 #include "libspe2-types.h"
 
+#include <pthread.h>
+
 #define __PRINTF(fmt, args...) { fprintf(stderr,fmt , ## args); }
 #ifdef DEBUG
 #define DEBUG_PRINTF(fmt, args...) __PRINTF(fmt , ## args)
@@ -43,6 +45,11 @@ enum fd_name { FD_MBOX, FD_MBOX_STAT, FD
  * "Private" structure -- do no use, if you want to achieve binary compatibility
  */
 struct spe_context_base_priv {
+	/* mutex to protect members which modified in lifetime of the context:
+	     spe_fds_array, entry
+	 */
+	pthread_mutex_t lock;
+
 	/* SPE Group base directory fd */
 	int		fd_grp_dir;
 	
@@ -556,6 +563,20 @@ int _base_spe_ls_size_get(spe_context_pt
  */
 void  __spe_context_update_event(void);
 
+/**
+ * _base_spe_context_lock locks members of the SPE context
+ *
+ * @param spectx Specifies the SPE context
+ */
+void _base_spe_context_lock(spe_context_ptr_t spe);
+
+/**
+ * _base_spe_context_unlock unlocks members of the SPE context
+ *
+ * @param spectx Specifies the SPE context
+ */
+void _base_spe_context_unlock(spe_context_ptr_t spe);
+
 #ifdef __cplusplus
 }
 #endif
Index: libspe2/spebase/create.c
===================================================================
--- libspe2.orig/spebase/create.c
+++ libspe2/spebase/create.c
@@ -66,15 +66,31 @@ int setsignotify(int dir, const char* fi
 	return 0;
 }
 
+void _base_spe_context_lock(spe_context_ptr_t spe)
+{
+	pthread_mutex_lock(&spe->base_private->lock);
+}
+
+void _base_spe_context_unlock(spe_context_ptr_t spe)
+{
+	pthread_mutex_unlock(&spe->base_private->lock);
+}
+
 int open_if_closed(struct spe_context *spe, enum fd_name fdesc)
 {
-	if (spe->base_private->spe_fds_array[(int)fdesc] != -1) { // already open
-		return spe->base_private->spe_fds_array[(int)fdesc];
+	int fd;
+
+	_base_spe_context_lock(spe);
+
+	if (spe->base_private->spe_fds_array[(int)fdesc] == -1) {
+		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]);
 	}
 	
-	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]);
-	
-	return spe->base_private->spe_fds_array[(int)fdesc];
+	fd = spe->base_private->spe_fds_array[(int)fdesc];
+
+	_base_spe_context_unlock(spe);
+
+	return fd;
 }
 
 spe_context_ptr_t _base_spe_context_create(unsigned int flags, spe_gang_context_ptr_t gctx)
@@ -195,6 +211,7 @@ spe_context_ptr_t _base_spe_context_crea
 		}
 	}
 
+	pthread_mutex_init(&spe->base_private->lock, NULL);
 	
 	return spe;
 }
@@ -263,6 +280,8 @@ int _base_spe_context_destroy(spe_contex
 		close(spe->base_private->spe_fds_array[i]);
 
 	close(spe->base_private->fd_spe_dir);
+
+	pthread_mutex_destroy(&spe->base_private->lock);
 	
 	free(spe->base_private);
 	free(spe);
Index: libspe2/Makefile
===================================================================
--- libspe2.orig/Makefile
+++ libspe2/Makefile
@@ -84,7 +84,7 @@ tags:
 	$(CTAGS) -R .
 
 $(libspe2_SO): $(libspe2_OBJS) base-all  event-all 
-	$(CC) $(CFLAGS) -shared -o $@ $(libspe2_OBJS) spebase/*.o speevent/*.o -lrt -Wl,--soname=${libspe2_SONAME}
+	$(CC) $(CFLAGS) -shared -o $@ $(libspe2_OBJS) spebase/*.o speevent/*.o -lrt -lpthread -Wl,--soname=${libspe2_SONAME}
 
 $(libspe2_A): $(libspe2_OBJS) base-all  event-all 
 	 $(CROSS)ar -r $(libspe2_A) $(libspe2_OBJS) spebase/*.o speevent/*.o $(libspe2_OBJS)
Index: libspe2/spebase/Makefile
===================================================================
--- libspe2.orig/spebase/Makefile
+++ libspe2/spebase/Makefile
@@ -48,7 +48,7 @@ clean:
 	rm -rf $(libspebase_A) $(libspebase_OBJS) $(libspebase_SO)
 
 $(libspebase_SO): $(libspebase_OBJS)
-	$(CC) $(CFLAGS) -shared -o $@ $^ -lrt -Wl,--soname=${libspebase_SONAME}
+	$(CC) $(CFLAGS) -shared -o $@ $^ -lrt -lpthread -Wl,--soname=${libspebase_SONAME}
 
 $(libspebase_A): $(libspebase_OBJS)
 	 $(CROSS)ar -r $(libspebase_A) $(libspebase_OBJS)


More information about the cbe-oss-dev mailing list