Skip to content

Commit 4ea3c5a

Browse files
committed
artifact cache cancellation mutex -> semaphore (fixes cancellation thread on linux - we cannot take/drop across threads with mutexes); flip no_meta -> meta
1 parent 8577891 commit 4ea3c5a

File tree

5 files changed

+32
-39
lines changed

5 files changed

+32
-39
lines changed

build.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ mkdir -p build
5353
mkdir -p local
5454

5555
# --- Build & Run Metaprogram -------------------------------------------------
56-
if [ -v no_meta ]; then echo "[skipping metagen]"; fi
57-
if [ ! -v no_meta ]
56+
if [ ! -v meta ]
5857
then
58+
echo "[doing metagen]"
5959
cd build
6060
$compile_debug ../src/metagen/metagen_main.c $compile_link $out metagen
6161
./metagen

src/artifact_cache/artifact_cache.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ ac_init(void)
1919
ac_shared->req_batches[idx].arena = arena_alloc();
2020
}
2121
ac_shared->cancel_thread = thread_launch(ac_cancel_thread_entry_point, 0);
22-
ac_shared->cancel_thread_mutex = mutex_alloc();
23-
mutex_take(ac_shared->cancel_thread_mutex);
22+
ac_shared->cancel_thread_semaphore = semaphore_alloc(0, 1, str8_zero());
2423
}
2524

2625
////////////////////////////////
@@ -218,7 +217,7 @@ ac_async_tick(void)
218217
//
219218
if(lane_idx() == 0)
220219
{
221-
mutex_drop(ac_shared->cancel_thread_mutex);
220+
semaphore_drop(ac_shared->cancel_thread_semaphore);
222221
}
223222

224223
//////////////////////////////
@@ -590,7 +589,7 @@ ac_async_tick(void)
590589
//
591590
if(lane_idx() == 0)
592591
{
593-
mutex_take(ac_shared->cancel_thread_mutex);
592+
semaphore_take(ac_shared->cancel_thread_semaphore, max_U64);
594593
}
595594
scratch_end(scratch);
596595
}
@@ -604,7 +603,7 @@ ac_cancel_thread_entry_point(void *p)
604603
for(;;)
605604
{
606605
os_sleep_milliseconds(50);
607-
MutexScope(ac_shared->cancel_thread_mutex)
606+
semaphore_take(ac_shared->cancel_thread_semaphore, max_U64);
608607
{
609608
for EachIndex(cache_slot_idx, ac_shared->cache_slots_count)
610609
{
@@ -650,5 +649,6 @@ ac_cancel_thread_entry_point(void *p)
650649
}
651650
}
652651
}
652+
semaphore_drop(ac_shared->cancel_thread_semaphore);
653653
}
654654
}

src/artifact_cache/artifact_cache.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ struct AC_Shared
129129

130130
// rjf: cancel thread
131131
Thread cancel_thread;
132-
Mutex cancel_thread_mutex;
132+
Semaphore cancel_thread_semaphore;
133133
};
134134

135135
////////////////////////////////

src/base/base_entry_point.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,6 @@ main_thread_base_entry_point(int arguments_count, char **arguments)
5757
#if defined(ARTIFACT_CACHE_H) && !defined(AC_INIT_MANUAL)
5858
ac_init();
5959
#endif
60-
#if defined(ASYNC_H) && !defined(ASYNC_INIT_MANUAL)
61-
async_init(&cmdline);
62-
#endif
6360
#if defined(CONTENT_H) && !defined(C_INIT_MANUAL)
6461
c_init();
6562
#endif

src/os/core/linux/os_core_linux.c

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -771,41 +771,41 @@ internal OS_Handle
771771
os_process_launch(OS_ProcessLaunchParams *params)
772772
{
773773
OS_Handle handle = {0};
774-
774+
775775
posix_spawn_file_actions_t file_actions = {0};
776776
int file_actions_init_code = posix_spawn_file_actions_init(&file_actions);
777777
if(file_actions_init_code == 0)
778778
{
779779
// redirect STDOUT
780780
int stdout_code = posix_spawn_file_actions_adddup2(&file_actions, (int)params->stdout_file.u64[0], STDOUT_FILENO);
781781
Assert(stdout_code == 0);
782-
782+
783783
// redirect STDERR
784784
int stderr_code = posix_spawn_file_actions_adddup2(&file_actions, (int)params->stderr_file.u64[0], STDERR_FILENO);
785785
Assert(stderr_code == 0);
786-
786+
787787
// redirect STDIN
788788
int stdin_code = posix_spawn_file_actions_adddup2(&file_actions, (int)params->stdin_file.u64[0], STDIN_FILENO);
789789
Assert(stdin_code == 0);
790-
790+
791791
posix_spawnattr_t attr = {0};
792792
int attr_init_code = posix_spawnattr_init(&attr);
793793
if(attr_init_code == 0)
794794
{
795795
Temp scratch = scratch_begin(0, 0);
796-
796+
797797
// package argv
798798
char **argv = push_array(scratch.arena, char *, params->cmd_line.node_count + 1);
799799
{
800800
String8List l = str8_split_path(scratch.arena, params->path);
801801
str8_list_push(scratch.arena, &l, params->cmd_line.first->string);
802802
String8 path_to_exe = str8_path_list_join_by_style(scratch.arena, &l, PathStyle_SystemAbsolute);
803-
803+
804804
argv[0] = (char *)path_to_exe.str;
805805
U64 arg_idx = 1;
806806
for EachNode(n, String8Node, params->cmd_line.first->next) { argv[arg_idx++] = (char *)n->string.str; }
807807
}
808-
808+
809809
// package envp
810810
char **envp = 0;
811811
if(params->inherit_env)
@@ -821,39 +821,39 @@ os_process_launch(OS_ProcessLaunchParams *params)
821821
envp[env_idx] = (char *)n->string.str;
822822
}
823823
}
824-
824+
825825
if(params->debug_subprocesses)
826826
{
827827
// not suported
828828
InvalidPath;
829829
}
830-
830+
831831
if(!params->consoleless)
832832
{
833833
NotImplemented;
834834
}
835-
835+
836836
// spawn process
837837
pid_t pid = 0;
838838
int spawn_code = posix_spawn(&pid, argv[0], &file_actions, &attr, argv, envp);
839-
839+
840840
if(spawn_code == 0)
841841
{
842842
handle.u64[0] = (U64)pid;
843843
}
844-
844+
845845
// clean up attributes
846846
int attr_destroy_code = posix_spawnattr_destroy(&attr);
847847
Assert(attr_destroy_code == 0);
848-
848+
849849
scratch_end(scratch);
850850
}
851-
851+
852852
// clean up file actions
853853
int file_actions_destroy_code = posix_spawn_file_actions_destroy(&file_actions);
854854
Assert(file_actions_destroy_code == 0);
855855
}
856-
856+
857857
return handle;
858858
}
859859

