Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
make sure that if semaphore are not usable, we detect it and complain as we should
authorMartin Quinson <martin.quinson@loria.fr>
Mon, 27 Jun 2011 11:57:37 +0000 (13:57 +0200)
committerMartin Quinson <martin.quinson@loria.fr>
Mon, 27 Jun 2011 11:57:37 +0000 (13:57 +0200)
buildtools/Cmake/CompleteInFiles.cmake
buildtools/Cmake/test_prog/prog_sem_open.c [new file with mode: 0644]

index 94271ab..c5883ac 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;
+}