machinarium: add name to machine_create(); use it as a thread name

This commit is contained in:
Dmitry Simonenko 2017-05-17 17:35:55 +03:00
parent 754cef466b
commit c1d7c58bc6
26 changed files with 50 additions and 24 deletions

View File

@ -39,7 +39,7 @@ machinarium_free(void);
/* machine control */
MACHINE_API int
machine_create(machine_function_t, void *arg);
machine_create(char *name, machine_function_t, void *arg);
MACHINE_API void
machine_stop(void);

View File

@ -34,24 +34,31 @@ machine_main(void *arg)
{
mm_machine_t *machine = arg;
mm_self = machine;
machine->online = 1;
/* set thread name */
if (machine->name)
mm_thread_set_name(&machine->thread, machine->name);
/* create main fiber */
int64_t id;
id = machine_create_fiber(machine->main, machine->main_arg);
(void)id;
/* run main loop */
machine->online = 1;
for (;;) {
if (! mm_scheduler_online(&machine->scheduler))
break;
mm_loop_step(&machine->loop);
}
machine->online = 0;
machine_free(machine);
return NULL;
}
MACHINE_API int
machine_create(machine_function_t function, void *arg)
machine_create(char *name, machine_function_t function, void *arg)
{
mm_machine_t *machine;
machine = malloc(sizeof(*machine));
@ -61,6 +68,14 @@ machine_create(machine_function_t function, void *arg)
machine->id = 0;
machine->main = function;
machine->main_arg = arg;
machine->name = NULL;
if (name) {
machine->name = strdup(name);
if (machine->name == NULL) {
free(machine);
return -1;
}
}
mm_list_init(&machine->link);
mm_scheduler_init(&machine->scheduler, 2048 /* 16K */, machine);
int rc;
@ -92,6 +107,8 @@ machine_join(int id)
if (rc == -1)
return -1;
rc = mm_thread_join(&machine->thread);
if (machine->name)
free(machine->name);
free(machine);
return rc;
}

View File

@ -12,6 +12,7 @@ typedef struct mm_machine_t mm_machine_t;
struct mm_machine_t {
int online;
int id;
char *name;
machine_function_t main;
void *main_arg;
mm_thread_t thread;

View File

@ -25,3 +25,10 @@ int mm_thread_join(mm_thread_t *thread)
rc = pthread_join(thread->id, NULL);
return rc;
}
int mm_thread_set_name(mm_thread_t *thread, char *name)
{
int rc;
rc = pthread_setname_np(thread->id, name);
return rc;
}

View File

@ -19,5 +19,6 @@ struct mm_thread_t {
int mm_thread_create(mm_thread_t*, mm_thread_function_t, void*);
int mm_thread_join(mm_thread_t*);
int mm_thread_set_name(mm_thread_t*, char*);
#endif

View File

@ -58,7 +58,7 @@ test_accept_cancel(void)
machinarium_init();
int id;
id = machine_create(test_waiter, NULL);
id = machine_create("test", test_waiter, NULL);
test(id != -1);
int rc;

View File

@ -41,7 +41,7 @@ test_accept_timeout(void)
machinarium_init();
int id;
id = machine_create(test_server, NULL);
id = machine_create("test", test_server, NULL);
test(id != -1);
int rc;

View File

@ -87,7 +87,7 @@ test_client_server(void)
machinarium_init();
int id;
id = machine_create(test_cs, NULL);
id = machine_create("test", test_cs, NULL);
test(id != -1);
int rc;

View File

@ -110,7 +110,7 @@ test_client_server_readahead(void)
machinarium_init();
int id;
id = machine_create(test_cs, NULL);
id = machine_create("test", test_cs, NULL);
test(id != -1);
int rc;

View File

@ -38,7 +38,7 @@ test_condition0(void)
machinarium_init();
int id;
id = machine_create(test_waiter, NULL);
id = machine_create("test", test_waiter, NULL);
test(id != -1);
int rc;

View File

@ -37,7 +37,7 @@ test_condition1(void)
machinarium_init();
int id;
id = machine_create(test_waiter, NULL);
id = machine_create("test", test_waiter, NULL);
test(id != -1);
int rc;

View File

@ -52,7 +52,7 @@ test_connect(void)
machinarium_init();
int id;
id = machine_create(test_waiter, NULL);
id = machine_create("test", test_waiter, NULL);
test(id != -1);
int rc;

View File

@ -50,7 +50,7 @@ test_connect_cancel0(void)
machinarium_init();
int id;
id = machine_create(test_waiter, NULL);
id = machine_create("test", test_waiter, NULL);
test(id != -1);
int rc;

View File

@ -52,7 +52,7 @@ test_connect_cancel1(void)
machinarium_init();
int id;
id = machine_create(test_waiter, NULL);
id = machine_create("test", test_waiter, NULL);
test(id != -1);
int rc;

View File

@ -46,7 +46,7 @@ test_connect_timeout(void)
machinarium_init();
int id;
id = machine_create(test_waiter, NULL);
id = machine_create("test", test_waiter, NULL);
test(id != -1);
int rc;

View File

@ -39,7 +39,7 @@ test_context_switch(void)
machinarium_init();
int id;
id = machine_create(csw_runner, NULL);
id = machine_create("test", csw_runner, NULL);
test(id != -1);
int rc;

View File

@ -23,7 +23,7 @@ test_create(void)
machinarium_init();
int id;
id = machine_create(fiber, NULL);
id = machine_create("test", fiber, NULL);
test(id != -1);
int rc;

View File

@ -31,7 +31,7 @@ test_getaddrinfo0(void)
machinarium_init();
int id;
id = machine_create(test_gai, NULL);
id = machine_create("test", test_gai, NULL);
test(id != -1);
int rc;

View File

@ -59,7 +59,7 @@ test_getaddrinfo1(void)
machinarium_init();
int id;
id = machine_create(test_gai, NULL);
id = machine_create("test", test_gai, NULL);
test(id != -1);
int rc;

View File

@ -23,7 +23,7 @@ test_io_new(void)
machinarium_init();
int id;
id = machine_create(fiber, NULL);
id = machine_create("test", fiber, NULL);
test(id != -1);
int rc;

View File

@ -87,7 +87,7 @@ test_read_cancel(void)
machinarium_init();
int id;
id = machine_create(test_cs, NULL);
id = machine_create("test", test_cs, NULL);
test(id != -1);
int rc;

View File

@ -79,7 +79,7 @@ test_read_timeout(void)
machinarium_init();
int id;
id = machine_create(test_cs, NULL);
id = machine_create("test", test_cs, NULL);
test(id != -1);
int rc;

View File

@ -21,7 +21,7 @@ test_sleep(void)
machinarium_init();
int id;
id = machine_create(fiber, NULL);
id = machine_create("test", fiber, NULL);
test(id != -1);
int rc;

View File

@ -40,7 +40,7 @@ test_sleep_cancel0(void)
machinarium_init();
int id;
id = machine_create(test_sleep_cancel0_parent, NULL);
id = machine_create("test", test_sleep_cancel0_parent, NULL);
test(id != -1);
int rc;

View File

@ -21,7 +21,7 @@ test_sleep_yield(void)
machinarium_init();
int id;
id = machine_create(fiber, NULL);
id = machine_create("test", fiber, NULL);
test(id != -1);
int rc;

View File

@ -46,7 +46,7 @@ test_wait(void)
machinarium_init();
int id;
id = machine_create(test_waiter, NULL);
id = machine_create("test", test_waiter, NULL);
test(id != -1);
int rc;