@@ -867,7 +867,7 @@ os_process_join(OS_Handle handle, U64 endt_us, U64 *exit_code_out)
867867
if(kill(pid, 0) >= 0)
868868
{
869869
result = (errno == ENOENT);
870-
870+
871871
if(result)
872872
{
873873
int status;
@@ -969,11 +969,7 @@ internal Mutex
969969
os_mutex_alloc(void)
970970
{
971971
OS_LNX_Entity *entity = os_lnx_entity_alloc(OS_LNX_EntityKind_Mutex);
972-
pthread_mutexattr_t attr;
973-
pthread_mutexattr_init(&attr);
974-
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
975-
int init_result = pthread_mutex_init(&entity->mutex_handle, &attr);
976-
pthread_mutexattr_destroy(&attr);
972+
int init_result = pthread_mutex_init(&entity->mutex_handle, 0);
977973
if(init_result == -1)
978974
{
979975
os_lnx_entity_release(entity);
@@ -1410,18 +1406,18 @@ lnx_signal_handler(int sig, siginfo_t *info, void *arg)
14101406
sleep(UINT32_MAX);
14111407
}
14121408
}
1413-
1409+
14141410
local_persist void *ips[4096];
14151411
int ips_count = backtrace(ips, ArrayCount(ips));
1416-
1412+
14171413
fprintf(stderr, "A fatal signal was received: %s (%d). The process is terminating.\n", strsignal(sig), sig);
14181414
fprintf(stderr, "Create a new issue with this report at %s.\n\n", BUILD_ISSUES_LINK_STRING_LITERAL);
14191415
fprintf(stderr, "Callstack:\n");
14201416
for EachIndex(i, ips_count)
14211417
{
14221418
Dl_info info = {0};
14231419
dladdr(ips[i], &info);
1424-
1420+
14251421
char cmd[2048];
14261422
snprintf(cmd, sizeof(cmd), "llvm-symbolizer --relative-address -f -e %s %lu", info.dli_fname, (unsigned long)ips[i] - (unsigned long)info.dli_fbase);
14271423
FILE *f = popen(cmd, "r");
@@ -1435,12 +1431,12 @@ lnx_signal_handler(int sig, siginfo_t *info, void *arg)
14351431
String8 module = str8_skip_last_slash(str8_cstring(info.dli_fname));
14361432
String8 file = str8_skip_last_slash(str8_cstring_capped(file_name, file_name + sizeof(file_name)));
14371433
if(file.size > 0) file.size -= 1;
1438-
1434+
14391435
B32 no_func = str8_match(func, str8_lit("??"), StringMatchFlag_RightSideSloppy);
14401436
B32 no_file = str8_match(file, str8_lit("??"), StringMatchFlag_RightSideSloppy);
14411437
if(no_func) { func = str8_zero(); }
14421438
if(no_file) { file = str8_zero(); }
1443-
1439+
14441440
fprintf(stderr, "%ld. [0x%016lx] %.*s%s%.*s %.*s\n", i+1, (unsigned long)ips[i], (int)module.size, module.str, (!no_func || !no_file) ? ", " : "", (int)func.size, func.str, (int)file.size, file.str);
14451441
}
14461442
pclose(f);
@@ -1451,7 +1447,7 @@ lnx_signal_handler(int sig, siginfo_t *info, void *arg)
14511447
}
14521448
}
14531449
fprintf(stderr, "\nVersion: %s%s\n\n", BUILD_VERSION_STRING_LITERAL, BUILD_GIT_HASH_STRING_LITERAL_APPEND);
1454-
1450+
14551451
_exit(0);
14561452
}
14571453

@@ -1470,7 +1466,7 @@ main(int argc, char **argv)
14701466
sigaction(SIGSEGV, &handler, NULL);
14711467
sigaction(SIGQUIT, &handler, NULL);
14721468
}
1473-
1469+
14741470
//- rjf: set up OS layer
14751471
{
14761472
//- rjf: get statically-allocated system/process info

0 commit comments

Comments
 (0)