Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Workers #50

Merged
merged 11 commits into from
Mar 10, 2022
5 changes: 5 additions & 0 deletions core/platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ typedef _microstep_t microstep_t;

#ifdef NUMBER_OF_WORKERS

/**
* @brief Get the number of cores on the host machine.
*/
extern int lf_host_cores();
Soroosh129 marked this conversation as resolved.
Show resolved Hide resolved

/**
* Create a new thread, starting with execution of lf_thread
* getting passed arguments. The new handle is stored in thread_id.
Expand Down
1 change: 1 addition & 0 deletions core/platform/lf_linux_support.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#endif

#include "lf_unix_clock_support.c"
#include "lf_unix_syscall_support.c"

/**
* Pause execution for a number of nanoseconds.
Expand Down
1 change: 1 addition & 0 deletions core/platform/lf_macos_support.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#endif

#include "lf_unix_clock_support.c"
#include "lf_unix_syscall_support.c"

/**
* Pause execution for a number of nanoseconds.
Expand Down
19 changes: 19 additions & 0 deletions core/platform/lf_unix_syscall_support.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* @file lf_unix_syscall_support.c
* @author Soroush Bateni ([email protected])
* @brief Platform support for syscalls in Unix-like systems.
* @version 0.1
* @date 2022-03-09
*
* @copyright Copyright (c) 2022 The University of Texas at Dallas
*
*/

#include <unistd.h>

/**
* @brief Get the number of cores on the host machine.
*/
int lf_host_cores() {
return (int)sysconf(_SC_NPROCESSORS_ONLN);
}
Soroosh129 marked this conversation as resolved.
Show resolved Hide resolved
10 changes: 10 additions & 0 deletions core/platform/lf_windows_support.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include <windows.h>
#include <process.h>
#include <sysinfoapi.h>
#include <errno.h>
#include "lf_windows_support.h"
#include "../platform.h"
Expand All @@ -56,6 +57,15 @@ double _lf_frequency_to_ns = 1.0;
#define BILLION 1000000000

#ifdef NUMBER_OF_WORKERS
/**
* @brief Get the number of cores on the host machine.
*/
int lf_host_cores() {
SYSTEM_INFO sysinfo;
GetSystemInfo(&sysinfo);
return sysinfo.dwNumberOfProcessors;
}

#if __STDC_VERSION__ < 201112L || defined (__STDC_NO_THREADS__) // (Not C++11 or later) or no threads support

/**
Expand Down
19 changes: 17 additions & 2 deletions core/threaded/reactor_threaded.c
Original file line number Diff line number Diff line change
Expand Up @@ -1137,8 +1137,23 @@ int lf_reactor_c_main(int argc, char* argv[]) {

if (process_args(default_argc, default_argv)
&& process_args(argc, argv)) {
lf_mutex_lock(&mutex); // Sets start_time
initialize();

// If _lf_number_of_threads is 0, it means that it was not provided on
Soroosh129 marked this conversation as resolved.
Show resolved Hide resolved
// the command-line using the --threads argument.
lhstrh marked this conversation as resolved.
Show resolved Hide resolved
if (_lf_number_of_threads == 0u) {
#if !defined(NUMBER_OF_WORKERS) || NUMBER_OF_WORKERS == 0
// Use the number of cores on the host machine.
_lf_number_of_threads = lf_host_cores();
#else
// Use the provided number of workers by the user
_lf_number_of_threads = NUMBER_OF_WORKERS;
#endif
}

lf_mutex_lock(&mutex);
initialize(); // Sets start_time

info_print("---- Using %d workers.", _lf_number_of_threads);

// Initialize the scheduler
lf_sched_init(
Expand Down