1 /* Copyright (c) 2006-2018. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
6 #include "simgrid/s4u.hpp"
10 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_test, "a sample log category");
12 // This example implements a one-time use barrier with 1 semaphore and 1 mutex.
14 static void worker(simgrid::s4u::SemaphorePtr semaphore, simgrid::s4u::MutexPtr mutex, int process_count, std::shared_ptr<int> count)
17 XBT_INFO("Got mutex. Incrementing count.");
18 XBT_INFO("Count is %d", *count);
19 *count = (*count) + 1;
20 XBT_INFO("Count is now %d. Process count is %d.", *count, process_count);
22 if (*count == process_count) {
23 XBT_INFO("Releasing the semaphore %d times.", process_count-1);
24 for (int i = 0; i < process_count-1; i++) {
28 XBT_INFO("Releasing mutex.");
32 XBT_INFO("Releasing mutex.");
34 XBT_INFO("Acquiring semaphore.");
41 static void master(unsigned int process_count)
43 simgrid::s4u::SemaphorePtr semaphore = simgrid::s4u::Semaphore::create(0);
44 simgrid::s4u::MutexPtr mutex = simgrid::s4u::Mutex::create();
45 std::shared_ptr<int> count(new int);
48 XBT_INFO("Spawning %d workers", process_count);
49 for (unsigned int i = 0; i < process_count; i++) {
50 simgrid::s4u::Actor::create("worker", simgrid::s4u::Host::by_name("Jupiter"), worker, semaphore, mutex, process_count, count);
54 int main(int argc, char **argv)
56 // Parameter: Number of processes in the barrier
57 xbt_assert(argc >= 2, "Usage: %s <process-count>\n", argv[0]);
58 unsigned int process_count = std::stoi(argv[1]);
59 xbt_assert(process_count > 0, "<process-count> must be greater than 0");
61 simgrid::s4u::Engine e(&argc, argv);
62 e.load_platform("../../platforms/two_hosts.xml");
63 simgrid::s4u::Actor::create("master", simgrid::s4u::Host::by_name("Tremblay"), master, process_count);