Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' into mq
authorFred Suter <suterf@ornl.gov>
Fri, 27 Oct 2023 12:34:54 +0000 (08:34 -0400)
committerFred Suter <suterf@ornl.gov>
Fri, 27 Oct 2023 12:34:54 +0000 (08:34 -0400)
ChangeLog
MANIFEST.in
examples/sthread/pthread-mutex-recursive.c [new file with mode: 0644]
tools/cmake/Distrib.cmake

index 893664d..605242b 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -8,11 +8,16 @@ S4U:
    on symmetric routes.
  - Introduce a Mailbox::get_async() with no payload parameter. You can use the new 
    Comm::get_payload() once the communication is over to retrieve the payload.
+ - Implement recursive mutexes. Simply pass true to the constructor to get one.
 
 SMPI:
  - New SMPI_app_instance_join(): wait for the completion of a started MPI instance
  - MPI_UNIVERSE_SIZE now initialized to the total amount of hosts in the platform
 
+sthread:
+ - Implement recursive pthreads.
+ - Many bug fixes.
+
 Python:
  - Make the host_load plugin available from Python. See examples/python/plugin-host-load
  - Mailbox::get_async() does not return a pair anymore. Use comm.get_payload() instead.
index 2a24cda..f2f6e1d 100644 (file)
@@ -645,9 +645,12 @@ include examples/smpi/trace_call_location/trace_call_location.c
 include examples/smpi/trace_call_location/trace_call_location.tesh
 include examples/smpi/trace_simple/trace_simple.c
 include examples/smpi/trace_simple/trace_simple.tesh
+include examples/sthread/pthread-mc-mutex-recursive.tesh
 include examples/sthread/pthread-mc-mutex-simple.tesh
 include examples/sthread/pthread-mc-mutex-simpledeadlock.tesh
 include examples/sthread/pthread-mc-producer-consumer.tesh
+include examples/sthread/pthread-mutex-recursive.c
+include examples/sthread/pthread-mutex-recursive.tesh
 include examples/sthread/pthread-mutex-simple.c
 include examples/sthread/pthread-mutex-simple.tesh
 include examples/sthread/pthread-mutex-simpledeadlock.c
diff --git a/examples/sthread/pthread-mutex-recursive.c b/examples/sthread/pthread-mutex-recursive.c
new file mode 100644 (file)
index 0000000..482cf3e
--- /dev/null
@@ -0,0 +1,65 @@
+/* Copyright (c) 2002-2023. 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. */
+
+/* Code with both recursive and non-recursive mutexes */
+
+#include <pthread.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+// Structure to hold the mutex's name and pointer to the actual mutex
+typedef struct {
+  const char* name;
+  pthread_mutex_t* mutex;
+} ThreadData;
+
+static void* thread_function(void* arg)
+{
+  ThreadData* data       = (ThreadData*)arg;
+  pthread_mutex_t* mutex = data->mutex;
+  const char* name       = data->name;
+
+  pthread_mutex_lock(mutex);
+  fprintf(stderr, "Got the lock on the %s mutex.\n", name);
+
+  // Attempt to relock the mutex - This behavior depends on the mutex type
+  if (pthread_mutex_trylock(mutex) == 0) {
+    fprintf(stderr, "Got the lock again on the %s mutex.\n", name);
+    pthread_mutex_unlock(mutex);
+  } else {
+    fprintf(stderr, "Failed to relock the %s mutex.\n", name);
+  }
+
+  pthread_mutex_unlock(mutex);
+
+  // pthread_exit(NULL); TODO: segfaulting
+  return NULL;
+}
+
+int main()
+{
+  pthread_t thread1, thread2;
+  pthread_mutex_t mutex_dflt = PTHREAD_MUTEX_INITIALIZER; // Non-recursive mutex
+
+  pthread_mutexattr_t attr;
+  pthread_mutexattr_init(&attr);
+  pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
+  pthread_mutex_t mutex_rec;
+  pthread_mutex_init(&mutex_rec, &attr);
+
+  ThreadData data1 = {"default", &mutex_dflt};
+  ThreadData data2 = {"recursive", &mutex_rec};
+
+  pthread_create(&thread1, NULL, thread_function, &data1);
+  pthread_create(&thread2, NULL, thread_function, &data2);
+
+  pthread_join(thread1, NULL);
+  pthread_join(thread2, NULL);
+
+  pthread_mutex_destroy(&mutex_dflt);
+  pthread_mutex_destroy(&mutex_rec);
+
+  return 0;
+}
index 6a144b1..5095296 100644 (file)
@@ -43,7 +43,9 @@ add_custom_target(simgrid_convert_TI_traces ALL
 
 # libraries
 install(TARGETS simgrid DESTINATION ${CMAKE_INSTALL_LIBDIR}/)
-install(TARGETS sthread DESTINATION ${CMAKE_INSTALL_LIBDIR}/)
+if("${CMAKE_SYSTEM}" MATCHES "Linux")
+  install(TARGETS sthread DESTINATION ${CMAKE_INSTALL_LIBDIR}/)
+endif()
 
 # pkg-config files
 configure_file("${CMAKE_HOME_DIRECTORY}/tools/pkg-config/simgrid.pc.in"