/** =========================================================================
 * This is the main worker thread.  It looks for information comming in the
 * FIFO and submits the data to the database.  This ensures that the PerfMgr
 * (and therefore OpenSM threads) will not be delayed by DB activity.
 */
void *
db_write_thread(void *pd)
{
	plugin_data_t *plugin_data = (plugin_data_t *)pd;
	data_entry_t  *data = NULL;

	plugin_log(plugin_data->osmlog, OSM_LOG_INFO,
			"In DB write thread\n");

	plugin_data->exit_flag = 0;
	if (sql_setup_db_conn(plugin_data)) {
		thread_signal(plugin_data);
		return (NULL);
	}

	thread_signal(plugin_data);

	while (1) {
		if (plugin_data->exit_flag) {
			return (NULL);
		}

		if (!plugin_data->tail) {
			struct timespec ts;
			clock_gettime(CLOCK_REALTIME, &ts);
			ts.tv_sec += plugin_data->thread_sleep_s;
			pthread_mutex_lock(&(plugin_data->sig_lock));
			pthread_cond_timedwait(&(plugin_data->signal), &(plugin_data->sig_lock), &ts);
			pthread_mutex_unlock(&(plugin_data->sig_lock));
			continue;
		}

		data = pull_from_tail(plugin_data);

		if (!data) {
			plugin_log(plugin_data->osmlog, OSM_LOG_ERROR,
				"plugin_data->tail != NULL but pull returned NULL?");
			continue;
		}

		switch(data->type)
		{
			case OSM_EVENT_ID_PORT_ERRORS:
				sql_add_port_errors(plugin_data, &(data->data.pe_event));
				break;
			case OSM_EVENT_ID_PORT_DATA_COUNTERS:
				sql_add_data_counters(plugin_data, &(data->data.dc_event));
				break;
			case OSM_EVENT_ID_PORT_SELECT:
				sql_add_port_select(plugin_data, &(data->data.ps_event));
				break;
			case OSM_EVENT_ID_TRAP:
				break;
			case OSM_EVENT_ID_MAX: break;
		}

		free(data);
	}
}
static void poll_on_close (NetPoll *poll, NetPollConnection *connection, bool success)
{
        (void)poll;
        if (success == false) {
                printf ("on_close: success == false\n"); fflush (stdout);
                exit (-1);
        }
        if (connection == &poll_server) {
                thread_signal (&server_close_signal);
        }
        if (connection == &poll_client) {
                thread_signal (&client_close_signal);
        }
}
static void on_close (NetPoll *poll, NetPollConnection *connection, bool success)
{
        (void)poll;
        (void)connection;
        close_success = success;
        thread_signal (&signal);
}
static void on_monitor (NetPoll *poll, NetPollConnection *connection, bool success)
{
        (void)poll;
        (void)connection;
        (void)success;
        monitor_success = success;
        thread_signal (&monitor_signal);
}
Example #5
0
int luajack_signal(luajack_t *client, luajack_t *thread)
	{
	cud_t *cud = get_cud(client);	
	tud_t *tud = get_tud(thread);
	if(cud && tud)
		return thread_signal(cud, tud);
	return 0;
	}
