apr_time関数
先日の件(1・2・3)ですが。
「劣化移植なのでマルチスレッド環境で動かす場合は(別途)考慮が必要」てな感じで適当にお茶を濁していたのですが(改めて説明する事でもありませんが)要するにlocaltime関数とかはスレッド・セーフではないので(別途)考慮が必要という事でした。
大抵の場合はlocaltime_r関数とかサフィックスに「_r」がついたものを使うのですが、使ったこともないのでヘタれな自分としては不安が一杯です。
不安を抱えつつApacheのソースを色々と漁ってみると、素敵な関数が存在しました。これは便利!
詳しい内容はapr_*.hを参照してもらうとして(time系に限らず)file系とかstring系とか便利そうな関数が一杯あるので、ヘタれな自分としては非常に助かります。是非とも一通りは使いこなせるようになりたいなっと。
とりあえず差分ダケですが、変更した内容を掲載しておきます。
--- mod_datetimelimit22.c,orig 2009-06-12 22:51:29.329481000 +0900 +++ mod_datetimelimit22.c 2009-06-12 22:51:29.132987000 +0900 @@ -17,6 +17,7 @@ #include "http_log.h" #include "ap_config.h" #include "apr_tables.h" +#include "apr_time.h" #include "apr_lib.h" #include "scoreboard.h" @@ -466,20 +467,18 @@ datetimelimit22_config *cfg = (datetimelimit22_config *)ap_get_module_config( rec->per_dir_config, &datetimelimit22_module ); char *url; int Result, Target_ID; - time_t timer; - struct tm *localtimer; + apr_time_exp_t localtimer; char Buffer[10]; long target_date, target_time; - timer = time( NULL ); - localtimer = localtime( &timer ); + apr_time_exp_lt( &localtimer, apr_time_now( ) ); Buffer[0] = '\0'; - sprintf( Buffer, "%d%02d%02d", ( localtimer->tm_year + 1900 ), ( localtimer->tm_mon + 1 ), localtimer->tm_mday ); + sprintf( Buffer, "%d%02d%02d", ( localtimer.tm_year + 1900 ), ( localtimer.tm_mon + 1 ), localtimer.tm_mday ); target_date = atol( Buffer ); Buffer[0] = '\0'; - sprintf( Buffer, "%02d%02d%02d", localtimer->tm_hour, localtimer->tm_min, localtimer->tm_sec ); + sprintf( Buffer, "%02d%02d%02d", localtimer.tm_hour, localtimer.tm_min, localtimer.tm_sec ); target_time = atol( Buffer ); Target_ID = datetimelimit22_getenv( rec, cfg ); --- mod_maintenance22.c,orig 2009-06-12 22:51:30.431676000 +0900 +++ mod_maintenance22.c 2009-06-12 22:51:30.235388000 +0900 @@ -18,6 +18,7 @@ #include "http_log.h" #include "ap_config.h" #include "apr_tables.h" +#include "apr_time.h" #include "apr_lib.h" #include "scoreboard.h" @@ -291,19 +292,17 @@ maintenance22_config *cfg = (maintenance22_config *)ap_get_module_config( rec->per_dir_config, &maintenance22_module ); char *url; int Result; - time_t timer; - struct tm *localtimer; + apr_time_exp_t localtimer; char Buffer[10]; long target_time; long target_week; - timer = time( NULL ); - localtimer = localtime( &timer ); + apr_time_exp_lt( &localtimer, apr_time_now( ) ); Buffer[0] = '\0'; - sprintf( Buffer, "%02d%02d%02d", localtimer->tm_hour, localtimer->tm_min, localtimer->tm_sec ); + sprintf( Buffer, "%02d%02d%02d", localtimer.tm_hour, localtimer.tm_min, localtimer.tm_sec ); target_time = atol( Buffer ); - target_week = localtimer->tm_wday; + target_week = localtimer.tm_wday; if( cfg->week >= 0 && cfg->week != target_week ) {