示例#1
0
int main()
{
	/* Thread and corresponding parameter declaration */
	pthread_t regul_thread;
	pthread_t gui_thread;
	struct sched_param regul_sp = { 99 };
	struct sched_param gui_sp = { 1 };
	int policy = SCHED_RR;

	/* Creating thread arguments struct and its contents */
	thread_args_t *thread_args = (thread_args_t*) malloc(
			sizeof(thread_args_t));
	thread_args->regul = init_regul();
	thread_args->data = init_data_struct();
	thread_args->run = 1;

	/* Creating Threads*/
	if (pthread_create(&regul_thread, NULL, run_regul,
			(void*) thread_args)) {
		printf("Failed to create regulator thread.\n");
		exit(1);
	}

	if (pthread_create(&gui_thread, NULL, run_gui, (void*) thread_args)) {
		printf("Failed to create GUI thread.\n");
		exit(1);
	}

	/* Only works if superuser or privileged user */
	pthread_setschedparam(regul_thread, policy, &regul_sp);
	pthread_setschedparam(gui_thread, policy, &gui_sp);
	
	/* Wait for GUI thread to finish, which turns regulator off */
	pthread_join(gui_thread, NULL);
	/* Wait for regulator to finish last loop */
	pthread_join(regul_thread, NULL);

	/* Free structs*/
	free_regul(thread_args->regul);
	free_data_struct(thread_args->data);
	free(thread_args);

	return 0;
}
示例#2
0
/** Implementation of rpc_add function required by the management API.
 *
 * This function will be called when an RPC management function calls
 * rpc->add to add a parameter to the xhttp_rpc reply being generated.
 */
static int rpc_add(rpc_ctx_t* ctx, char* fmt, ...)
{
	void **void_ptr;
	struct rpc_data_struct *ds;
	va_list ap;

	if (0!=xhttp_rpc_build_content(ctx, NULL, NULL)) {
		rpc_fault(ctx, 500, "Internal Server Error");
		return -1;
	}
	va_start(ap, fmt);
	while(*fmt) {
		if (*fmt == '{' || *fmt == '[') {
			void_ptr = va_arg(ap, void**);
			ds = new_data_struct(ctx);
			if (!ds) goto err;
			if (ctx->data_structs) free_data_struct(ctx->data_structs);
			ctx->data_structs = ds;
			*void_ptr = ds;
		} else {
			if (print_value(ctx, *fmt, &ap, NULL) < 0) goto err;
示例#3
0
/** Implementation of rpc_send function required by the management API.
 *
 * This is the function that will be called whenever a management function
 * asks the management interface to send the reply to the client.
 * The SIP/HTTP reply sent to
 * the client will be always 200 OK, if an error ocurred on the server then it
 * will be indicated in the html document in body.
 *
 * @param ctx A pointer to the context structure of the xhttp_rpc request that
 *            generated the reply.
 * @return 1 if the reply was already sent, 0 on success, a negative number on
 *            error
 */
static int rpc_send(rpc_ctx_t* ctx)
{
	struct xhttp_rpc_reply* reply;

	if (ctx->reply_sent) return 1;

	reply = &ctx->reply;

	if (0!=xhttp_rpc_build_page(ctx)){
		rpc_fault(ctx, 500, "Internal Server Error");
	}

	ctx->reply_sent = 1;
	if (reply->body.len)
		xhttp_api.reply(ctx->msg, reply->code, &reply->reason,
			&XHTTP_RPC_CONTENT_TYPE_TEXT_HTML, &reply->body);
	else
		xhttp_api.reply(ctx->msg, reply->code, &reply->reason,
			&XHTTP_RPC_CONTENT_TYPE_TEXT_HTML, &reply->reason);

	if (reply->buf.s) {
		pkg_free(reply->buf.s);
		reply->buf.s = NULL;
		reply->buf.len = 0;
	}
	if (ctx->arg.s) {
		pkg_free(ctx->arg.s);
		ctx->arg.s = NULL;
		ctx->arg.len = 0;
	}
	if (ctx->data_structs) {
		free_data_struct(ctx->data_structs);
		ctx->data_structs = NULL;
	}

	return 0;
}