ruby-changes:54560
From: k0kubun <ko1@a...>
Date: Thu, 10 Jan 2019 22:22:04 +0900 (JST)
Subject: [ruby-changes:54560] k0kubun:r66775 (trunk): mjit.c: use boolean type for boolean variables
k0kubun 2019-01-10 22:21:58 +0900 (Thu, 10 Jan 2019) New Revision: 66775 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=66775 Log: mjit.c: use boolean type for boolean variables and functions to clarify the intention and make sure it's not used in a surprising way (like using 2, 3, ... other than 0, 1 even while it seems to be a boolean). Modified files: trunk/eval.c trunk/internal.h trunk/mjit.c trunk/mjit.h trunk/mjit_compile.c trunk/mjit_worker.c trunk/process.c trunk/vm_insnhelper.c Index: eval.c =================================================================== --- eval.c (revision 66774) +++ eval.c (revision 66775) @@ -233,7 +233,7 @@ ruby_cleanup(volatile int ex) https://github.com/ruby/ruby/blob/trunk/eval.c#L233 } } - mjit_finish(TRUE); /* We still need ISeqs here. */ + mjit_finish(true); // We still need ISeqs here. ruby_finalize_1(); Index: mjit_worker.c =================================================================== --- mjit_worker.c (revision 66774) +++ mjit_worker.c (revision 66775) @@ -171,11 +171,11 @@ extern rb_pid_t ruby_waitpid_locked(rb_v https://github.com/ruby/ruby/blob/trunk/mjit_worker.c#L171 freed. */ struct mjit_options mjit_opts; -/* TRUE if MJIT is enabled. */ -int mjit_enabled = FALSE; -/* TRUE if JIT-ed code should be called. When `ruby_vm_event_enabled_global_flags & ISEQ_TRACE_EVENTS` - and `mjit_call_p == FALSE`, any JIT-ed code execution is cancelled as soon as possible. */ -int mjit_call_p = FALSE; +// true if MJIT is enabled. +bool mjit_enabled = false; +// TRUE if JIT-ed code should be called. When `ruby_vm_event_enabled_global_flags & ISEQ_TRACE_EVENTS` +// and `mjit_call_p == false`, any JIT-ed code execution is cancelled as soon as possible. +bool mjit_call_p = false; /* Priority queue of iseqs waiting for JIT compilation. This variable is a pointer to head unit of the queue. */ @@ -198,14 +198,14 @@ static rb_nativethread_cond_t mjit_clien https://github.com/ruby/ruby/blob/trunk/mjit_worker.c#L198 static rb_nativethread_cond_t mjit_worker_wakeup; /* A thread conditional to wake up workers if at the end of GC. */ static rb_nativethread_cond_t mjit_gc_wakeup; -/* True when GC is working. */ -static int in_gc; -/* True when JIT is working. */ -static int in_jit; -/* Set to TRUE to stop worker. */ -static int stop_worker_p; -/* Set to TRUE if worker is stopped. */ -static int worker_stopped; +// true when GC is working. +static bool in_gc; +// true when JIT is working. +static bool in_jit; +// Set to true to stop worker. +static bool stop_worker_p; +// Set to true if worker is stopped. +static bool worker_stopped; /* Path of "/tmp", which can be changed to $TMP in MinGW. */ static char *tmp_dir; @@ -363,7 +363,7 @@ clean_object_files(struct rb_mjit_unit * https://github.com/ruby/ruby/blob/trunk/mjit_worker.c#L363 char *so_file = unit->so_file; unit->so_file = NULL; - /* unit->so_file is set only when mjit_opts.save_temps is FALSE. */ + // unit->so_file is set only when mjit_opts.save_temps is false. remove_file(so_file); free(so_file); } @@ -444,14 +444,12 @@ real_ms_time(void) https://github.com/ruby/ruby/blob/trunk/mjit_worker.c#L444 } #endif -/* Return TRUE if class_serial is not obsoleted. This is used by mjit_compile.c. */ -int +// Return true if class_serial is not obsoleted. This is used by mjit_compile.c. +bool mjit_valid_class_serial_p(rb_serial_t class_serial) { - int found_p; - CRITICAL_SECTION_START(3, "in valid_class_serial_p"); - found_p = rb_hash_stlike_lookup(valid_class_serials, LONG2FIX(class_serial), NULL); + bool found_p = rb_hash_stlike_lookup(valid_class_serials, LONG2FIX(class_serial), NULL); CRITICAL_SECTION_FINISH(3, "in valid_class_serial_p"); return found_p; } @@ -653,7 +651,7 @@ remove_so_file(const char *so_file, stru https://github.com/ruby/ruby/blob/trunk/mjit_worker.c#L651 #ifdef _MSC_VER /* Compile C file to so. It returns 1 if it succeeds. (mswin) */ -static int +static bool compile_c_to_so(const char *c_file, const char *so_file) { int exit_code; @@ -703,7 +701,7 @@ compile_c_to_so(const char *c_file, cons https://github.com/ruby/ruby/blob/trunk/mjit_worker.c#L701 args = form_args(5, CC_LDSHARED_ARGS, CC_CODEFLAG_ARGS, files, CC_LIBS, CC_DLDFLAGS_ARGS); if (args == NULL) - return FALSE; + return false; exit_code = exec_process(cc_path, args); free(args); @@ -771,7 +769,7 @@ make_pch(void) https://github.com/ruby/ruby/blob/trunk/mjit_worker.c#L769 } /* Compile .c file to .o file. It returns 1 if it succeeds. (non-mswin) */ -static int +static bool compile_c_to_o(const char *c_file, const char *o_file) { int exit_code; @@ -791,7 +789,7 @@ compile_c_to_o(const char *c_file, const https://github.com/ruby/ruby/blob/trunk/mjit_worker.c#L789 # endif args = form_args(5, cc_common_args, CC_CODEFLAG_ARGS, files, CC_LIBS, CC_DLDFLAGS_ARGS); if (args == NULL) - return FALSE; + return false; exit_code = exec_process(cc_path, args); free(args); @@ -802,7 +800,7 @@ compile_c_to_o(const char *c_file, const https://github.com/ruby/ruby/blob/trunk/mjit_worker.c#L800 } /* Link .o files to .so file. It returns 1 if it succeeds. (non-mswin) */ -static int +static bool link_o_to_so(const char **o_files, const char *so_file) { int exit_code; @@ -819,7 +817,7 @@ link_o_to_so(const char **o_files, const https://github.com/ruby/ruby/blob/trunk/mjit_worker.c#L817 args = form_args(6, CC_LDSHARED_ARGS, CC_CODEFLAG_ARGS, options, o_files, CC_LIBS, CC_DLDFLAGS_ARGS); if (args == NULL) - return FALSE; + return false; exit_code = exec_process(cc_path, args); free(args); @@ -840,7 +838,7 @@ compact_all_jit_code(void) https://github.com/ruby/ruby/blob/trunk/mjit_worker.c#L838 static const char so_ext[] = DLEXT; char so_file[MAXPATHLEN]; const char **o_files; - int i = 0, success; + int i = 0; /* Abnormal use case of rb_mjit_unit that doesn't have ISeq */ unit = calloc(1, sizeof(struct rb_mjit_unit)); /* To prevent GC, don't use ZALLOC */ @@ -858,7 +856,7 @@ compact_all_jit_code(void) https://github.com/ruby/ruby/blob/trunk/mjit_worker.c#L856 } start_time = real_ms_time(); - success = link_o_to_so(o_files, so_file); + bool success = link_o_to_so(o_files, so_file); end_time = real_ms_time(); /* TODO: Shrink this big critical section. For now, this is needed to prevent failure by missing .o files. @@ -983,7 +981,6 @@ static mjit_func_t https://github.com/ruby/ruby/blob/trunk/mjit_worker.c#L981 convert_unit_to_func(struct rb_mjit_unit *unit, struct rb_call_cache *cc_entries, union iseq_inline_storage_entry *is_entries) { char c_file_buff[MAXPATHLEN], *c_file = c_file_buff, *so_file, funcname[35]; /* TODO: reconsider `35` */ - int success; int fd; FILE *f; void *func; @@ -1044,10 +1041,10 @@ convert_unit_to_func(struct rb_mjit_unit https://github.com/ruby/ruby/blob/trunk/mjit_worker.c#L1041 if (!mjit_opts.save_temps) remove_file(c_file); free_unit(unit); - in_jit = FALSE; /* just being explicit for return */ + in_jit = false; // just being explicit for return } else { - in_jit = TRUE; + in_jit = true; } CRITICAL_SECTION_FINISH(3, "before mjit_compile to wait GC finish"); if (!in_jit) { @@ -1062,11 +1059,11 @@ convert_unit_to_func(struct rb_mjit_unit https://github.com/ruby/ruby/blob/trunk/mjit_worker.c#L1059 verbose(2, "start compilation: %s@%s:%d -> %s", label, path, lineno, c_file); fprintf(f, "/* %s@%s:%d */\n\n", label, path, lineno); } - success = mjit_compile(f, unit->iseq->body, funcname, cc_entries, is_entries); + bool success = mjit_compile(f, unit->iseq->body, funcname, cc_entries, is_entries); /* release blocking mjit_gc_start_hook */ CRITICAL_SECTION_START(3, "after mjit_compile to wakeup client for GC"); - in_jit = FALSE; + in_jit = false; verbose(3, "Sending wakeup signal to client in a mjit-worker for GC"); rb_native_cond_signal(&mjit_client_wakeup); CRITICAL_SECTION_FINISH(3, "in worker to wakeup client for GC"); @@ -1124,7 +1121,7 @@ typedef struct { https://github.com/ruby/ruby/blob/trunk/mjit_worker.c#L1121 struct rb_mjit_unit *unit; struct rb_call_cache *cc_entries; union iseq_inline_storage_entry *is_entries; - int finish_p; + bool finish_p; } mjit_copy_job_t; /* Singleton MJIT copy job. This is made global since it needs to be durable even when MJIT worker thread is stopped. @@ -1139,11 +1136,11 @@ int rb_workqueue_register(unsigned flags https://github.com/ruby/ruby/blob/trunk/mjit_worker.c#L1136 /* We're lazily copying cache values from main thread because these cache values could be different between ones on enqueue timing and ones on dequeue timing. Return TRUE if copy succeeds. */ -static int +static bool copy_cache_from_main_thread(mjit_copy_job_t *job) { CRITICAL_SECTION_START(3, "in copy_cache_from_main_thread"); - job->finish_p = FALSE; /* allow dispatching this job in mjit_copy_job_handler */ + job->finish_p = false; // allow dispatching this job in mjit_copy_job_handler CRITICAL_SECTION_FINISH(3, "in copy_cache_from_main_thread"); if (UNLIKELY(mjit_opts.wait)) { @@ -1152,7 +1149,7 @@ copy_cache_from_main_thread(mjit_copy_jo https://github.com/ruby/ruby/blob/trunk/mjit_worker.c#L1149 } if (!rb_workqueue_register(0, mjit_copy_job_handler, (void *)job)) - return FALSE; + return false; CRITICAL_SECTION_START(3, "in MJIT copy job wait"); /* checking `stop_worker_p` too because `RUBY_VM_CHECK_INTS(ec)` may not lush mjit_copy_job_handler when EC_EXEC_TAG() is not TAG_NONE, and then @@ -1179,9 +1176,9 @@ mjit_worker(void) https://github.com/ruby/ruby/blob/trunk/mjit_worker.c#L1176 } #endif if (pch_status == PCH_FAILED) { - mjit_enabled = FALSE; + mjit_enabled = false; CRITICAL_SECTION_START(3, "in worker to update worker_stopped"); - worker_stopped = TRUE; + worker_stopped = true; verbose(3, "Sending wakeup signal to client in a mjit-worker"); rb_native_cond_signal(&mjit_client_wakeup); CRITICAL_SECTION_FINISH(3, "in worker to update worker_stopped"); @@ -1199,7 +1196,7 @@ mjit_worker(void) https://github.com/ruby/ruby/blob/trunk/mjit_worker.c#L1196 verbose(3, "Getting wakeup from client"); } unit = get_from_list(&unit_queue); - job->finish_p = TRUE; /* disable dispatching this job in mjit_copy_job_handler while it's being modified */ + job->finish_p = true; /* disable dispatching this job in mjit_copy_job_handler while it's being modified */ CRITICAL_SECTION_FINISH(3, "in worker dequeue"); if (unit) { @@ -1216,7 +1213,7 @@ mjit_worker(void) https://github.com/ruby/ruby/blob/trunk/mjit_worker.c#L1213 /* Copy ISeq's inline caches values to avoid race condition. */ if (job->cc_entries != NULL || job->is_entries != NULL) { - if (copy_cache_from_main_thread(job) == FALSE) { + if (copy_cache_from_main_thread(job) == false) { continue; /* retry postponed_job failure, or stop worker */ } } @@ -1247,8 +1244,8 @@ mjit_worker(void) https://github.com/ruby/ruby/blob/trunk/mjit_worker.c#L1244 /* Disable dispatching this job in mjit_copy_job_handler while memory allocated by alloca could be expired after finishing this function. */ - job->finish_p = TRUE; + job->finish_p = true; /* To keep mutex unlocked when it is destroyed by mjit_finish, don't wrap CRITICAL_SECTION here. */ - worker_stopped = TRUE; + worker_stopped = true; } Index: mjit.c =================================================================== --- mjit.c (revision 66774) +++ mjit.c (revision 66775) @@ -48,7 +48,7 @@ mjit_copy_job_handler(void *data) https://github.com/ruby/ruby/blob/trunk/mjit.c#L48 memcpy(job->is_entries, body->is_entries, sizeof(union iseq_inline_storage_entry) * body->is_size); } - job->finish_p = TRUE; + job->finish_p = true; rb_native_cond_broadcast(&mjit_worker_wakeup); CRITICAL_SECTION_FINISH(3, "in mjit_copy_job_handler"); } @@ -88,7 +88,7 @@ mjit_gc_start_hook(void) https://github.com/ruby/ruby/blob/trunk/mjit.c#L88 rb_native_cond_wait(&mjit_client_wakeup, &mjit_engine_mutex); verbose(4, "Getting wakeup from a worker for GC"); } - in_gc = TRUE; + in_gc = true; CRITICAL_SECTION_FINISH(4, "mjit_gc_start_hook"); } @@ -100,7 +100,7 @@ mjit_gc_finish_hook(void) https://github.com/ruby/ruby/blob/trunk/mjit.c#L100 if (!mjit_enabled) return; CRITICAL_SECTION_START(4, "mjit_gc_finish_hook"); - in_gc = FALSE; + in_gc = false; verbose(4, "Sending wakeup signal to workers after GC"); rb_native_cond_broadcast(&mjit_gc_wakeup); CRITICAL_SECTION_FINISH(4, "mjit_gc_finish_hook"); @@ -126,7 +126,7 @@ mjit_free_iseq(const rb_iseq_t *iseq) https://github.com/ruby/ruby/blob/trunk/mjit.c#L126 because node of unit_queue and one of active_units may have the same unit during proceeding unit. */ static void -free_list(struct rb_mjit_unit_list *list, int close_handle_p) +free_list(struct rb_mjit_unit_list *list, bool close_handle_p) { struct rb_mjit_unit *unit = 0, *next; @@ -572,11 +572,11 @@ system_tmpdir(void) https://github.com/ruby/ruby/blob/trunk/mjit.c#L572 #define MIN_CACHE_SIZE 10 /* Start MJIT worker. Return TRUE if worker is sucessfully started. */ -static int +static bool start_worker(void) { - stop_worker_p = FALSE; - worker_stopped = FALSE; + stop_worker_p = false; + worker_stopped = true; if (!rb_thread_create_mjit_thread(mjit_worker)) { mjit_enabled = FALSE; @@ -586,9 +586,9 @@ start_worker(void) https://github.com/ruby/ruby/blob/trunk/mjit.c#L586 rb_native_cond_destroy(&mjit_worker_wakeup); rb_native_cond_destroy(&mjit_gc_wakeup); verbose(1, "Failure in MJIT thread initialization\n"); - return FALSE; + return false; } - return TRUE; + return true; } /* Initialize MJIT. Start a thread creating the precompiled header and @@ -598,8 +598,8 @@ void https://github.com/ruby/ruby/blob/trunk/mjit.c#L598 mjit_init(struct mjit_options *opts) { mjit_opts = *opts; - mjit_enabled = TRUE; - mjit_call_p = TRUE; + mjit_enabled = true; + mjit_call_p = true; /* Normalize options */ if (mjit_opts.min_calls == 0) @@ -635,7 +635,7 @@ mjit_init(struct mjit_options *opts) https://github.com/ruby/ruby/blob/trunk/mjit.c#L635 verbose(2, "MJIT: tmp_dir is %s", tmp_dir); if (!init_header_filename()) { - mjit_enabled = FALSE; + mjit_enabled = false; verbose(1, "Failure in MJIT header file name initialization\n"); return; } @@ -670,7 +670,7 @@ stop_worker(void) https://github.com/ruby/ruby/blob/trunk/mjit.c#L670 while (!worker_stopped) { verbose(3, "Sending cancel signal to worker"); CRITICAL_SECTION_START(3, "in stop_worker"); - stop_worker_p = TRUE; /* Setting this inside loop because RUBY_VM_CHECK_INTS may make this FALSE. */ + stop_worker_p = true; /* Setting this inside loop because RUBY_VM_CHECK_INTS may make this FALSE. */ rb_native_cond_broadcast(&mjit_worker_wakeup); CRITICAL_SECTION_FINISH(3, "in stop_worker"); RUBY_VM_CHECK_INTS(ec); @@ -679,7 +679,7 @@ stop_worker(void) https://github.com/ruby/ruby/blob/trunk/mjit.c#L679 /* Stop JIT-compiling methods but compiled code is kept available. */ VALUE -mjit_pause(int wait_p) +mjit_pause(bool wait_p) { if (!mjit_enabled) { rb_raise(rb_eRuntimeError, "MJIT is not enabled"); @@ -775,10 +775,10 @@ mjit_child_after_fork(void) https://github.com/ruby/ruby/blob/trunk/mjit.c#L775 and free MJIT data. It should be called last during MJIT life. - If close_handle_p is TRUE, it calls dlclose() for JIT-ed code. So it should be FALSE + If close_handle_p is true, it calls dlclose() for JIT-ed code. So it should be false if the code can still be on stack. ...But it means to leak JIT-ed handle forever (FIXME). */ void -mjit_finish(int close_handle_p) +mjit_finish(bool close_handle_p) { if (!mjit_enabled) return; @@ -816,13 +816,13 @@ mjit_finish(int close_handle_p) https://github.com/ruby/ruby/blob/trunk/mjit.c#L816 xfree(tmp_dir); tmp_dir = NULL; xfree(pch_file); pch_file = NULL; - mjit_call_p = FALSE; + mjit_call_p = true; free_list(&unit_queue, close_handle_p); free_list(&active_units, close_handle_p); free_list(&compact_units, close_handle_p); finish_conts(); - mjit_enabled = FALSE; + mjit_enabled = false; verbose(1, "Successful MJIT finish"); } Index: mjit.h =================================================================== --- mjit.h (revision 66774) +++ mjit.h (revision 66775) @@ -58,13 +58,13 @@ typedef VALUE (*mjit_func_t)(rb_executio https://github.com/ruby/ruby/blob/trunk/mjit.h#L58 RUBY_SYMBOL_EXPORT_BEGIN RUBY_EXTERN struct mjit_options mjit_opts; -RUBY_EXTERN int mjit_call_p; +RUBY_EXTERN bool mjit_call_p; extern void mjit_add_iseq_to_process(const rb_iseq_t *iseq); extern VALUE mjit_wait_call(rb_execution_context_t *ec, struct rb_iseq_constant_body *body); RUBY_SYMBOL_EXPORT_END -extern int mjit_compile(FILE *f, const struct rb_iseq_constant_body *body, const char *funcname, struct rb_call_cache *cc_entries, union iseq_inline_storage_entry *is_entries); +extern bool mjit_compile(FILE *f, const struct rb_iseq_constant_body *body, const char *funcname, struct rb_call_cache *cc_entries, union iseq_inline_storage_entry *is_entries); extern void mjit_init(struct mjit_options *opts); extern void mjit_postponed_job_register_start_hook(void); extern void mjit_postponed_job_register_finish_hook(void); Index: process.c =================================================================== --- process.c (revision 66774) +++ process.c (revision 66775) @@ -2947,7 +2947,7 @@ rb_f_exec(int argc, const VALUE *argv) https://github.com/ruby/ruby/blob/trunk/process.c#L2947 execarg_obj = rb_execarg_new(argc, argv, TRUE, FALSE); eargp = rb_execarg_get(execarg_obj); - if (mjit_enabled) mjit_finish(FALSE); /* avoid leaking resources, and do not leave files. XXX: JIT-ed handle can leak after exec error is rescued. */ + if (mjit_enabled) mjit_finish(false); // avoid leaking resources, and do not leave files. XXX: JIT-ed handle can leak after exec error is rescued. before_exec(); /* stop timer thread before redirects */ rb_execarg_parent_start(execarg_obj); fail_str = eargp->use_shell ? eargp->invoke.sh.shell_script : eargp->invoke.cmd.command_name; @@ -4045,7 +4045,7 @@ rb_fork_ruby(int *status) https://github.com/ruby/ruby/blob/trunk/process.c#L4045 while (1) { prefork(); - if (mjit_enabled) mjit_pause(FALSE); /* Don't leave locked mutex to child. Note: child_handler must be enabled to pause MJIT. */ + if (mjit_enabled) mjit_pause(false); // Don't leave locked mutex to child. Note: child_handler must be enabled to pause MJIT. disable_child_handler_before_fork(&old); before_fork_ruby(); pid = fork(); @@ -6502,7 +6502,7 @@ rb_daemon(int nochdir, int noclose) https://github.com/ruby/ruby/blob/trunk/process.c#L6502 { int err = 0; #ifdef HAVE_DAEMON - if (mjit_enabled) mjit_pause(FALSE); /* Don't leave locked mutex to child. */ + if (mjit_enabled) mjit_pause(false); // Don't leave locked mutex to child. before_fork_ruby(); err = daemon(nochdir, noclose); after_fork_ruby(); Index: internal.h =================================================================== --- internal.h (revision 66774) +++ internal.h (revision 66775) @@ -1595,15 +1595,15 @@ VALUE rb_math_sqrt(VALUE); https://github.com/ruby/ruby/blob/trunk/internal.h#L1595 /* mjit.c */ #if USE_MJIT -extern int mjit_enabled; -VALUE mjit_pause(int wait_p); +extern bool mjit_enabled; +VALUE mjit_pause(bool wait_p); VALUE mjit_ (... truncated) -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/