static void poll_on_write (NetPoll *poll, NetPollConnection *connection, unsigned char *buffer, size_t length, bool success)
{
        (void)poll;
        (void)connection;
        (void)buffer;
        (void)length;
        (void)success;
        thread_signal (&server_write_signal);
}
Example #7
0
static void client_stream_on_add (NetStream *stream, NetStreamConnection *connection)
{
        (void)stream;
        streams[connections_count] = connection;
        connections_count++;
        if (connections_count == connections_max) {
                thread_signal (&connections_ready);
        }
}
Example #8
0
void app_event_exit (void)
{
        if (!lock_exit_set (true)) {
                error_code (FunctionCall, 1);
                return;
        }
        if (!thread_signal (&global.signal)) {
                error_code (FunctionCall, 2);
        }
}
Example #9
0
static void proc_stop(uint32_t pid)
{
	int ret;

	ret = TEMP_FAILURE_RETRY(sem_wait(&shm->sem));
	if (!ret) {
		if (thread_signal(pid, pid, SIGSTOP) != -1)
			for_each_thread(pid, thread_stop);

		sem_post(&shm->sem);
	}
}
Example #10
0
static void server_stream_on_read (NetStream *stream, NetStreamConnection *connection, unsigned char *buffer, size_t length)
{
        read_count += length;

        (void)stream;
        (void)connection;
        (void)buffer;
        (void)length;
        if (read_count == 2000) {
                thread_signal (&server_read_signal);
        }
}
Example #11
0
DECLARE_TEST(profile, thread) {
	thread_t thread[32];
	int ith;
	uint64_t frame;
	error_t err = error();

	_test_profile_offset = 0;
	atomic_store32(&_test_profile_output_counter, 0);

	profile_initialize(STRING_CONST("test_profile"), _test_profile_buffer, 30000);
	profile_enable(true);
	profile_set_output_wait(1);

	log_enable_stdout(false);
	for (ith = 0; ith < 32; ++ith)
		thread_initialize(&thread[ith], _profile_fail_thread, 0, STRING_CONST("profile_thread"),
		                  THREAD_PRIORITY_NORMAL, 0);
	for (ith = 0; ith < 32; ++ith)
		thread_start(&thread[ith]);

	test_wait_for_threads_startup(thread, 32);

	for (frame = 0; frame < 1000; ++frame) {
		thread_sleep(16);
		profile_end_frame(frame);
	}

	for (ith = 0; ith < 32; ++ith)
		thread_signal(&thread[ith]);

	test_wait_for_threads_finish(thread, 32);

	for (ith = 0; ith < 32; ++ith)
		thread_finalize(&thread[ith]);
	log_enable_stdout(true);

	err = error();
	thread_sleep(1000);

	profile_enable(false);
	profile_finalize();

#if BUILD_ENABLE_PROFILE
	EXPECT_INTGT(atomic_load32(&_test_profile_output_counter), 0);
	//TODO: Implement parsing output results
#else
	EXPECT_INTEQ(atomic_load32(&_test_profile_output_counter), 0);
#endif
	EXPECT_INTEQ(err, ERROR_NONE);

	return 0;
}
static void poll_on_read (NetPoll *poll, NetPollConnection *connection, unsigned char *buffer, size_t length)
{
    size_t i;

    (void)poll;
    (void)connection;
    (void)buffer;
    (void)length;

    if (connection == &poll_client) {
        for (i = 0; i < length; i++, client_read_count++) {
            if (buffer[i] != (unsigned char)client_read_count) {
                printf ("poll_on_read: read something strange\n");
                fflush (stdout);
                exit (-1);
            }
        }
        if (client_read_count == BufferSize) {
            thread_signal (&client_read_signal);
        }
    }
    else if (connection == &poll_server) {
        for (i = 0; i < length; i++, server_read_count++) {
            if (buffer[i] != (unsigned char)server_read_count) {
                printf ("poll_on_read: read something strange\n");
                fflush (stdout);
                exit (-1);
            }
        }
        if (server_read_count == BufferSize) {
            thread_signal (&server_read_signal);
        }
    }
    else {
        printf ("Connection is not known\n");
        fflush (stdout);
        exit (-1);
    }
}
static void poll_on_read (NetPoll *poll, NetPollConnection *connection, unsigned char *buffer, size_t length)
{
        (void)poll;
        (void)connection;
        (void)buffer;
        (void)length;
        if (length == 1 && buffer[0] == 'X') {
                thread_signal (&client_read_signal);
        }
        else {
                printf ("poll_on_read: read something strange\n");
                fflush (stdout);
                exit (-1);
        }
}
static void poll_on_read (NetPoll *poll, NetPollConnection *connection, unsigned char *buffer, size_t length)
{
        size_t i;

        (void)poll;
        (void)connection;
        (void)buffer;
        (void)length;

        for (i = 0; i < length; i++, read_count++) {
                if (buffer[i] != (unsigned char)read_count) {
                        printf ("poll_on_read: read something strange\n");
                        fflush (stdout);
                        exit (-1);
                }
        }
        if (read_count == BufferSize) {
                thread_signal (&client_read_signal);
        }
}
Example #15
0
int
main_run(void* main_arg) {
#if !BUILD_MONOLITHIC
	string_const_t pattern;
	string_t* exe_paths = 0;
	size_t iexe, exesize;
	process_t* process = 0;
	string_t process_path = { 0, 0 };
	unsigned int* exe_flags = 0;
#else
	void* test_result;
#endif
#if FOUNDATION_PLATFORM_IOS || FOUNDATION_PLATFORM_ANDROID || FOUNDATION_PLATFORM_PNACL
	int remain_counter = 0;
#endif
#if BUILD_DEBUG
	const string_const_t build_name = string_const(STRING_CONST("debug"));
#elif BUILD_RELEASE
	const string_const_t build_name = string_const(STRING_CONST("release"));
#elif BUILD_PROFILE
	const string_const_t build_name = string_const(STRING_CONST("profile"));
#elif BUILD_DEPLOY
	const string_const_t build_name = string_const(STRING_CONST("deploy"));
#endif
#if BUILD_MONOLITHIC
	const string_const_t build_type = string_const(STRING_CONST(" monolithic"));
#else
	const string_const_t build_type = string_empty();
#endif
	char* pathbuf;
	int process_result = 0;
	thread_t event_thread;
	FOUNDATION_UNUSED(main_arg);
	FOUNDATION_UNUSED(build_name);

	log_set_suppress(HASH_TEST, ERRORLEVEL_DEBUG);

	log_infof(HASH_TEST, STRING_CONST("Task library v%s built for %s using %s (%.*s%.*s)"),
	          string_from_version_static(task_module_version()).str, FOUNDATION_PLATFORM_DESCRIPTION,
	          FOUNDATION_COMPILER_DESCRIPTION, STRING_FORMAT(build_name), STRING_FORMAT(build_type));

	thread_initialize(&event_thread, event_loop, 0, STRING_CONST("event_thread"),
	                  THREAD_PRIORITY_NORMAL, 0);
	thread_start(&event_thread);

	pathbuf = memory_allocate(HASH_STRING, BUILD_MAX_PATHLEN, 0, MEMORY_PERSISTENT);

	while (!thread_is_running(&event_thread))
		thread_sleep(10);

#if FOUNDATION_PLATFORM_IOS || FOUNDATION_PLATFORM_ANDROID || FOUNDATION_PLATFORM_PNACL
	while (!_test_should_start) {
#if FOUNDATION_PLATFORM_ANDROID
		system_process_events();
#endif
		thread_sleep(100);
	}
#endif

	fs_remove_directory(STRING_ARGS(environment_temporary_directory()));

#if BUILD_MONOLITHIC

	test_run_fn tests[] = {
		test_task_run,
		0
	};

#if FOUNDATION_PLATFORM_ANDROID

	thread_t test_thread;
	thread_initialize(&test_thread, test_runner, tests, STRING_CONST("test_runner"),
	                  THREAD_PRIORITY_NORMAL, 0);
	thread_start(&test_thread);

	log_debug(HASH_TEST, STRING_CONST("Starting test runner thread"));

	while (!thread_is_running(&test_thread)) {
		system_process_events();
		thread_sleep(10);
	}

	while (thread_is_running(&test_thread)) {
		system_process_events();
		thread_sleep(10);
	}

	test_result = thread_join(&test_thread);
	process_result = (int)(intptr_t)test_result;

	thread_finalize(&test_thread);

#else

	test_result = test_runner(tests);
	process_result = (int)(intptr_t)test_result;

#endif

	if (process_result != 0)
		log_warnf(HASH_TEST, WARNING_SUSPICIOUS, STRING_CONST("Tests failed with exit code %d"),
		          process_result);

#if FOUNDATION_PLATFORM_IOS || FOUNDATION_PLATFORM_ANDROID || FOUNDATION_PLATFORM_PNACL

	while (!_test_should_terminate && _test_have_focus && (remain_counter < 50)) {
		system_process_events();
		thread_sleep(100);
		++remain_counter;
	}

#endif

	log_debug(HASH_TEST, STRING_CONST("Exiting main loop"));

#else // !BUILD_MONOLITHIC

	//Find all test executables in the current executable directory
#if FOUNDATION_PLATFORM_WINDOWS
	pattern = string_const(STRING_CONST("^test-.*\\.exe$"));
#elif FOUNDATION_PLATFORM_MACOSX
	pattern = string_const(STRING_CONST("^test-.*$"));
#elif FOUNDATION_PLATFORM_POSIX
	pattern = string_const(STRING_CONST("^test-.*$"));
#else
#  error Not implemented
#endif
	exe_paths = fs_matching_files(STRING_ARGS(environment_executable_directory()),
	                              STRING_ARGS(pattern), false);
	array_resize(exe_flags, array_size(exe_paths));
	memset(exe_flags, 0, sizeof(unsigned int) * array_size(exe_flags));
#if FOUNDATION_PLATFORM_MACOSX
	//Also search for test applications
	string_const_t app_pattern = string_const(STRING_CONST("^test-.*\\.app$"));
	regex_t* app_regex = regex_compile(app_pattern.str, app_pattern.length);
	string_t* subdirs = fs_subdirs(STRING_ARGS(environment_executable_directory()));
	for (size_t idir = 0, dirsize = array_size(subdirs); idir < dirsize; ++idir) {
		if (regex_match(app_regex, subdirs[idir].str, subdirs[idir].length, 0, 0)) {
			string_t exe_path = { subdirs[idir].str, subdirs[idir].length - 4 };
			array_push(exe_paths, exe_path);
			array_push(exe_flags, PROCESS_MACOSX_USE_OPENAPPLICATION);
		}
	}
	string_array_deallocate(subdirs);
	regex_deallocate(app_regex);
#endif
	for (iexe = 0, exesize = array_size(exe_paths); iexe < exesize; ++iexe) {
		string_const_t* process_args = 0;
		string_const_t exe_file_name = path_base_file_name(STRING_ARGS(exe_paths[iexe]));
		if (string_equal(STRING_ARGS(exe_file_name), STRING_ARGS(environment_executable_name())))
			continue; //Don't run self

		process_path = path_concat(pathbuf, BUILD_MAX_PATHLEN,
		                           STRING_ARGS(environment_executable_directory()),
		                           STRING_ARGS(exe_paths[iexe]));
		process = process_allocate();

		process_set_executable_path(process, STRING_ARGS(process_path));
		process_set_working_directory(process, STRING_ARGS(environment_executable_directory()));
		process_set_flags(process, PROCESS_ATTACHED | exe_flags[iexe]);

		if (!_test_memory_tracker)
			array_push(process_args, string_const(STRING_CONST("--no-memory-tracker")));
		process_set_arguments(process, process_args, array_size(process_args));

		log_infof(HASH_TEST, STRING_CONST("Running test executable: %.*s"),
		          STRING_FORMAT(exe_paths[iexe]));

		process_result = process_spawn(process);
		while (process_result == PROCESS_WAIT_INTERRUPTED) {
			thread_sleep(10);
			process_result = process_wait(process);
		}
		process_deallocate(process);
		array_deallocate(process_args);

		if (process_result != 0) {
			if (process_result >= PROCESS_INVALID_ARGS)
				log_warnf(HASH_TEST, WARNING_SUSPICIOUS,
				          STRING_CONST("Tests failed, process terminated with error %x"),
				          process_result);
			else
				log_warnf(HASH_TEST, WARNING_SUSPICIOUS, STRING_CONST("Tests failed with exit code %d"),
				          process_result);
			process_set_exit_code(-1);
			goto exit;
		}

		log_infof(HASH_TEST, STRING_CONST("All tests from %.*s passed (%d)"),
		          STRING_FORMAT(exe_paths[iexe]), process_result);
	}

	log_info(HASH_TEST, STRING_CONST("All tests passed"));

exit:

	if (exe_paths)
		string_array_deallocate(exe_paths);
	array_deallocate(exe_flags);

#endif

	_test_should_terminate = true;

	thread_signal(&event_thread);
	thread_finalize(&event_thread);

	memory_deallocate(pathbuf);

	log_infof(HASH_TEST, STRING_CONST("Tests exiting: %s (%d)"),
	          process_result ? "FAILED" : "PASSED", process_result);

	if (process_result)
		memory_set_tracker(memory_tracker_none());

	return process_result;
}
static void client_on_connect (NetClient *client, NetClientConnection *connection)
{
        (void)client;
        client_socket = connection->socket;
        thread_signal (&client_connect_signal);
}
static void server_on_connect (NetServer *server, int socket)
{
        (void)server;
        server_socket = socket;
        thread_signal (&server_connect_signal);
}
static void client_on_connect_error (NetClient *client, NetClientConnection *connection)
{
        thread_signal (&client_ready);
        (void)connection;
        (void)client;
}
static void client_on_error (NetClient *client)
{
        thread_signal (&client_ready);
        (void)client;
} 
Example #20
0
static void client_stream_on_close (NetStream *stream, NetStreamConnection *connection)
{
        (void)stream;
        (void)connection;
        thread_signal (&client_close_signal);
}
Example #21
0
static void client_stream_on_add (NetStream *stream, NetStreamConnection *connection)
{
        (void)stream;
        stream_connection_client = connection;
        thread_signal (&client_add_signal);
}
Example #22
0
static void thread_stop(pid_t pid, pid_t tid)
{
	thread_signal(pid, tid, SIGSTOP);
}
Example #23
0
static void server_stream_on_close (NetStream *stream, NetStreamConnection *connection)
{
        (void)stream;
        (void)connection;
        thread_signal (&server_close_signal);
}
Example #24
0
static void server_stream_on_add (NetStream *stream, NetStreamConnection *connection)
{
        (void)stream;
        stream_connection_server = connection;
        thread_signal (&server_add_signal);
}
Example #25
0
DECLARE_TEST(profile, stream) {
	thread_t thread[32];
	int ith;
	uint64_t frame;
	string_t filename;

	error(); //Clear error

	filename = path_allocate_concat(STRING_ARGS(environment_temporary_directory()),
	                                STRING_CONST("test.profile"));
	//log_infof(HASH_TEST, STRING_CONST("Output to profile file: %.*s"), STRING_FORMAT(filename));
	fs_make_directory(STRING_ARGS(environment_temporary_directory()));
	_profile_stream = fs_open_file(STRING_ARGS(filename), STREAM_OUT | STREAM_BINARY);
	string_deallocate(filename.str);

	profile_initialize(STRING_CONST("test_profile"), _test_profile_buffer, TEST_PROFILE_BUFFER_SIZE);
	profile_set_output(_profile_file_writer);
	profile_set_output_wait(10);
	profile_enable(true);

	for (ith = 0; ith < 32; ++ith)
		thread_initialize(&thread[ith], _profile_stream_thread, 0, STRING_CONST("profile_thread"),
		                  THREAD_PRIORITY_NORMAL, 0);
	for (ith = 0; ith < 32; ++ith)
		thread_start(&thread[ith]);

	test_wait_for_threads_startup(thread, 32);

	for (frame = 0; frame < 1000; ++frame) {
		thread_sleep(16);
		profile_log(
		    STRING_CONST("This is a really long profile log line that should break into multiple profile blocks automatically without causing any issues whatsoever if everything works as expected which it should or the code needs to be fixed"));
		profile_end_frame(frame++);
		if ((frame % 30) == 0) {
			profile_enable(false);
			thread_sleep(10);
			profile_enable(true);
		}
	}

	for (ith = 0; ith < 32; ++ith)
		thread_signal(&thread[ith]);

	test_wait_for_threads_finish(thread, 32);

	for (ith = 0; ith < 32; ++ith)
		thread_finalize(&thread[ith]);

	profile_end_frame(frame++);
	profile_set_output_wait(10000);

	thread_sleep(1000);

	profile_begin_block(STRING_CONST("Should be cleaned up"));
	profile_end_block();

	profile_enable(false);
	profile_finalize();

	error();

	stream_deallocate(_profile_stream);

//TODO: Validate that output is sane
	log_debugf(HASH_TEST, STRING_CONST("Generated %" PRId64 " blocks"),
	           atomic_load64(&_profile_generated_blocks));

	return 0;
}
Example #26
0
void thread_kill(thread_t * thread) {
    thread_signal(thread, SIGKILL);
    thread->state.stopme = false;
    while (!thread->state.dead)
        thread_wait(thread, false);
}
Example #27
0
/* Initialize the server - return fd of the listening socket or -1 on error */
int server_init (int debugging, int foreground)
{
	struct sockaddr_un sock_name;
	int server_sock;
	int pid;

	logit ("Starting MOC Server");

	pid = check_pid_file ();
	if (pid && valid_pid(pid)) {
		fprintf (stderr, "\nIt seems that the server is already running"
				" with pid %d.\n", pid);
		fprintf (stderr, "If it is not true, remove the pid file (%s)"
				" and try again.\n",
				create_file_name(PID_FILE));
		fatal ("Exiting!");
	}

	if (foreground)
		log_init_stream (stdout, "stdout");
	else {
		FILE *logfp;

		logfp = NULL;
		if (debugging) {
			logfp = fopen (SERVER_LOG, "a");
			if (!logfp)
				fatal ("Can't open server log file: %s", strerror (errno));
		}
		log_init_stream (logfp, SERVER_LOG);
	}

	if (pipe(wake_up_pipe) < 0)
		fatal ("pipe() failed: %s", strerror(errno));

	unlink (socket_name());

	/* Create a socket */
	if ((server_sock = socket (PF_LOCAL, SOCK_STREAM, 0)) == -1)
		fatal ("Can't create socket: %s", strerror(errno));
	sock_name.sun_family = AF_LOCAL;
	strcpy (sock_name.sun_path, socket_name());

	/* Bind to socket */
	if (bind(server_sock, (struct sockaddr *)&sock_name, SUN_LEN(&sock_name)) == -1)
		fatal ("Can't bind() to the socket: %s", strerror(errno));

	if (listen(server_sock, 1) == -1)
		fatal ("listen() failed: %s", strerror(errno));

	audio_initialize ();
	tags_cache_init (&tags_cache, options_get_int("TagsCacheSize"));
	tags_cache_load (&tags_cache, create_file_name("cache"));
	clients_init ();

	server_tid = pthread_self ();
	thread_signal (SIGTERM, sig_exit);
	thread_signal (SIGINT, foreground ? sig_exit : SIG_IGN);
	thread_signal (SIGHUP, SIG_IGN);
	thread_signal (SIGQUIT, sig_exit);
	thread_signal (SIGPIPE, SIG_IGN);

	write_pid_file ();

	if (!foreground) {
		setsid ();
		redirect_output (stdin);
		redirect_output (stdout);
		redirect_output (stderr);
	}

	return server_sock;
}