Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of scm.gforge.inria.fr:/gitroot/simgrid/simgrid
authorMartin Quinson <martin.quinson@loria.fr>
Mon, 27 Jun 2011 11:57:57 +0000 (13:57 +0200)
committerMartin Quinson <martin.quinson@loria.fr>
Mon, 27 Jun 2011 11:57:57 +0000 (13:57 +0200)
buildtools/Cmake/CompleteInFiles.cmake
buildtools/Cmake/test_prog/prog_sem_open.c [new file with mode: 0644]
src/xbt/xbt_os_thread.c

index 8b8638b..a856ebb 100644 (file)
@@ -88,6 +88,7 @@ endif(enable_ns3)
 # Checks for header libraries functions.
 CHECK_LIBRARY_EXISTS(pthread   pthread_create                  "" pthread)
 CHECK_LIBRARY_EXISTS(pthread   sem_init                                "" HAVE_SEM_INIT_LIB)
+CHECK_LIBRARY_EXISTS(pthread   sem_open                                "" HAVE_SEM_OPEN_LIB)
 CHECK_LIBRARY_EXISTS(pthread   sem_timedwait                   "" HAVE_SEM_TIMEDWAIT_LIB)
 CHECK_LIBRARY_EXISTS(pthread   pthread_mutex_timedlock "" HAVE_MUTEX_TIMEDLOCK_LIB)
 CHECK_LIBRARY_EXISTS(rt                clock_gettime                   "" HAVE_POSIX_GETTIME)
@@ -205,18 +206,47 @@ elseif(pthread)
 endif(pthread)
 
 if(pthread)
-       ### HAVE_SEM_INIT
+       ### Test that we have a way to create semaphores
        
+       if(HAVE_SEM_OPEN_LIB)
+               exec_program("${CMAKE_C_COMPILER} -lpthread ${CMAKE_HOME_DIRECTORY}/buildtools/Cmake/test_prog/prog_sem_open.c -o testprog"
+                            OUTPUT_VARIABLE HAVE_SEM_OPEN_run)
+               if(HAVE_SEM_OPEN_run)
+                       set(HAVE_SEM_OPEN 0)
+               else(HAVE_SEM_OPEN_run)
+                       exec_program("./testprog" RETURN_VALUE HAVE_SEM_OPEN_run2)
+                       if(HAVE_SEM_OPEN_run2)
+                               set(HAVE_SEM_OPEN 0)
+                       else(HAVE_SEM_OPEN_run2)
+                               set(HAVE_SEM_OPEN 1)
+                       endif(HAVE_SEM_OPEN_run2)       
+               endif(HAVE_SEM_OPEN_run)
+        else(HAVE_SEM_OPEN_LIB)
+               set(HAVE_SEM_OPEN 0)
+       endif(HAVE_SEM_OPEN_LIB)
+
        if(HAVE_SEM_INIT_LIB)
-               exec_program("${CMAKE_C_COMPILER} -lpthread ${CMAKE_HOME_DIRECTORY}/buildtools/Cmake/test_prog/prog_sem_init.c" OUTPUT_VARIABLE HAVE_SEM_INIT_run)
+               exec_program("${CMAKE_C_COMPILER} -lpthread ${CMAKE_HOME_DIRECTORY}/buildtools/Cmake/test_prog/prog_sem_init.c -o testprog" 
+                            OUTPUT_VARIABLE HAVE_SEM_INIT_run)
                if(HAVE_SEM_INIT_run)
                        set(HAVE_SEM_INIT 0)
                else(HAVE_SEM_INIT_run)
-                       set(HAVE_SEM_INIT 1)
+                       exec_program("./testprog" RETURN_VALUE HAVE_SEM_INIT_run)
+                       if(HAVE_SEM_INIT_run)
+                               set(HAVE_SEM_INIT 0)
+                       else(HAVE_SEM_INIT_run)
+                               set(HAVE_SEM_INIT 1)
+                       endif(HAVE_SEM_INIT_run)
                endif(HAVE_SEM_INIT_run)
+        else(HAVE_SEM_INIT_LIB)
+               set(HAVE_SEM_INIT 0)
        endif(HAVE_SEM_INIT_LIB)
 
-       ### HAVE_SEM_TIMEDWAIT
+       if(NOT HAVE_SEM_OPEN AND NOT HAVE_SEM_INIT)
+               message(FATAL_ERROR "Semaphores are not usable, but they are mandatory to threads (you may need to mount /dev).")
+       endif(NOT HAVE_SEM_OPEN AND NOT HAVE_SEM_INIT)
+
+       ### Test that we have a way to timewait for semaphores
 
        if(HAVE_SEM_TIMEDWAIT_LIB)
                exec_program("${CMAKE_C_COMPILER} -lpthread ${CMAKE_HOME_DIRECTORY}/buildtools/Cmake/test_prog/prog_sem_timedwait.c" OUTPUT_VARIABLE HAVE_SEM_TIMEDWAIT_run)
diff --git a/buildtools/Cmake/test_prog/prog_sem_open.c b/buildtools/Cmake/test_prog/prog_sem_open.c
new file mode 100644 (file)
index 0000000..4679c32
--- /dev/null
@@ -0,0 +1,23 @@
+/* Copyright (c) 2010. The SimGrid Team.
+ * All rights reserved.                                                     */
+
+/* This program is free software; you can redistribute it and/or modify it
+ * under the terms of the license (GNU LGPL) which comes with this package. */
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <semaphore.h>
+#include <stdio.h>
+
+int main(void) {   
+   sem_t * s;
+   
+   s = sem_open("/0", O_CREAT, 0644, 10);
+   if (s == SEM_FAILED){
+//     printf("sem_open failed\n");
+     return 1;
+   }
+//   printf("sem_open succeeded\n");   
+   return 0;
+}
index b020ebd..b2be4c7 100644 (file)
@@ -495,7 +495,7 @@ xbt_os_sem_t xbt_os_sem_init(unsigned int value)
   if ((res->ps == (sem_t *) SEM_FAILED) && (errno == ENAMETOOLONG)) {
     /* Old darwins only allow 13 chars. Did you create *that* amount of semaphores? */
     res->name[13] = '\0';
-    res->ps = sem_open(res->name, O_CREAT, 0644, 1);
+    res->ps = sem_open(res->name, O_CREAT, 0644, value);
   }
   if ((res->ps == (sem_t *) SEM_FAILED))
     THROWF(system_error, errno, "sem_open() failed: %s", strerror(errno));